From bf7277f8fe4df3b7b354ac6f887147d76ee06ab6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 19 Oct 2025 13:48:29 +0200 Subject: [PATCH] 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 --- .../libnetwork/drivers/ipvlan/ipvlan_joinleave.go | 15 +++------------ .../drivers/macvlan/macvlan_joinleave.go | 15 +++------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/daemon/libnetwork/drivers/ipvlan/ipvlan_joinleave.go b/daemon/libnetwork/drivers/ipvlan/ipvlan_joinleave.go index f23862d0bb..c43cc39c3e 100644 --- a/daemon/libnetwork/drivers/ipvlan/ipvlan_joinleave.go +++ b/daemon/libnetwork/drivers/ipvlan/ipvlan_joinleave.go @@ -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) diff --git a/daemon/libnetwork/drivers/macvlan/macvlan_joinleave.go b/daemon/libnetwork/drivers/macvlan/macvlan_joinleave.go index 339e8cd5e2..cd339ddfc1 100644 --- a/daemon/libnetwork/drivers/macvlan/macvlan_joinleave.go +++ b/daemon/libnetwork/drivers/macvlan/macvlan_joinleave.go @@ -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)