diff --git a/libnetwork/drivers/ipvlan/ipvlan_joinleave.go b/libnetwork/drivers/ipvlan/ipvlan_joinleave.go index de143db4b4..360c62fe26 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_joinleave.go +++ b/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 diff --git a/libnetwork/drivers/ipvlan/ipvlan_network.go b/libnetwork/drivers/ipvlan/ipvlan_network.go index d1d7fd071c..b4875250a7 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_network.go +++ b/libnetwork/drivers/ipvlan/ipvlan_network.go @@ -260,7 +260,7 @@ func (config *configuration) fromOptions(labels map[string]string) error { func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error { if len(ipamV4Data) > 0 { for _, ipd := range ipamV4Data { - s := &ipv4Subnet{ + s := &ipSubnet{ SubnetIP: ipd.Pool.String(), GwIP: ipd.Gateway.String(), } @@ -269,7 +269,7 @@ func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []dri } if len(ipamV6Data) > 0 { for _, ipd := range ipamV6Data { - s := &ipv6Subnet{ + s := &ipSubnet{ SubnetIP: ipd.Pool.String(), GwIP: ipd.Gateway.String(), } diff --git a/libnetwork/drivers/ipvlan/ipvlan_store.go b/libnetwork/drivers/ipvlan/ipvlan_store.go index ec6dd5cecd..cb8eafce2b 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_store.go +++ b/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 }