libnetwork/drivers/macvlan, ipvlan: remove getSubnetforIPv6, getSubnetforIPv4

These methods were just wrappers around getSubnetforIP; let's peel away the
abstraction and call it directly; we're already checking for n.config.Ipv4Subnet
and n.config.Ipv6Subnets on the call-site, so may as well just pass it in.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-19 13:48:29 +02:00
parent a294445345
commit bf7277f8fe
2 changed files with 6 additions and 24 deletions

View File

@@ -91,7 +91,7 @@ func (d *driver) Join(ctx context.Context, nid, eid string, sboxKey string, jinf
case modeL2:
// parse and correlate the endpoint v4 address with the available v4 subnets
if ep.addr != nil && len(n.config.Ipv4Subnets) > 0 {
s := n.getSubnetforIPv4(ep.addr)
s := getSubnetForIP(ep.addr, n.config.Ipv4Subnets)
if s == nil {
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
}
@@ -116,7 +116,7 @@ func (d *driver) Join(ctx context.Context, nid, eid string, sboxKey string, jinf
}
// parse and correlate the endpoint v6 address with the available v6 subnets
if ep.addrv6 != nil && len(n.config.Ipv6Subnets) > 0 {
s := n.getSubnetforIPv6(ep.addrv6)
s := getSubnetForIP(ep.addrv6, n.config.Ipv6Subnets)
if s == nil {
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
}
@@ -195,16 +195,7 @@ func ifaceGateway(dfNet string) (*staticRoute, error) {
return defaultRoute, 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) *ipSubnet {
return getSubnetForIP(ip, n.config.Ipv6Subnets)
}
// getSubnetForIP returns the (IPv4 or IPv6) subnet to which the given IP belongs.
func getSubnetForIP(ip *net.IPNet, subnets []*ipSubnet) *ipSubnet {
for _, s := range subnets {
_, snet, err := net.ParseCIDR(s.SubnetIP)

View File

@@ -51,7 +51,7 @@ func (d *driver) Join(ctx context.Context, nid, eid string, sboxKey string, jinf
if !n.config.Internal {
// parse and correlate the endpoint v4 address with the available v4 subnets
if ep.addr != nil && len(n.config.Ipv4Subnets) > 0 {
s := n.getSubnetforIPv4(ep.addr)
s := getSubnetForIP(ep.addr, n.config.Ipv4Subnets)
if s == nil {
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
}
@@ -76,7 +76,7 @@ func (d *driver) Join(ctx context.Context, nid, eid string, sboxKey string, jinf
}
// parse and correlate the endpoint v6 address with the available v6 subnets
if ep.addrv6 != nil && len(n.config.Ipv6Subnets) > 0 {
s := n.getSubnetforIPv6(ep.addrv6)
s := getSubnetForIP(ep.addrv6, n.config.Ipv6Subnets)
if s == nil {
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
}
@@ -139,16 +139,7 @@ func (d *driver) Leave(nid, eid string) error {
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) *ipSubnet {
return getSubnetForIP(ip, n.config.Ipv6Subnets)
}
// getSubnetForIP returns the (IPv4 or IPv6) subnet to which the given IP belongs.
func getSubnetForIP(ip *net.IPNet, subnets []*ipSubnet) *ipSubnet {
for _, s := range subnets {
_, snet, err := net.ParseCIDR(s.SubnetIP)