From 80452e5d4a14baec4a8d3cc24c2154f5fa60fe41 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 10 Aug 2025 15:41:54 +0200 Subject: [PATCH] daemon/libnetwork/types: PortBinding.Equal: use non-pointer receiver Change `PortBinding.Equal` to use a value receiver and parameter, this allows us to use it directly with `slices.IndexFunc`, `DeleteFunc`, without having to add a wrapper func. The only exception currently is the `UnmapPorts` function (stub), which takes portmapperapi.PortBinding as argument; the portmapperapi.PortBinding type embeds `types.PortBinding`, and it's the only field that's compared as part of `UnmapPorts` Signed-off-by: Sebastiaan van Stijn --- .../libnetwork/drivers/bridge/internal/firewaller/stub.go | 8 ++------ .../libnetwork/drivers/bridge/port_mapping_linux_test.go | 4 +++- daemon/libnetwork/types/types.go | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/daemon/libnetwork/drivers/bridge/internal/firewaller/stub.go b/daemon/libnetwork/drivers/bridge/internal/firewaller/stub.go index e88058a80a..0b4a522272 100644 --- a/daemon/libnetwork/drivers/bridge/internal/firewaller/stub.go +++ b/daemon/libnetwork/drivers/bridge/internal/firewaller/stub.go @@ -116,9 +116,7 @@ func (nw *StubFirewallerNetwork) AddPorts(_ context.Context, pbs []types.PortBin func (nw *StubFirewallerNetwork) DelPorts(_ context.Context, pbs []types.PortBinding) error { for _, pb := range pbs { - nw.Ports = slices.DeleteFunc(nw.Ports, func(p types.PortBinding) bool { - return p.Equal(&pb) - }) + nw.Ports = slices.DeleteFunc(nw.Ports, pb.Equal) } return nil } @@ -148,9 +146,7 @@ func (nw *StubFirewallerNetwork) DelLink(_ context.Context, parentIP, childIP ne } func (nw *StubFirewallerNetwork) PortExists(pb types.PortBinding) bool { - return slices.ContainsFunc(nw.Ports, func(p types.PortBinding) bool { - return p.Equal(&pb) - }) + return slices.ContainsFunc(nw.Ports, pb.Equal) } func (nw *StubFirewallerNetwork) LinkExists(parentIP, childIP netip.Addr, ports []types.TransportPort) bool { diff --git a/daemon/libnetwork/drivers/bridge/port_mapping_linux_test.go b/daemon/libnetwork/drivers/bridge/port_mapping_linux_test.go index b3637e7af3..72ad73da5a 100644 --- a/daemon/libnetwork/drivers/bridge/port_mapping_linux_test.go +++ b/daemon/libnetwork/drivers/bridge/port_mapping_linux_test.go @@ -1009,8 +1009,10 @@ func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortB func (pm *stubPortMapper) UnmapPorts(_ context.Context, reqs []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error { for _, req := range reqs { + // We're only checking for the PortBinding here, not any other + // property of [portmapperapi.PortBinding]. idx := slices.IndexFunc(pm.mapped, func(pb portmapperapi.PortBinding) bool { - return pb.Equal(&req.PortBinding) + return pb.Equal(req.PortBinding) }) if idx == -1 { return fmt.Errorf("stubPortMapper.UnmapPorts: pb doesn't exist %v", req) diff --git a/daemon/libnetwork/types/types.go b/daemon/libnetwork/types/types.go index d11ead04c7..c41d6784dd 100644 --- a/daemon/libnetwork/types/types.go +++ b/daemon/libnetwork/types/types.go @@ -121,7 +121,7 @@ func (p PortBinding) Copy() PortBinding { } // Equal returns true if o has the same values as p, else false. -func (p *PortBinding) Equal(o *PortBinding) bool { +func (p PortBinding) Equal(o PortBinding) bool { return p.Proto == o.Proto && p.IP.Equal(o.IP) && p.Port == o.Port &&