Explorar o código

Merge pull request #43760 from thaJeztah/vlan_cleanups

libnetwork: some cleaning up in ipvlan and macvlan drivers
Tianon Gravi %!s(int64=3) %!d(string=hai) anos
pai
achega
c8d18e27bd

+ 6 - 6
libnetwork/drivers/ipvlan/ipvlan.go

@@ -18,10 +18,10 @@ const (
 	containerVethPrefix = "eth"
 	vethPrefix          = "veth"
 
-	ipvlanType    = "ipvlan"             // driver type name
-	parentOpt     = "parent"             // parent interface -o parent
-	driverModeOpt = ipvlanType + "_mode" // mode -o ipvlan_mode
-	driverFlagOpt = ipvlanType + "_flag" // flag -o ipvlan_flag
+	driverName    = "ipvlan"      // driver type name
+	parentOpt     = "parent"      // parent interface -o parent
+	driverModeOpt = "ipvlan_mode" // mode -o ipvlan_mode
+	driverFlagOpt = "ipvlan_flag" // flag -o ipvlan_flag
 
 	modeL2  = "l2"  // ipvlan L2 mode (default)
 	modeL3  = "l3"  // ipvlan L3 mode
@@ -75,7 +75,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
 		return err
 	}
 
-	return dc.RegisterDriver(ipvlanType, d, c)
+	return dc.RegisterDriver(driverName, d, c)
 }
 
 func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
@@ -91,7 +91,7 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, erro
 }
 
 func (d *driver) Type() string {
-	return ipvlanType
+	return driverName
 }
 
 func (d *driver) IsBuiltIn() bool {

+ 3 - 3
libnetwork/drivers/ipvlan/ipvlan_endpoint.go

@@ -27,7 +27,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
 		return fmt.Errorf("network id %q not found", nid)
 	}
 	if ifInfo.MacAddress() != nil {
-		return fmt.Errorf("%s interfaces do not support custom mac address assignment", ipvlanType)
+		return fmt.Errorf("ipvlan interfaces do not support custom mac address assignment")
 	}
 	ep := &endpoint{
 		id:     eid,
@@ -42,7 +42,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
 	if opt, ok := epOptions[netlabel.PortMap]; ok {
 		if _, ok := opt.([]types.PortBinding); ok {
 			if len(opt.([]types.PortBinding)) > 0 {
-				logrus.Warnf("%s driver does not support port mappings", ipvlanType)
+				logrus.Warnf("ipvlan driver does not support port mappings")
 			}
 		}
 	}
@@ -50,7 +50,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
 	if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
 		if _, ok := opt.([]types.TransportPort); ok {
 			if len(opt.([]types.TransportPort)) > 0 {
-				logrus.Warnf("%s driver does not support port exposures", ipvlanType)
+				logrus.Warnf("ipvlan driver does not support port exposures")
 			}
 		}
 	}

+ 8 - 20
libnetwork/drivers/ipvlan/ipvlan_joinleave.go

@@ -171,29 +171,17 @@ func ifaceGateway(dfNet string) (*staticRoute, error) {
 }
 
 // getSubnetforIPv4 returns the ipv4 subnet to which the given IP belongs
-func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
-	for _, s := range n.config.Ipv4Subnets {
-		_, snet, err := net.ParseCIDR(s.SubnetIP)
-		if err != nil {
-			return nil
-		}
-		// first check if the mask lengths are the same
-		i, _ := snet.Mask.Size()
-		j, _ := ip.Mask.Size()
-		if i != j {
-			continue
-		}
-		if snet.Contains(ip.IP) {
-			return s
-		}
-	}
-
-	return nil
+func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipSubnet {
+	return getSubnetForIP(ip, n.config.Ipv4Subnets)
 }
 
 // getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs
