Explorar o código

Merge pull request #857 from rmb938/ipam_options

Accept IpamOptions via NetworkOptionIpam
Madhu Venugopal %!s(int64=9) %!d(string=hai) anos
pai
achega
291ab6fb5a

+ 1 - 1
libnetwork/cmd/dnet/dnet.go

@@ -435,7 +435,7 @@ func ipamOption(bridgeName string) libnetwork.NetworkOption {
 		if hip.IsGlobalUnicast() {
 		if hip.IsGlobalUnicast() {
 			ipamV4Conf.Gateway = nw.IP.String()
 			ipamV4Conf.Gateway = nw.IP.String()
 		}
 		}
-		return libnetwork.NetworkOptionIpam("default", "", []*libnetwork.IpamConf{ipamV4Conf}, nil)
+		return libnetwork.NetworkOptionIpam("default", "", []*libnetwork.IpamConf{ipamV4Conf}, nil, nil)
 	}
 	}
 	return nil
 	return nil
 }
 }

+ 1 - 1
libnetwork/docs/ipam.md

@@ -56,7 +56,7 @@ The following sections explain the each of the above API's semantics, when they
 A libnetwork user can provide IPAM related configuration when creating a network, via the `NetworkOptionIpam` setter function. 
 A libnetwork user can provide IPAM related configuration when creating a network, via the `NetworkOptionIpam` setter function. 
 
 
 ```go
 ```go
-func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf) NetworkOption
+func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption
 ```
 ```
 
 
 The caller has to provide the IPAM driver name and may provide the address space and a list of `IpamConf` structures for IPv4 and a list for IPv6. The IPAM driver name is the only mandatory field. If not provided, network creation will fail.
 The caller has to provide the IPAM driver name and may provide the address space and a list of `IpamConf` structures for IPv4 and a list for IPv6. The IPAM driver name is the only mandatory field. If not provided, network creation will fail.

+ 7 - 9
libnetwork/libnetwork_internal_test.go

@@ -67,20 +67,19 @@ func TestNetworkMarshalling(t *testing.T) {
 		networkType: "bridge",
 		networkType: "bridge",
 		enableIPv6:  true,
 		enableIPv6:  true,
 		persist:     true,
 		persist:     true,
+		ipamOptions: map[string]string{
+			netlabel.MacAddress: "a:b:c:d:e:f",
+		},
 		ipamV4Config: []*IpamConf{
 		ipamV4Config: []*IpamConf{
 			&IpamConf{
 			&IpamConf{
 				PreferredPool: "10.2.0.0/16",
 				PreferredPool: "10.2.0.0/16",
 				SubPool:       "10.2.0.0/24",
 				SubPool:       "10.2.0.0/24",
-				Options: map[string]string{
-					netlabel.MacAddress: "a:b:c:d:e:f",
-				},
-				Gateway:      "",
-				AuxAddresses: nil,
+				Gateway:       "",
+				AuxAddresses:  nil,
 			},
 			},
 			&IpamConf{
 			&IpamConf{
 				PreferredPool: "10.2.0.0/16",
 				PreferredPool: "10.2.0.0/16",
 				SubPool:       "10.2.1.0/24",
 				SubPool:       "10.2.1.0/24",
-				Options:       nil,
 				Gateway:       "10.2.1.254",
 				Gateway:       "10.2.1.254",
 			},
 			},
 		},
 		},
@@ -265,7 +264,6 @@ func compareIpamConfList(listA, listB []*IpamConf) bool {
 		b = listB[i]
 		b = listB[i]
 		if a.PreferredPool != b.PreferredPool ||
 		if a.PreferredPool != b.PreferredPool ||
 			a.SubPool != b.SubPool ||
 			a.SubPool != b.SubPool ||
-			!compareStringMaps(a.Options, b.Options) ||
 			a.Gateway != b.Gateway || !compareStringMaps(a.AuxAddresses, b.AuxAddresses) {
 			a.Gateway != b.Gateway || !compareStringMaps(a.AuxAddresses, b.AuxAddresses) {
 			return false
 			return false
 		}
 		}
@@ -374,7 +372,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
 
 
 	// Test whether ipam state release is invoked  on network create failure from net driver
 	// Test whether ipam state release is invoked  on network create failure from net driver
 	// by checking whether subsequent network creation requesting same gateway IP succeeds
 	// by checking whether subsequent network creation requesting same gateway IP succeeds
-	ipamOpt := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.254"}}, nil)
+	ipamOpt := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.254"}}, nil, nil)
 	if _, err := c.NewNetwork(badDriverName, "badnet1", ipamOpt); err == nil {
 	if _, err := c.NewNetwork(badDriverName, "badnet1", ipamOpt); err == nil {
 		t.Fatalf("bad network driver should have failed network creation")
 		t.Fatalf("bad network driver should have failed network creation")
 	}
 	}
@@ -398,7 +396,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
 	}
 	}
 
 
 	// Now create good bridge network with different gateway
 	// Now create good bridge network with different gateway
