libnet/pmapi: remove firewaller arg from Map/UnmapPorts

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
Albin Kerouanton
2025-08-11 02:23:44 +02:00
parent 9b1c4ad3b1
commit fc045ad139
8 changed files with 15 additions and 29 deletions

View File

@@ -102,13 +102,13 @@ func (n *bridgeNetwork) mapPorts(ctx context.Context, pms *drvregistry.PortMappe
return nil, err return nil, err
} }
bindings, err := pm.MapPorts(ctx, reqs, n.firewallerNetwork) bindings, err := pm.MapPorts(ctx, reqs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() { defer func() {
if retErr != nil { if retErr != nil {
if err := pm.UnmapPorts(ctx, bindings, n.firewallerNetwork); err != nil { if err := pm.UnmapPorts(ctx, bindings); err != nil {
log.G(ctx).WithFields(log.Fields{ log.G(ctx).WithFields(log.Fields{
"bindings": bindings, "bindings": bindings,
"error": err, "error": err,
@@ -413,7 +413,7 @@ func (n *bridgeNetwork) unmapPBs(ctx context.Context, bindings []portmapperapi.P
continue continue
} }
if err := pm.UnmapPorts(ctx, []portmapperapi.PortBinding{b}, n.firewallerNetwork); err != nil { if err := pm.UnmapPorts(ctx, []portmapperapi.PortBinding{b}); err != nil {
errs = append(errs, fmt.Errorf("unmapping port binding %s: %w", b.PortBinding, err)) errs = append(errs, fmt.Errorf("unmapping port binding %s: %w", b.PortBinding, err))
} }
if b.StopProxy != nil { if b.StopProxy != nil {

View File

@@ -990,7 +990,7 @@ type stubPortMapper struct {
mapped []portmapperapi.PortBinding mapped []portmapperapi.PortBinding
} }
func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortBindingReq, _ portmapperapi.Firewaller) ([]portmapperapi.PortBinding, error) { func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortBindingReq) ([]portmapperapi.PortBinding, error) {
if len(reqs) == 0 { if len(reqs) == 0 {
return []portmapperapi.PortBinding{}, nil return []portmapperapi.PortBinding{}, nil
} }
@@ -1002,7 +1002,7 @@ func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortB
return pbs, nil return pbs, nil
} }
func (pm *stubPortMapper) UnmapPorts(_ context.Context, reqs []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error { func (pm *stubPortMapper) UnmapPorts(_ context.Context, reqs []portmapperapi.PortBinding) error {
for _, req := range reqs { for _, req := range reqs {
// We're only checking for the PortBinding here, not any other // We're only checking for the PortBinding here, not any other
// property of [portmapperapi.PortBinding]. // property of [portmapperapi.PortBinding].

View File

@@ -10,11 +10,11 @@ import (
type fakePortMapper struct{} type fakePortMapper struct{}
func (f fakePortMapper) MapPorts(_ context.Context, _ []portmapperapi.PortBindingReq, _ portmapperapi.Firewaller) ([]portmapperapi.PortBinding, error) { func (f fakePortMapper) MapPorts(_ context.Context, _ []portmapperapi.PortBindingReq) ([]portmapperapi.PortBinding, error) {
return nil, nil return nil, nil
} }
func (f fakePortMapper) UnmapPorts(_ context.Context, _ []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error { func (f fakePortMapper) UnmapPorts(_ context.Context, _ []portmapperapi.PortBinding) error {
return nil return nil
} }

View File

@@ -29,10 +29,10 @@ type PortMapper interface {
// When an ephemeral port, or a single port from a range is requested // When an ephemeral port, or a single port from a range is requested
// MapPorts should attempt a few times to find a free port available // MapPorts should attempt a few times to find a free port available
// across all IP addresses. // across all IP addresses.
MapPorts(ctx context.Context, reqs []PortBindingReq, fwn Firewaller) ([]PortBinding, error) MapPorts(ctx context.Context, reqs []PortBindingReq) ([]PortBinding, error)
// UnmapPorts takes a list of port bindings to unmap. // UnmapPorts takes a list of port bindings to unmap.
UnmapPorts(ctx context.Context, pbs []PortBinding, fwn Firewaller) error UnmapPorts(ctx context.Context, pbs []PortBinding) error
} }
type PortBindingReq struct { type PortBindingReq struct {

View File

@@ -1,14 +0,0 @@
package portmapperapi
import (
"context"
"github.com/moby/moby/v2/daemon/libnetwork/types"
)
type Firewaller interface {
// AddPorts adds the configuration needed for NATing ports.
AddPorts(ctx context.Context, pbs []types.PortBinding) error
// DelPorts deletes the configuration needed for NATing ports.
DelPorts(ctx context.Context, pbs []types.PortBinding) error
}

View File

@@ -47,7 +47,7 @@ func NewPortMapper(cfg Config) PortMapper {
// MapPorts allocates and binds host ports for the given cfg. The caller is // MapPorts allocates and binds host ports for the given cfg. The caller is
// responsible for ensuring that all entries in cfg have the same proto, // responsible for ensuring that all entries in cfg have the same proto,
// container port, and host port range (their host addresses must differ). // container port, and host port range (their host addresses must differ).
func (pm PortMapper) MapPorts(ctx context.Context, cfg []portmapperapi.PortBindingReq, fwn portmapperapi.Firewaller) (_ []portmapperapi.PortBinding, retErr error) { func (pm PortMapper) MapPorts(ctx context.Context, cfg []portmapperapi.PortBindingReq) (_ []portmapperapi.PortBinding, retErr error) {
if len(cfg) == 0 { if len(cfg) == 0 {
return nil, nil return nil, nil
} }
@@ -64,7 +64,7 @@ func (pm PortMapper) MapPorts(ctx context.Context, cfg []portmapperapi.PortBindi
bindings := make([]portmapperapi.PortBinding, 0, len(cfg)) bindings := make([]portmapperapi.PortBinding, 0, len(cfg))
defer func() { defer func() {
if retErr != nil { if retErr != nil {
if err := pm.UnmapPorts(ctx, bindings, fwn); err != nil { if err := pm.UnmapPorts(ctx, bindings); err != nil {
log.G(ctx).WithFields(log.Fields{ log.G(ctx).WithFields(log.Fields{
"pbs": bindings, "pbs": bindings,
"error": err, "error": err,
@@ -107,7 +107,7 @@ func (pm PortMapper) MapPorts(ctx context.Context, cfg []portmapperapi.PortBindi
return bindings, nil return bindings, nil
} }
func (pm PortMapper) UnmapPorts(ctx context.Context, pbs []portmapperapi.PortBinding, fwn portmapperapi.Firewaller) error { func (pm PortMapper) UnmapPorts(ctx context.Context, pbs []portmapperapi.PortBinding) error {
var errs []error var errs []error
for _, pb := range pbs { for _, pb := range pbs {
if pb.BoundSocket != nil { if pb.BoundSocket != nil {

View File

@@ -30,7 +30,7 @@ func TestBindHostPortsError(t *testing.T) {
}, },
} }
pm := &PortMapper{} pm := &PortMapper{}
pbs, err := pm.MapPorts(context.Background(), cfg, nil) pbs, err := pm.MapPorts(context.Background(), cfg)
assert.Check(t, is.Error(err, "port binding mismatch 80/tcp:8080-8080, 80/tcp:8080-8081")) assert.Check(t, is.Error(err, "port binding mismatch 80/tcp:8080-8080, 80/tcp:8080-8081"))
assert.Check(t, is.Nil(pbs)) assert.Check(t, is.Nil(pbs))
} }

View File

@@ -22,7 +22,7 @@ func NewPortMapper() PortMapper {
// MapPorts returns a PortBinding for every PortBindingReq received, with Forwarding enabled for each. If a HostPort is // MapPorts returns a PortBinding for every PortBindingReq received, with Forwarding enabled for each. If a HostPort is
// specified, it's logged and ignored. // specified, it's logged and ignored.
func (pm PortMapper) MapPorts(ctx context.Context, reqs []portmapperapi.PortBindingReq, fwn portmapperapi.Firewaller) ([]portmapperapi.PortBinding, error) { func (pm PortMapper) MapPorts(ctx context.Context, reqs []portmapperapi.PortBindingReq) ([]portmapperapi.PortBinding, error) {
if len(reqs) == 0 { if len(reqs) == 0 {
return nil, nil return nil, nil
} }
@@ -45,6 +45,6 @@ func (pm PortMapper) MapPorts(ctx context.Context, reqs []portmapperapi.PortBind
return res, nil return res, nil
} }
func (pm PortMapper) UnmapPorts(_ context.Context, _ []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error { func (pm PortMapper) UnmapPorts(_ context.Context, _ []portmapperapi.PortBinding) error {
return nil return nil
} }