create.go 5.9 KB

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