-func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
-	for _, s := range n.config.Ipv6Subnets {
+func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipSubnet {
+	return getSubnetForIP(ip, n.config.Ipv6Subnets)
+}
+
+func getSubnetForIP(ip *net.IPNet, subnets []*ipSubnet) *ipSubnet {
+	for _, s := range subnets {
 		_, snet, err := net.ParseCIDR(s.SubnetIP)
 		if err != nil {
 			return nil

+ 60 - 70
libnetwork/drivers/ipvlan/ipvlan_network.go

@@ -22,7 +22,7 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
 	defer osl.InitOSContext()()
 	kv, err := kernel.GetKernelVersion()
 	if err != nil {
-		return fmt.Errorf("Failed to check kernel version for %s driver support: %v", ipvlanType, err)
+		return fmt.Errorf("failed to check kernel version for ipvlan driver support: %v", err)
 	}
 	// ensure Kernel version is >= v4.2 for ipvlan support
 	if kv.Kernel < ipvlanKernelVer || (kv.Kernel == ipvlanKernelVer && kv.Major < ipvlanMajorVer) {
@@ -38,39 +38,8 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
 	if err != nil {
 		return err
 	}
-	config.ID = nid
-	err = config.processIPAM(nid, ipV4Data, ipV6Data)
-	if err != nil {
-		return err
-	}
-	// verify the ipvlan mode from -o ipvlan_mode option
-	switch config.IpvlanMode {
-	case "", modeL2:
-		// default to ipvlan L2 mode if -o ipvlan_mode is empty
-		config.IpvlanMode = modeL2
-	case modeL3:
-		config.IpvlanMode = modeL3
-	case modeL3S:
-		config.IpvlanMode = modeL3S
-	default:
-		return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
-	}
-	// verify the ipvlan flag from -o ipvlan_flag option
-	switch config.IpvlanFlag {
-	case "", flagBridge:
-		// default to bridge if -o ipvlan_flag is empty
-		config.IpvlanFlag = flagBridge
-	case flagPrivate:
-		config.IpvlanFlag = flagPrivate
-	case flagVepa:
-		config.IpvlanFlag = flagVepa
-	default:
-		return fmt.Errorf("requested ipvlan flag '%s' is not valid, 'bridge' is the ipvlan driver default", config.IpvlanFlag)
-	}
-	// loopback is not a valid parent link
-	if config.Parent == "lo" {
-		return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
-	}
+	config.processIPAM(ipV4Data, ipV6Data)
+
 	// if parent interface not specified, create a dummy type link to use named dummy+net_id
 	if config.Parent == "" {
 		config.Parent = getDummyName(stringid.TruncateID(config.ID))
@@ -83,6 +52,7 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
 	if foundExisting {
 		return types.InternalMaskableErrorf("restoring existing network %s", config.ID)
 	}
+
 	// update persistent db, rollback on fail
 	err = d.storeUpdate(config)
 	if err != nil {
@@ -118,7 +88,7 @@ func (d *driver) createNetwork(config *configuration) (bool, error) {
 			}
 			config.CreatedSlaveLink = true
 
-			// notify the user in logs they have limited communications
+			// notify the user in logs that they have limited communications
 			logrus.Debugf("Empty -o parent= flags limit communications to other containers inside of network: %s",
 				config.Parent)
 		} else {
@@ -146,7 +116,7 @@ func (d *driver) createNetwork(config *configuration) (bool, error) {
 	return foundExisting, nil
 }
 
-// DeleteNetwork the network for the specified driver type
+// DeleteNetwork deletes the network for the specified driver type
 func (d *driver) DeleteNetwork(nid string) error {
 	defer osl.InitOSContext()()
 	n := d.network(nid)
@@ -195,7 +165,7 @@ func (d *driver) DeleteNetwork(nid string) error {
 	return nil
 }
 
-// parseNetworkOptions parse docker network options
+// parseNetworkOptions parses docker network options
 func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
 	var (
 		err    error
@@ -212,34 +182,60 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, err
 			config.Internal = true
 		}
 	}
+
+	// verify the ipvlan mode from -o ipvlan_mode option
+	switch config.IpvlanMode {
+	case "":
+		// default to ipvlan L2 mode if -o ipvlan_mode is empty
+		config.IpvlanMode = modeL2
+	case modeL2, modeL3, modeL3S:
+		// valid option
+	default:
+		return nil, fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
+	}
+
+	// verify the ipvlan flag from -o ipvlan_flag option
+	switch config.IpvlanFlag {
+	case "":
+		// default to bridge if -o ipvlan_flag is empty
+		config.IpvlanFlag = flagBridge
+	case flagBridge, flagPrivate, flagVepa:
+		// valid option
+	default:
+		return nil, fmt.Errorf("requested ipvlan flag '%s' is not valid, 'bridge' is the ipvlan driver default", config.IpvlanFlag)
+	}
+
+	// loopback is not a valid parent link
+	if config.Parent == "lo" {
+		return nil, fmt.Errorf("loopback interface is not a valid ipvlan parent link")
+	}
+
+	config.ID = id
 	return config, nil
 }
 
 // parseNetworkGenericOptions parse generic driver docker network options
 func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
-	var (
-		err    error
-		config *configuration
-	)
 	switch opt := data.(type) {
 	case *configuration:
-		config = opt
+		return opt, nil
 	case map[string]string:
-		config = &configuration{}
-		err = config.fromOptions(opt)
+		return newConfigFromLabels(opt), nil
 	case options.Generic:
-		var opaqueConfig interface{}
-		if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
-			config = opaqueConfig.(*configuration)
+		var config *configuration
+		opaqueConfig, err := options.GenerateFromModel(opt, config)
+		if err != nil {
+			return nil, err
 		}
+		return opaqueConfig.(*configuration), nil
 	default:
-		err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
+		return nil, types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
 	}
-	return config, err
 }
 
-// fromOptions binds the generic options to networkConfiguration to cache
-func (config *configuration) fromOptions(labels map[string]string) error {
+// newConfigFromLabels creates a new configuration from the given labels.
+func newConfigFromLabels(labels map[string]string) *configuration {
+	config := &configuration{}
 	for label, value := range labels {
 		switch label {
 		case parentOpt:
@@ -253,28 +249,22 @@ func (config *configuration) fromOptions(labels map[string]string) error {
 			config.IpvlanFlag = value
 		}
 	}
-	return nil
+
+	return config
 }
 
 // processIPAM parses v4 and v6 IP information and binds it to the network configuration
-func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
-	if len(ipamV4Data) > 0 {
-		for _, ipd := range ipamV4Data {
-			s := &ipv4Subnet{
-				SubnetIP: ipd.Pool.String(),
-				GwIP:     ipd.Gateway.String(),
-			}
-			config.Ipv4Subnets = append(config.Ipv4Subnets, s)
-		}
+func (config *configuration) processIPAM(ipamV4Data, ipamV6Data []driverapi.IPAMData) {
+	for _, ipd := range ipamV4Data {
+		config.Ipv4Subnets = append(config.Ipv4Subnets, &ipSubnet{
+			SubnetIP: ipd.Pool.String(),
+			GwIP:     ipd.Gateway.String(),
+		})
 	}
-	if len(ipamV6Data) > 0 {
-		for _, ipd := range ipamV6Data {
-			s := &ipv6Subnet{
-				SubnetIP: ipd.Pool.String(),
-				GwIP:     ipd.Gateway.String(),
-			}
-			config.Ipv6Subnets = append(config.Ipv6Subnets, s)
-		}
+	for _, ipd := range ipamV6Data {
+		config.Ipv6Subnets = append(config.Ipv6Subnets, &ipSubnet{
+			SubnetIP: ipd.Pool.String(),
+			GwIP:     ipd.Gateway.String(),
+		})
 	}
-	return nil
 }

+ 3 - 3
libnetwork/drivers/ipvlan/ipvlan_setup.go

@@ -38,7 +38,7 @@ func createIPVlan(containerIfName, parent, ipvlanMode, ipvlanFlag string) (strin
 	// Get the link for the master index (Example: the docker host eth iface)
 	parentLink, err := ns.NlHandle().LinkByName(parent)
 	if err != nil {
-		return "", fmt.Errorf("error occurred looking up the %s parent iface %s error: %s", ipvlanType, parent, err)
+		return "", fmt.Errorf("error occurred looking up the ipvlan parent iface %s error: %s", parent, err)
 	}
 	// Create an ipvlan link
 	ipvlan := &netlink.IPVlan{
@@ -51,7 +51,7 @@ func createIPVlan(containerIfName, parent, ipvlanMode, ipvlanFlag string) (strin
 	}
 	if err := ns.NlHandle().LinkAdd(ipvlan); err != nil {
 		// If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
-		return "", fmt.Errorf("failed to create the %s port: %v", ipvlanType, err)
+		return "", fmt.Errorf("failed to create the ipvlan port: %v", err)
 	}
 
 	return ipvlan.Attrs().Name, nil
@@ -190,7 +190,7 @@ func createDummyLink(dummyName, truncNetID string) error {
 	}
 	parentDummyLink, err := ns.NlHandle().LinkByName(dummyName)
 	if err != nil {
-		return fmt.Errorf("error occurred looking up the %s parent iface %s error: %s", ipvlanType, dummyName, err)
+		return fmt.Errorf("error occurred looking up the ipvlan parent iface %s error: %s", dummyName, err)
 	}
 	// bring the new netlink iface up
 	if err := ns.NlHandle().LinkSetUp(parentDummyLink); err != nil {

+ 3 - 8
libnetwork/drivers/ipvlan/ipvlan_store.go

@@ -32,16 +32,11 @@ type configuration struct {
 	IpvlanMode       string
 	IpvlanFlag       string
 	CreatedSlaveLink bool
-	Ipv4Subnets      []*ipv4Subnet
-	Ipv6Subnets      []*ipv6Subnet
+	Ipv4Subnets      []*ipSubnet
+	Ipv6Subnets      []*ipSubnet
 }
 
-type ipv4Subnet struct {
-	SubnetIP string
-	GwIP     string
-}
-
-type ipv6Subnet struct {
+type ipSubnet struct {
 	SubnetIP string
 	GwIP     string
 }

+ 9 - 11
libnetwork/drivers/macvlan/macvlan.go

@@ -17,17 +17,15 @@ const (
 	vethLen             = 7
 	containerVethPrefix = "eth"
 	vethPrefix          = "veth"
-	macvlanType         = "macvlan"  // driver type name
-	modePrivate         = "private"  // macvlan mode private
-	modeVepa            = "vepa"     // macvlan mode vepa
-	modeBridge          = "bridge"   // macvlan mode bridge
-	modePassthru        = "passthru" // macvlan mode passthrough
-	parentOpt           = "parent"   // parent interface -o parent
-	modeOpt             = "_mode"    // macvlan mode ux opt suffix
+	driverName          = "macvlan"      // driver type name
+	modePrivate         = "private"      // macvlan mode private
+	modeVepa            = "vepa"         // macvlan mode vepa
+	modeBridge          = "bridge"       // macvlan mode bridge
+	modePassthru        = "passthru"     // macvlan mode passthrough
+	parentOpt           = "parent"       // parent interface -o parent
+	driverModeOpt       = "macvlan_mode" // macvlan mode ux opt suffix
 )
 
-var driverModeOpt = macvlanType + modeOpt // mode --option macvlan_mode
-
 type endpointTable map[string]*endpoint
 
 type networkTable map[string]*network
@@ -71,7 +69,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
 		return err
 	}
 
-	return dc.RegisterDriver(macvlanType, d, c)
+	return dc.RegisterDriver(driverName, d, c)
 }
 
 func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
@@ -87,7 +85,7 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, erro
 }
 
 func (d *driver) Type() string {
-	return macvlanType
+	return driverName
 }
 
 func (d *driver) IsBuiltIn() bool {

+ 2 - 2
libnetwork/drivers/macvlan/macvlan_endpoint.go

@@ -47,7 +47,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
 	if opt, ok := epOptions[netlabel.PortMap]; ok {
 		if _, ok := opt.([]types.PortBinding); ok {
 			if len(opt.([]types.PortBinding)) > 0 {
-				logrus.Warnf("%s driver does not support port mappings", macvlanType)
+				logrus.Warnf("macvlan driver does not support port mappings")
 			}
 		}
 	}
@@ -55,7 +55,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
 	if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
 		if _, ok := opt.([]types.TransportPort); ok {
 			if len(opt.([]types.TransportPort)) > 0 {
-				logrus.Warnf("%s driver does not support port exposures", macvlanType)
+				logrus.Warnf("macvlan driver does not support port exposures")
 			}
 		}
 	}

+ 10 - 21
libnetwork/drivers/macvlan/macvlan_joinleave.go

@@ -94,6 +94,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
 	if err := d.storeUpdate(ep); err != nil {
 		return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
 	}
+
 	return nil
 }
 
@@ -115,30 +116,18 @@ func (d *driver) Leave(nid, eid string) error {
 	return nil
 }
 
-// getSubnetforIP returns the ipv4 subnet to which the given IP belongs
-func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
-	for _, s := range n.config.Ipv4Subnets {
-		_, snet, err := net.ParseCIDR(s.SubnetIP)
-		if err != nil {
-			return nil
-		}
-		// first check if the mask lengths are the same
-		i, _ := snet.Mask.Size()
-		j, _ := ip.Mask.Size()
-		if i != j {
-			continue
-		}
-		if snet.Contains(ip.IP) {
-			return s
-		}
-	}
-
-	return nil
+// getSubnetforIPv4 returns the ipv4 subnet to which the given IP belongs
+func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipSubnet {
+	return getSubnetForIP(ip, n.config.Ipv4Subnets)
 }
 
 // getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs
-func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
-	for _, s := range n.config.Ipv6Subnets {
+func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipSubnet {
+	return getSubnetForIP(ip, n.config.Ipv6Subnets)
+}
+
+func getSubnetForIP(ip *net.IPNet, subnets []*ipSubnet) *ipSubnet {
+	for _, s := range subnets {
 		_, snet, err := net.ParseCIDR(s.SubnetIP)
 		if err != nil {
 			return nil

+ 45 - 60
libnetwork/drivers/macvlan/macvlan_network.go

@@ -29,29 +29,8 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
 	if err != nil {
 		return err
 	}
-	config.ID = nid
-	err = config.processIPAM(nid, ipV4Data, ipV6Data)
-	if err != nil {
-		return err
-	}
-	// verify the macvlan mode from -o macvlan_mode option
-	switch config.MacvlanMode {
-	case "", modeBridge:
-		// default to macvlan bridge mode if -o macvlan_mode is empty
-		config.MacvlanMode = modeBridge
-	case modePrivate:
-		config.MacvlanMode = modePrivate
-	case modePassthru:
-		config.MacvlanMode = modePassthru
-	case modeVepa:
-		config.MacvlanMode = modeVepa
-	default:
-		return fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode)
-	}
-	// loopback is not a valid parent link
-	if config.Parent == "lo" {
-		return fmt.Errorf("loopback interface is not a valid %s parent link", macvlanType)
-	}
+	config.processIPAM(ipV4Data, ipV6Data)
+
 	// if parent interface not specified, create a dummy type link to use named dummy+net_id
 	if config.Parent == "" {
 		config.Parent = getDummyName(stringid.TruncateID(config.ID))
@@ -99,8 +78,9 @@ func (d *driver) createNetwork(config *configuration) (bool, error) {
 				return false, err
 			}
 			config.CreatedSlaveLink = true
+
 			// notify the user in logs that they have limited communications
-			logrus.Debugf("Empty -o parent= limit communications to other containers inside of network: %s",
+			logrus.Debugf("Empty -o parent= flags limit communications to other containers inside of network: %s",
 				config.Parent)
 		} else {
 			// if the subinterface parent_iface.vlan_id checks do not pass, return err.
@@ -194,35 +174,48 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, err
 		}
 	}
 
+	// verify the macvlan mode from -o macvlan_mode option
+	switch config.MacvlanMode {
+	case "":
+		// default to macvlan bridge mode if -o macvlan_mode is empty
+		config.MacvlanMode = modeBridge
+	case modeBridge, modePrivate, modePassthru, modeVepa:
+		// valid option
+	default:
+		return nil, fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode)
+	}
+
+	// loopback is not a valid parent link
+	if config.Parent == "lo" {
+		return nil, fmt.Errorf("loopback interface is not a valid macvlan parent link")
+	}
+
+	config.ID = id
 	return config, nil
 }
 
 // parseNetworkGenericOptions parses generic driver docker network options
 func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
-	var (
-		err    error
-		config *configuration
-	)
 	switch opt := data.(type) {
 	case *configuration:
-		config = opt
+		return opt, nil
 	case map[string]string:
-		config = &configuration{}
-		err = config.fromOptions(opt)
+		return newConfigFromLabels(opt), nil
 	case options.Generic:
-		var opaqueConfig interface{}
-		if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
-			config = opaqueConfig.(*configuration)
+		var config *configuration
+		opaqueConfig, err := options.GenerateFromModel(opt, config)
+		if err != nil {
+			return nil, err
 		}
+		return opaqueConfig.(*configuration), nil
 	default:
-		err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
+		return nil, types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
 	}
-
-	return config, err
 }
 
-// fromOptions binds the generic options to networkConfiguration to cache
-func (config *configuration) fromOptions(labels map[string]string) error {
+// newConfigFromLabels creates a new configuration from the given labels.
+func newConfigFromLabels(labels map[string]string) *configuration {
+	config := &configuration{}
 	for label, value := range labels {
 		switch label {
 		case parentOpt:
@@ -234,29 +227,21 @@ func (config *configuration) fromOptions(labels map[string]string) error {
 		}
 	}
 
-	return nil
+	return config
 }
 
 // processIPAM parses v4 and v6 IP information and binds it to the network configuration
-func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
-	if len(ipamV4Data) > 0 {
-		for _, ipd := range ipamV4Data {
-			s := &ipv4Subnet{
-				SubnetIP: ipd.Pool.String(),
-				GwIP:     ipd.Gateway.String(),
-			}
-			config.Ipv4Subnets = append(config.Ipv4Subnets, s)
-		}
+func (config *configuration) processIPAM(ipamV4Data, ipamV6Data []driverapi.IPAMData) {
+	for _, ipd := range ipamV4Data {
+		config.Ipv4Subnets = append(config.Ipv4Subnets, &ipSubnet{
+			SubnetIP: ipd.Pool.String(),
+			GwIP:     ipd.Gateway.String(),
+		})
+	}
+	for _, ipd := range ipamV6Data {
+		config.Ipv6Subnets = append(config.Ipv6Subnets, &ipSubnet{
+			SubnetIP: ipd.Pool.String(),
+			GwIP:     ipd.Gateway.String(),
+		})
 	}
-	if len(ipamV6Data) > 0 {
-		for _, ipd := range ipamV6Data {
-			s := &ipv6Subnet{
-				SubnetIP: ipd.Pool.String(),
-				GwIP:     ipd.Gateway.String(),
-			}
-			config.Ipv6Subnets = append(config.Ipv6Subnets, s)
-		}
-	}
-
-	return nil
 }

+ 3 - 3
libnetwork/drivers/macvlan/macvlan_setup.go

@@ -31,7 +31,7 @@ func createMacVlan(containerIfName, parent, macvlanMode string) (string, error)
 	// Get the link for the master index (Example: the docker host eth iface)
 	parentLink, err := ns.NlHandle().LinkByName(parent)
 	if err != nil {
-		return "", fmt.Errorf("error occurred looking up the %s parent iface %s error: %s", macvlanType, parent, err)
+		return "", fmt.Errorf("error occurred looking up the macvlan parent iface %s error: %s", parent, err)
 	}
 	// Create a macvlan link
 	macvlan := &netlink.Macvlan{
@@ -43,7 +43,7 @@ func createMacVlan(containerIfName, parent, macvlanMode string) (string, error)
 	}
 	if err := ns.NlHandle().LinkAdd(macvlan); err != nil {
 		// If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
-		return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
+		return "", fmt.Errorf("failed to create the macvlan port: %v", err)
 	}
 
 	return macvlan.Attrs().Name, nil
@@ -170,7 +170,7 @@ func createDummyLink(dummyName, truncNetID string) error {
 	}
 	parentDummyLink, err := ns.NlHandle().LinkByName(dummyName)
 	if err != nil {
-		return fmt.Errorf("error occurred looking up the %s parent iface %s error: %s", macvlanType, dummyName, err)
+		return fmt.Errorf("error occurred looking up the macvlan parent iface %s error: %s", dummyName, err)
 	}
 	// bring the new netlink iface up
 	if err := ns.NlHandle().LinkSetUp(parentDummyLink); err != nil {

+ 3 - 8
libnetwork/drivers/macvlan/macvlan_store.go

@@ -31,16 +31,11 @@ type configuration struct {
 	Parent           string
 	MacvlanMode      string
 	CreatedSlaveLink bool
-	Ipv4Subnets      []*ipv4Subnet
-	Ipv6Subnets      []*ipv6Subnet
+	Ipv4Subnets      []*ipSubnet
+	Ipv6Subnets      []*ipSubnet
 }
 
-type ipv4Subnet struct {
-	SubnetIP string
-	GwIP     string
-}
-
-type ipv6Subnet struct {
+type ipSubnet struct {
 	SubnetIP string
 	GwIP     string
 }