Browse code

Return concrete types from NewNftabler/NewIptabler

Signed-off-by: Rob Murray <rob.murray@docker.com>

Rob Murray authored on 2025/08/05 18:28:37
Showing 7 changed files
... ...
@@ -556,7 +556,7 @@ var newFirewaller = func(ctx context.Context, config firewaller.Config) (firewal
556 556
 		// cleaner can't clean up network or port-specific rules that may have been added
557 557
 		// to iptables built-in chains. So, if cleanup is needed, give the cleaner to
558 558
 		// the nftabler. Then, it'll use it to delete old rules as networks are restored.
559
-		fw.(firewaller.FirewallCleanerSetter).SetFirewallCleaner(iptabler.NewCleaner(ctx, config))
559
+		fw.SetFirewallCleaner(iptabler.NewCleaner(ctx, config))
560 560
 		return fw, nil
561 561
 	}
562 562
 
... ...
@@ -17,7 +17,7 @@ type iptablesCleaner struct {
17 17
 }
18 18
 
19 19
 // NewCleaner checks for iptables rules left behind by an old daemon that was using
20
-// the iptabler.
20
+// the Iptabler.
21 21
 //
22 22
 // If there are old rules present, it deletes as much as possible straight away
23 23
 // (user-defined chains and jumps from the built-in chains).
... ...
@@ -62,7 +62,7 @@ func (ic iptablesCleaner) DelNetwork(ctx context.Context, nc firewaller.NetworkC
62 62
 	}
63 63
 	n := network{
64 64
 		config: nc,
65
-		ipt:    &iptabler{config: ic.config},
65
+		ipt:    &Iptabler{config: ic.config},
66 66
 	}
