Skip to content

Commit f17f923

Browse files
authored
Merge pull request moby#50686 from thaJeztah/libnet_less_copyto
daemon/libnetwork: refactor, modernize various `CopyTo` functions, remove redundant utilities
2 parents 82ea65e + 6505e8d commit f17f923

File tree

15 files changed

+145
-250
lines changed

15 files changed

+145
-250
lines changed

daemon/libnetwork/drivers/bridge/bridge_linux.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,19 +1399,14 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]any, error) {
13991399
m := make(map[string]any)
14001400

14011401
if ep.extConnConfig != nil && ep.extConnConfig.ExposedPorts != nil {
1402-
// Return a copy of the config data
1403-
epc := make([]types.TransportPort, 0, len(ep.extConnConfig.ExposedPorts))
1404-
for _, tp := range ep.extConnConfig.ExposedPorts {
1405-
epc = append(epc, tp.GetCopy())
1406-
}
1407-
m[netlabel.ExposedPorts] = epc
1402+
m[netlabel.ExposedPorts] = slices.Clone(ep.extConnConfig.ExposedPorts)
14081403
}
14091404

14101405
if ep.portMapping != nil {
14111406
// Return a copy of the operational data
14121407
pmc := make([]types.PortBinding, 0, len(ep.portMapping))
14131408
for _, pm := range ep.portMapping {
1414-
pmc = append(pmc, pm.PortBinding.GetCopy())
1409+
pmc = append(pmc, pm.PortBinding.Copy())
14151410
}
14161411
m[netlabel.PortMap] = pmc
14171412
}
@@ -1643,7 +1638,7 @@ func (ep *bridgeEndpoint) trimPortBindings(ctx context.Context, n *bridgeNetwork
16431638
undo := func() []portmapperapi.PortBinding {
16441639
pbReq := make([]portmapperapi.PortBindingReq, 0, len(toDrop))
16451640
for _, pb := range toDrop {
1646-
pbReq = append(pbReq, portmapperapi.PortBindingReq{PortBinding: pb.GetCopy()})
1641+
pbReq = append(pbReq, portmapperapi.PortBindingReq{PortBinding: pb.Copy()})
16471642
}
16481643
pbs, err := n.addPortMappings(ctx, ep, pbReq, n.config.DefaultBindingIP, ep.portBindingState)
16491644
if err != nil {
@@ -1895,7 +1890,7 @@ func parseConnectivityOptions(cOptions map[string]any) (*connectivityConfigurati
18951890
if pbs, ok := opt.([]types.PortBinding); ok {
18961891
cc.PortBindings = sliceutil.Map(pbs, func(pb types.PortBinding) portmapperapi.PortBindingReq {
18971892
return portmapperapi.PortBindingReq{
1898-
PortBinding: pb.GetCopy(),
1893+
PortBinding: pb.Copy(),
18991894
}
19001895
})
19011896
} else {

daemon/libnetwork/drivers/bridge/bridge_linux_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,9 @@ func compareConnConfig(a, b *connectivityConfiguration) bool {
158158
if a == nil || b == nil {
159159
return false
160160
}
161-
if len(a.ExposedPorts) != len(b.ExposedPorts) ||
162-
len(a.PortBindings) != len(b.PortBindings) {
161+
if !slices.Equal(a.ExposedPorts, b.ExposedPorts) {
163162
return false
164163
}
165-
for i := 0; i < len(a.ExposedPorts); i++ {
166-
if !a.ExposedPorts[i].Equal(&b.ExposedPorts[i]) {
167-
return false
168-
}
169-
}
170164
for i := 0; i < len(a.PortBindings); i++ {
171165
if !comparePortBinding(&a.PortBindings[i].PortBinding, &b.PortBindings[i].PortBinding) {
172166
return false
@@ -718,7 +712,7 @@ func (i *testInterface) SetMacAddress(mac net.HardwareAddr) error {
718712
if mac == nil {
719713
return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
720714
}
721-
i.mac = types.GetMacCopy(mac)
715+
i.mac = slices.Clone(mac)
722716
return nil
723717
}
724718

@@ -1181,10 +1175,10 @@ func TestSetDefaultGw(t *testing.T) {
11811175
}
11821176

11831177
ipam4 := getIPv4Data(t)
1184-
gw4 := types.GetIPCopy(ipam4[0].Pool.IP).To4()
1178+
gw4 := slices.Clone(ipam4[0].Pool.IP).To4()
11851179
gw4[3] = 254
11861180
ipam6 := getIPv6Data(t)
1187-
gw6 := types.GetIPCopy(ipam6[0].Pool.IP)
1181+
gw6 := slices.Clone(ipam6[0].Pool.IP)
11881182
gw6[15] = 0x42
11891183

11901184
option := map[string]any{

daemon/libnetwork/drivers/bridge/internal/firewaller/stub.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,14 @@ func (nw *StubFirewallerNetwork) AddPorts(_ context.Context, pbs []types.PortBin
109109
if nw.PortExists(pb) {
110110
return nil
111111
}
112-
nw.Ports = append(nw.Ports, pb.GetCopy())
112+
nw.Ports = append(nw.Ports, pb.Copy())
113113
}
114114
return nil
115115
}
116116

117117
func (nw *StubFirewallerNetwork) DelPorts(_ context.Context, pbs []types.PortBinding) error {
118118
for _, pb := range pbs {
119-
nw.Ports = slices.DeleteFunc(nw.Ports, func(p types.PortBinding) bool {
120-
return p.Equal(&pb)
121-
})
119+
nw.Ports = slices.DeleteFunc(nw.Ports, pb.Equal)
122120
}
123121
return nil
124122
}
@@ -130,13 +128,7 @@ func (nw *StubFirewallerNetwork) AddLink(_ context.Context, parentIP, childIP ne
130128
nw.Links = append(nw.Links, stubFirewallerLink{
131129
parentIP: parentIP,
132130
childIP: childIP,
133-
ports: func() []types.TransportPort {
134-
res := make([]types.TransportPort, 0, len(ports))
135-
for _, p := range ports {
136-
res = append(res, p.GetCopy())
137-
}
138-
return res
139-
}(),
131+
ports: slices.Clone(ports),
140132
})
141133
return nil
142134
}
@@ -148,9 +140,7 @@ func (nw *StubFirewallerNetwork) DelLink(_ context.Context, parentIP, childIP ne
148140
}
149141

150142
func (nw *StubFirewallerNetwork) PortExists(pb types.PortBinding) bool {
151-
return slices.ContainsFunc(nw.Ports, func(p types.PortBinding) bool {
152-
return p.Equal(&pb)
153-
})
143+
return slices.ContainsFunc(nw.Ports, pb.Equal)
154144
}
155145

156146
func (nw *StubFirewallerNetwork) LinkExists(parentIP, childIP netip.Addr, ports []types.TransportPort) bool {
@@ -164,7 +154,7 @@ func matchLink(l stubFirewallerLink, parentIP, childIP netip.Addr, ports []types
164154
return false
165155
}
166156
for i, p := range l.ports {
167-
if !p.Equal(&ports[i]) {
157+
if p != ports[i] {
168158
return false
169159
}
170160
}

daemon/libnetwork/drivers/bridge/port_mapping_linux_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,10 @@ func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortB
10091009

10101010
func (pm *stubPortMapper) UnmapPorts(_ context.Context, reqs []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error {
10111011
for _, req := range reqs {
1012+
// We're only checking for the PortBinding here, not any other
1013+
// property of [portmapperapi.PortBinding].
10121014
idx := slices.IndexFunc(pm.mapped, func(pb portmapperapi.PortBinding) bool {
1013-
return pb.Equal(&req.PortBinding)
1015+
return pb.Equal(req.PortBinding)
10141016
})
10151017
if idx == -1 {
10161018
return fmt.Errorf("stubPortMapper.UnmapPorts: pb doesn't exist %v", req)

daemon/libnetwork/drivers/windows/overlay/ov_endpoint_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]any, error) {
262262
// Return a copy of the operational data
263263
pmc := make([]types.PortBinding, 0, len(ep.portMapping))
264264
for _, pm := range ep.portMapping {
265-
pmc = append(pmc, pm.GetCopy())
265+
pmc = append(pmc, pm.Copy())
266266
}
267267
data[netlabel.PortMap] = pmc
268268
}

daemon/libnetwork/drivers/windows/port_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const maxAllocatePortAttempts = 10
2121
func AllocatePorts(portMapper *portmapper.PortMapper, bindings []types.PortBinding, containerIP net.IP) ([]types.PortBinding, error) {
2222
bs := make([]types.PortBinding, 0, len(bindings))
2323
for _, c := range bindings {
24-
b := c.GetCopy()
24+
b := c.Copy()
2525
if err := allocatePort(portMapper, &b, containerIP); err != nil {
2626
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
2727
if cuErr := ReleasePorts(portMapper, bs); cuErr != nil {

daemon/libnetwork/drivers/windows/windows.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"net"
1919
"net/http"
20+
"slices"
2021
"strconv"
2122
"strings"
2223
"sync"
@@ -863,19 +864,14 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]any, error) {
863864

864865
data["hnsid"] = ep.profileID
865866
if ep.epConnectivity.ExposedPorts != nil {
866-
// Return a copy of the config data
867-
epc := make([]types.TransportPort, 0, len(ep.epConnectivity.ExposedPorts))
868-
for _, tp := range ep.epConnectivity.ExposedPorts {
869-
epc = append(epc, tp.GetCopy())
870-
}
871-
data[netlabel.ExposedPorts] = epc
867+
data[netlabel.ExposedPorts] = slices.Clone(ep.epConnectivity.ExposedPorts)
872868
}
873869

874870
if ep.portMapping != nil {
875871
// Return a copy of the operational data
876872
pmc := make([]types.PortBinding, 0, len(ep.portMapping))
877873
for _, pm := range ep.portMapping {
878-
pmc = append(pmc, pm.GetCopy())
874+
pmc = append(pmc, pm.Copy())
879875
}
880876
data[netlabel.PortMap] = pmc
881877
}

daemon/libnetwork/endpoint.go

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"maps"
89
"net"
910
"net/netip"
11+
"slices"
1012
"strings"
1113
"sync"
1214

@@ -18,7 +20,6 @@ import (
1820
"github.com/moby/moby/v2/daemon/libnetwork/driverapi"
1921
"github.com/moby/moby/v2/daemon/libnetwork/ipamapi"
2022
"github.com/moby/moby/v2/daemon/libnetwork/netlabel"
21-
"github.com/moby/moby/v2/daemon/libnetwork/options"
2223
"github.com/moby/moby/v2/daemon/libnetwork/scope"
2324
"github.com/moby/moby/v2/daemon/libnetwork/types"
2425
"github.com/moby/moby/v2/errdefs"
@@ -272,10 +273,13 @@ func (ep *Endpoint) New() datastore.KVObject {
272273
return &Endpoint{network: ep.getNetwork()}
273274
}
274275

276+
var _ datastore.KVObject = (*Endpoint)(nil)
277+
275278
func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
276279
ep.mu.Lock()
277280
defer ep.mu.Unlock()
278281

282+
// TODO(thaJeztah): should dstEp be locked during this copy? (ideally we'd not have a mutex at all in this struct).
279283
dstEp := o.(*Endpoint)
280284
dstEp.name = ep.name
281285
dstEp.id = ep.id
@@ -286,40 +290,15 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
286290
dstEp.disableIPv6 = ep.disableIPv6
287291
dstEp.svcName = ep.svcName
288292
dstEp.svcID = ep.svcID
289-
dstEp.virtualIP = ep.virtualIP
293+
dstEp.virtualIP = ep.virtualIP // TODO(thaJeztah): should this be cloned? (net.IP)?
290294
dstEp.loadBalancer = ep.loadBalancer
291-
292-
dstEp.svcAliases = make([]string, len(ep.svcAliases))
293-
copy(dstEp.svcAliases, ep.svcAliases)
294-
295-
dstEp.ingressPorts = make([]*PortConfig, len(ep.ingressPorts))
296-
copy(dstEp.ingressPorts, ep.ingressPorts)
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-
}
304-
305-
if ep.joinInfo != nil {
306-
dstEp.joinInfo = &endpointJoinInfo{}
307-
if err := ep.joinInfo.CopyTo(dstEp.joinInfo); err != nil {
308-
return err
309-
}
310-
}
311-
312-
dstEp.exposedPorts = make([]types.TransportPort, len(ep.exposedPorts))
313-
copy(dstEp.exposedPorts, ep.exposedPorts)
314-
315-
dstEp.dnsNames = make([]string, len(ep.dnsNames))
316-
copy(dstEp.dnsNames, ep.dnsNames)
317-
318-
dstEp.generic = options.Generic{}
319-
for k, v := range ep.generic {
320-
dstEp.generic[k] = v
321-
}
322-
295+
dstEp.svcAliases = slices.Clone(ep.svcAliases)
296+
dstEp.ingressPorts = slices.Clone(ep.ingressPorts) // TODO(thaJeztah): should this be copied in depth? ([]*PortConfig)
297+
dstEp.iface = ep.iface.Copy()
298+
dstEp.joinInfo = ep.joinInfo.Copy()
299+
dstEp.exposedPorts = slices.Clone(ep.exposedPorts)
300+
dstEp.dnsNames = slices.Clone(ep.dnsNames)
301+
dstEp.generic = maps.Clone(ep.generic)
323302
return nil
324303
}
325304

0 commit comments

Comments
 (0)