create.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package network
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/network"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/cli/command"
  11. "github.com/docker/docker/opts"
  12. runconfigopts "github.com/docker/docker/runconfig/opts"
  13. "github.com/spf13/cobra"
  14. )
  15. type createOptions struct {
  16. name string
  17. driver string
  18. driverOpts opts.MapOpts
  19. labels opts.ListOpts
  20. internal bool
  21. ipv6 bool
  22. attachable bool
  23. ipamDriver string
  24. ipamSubnet []string
  25. ipamIPRange []string
  26. ipamGateway []string
  27. ipamAux opts.MapOpts
  28. ipamOpt opts.MapOpts
  29. }
  30. func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
  31. opts := createOptions{
  32. driverOpts: *opts.NewMapOpts(nil, nil),
  33. labels: opts.NewListOpts(runconfigopts.ValidateEnv),
  34. ipamAux: *opts.NewMapOpts(nil, nil),
  35. ipamOpt: *opts.NewMapOpts(nil, nil),
  36. }
  37. cmd := &cobra.Command{
  38. Use: "create [OPTIONS] NETWORK",
  39. Short: "Create a network",
  40. Args: cli.ExactArgs(1),
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. opts.name = args[0]
  43. return runCreate(dockerCli, opts)
  44. },
  45. }
  46. flags := cmd.Flags()
  47. flags.StringVarP(&opts.driver, "driver", "d", "bridge", "Driver to manage the Network")
  48. flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
  49. flags.Var(&opts.labels, "label", "Set metadata on a network")
  50. flags.BoolVar(&opts.internal, "internal", false, "Restrict external access to the network")
  51. flags.BoolVar(&opts.ipv6, "ipv6", false, "Enable IPv6 networking")
  52. flags.BoolVar(&opts.attachable, "attachable", false, "Enable manual container attachment")
  53. flags.StringVar(&opts.ipamDriver, "ipam-driver", "default", "IP Address Management Driver")
  54. flags.StringSliceVar(&opts.ipamSubnet, "subnet", []string{}, "Subnet in CIDR format that represents a network segment")
  55. flags.StringSliceVar(&opts.ipamIPRange, "ip-range", []string{}, "Allocate container ip from a sub-range")
  56. flags.StringSliceVar(&opts.ipamGateway, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet")
  57. flags.Var(&opts.ipamAux, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver")
  58. flags.Var(&opts.ipamOpt, "ipam-opt", "Set IPAM driver specific options")
  59. return cmd
  60. }
  61. func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
  62. client := dockerCli.Client()
  63. ipamCfg, err := consolidateIpam(opts.ipamSubnet, opts.ipamIPRange, opts.ipamGateway, opts.ipamAux.GetAll())
  64. if err != nil {
  65. return err
  66. }
  67. // Construct network create request body
  68. nc := types.NetworkCreate{
  69. Driver: opts.driver,
  70. Options: opts.driverOpts.GetAll(),
  71. IPAM: &network.IPAM{
  72. Driver: opts.ipamDriver,
  73. Config: ipamCfg,
  74. Options: opts.ipamOpt.GetAll(),
  75. },
  76. CheckDuplicate: true,
  77. Internal: opts.internal,
  78. EnableIPv6: opts.ipv6,
  79. Attachable: opts.attachable,
  80. Labels: runconfigopts.ConvertKVStringsToMap(opts.labels.GetAll()),
  81. }
  82. resp, err := client.NetworkCreate(context.Background(), opts.name, nc)
  83. if err != nil {
  84. return err
  85. }
  86. fmt.Fprintf(dockerCli.Out(), "%s\n", resp.ID)
  87. return nil
  88. }
  89. // Consolidates the ipam configuration as a group from different related configurations
  90. // user can configure network with multiple non-overlapping subnets and hence it is
  91. // possible to correlate the various related parameters and consolidate them.
  92. // consoidateIpam consolidates subnets, ip-ranges, gateways and auxiliary addresses into
  93. // structured ipam data.
  94. func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]string) ([]network.IPAMConfig, error) {
  95. if len(subnets) < len(ranges) || len(subnets) < len(gateways) {
  96. return nil, fmt.Errorf("every ip-range or gateway must have a corresponding subnet")
  97. }
  98. iData := map[string]*network.IPAMConfig{}
  99. // Populate non-overlapping subnets into consolidation map
  100. for _, s := range subnets {
  101. for k := range iData {
  102. ok1, err := subnetMatches(s, k)
  103. if err != nil {
  104. return nil, err
  105. }
  106. ok2, err := subnetMatches(k, s)
  107. if err != nil {
  108. return nil, err
  109. }
  110. if ok1 || ok2 {
  111. return nil, fmt.Errorf("multiple overlapping subnet configuration is not supported")
  112. }
  113. }
  114. iData[s] = &network.IPAMConfig{Subnet: s, AuxAddress: map[string]string{}}
  115. }
  116. // Validate and add valid ip ranges
  117. for _, r := range ranges {
  118. match := false
  119. for _, s := range subnets {
  120. ok, err := subnetMatches(s, r)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if !ok {
  125. continue
  126. }
  127. if iData[s].IPRange != "" {
  128. return nil, fmt.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r, iData[s].IPRange, s)
  129. }
  130. d := iData[s]
  131. d.IPRange = r
  132. match = true
  133. }
  134. if !match {
  135. return nil, fmt.Errorf("no matching subnet for range %s", r)
  136. }
  137. }
  138. // Validate and add valid gateways
  139. for _, g := range gateways {
  140. match := false
  141. for _, s := range subnets {
  142. ok, err := subnetMatches(s, g)
  143. if err != nil {
  144. return nil, err
  145. }
  146. if !ok {
  147. continue
  148. }
  149. if iData[s].Gateway != "" {
  150. return nil, fmt.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
  151. }
  152. d := iData[s]
  153. d.Gateway = g
  154. match = true
  155. }
  156. if !match {
  157. return nil, fmt.Errorf("no matching subnet for gateway %s", g)
  158. }
  159. }
  160. // Validate and add aux-addresses
  161. for key, aa := range auxaddrs {
  162. match := false
  163. for _, s := range subnets {
  164. ok, err := subnetMatches(s, aa)
  165. if err != nil {
  166. return nil, err
  167. }
  168. if !ok {
  169. continue
  170. }
  171. iData[s].AuxAddress[key] = aa
  172. match = true
  173. }
  174. if !match {
  175. return nil, fmt.Errorf("no matching subnet for aux-address %s", aa)
  176. }
  177. }
  178. idl := []network.IPAMConfig{}
  179. for _, v := range iData {
  180. idl = append(idl, *v)
  181. }
  182. return idl, nil
  183. }
  184. func subnetMatches(subnet, data string) (bool, error) {
  185. var (
  186. ip net.IP
  187. )
  188. _, s, err := net.ParseCIDR(subnet)
  189. if err != nil {
  190. return false, fmt.Errorf("Invalid subnet %s : %v", s, err)
  191. }
  192. if strings.Contains(data, "/") {
  193. ip, _, err = net.ParseCIDR(data)
  194. if err != nil {
  195. return false, fmt.Errorf("Invalid cidr %s : %v", data, err)
  196. }
  197. } else {
  198. ip = net.ParseIP(data)
  199. }
  200. return s.Contains(ip), nil
  201. }