Browse code

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>

Sebastiaan van Stijn authored on 2025/08/10 22:59:31
Showing 2 changed files
... ...
@@ -295,12 +295,7 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
295 295
 	dstEp.ingressPorts = make([]*PortConfig, len(ep.ingressPorts))
296 296
 	copy(dstEp.ingressPorts, ep.ingressPorts)
297 297
 
298
-	if ep.iface != nil {
299
-		dstEp.iface = &EndpointInterface{}
300
-		if err := ep.iface.CopyTo(dstEp.iface); err != nil {
301
-			return err
302
-		}
303
-	}
298
+	dstEp.iface = ep.iface.Copy()
304 299
 
305 300
 	if ep.joinInfo != nil {
306 301
 		dstEp.joinInfo = &endpointJoinInfo{}
... ...
@@ -143,26 +143,32 @@ func (epi *EndpointInterface) UnmarshalJSON(b []byte) error {
143 143
 	return nil
144 144
 }
145 145
 
146
-func (epi *EndpointInterface) CopyTo(dstEpi *EndpointInterface) error {
147
-	dstEpi.mac = slices.Clone(epi.mac)
148
-	dstEpi.addr = types.GetIPNetCopy(epi.addr)
149
-	dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
150
-	dstEpi.srcName = epi.srcName
151
-	dstEpi.dstPrefix = epi.dstPrefix
152
-	dstEpi.dstName = epi.dstName
153
-	dstEpi.v4PoolID = epi.v4PoolID
154
-	dstEpi.v6PoolID = epi.v6PoolID
155
-	dstEpi.createdInContainer = epi.createdInContainer
156
-	if len(epi.llAddrs) != 0 {
157
-		dstEpi.llAddrs = make([]*net.IPNet, 0, len(epi.llAddrs))
158
-		dstEpi.llAddrs = append(dstEpi.llAddrs, epi.llAddrs...)
146
+// Copy returns a deep copy of [EndpointInterface]. If the receiver is nil,
147
+// Copy returns nil.
148
+func (epi *EndpointInterface) Copy() *EndpointInterface {
149
+	if epi == nil {
150
+		return nil
159 151
 	}
160 152
 
153
+	var routes []*net.IPNet
161 154
 	for _, route := range epi.routes {
162
-		dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route))
155
+		routes = append(routes, types.GetIPNetCopy(route))
156
+	}
157
+
158
+	return &EndpointInterface{
159
+		mac:                slices.Clone(epi.mac),
160
+		addr:               types.GetIPNetCopy(epi.addr),
161
+		addrv6:             types.GetIPNetCopy(epi.addrv6),
162
+		llAddrs:            slices.Clone(epi.llAddrs),
163
+		srcName:            epi.srcName,
164
+		dstPrefix:          epi.dstPrefix,
165
+		dstName:            epi.dstName,
166
+		routes:             routes,
167
+		v4PoolID:           epi.v4PoolID,
168
+		v6PoolID:           epi.v6PoolID,
169
+		netnsPath:          epi.netnsPath,
170
+		createdInContainer: epi.createdInContainer,
163 171
 	}
164
-
165
-	return nil
166 172
 }
167 173
 
168 174
 type endpointJoinInfo struct {