daemon/libnetwork/types: remove GetIPCopy; use slices.Clone

We can replace this utility with slices.Clone, which provides the
same functionality.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-10 14:44:41 +02:00
parent 115b801a3b
commit 68a94ecbb5
3 changed files with 15 additions and 24 deletions

View File

@@ -1181,10 +1181,10 @@ func TestSetDefaultGw(t *testing.T) {
}
ipam4 := getIPv4Data(t)
gw4 := types.GetIPCopy(ipam4[0].Pool.IP).To4()
gw4 := slices.Clone(ipam4[0].Pool.IP).To4()
gw4[3] = 254
ipam6 := getIPv6Data(t)
gw6 := types.GetIPCopy(ipam6[0].Pool.IP)
gw6 := slices.Clone(ipam6[0].Pool.IP)
gw6[15] = 0x42
option := map[string]any{

View File

@@ -372,7 +372,7 @@ func (ep *Endpoint) Gateway() net.IP {
return net.IP{}
}
return types.GetIPCopy(ep.joinInfo.gw)
return slices.Clone(ep.joinInfo.gw)
}
// GatewayIPv6 returns the IPv6 gateway assigned by the driver.
@@ -385,7 +385,7 @@ func (ep *Endpoint) GatewayIPv6() net.IP {
return net.IP{}
}
return types.GetIPCopy(ep.joinInfo.gw6)
return slices.Clone(ep.joinInfo.gw6)
}
// SetGateway sets the default IPv4 gateway when a container joins the endpoint.
@@ -393,7 +393,7 @@ func (ep *Endpoint) SetGateway(gw net.IP) error {
ep.mu.Lock()
defer ep.mu.Unlock()
ep.joinInfo.gw = types.GetIPCopy(gw)
ep.joinInfo.gw = slices.Clone(gw)
return nil
}
@@ -402,7 +402,7 @@ func (ep *Endpoint) SetGatewayIPv6(gw6 net.IP) error {
ep.mu.Lock()
defer ep.mu.Unlock()
ep.joinInfo.gw6 = types.GetIPCopy(gw6)
ep.joinInfo.gw6 = slices.Clone(gw6)
return nil
}
@@ -510,7 +510,7 @@ func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
copy(dstEpj.StaticRoutes, epj.StaticRoutes)
dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
copy(dstEpj.driverTableEntries, epj.driverTableEntries)
dstEpj.gw = types.GetIPCopy(epj.gw)
dstEpj.gw6 = types.GetIPCopy(epj.gw6)
dstEpj.gw = slices.Clone(epj.gw)
dstEpj.gw6 = slices.Clone(epj.gw6)
return nil
}

View File

@@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"net"
"slices"
"strconv"
"strings"
"syscall"
@@ -111,9 +112,9 @@ func (p PortBinding) ContainerAddr() (net.Addr, error) {
func (p *PortBinding) GetCopy() PortBinding {
return PortBinding{
Proto: p.Proto,
IP: GetIPCopy(p.IP),
IP: slices.Clone(p.IP),
Port: p.Port,
HostIP: GetIPCopy(p.HostIP),
HostIP: slices.Clone(p.HostIP),
HostPort: p.HostPort,
HostPortEnd: p.HostPortEnd,
}
@@ -214,16 +215,6 @@ func ParseProtocol(s string) Protocol {
}
}
// GetIPCopy returns a copy of the passed IP address
func GetIPCopy(from net.IP) net.IP {
if from == nil {
return nil
}
to := make(net.IP, len(from))
copy(to, from)
return to
}
// GetIPNetCopy returns a copy of the passed IP Network
func GetIPNetCopy(from *net.IPNet) *net.IPNet {
if from == nil {
@@ -231,7 +222,7 @@ func GetIPNetCopy(from *net.IPNet) *net.IPNet {
}
bm := make(net.IPMask, len(from.Mask))
copy(bm, from.Mask)
return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
return &net.IPNet{IP: slices.Clone(from.IP), Mask: bm}
}
// GetIPNetCanonical returns the canonical form for the passed network
@@ -292,7 +283,7 @@ func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
}
// Compute host portion
out := GetIPCopy(ip)
out := slices.Clone(ip)
for i := 0; i < len(mask[ms:]); i++ {
out[is+i] &= ^mask[ms+i]
}
@@ -311,7 +302,7 @@ func GetBroadcastIP(ip net.IP, mask net.IPMask) (net.IP, error) {
}
// Compute broadcast address
out := GetIPCopy(ip)
out := slices.Clone(ip)
for i := 0; i < len(mask[ms:]); i++ {
out[is+i] |= ^mask[ms+i]
}
@@ -348,7 +339,7 @@ func (r *StaticRoute) GetCopy() *StaticRoute {
return &StaticRoute{
Destination: GetIPNetCopy(r.Destination),
RouteType: r.RouteType,
NextHop: GetIPCopy(r.NextHop),
NextHop: slices.Clone(r.NextHop),
}
}