-	ipamOpt2 := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.253"}}, nil)
+	ipamOpt2 := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.253"}}, nil, nil)
 	gnw, err = c.NewNetwork("bridge", "goodnet2", ipamOpt2)
 	gnw, err = c.NewNetwork("bridge", "goodnet2", ipamOpt2)
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)

+ 3 - 3
libnetwork/libnetwork_test.go

@@ -87,7 +87,7 @@ func createController() error {
 func createTestNetwork(networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) {
 func createTestNetwork(networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) {
 	return controller.NewNetwork(networkType, networkName,
 	return controller.NewNetwork(networkType, networkName,
 		libnetwork.NetworkOptionGeneric(netOption),
 		libnetwork.NetworkOptionGeneric(netOption),
-		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs))
+		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs, nil))
 }
 }
 
 
 func getEmptyGenericOption() map[string]interface{} {
 func getEmptyGenericOption() map[string]interface{} {
@@ -333,7 +333,7 @@ func TestBridgeIpv6FromMac(t *testing.T) {
 
 
 	network, err := controller.NewNetwork(bridgeNetType, "testipv6mac",
 	network, err := controller.NewNetwork(bridgeNetType, "testipv6mac",
 		libnetwork.NetworkOptionGeneric(netOption),
 		libnetwork.NetworkOptionGeneric(netOption),
-		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4ConfList, ipamV6ConfList),
+		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4ConfList, ipamV6ConfList, nil),
 		libnetwork.NetworkOptionDeferIPv6Alloc(true))
 		libnetwork.NetworkOptionDeferIPv6Alloc(true))
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
@@ -1016,7 +1016,7 @@ func TestEndpointJoin(t *testing.T) {
 	ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe90::/64", Gateway: "fe90::22"}}
 	ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe90::/64", Gateway: "fe90::22"}}
 	n1, err := controller.NewNetwork(bridgeNetType, "testnetwork1",
 	n1, err := controller.NewNetwork(bridgeNetType, "testnetwork1",
 		libnetwork.NetworkOptionGeneric(netOption),
 		libnetwork.NetworkOptionGeneric(netOption),
-		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, ipamV6ConfList),
+		libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, ipamV6ConfList, nil),
 		libnetwork.NetworkOptionDeferIPv6Alloc(true))
 		libnetwork.NetworkOptionDeferIPv6Alloc(true))
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)

+ 7 - 13
libnetwork/network.go