67 67
 	if ic.config.IPv4 && nc.Config4.Prefix.IsValid() {
68 68
 		_ = deleteLegacyFilterRules(iptables.IPv4, nc.IfName)
... ...
@@ -77,7 +77,7 @@ func (ic iptablesCleaner) DelNetwork(ctx context.Context, nc firewaller.NetworkC
77 77
 func (ic iptablesCleaner) DelEndpoint(ctx context.Context, nc firewaller.NetworkConfig, epIPv4, epIPv6 netip.Addr) {
78 78
 	n := network{
79 79
 		config: nc,
80
-		ipt:    &iptabler{config: ic.config},
80
+		ipt:    &Iptabler{config: ic.config},
81 81
 	}
82 82
 	if n.ipt.config.IPv4 && epIPv4.IsValid() {
83 83
 		_ = n.filterDirectAccess(ctx, iptables.IPv4, n.config.Config4, epIPv4, false)
... ...
@@ -90,7 +90,7 @@ func (ic iptablesCleaner) DelEndpoint(ctx context.Context, nc firewaller.Network
90 90
 func (ic iptablesCleaner) DelPorts(ctx context.Context, nc firewaller.NetworkConfig, pbs []types.PortBinding) {
91 91
 	n := network{
92 92
 		config: nc,
93
-		ipt:    &iptabler{config: ic.config},
93
+		ipt:    &Iptabler{config: ic.config},
94 94
 	}
95 95
 	_ = n.DelPorts(ctx, pbs)
96 96
 }
... ...
@@ -35,12 +35,12 @@ const (
35 35
 	isolationChain2 = "DOCKER-ISOLATION-STAGE-2"
36 36
 )
37 37
 
38
-type iptabler struct {
38
+type Iptabler struct {
39 39
 	config firewaller.Config
40 40
 }
41 41
 
42
-func NewIptabler(ctx context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
43
-	ipt := &iptabler{config: config}
42
+func NewIptabler(ctx context.Context, config firewaller.Config) (*Iptabler, error) {
43
+	ipt := &Iptabler{config: config}
44 44
 
45 45
 	if ipt.config.IPv4 {
46 46
 		removeIPChains(ctx, iptables.IPv4)
... ...
@@ -91,7 +91,7 @@ func NewIptabler(ctx context.Context, config firewaller.Config) (firewaller.Fire
91 91
 	return ipt, nil
92 92
 }
93 93
 
94
-func (ipt *iptabler) FilterForwardDrop(ctx context.Context, ipv firewaller.IPVersion) error {
94
+func (ipt *Iptabler) FilterForwardDrop(ctx context.Context, ipv firewaller.IPVersion) error {
95 95
 	var iptv iptables.IPVersion
96 96
 	switch ipv {
97 97
 	case firewaller.IPv4:
... ...
@@ -22,11 +22,11 @@ type (
22 22
 
23 23
 type network struct {
24 24
 	config     firewaller.NetworkConfig
25
-	ipt        *iptabler
25
+	ipt        *Iptabler
26 26
 	cleanFuncs iptablesCleanFuncs
27 27
 }
28 28
 
29
-func (ipt *iptabler) NewNetwork(ctx context.Context, nc firewaller.NetworkConfig) (_ firewaller.Network, retErr error) {
29
+func (ipt *Iptabler) NewNetwork(ctx context.Context, nc firewaller.NetworkConfig) (_ firewaller.Network, retErr error) {
30 30
 	n := &network{
31 31
 		ipt:    ipt,
32 32
 		config: nc,
... ...
@@ -11,7 +11,7 @@ import (
11 11
 	"github.com/moby/moby/v2/daemon/libnetwork/internal/nftables"
12 12
 )
13 13
 
14
-// Cleanup deletes all rules created by nftabler; it's intended to be used
14
+// Cleanup deletes all rules created by Nftabler; it's intended to be used
15 15
 // during startup, to clean up rules created by an old incarnation of the daemon
16 16
 // after switching to a different Firewaller implementation.
17 17
 func Cleanup(ctx context.Context, config firewaller.Config) {
... ...
@@ -31,6 +31,6 @@ func Cleanup(ctx context.Context, config firewaller.Config) {
31 31
 	}
32 32
 }
33 33
 
34
-func (nft *nftabler) SetFirewallCleaner(fc firewaller.FirewallCleaner) {
34
+func (nft *Nftabler) SetFirewallCleaner(fc firewaller.FirewallCleaner) {
35 35
 	nft.cleaner = fc
36 36
 }
... ...
@@ -18,10 +18,10 @@ import (
18 18
 type network struct {
19 19
 	config  firewaller.NetworkConfig
20 20
 	cleaner func(ctx context.Context) error
21
-	fw      *nftabler
21
+	fw      *Nftabler
22 22
 }
23 23
 
24
-func (nft *nftabler) NewNetwork(ctx context.Context, nc firewaller.NetworkConfig) (_ firewaller.Network, retErr error) {
24
+func (nft *Nftabler) NewNetwork(ctx context.Context, nc firewaller.NetworkConfig) (_ firewaller.Network, retErr error) {
25 25
 	n := &network{
26 26
 		fw:     nft,
27 27
 		config: nc,
... ...
@@ -45,15 +45,15 @@ const (
45 45
 	rawPreroutingPortsRuleGroup = iota + initialRuleGroup + 1
46 46
 )
47 47
 
48
-type nftabler struct {
48
+type Nftabler struct {
49 49
 	config  firewaller.Config
50 50
 	cleaner firewaller.FirewallCleaner
51 51
 	table4  nftables.TableRef
52 52
 	table6  nftables.TableRef
53 53
 }
54 54
 
55
-func NewNftabler(ctx context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
56
-	nft := &nftabler{config: config}
55
+func NewNftabler(ctx context.Context, config firewaller.Config) (*Nftabler, error) {
56
+	nft := &Nftabler{config: config}
57 57
 
58 58
 	if nft.config.IPv4 {
59 59
 		var err error
... ...
@@ -85,14 +85,14 @@ func NewNftabler(ctx context.Context, config firewaller.Config) (firewaller.Fire
85 85
 	return nft, nil
86 86
 }
87 87
 
88
-func (nft *nftabler) getTable(ipv firewaller.IPVersion) nftables.TableRef {
88
+func (nft *Nftabler) getTable(ipv firewaller.IPVersion) nftables.TableRef {
89 89
 	if ipv == firewaller.IPv4 {
90 90
 		return nft.table4
91 91
 	}
92 92
 	return nft.table6
93 93
 }
94 94
 
95
-func (nft *nftabler) FilterForwardDrop(ctx context.Context, ipv firewaller.IPVersion) error {
95
+func (nft *Nftabler) FilterForwardDrop(ctx context.Context, ipv firewaller.IPVersion) error {
96 96
 	table := nft.getTable(ipv)
97 97
 	if err := table.Chain(ctx, forwardChain).SetPolicy("drop"); err != nil {
98 98
 		return err
... ...
@@ -101,7 +101,7 @@ func (nft *nftabler) FilterForwardDrop(ctx context.Context, ipv firewaller.IPVer
101 101
 }
102 102
 
103 103
 // init creates the bridge driver's nftables table for IPv4 or IPv6.
104
-func (nft *nftabler) init(ctx context.Context, family nftables.Family) (nftables.TableRef, error) {
104
+func (nft *Nftabler) init(ctx context.Context, family nftables.Family) (nftables.TableRef, error) {
105 105
 	// Instantiate the table.
106 106
 	table, err := nftables.NewTable(family, dockerTable)
107 107
 	if err != nil {