daemon/libnetwork: replace EndpointInterface.CopyTo with Copy()

The EndpointInterface.CopyTo function expected the caller to construct an
EndpointInterface to copy to, but all callsites created an empty struct.
In addition, `CopyTo` would never return an error, so the error return
was redundant.

Replace it with a `Copy()` function, which makes it easier to
consume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-10 15:59:31 +02:00
parent 80452e5d4a
commit 2d5100749d
2 changed files with 22 additions and 21 deletions

View File

@@ -295,12 +295,7 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
dstEp.ingressPorts = make([]*PortConfig, len(ep.ingressPorts))
copy(dstEp.ingressPorts, ep.ingressPorts)
if ep.iface != nil {
dstEp.iface = &EndpointInterface{}
if err := ep.iface.CopyTo(dstEp.iface); err != nil {
return err
}
}
dstEp.iface = ep.iface.Copy()
if ep.joinInfo != nil {
dstEp.joinInfo = &endpointJoinInfo{}

View File

@@ -143,26 +143,32 @@ func (epi *EndpointInterface) UnmarshalJSON(b []byte) error {
return nil
}
func (epi *EndpointInterface) CopyTo(dstEpi *EndpointInterface) error {
dstEpi.mac = slices.Clone(epi.mac)
dstEpi.addr = types.GetIPNetCopy(epi.addr)
dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
dstEpi.srcName = epi.srcName
dstEpi.dstPrefix = epi.dstPrefix
dstEpi.dstName = epi.dstName
dstEpi.v4PoolID = epi.v4PoolID
dstEpi.v6PoolID = epi.v6PoolID
dstEpi.createdInContainer = epi.createdInContainer
if len(epi.llAddrs) != 0 {
dstEpi.llAddrs = make([]*net.IPNet, 0, len(epi.llAddrs))
dstEpi.llAddrs = append(dstEpi.llAddrs, epi.llAddrs...)
// Copy returns a deep copy of [EndpointInterface]. If the receiver is nil,
// Copy returns nil.
func (epi *EndpointInterface) Copy() *EndpointInterface {
if epi == nil {
return nil
}
var routes []*net.IPNet
for _, route := range epi.routes {
dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route))
routes = append(routes, types.GetIPNetCopy(route))
}
return nil
return &EndpointInterface{
mac: slices.Clone(epi.mac),
addr: types.GetIPNetCopy(epi.addr),
addrv6: types.GetIPNetCopy(epi.addrv6),
llAddrs: slices.Clone(epi.llAddrs),
srcName: epi.srcName,
dstPrefix: epi.dstPrefix,
dstName: epi.dstName,
routes: routes,
v4PoolID: epi.v4PoolID,
v6PoolID: epi.v6PoolID,
netnsPath: epi.netnsPath,
createdInContainer: epi.createdInContainer,
}
}
type endpointJoinInfo struct {