@@ -58,7 +58,7 @@ type Network interface {
 
 
 // NetworkInfo returns some configuration and operational information about the network
 // NetworkInfo returns some configuration and operational information about the network
 type NetworkInfo interface {
 type NetworkInfo interface {
-	IpamConfig() (string, []*IpamConf, []*IpamConf)
+	IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf)
 	IpamInfo() ([]*IpamInfo, []*IpamInfo)
 	IpamInfo() ([]*IpamInfo, []*IpamInfo)
 	DriverOptions() map[string]string
 	DriverOptions() map[string]string
 	Scope() string
 	Scope() string
@@ -81,8 +81,6 @@ type IpamConf struct {
 	// A subset of the master pool. If specified,
 	// A subset of the master pool. If specified,
 	// this becomes the container pool
 	// this becomes the container pool
 	SubPool string
 	SubPool string
-	// Input options for IPAM Driver (optional)
-	Options map[string]string
 	// Preferred Network Gateway address (optional)
 	// Preferred Network Gateway address (optional)
 	Gateway string
 	Gateway string
 	// Auxiliary addresses for network driver. Must be within the master pool.
 	// Auxiliary addresses for network driver. Must be within the master pool.
@@ -152,6 +150,7 @@ type network struct {
 	networkType  string
 	networkType  string
 	id           string
 	id           string
 	ipamType     string
 	ipamType     string
+	ipamOptions  map[string]string
 	addrSpace    string
 	addrSpace    string
 	ipamV4Config []*IpamConf
 	ipamV4Config []*IpamConf
 	ipamV6Config []*IpamConf
 	ipamV6Config []*IpamConf
@@ -255,12 +254,6 @@ func (c *IpamConf) CopyTo(dstC *IpamConf) error {
 	dstC.PreferredPool = c.PreferredPool
 	dstC.PreferredPool = c.PreferredPool
 	dstC.SubPool = c.SubPool
 	dstC.SubPool = c.SubPool
 	dstC.Gateway = c.Gateway
 	dstC.Gateway = c.Gateway
-	if c.Options != nil {
-		dstC.Options = make(map[string]string, len(c.Options))
-		for k, v := range c.Options {
-			dstC.Options[k] = v
-		}
-	}
 	if c.AuxAddresses != nil {
 	if c.AuxAddresses != nil {
 		dstC.AuxAddresses = make(map[string]string, len(c.AuxAddresses))
 		dstC.AuxAddresses = make(map[string]string, len(c.AuxAddresses))
 		for k, v := range c.AuxAddresses {
 		for k, v := range c.AuxAddresses {
@@ -502,11 +495,12 @@ func NetworkOptionInternalNetwork() NetworkOption {
 }
 }
 
 
 // NetworkOptionIpam function returns an option setter for the ipam configuration for this network
 // NetworkOptionIpam function returns an option setter for the ipam configuration for this network
-func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf) NetworkOption {
+func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption {
 	return func(n *network) {
 	return func(n *network) {
 		if ipamDriver != "" {
 		if ipamDriver != "" {
 			n.ipamType = ipamDriver
 			n.ipamType = ipamDriver
 		}
 		}
+		n.ipamOptions = opts
 		n.addrSpace = addrSpace
 		n.addrSpace = addrSpace
 		n.ipamV4Config = ipV4
 		n.ipamV4Config = ipV4
 		n.ipamV6Config = ipV6
 		n.ipamV6Config = ipV6
@@ -957,7 +951,7 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
 		d := &IpamInfo{}
 		d := &IpamInfo{}
 		(*infoList)[i] = d
 		(*infoList)[i] = d
 
 
-		d.PoolID, d.Pool, d.Meta, err = ipam.RequestPool(n.addrSpace, cfg.PreferredPool, cfg.SubPool, cfg.Options, ipVer == 6)
+		d.PoolID, d.Pool, d.Meta, err = ipam.RequestPool(n.addrSpace, cfg.PreferredPool, cfg.SubPool, n.ipamOptions, ipVer == 6)
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
@@ -1136,7 +1130,7 @@ func (n *network) Scope() string {
 	return n.driverScope()
 	return n.driverScope()
 }
 }
 
 
-func (n *network) IpamConfig() (string, []*IpamConf, []*IpamConf) {
+func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf) {
 	n.Lock()
 	n.Lock()
 	defer n.Unlock()
 	defer n.Unlock()
 
 
@@ -1155,7 +1149,7 @@ func (n *network) IpamConfig() (string, []*IpamConf, []*IpamConf) {
 		v6L[i] = cc
 		v6L[i] = cc
 	}
 	}
 
 
-	return n.ipamType, v4L, v6L
+	return n.ipamType, n.ipamOptions, v4L, v6L
 }
 }
 
 
 func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
 func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {