Преглед изворни кода

libnetwork: macvlan: use single ipSubnet type

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn пре 3 година
родитељ
комит
afeb4c7a6e

+ 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 {
 	if err := d.storeUpdate(ep); err != nil {
 		return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
 		return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
 	}
 	}
+
 	return nil
 	return nil
 }
 }
 
 
@@ -115,30 +116,18 @@ func (d *driver) Leave(nid, eid string) error {
 	return nil
 	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
 // 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)
 		_, snet, err := net.ParseCIDR(s.SubnetIP)
 		if err != nil {
 		if err != nil {
 			return nil
 			return nil

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

@@ -241,7 +241,7 @@ func (config *configuration) fromOptions(labels map[string]string) error {
 func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
 func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
 	if len(ipamV4Data) > 0 {
 	if len(ipamV4Data) > 0 {
 		for _, ipd := range ipamV4Data {
 		for _, ipd := range ipamV4Data {
-			s := &ipv4Subnet{
+			s := &ipSubnet{
 				SubnetIP: ipd.Pool.String(),
 				SubnetIP: ipd.Pool.String(),
 				GwIP:     ipd.Gateway.String(),
 				GwIP:     ipd.Gateway.String(),
 			}
 			}
@@ -250,7 +250,7 @@ func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []dri
 	}
 	}
 	if len(ipamV6Data) > 0 {
 	if len(ipamV6Data) > 0 {
 		for _, ipd := range ipamV6Data {
 		for _, ipd := range ipamV6Data {
-			s := &ipv6Subnet{
+			s := &ipSubnet{
 				SubnetIP: ipd.Pool.String(),
 				SubnetIP: ipd.Pool.String(),
 				GwIP:     ipd.Gateway.String(),
 				GwIP:     ipd.Gateway.String(),
 			}
 			}

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

@@ -31,16 +31,11 @@ type configuration struct {
 	Parent           string
 	Parent           string
 	MacvlanMode      string
 	MacvlanMode      string
 	CreatedSlaveLink bool
 	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
 	SubnetIP string
 	GwIP     string
 	GwIP     string
 }
 }