Skip to content

Commit 678eb1e

Browse files
committed
use a constance for the filter table and re-organize tests
1 parent 6c5e170 commit 678eb1e

File tree

2 files changed

+422
-1427
lines changed

2 files changed

+422
-1427
lines changed

pkg/iptables/iptables_manager.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ func NewIPTablesManager(mainChainName, defaultAction, logIdentifier string, dryR
8383

8484
// EnsureMainChainExists creates the main chain if it doesn't exist and inserts a jump in CNI-FORWARD.
8585
func (m *IPTablesManager) EnsureMainChainExists() error {
86-
exists, err := m.ipt.ChainExists("filter", m.mainChainName)
86+
exists, err := m.ipt.ChainExists(table, m.mainChainName)
8787
if err != nil {
8888
return fmt.Errorf("failed to check main chain existence: %v", err)
8989
}
9090
if !exists {
91-
if err := m.ipt.NewChain("filter", m.mainChainName); err != nil {
91+
if err := m.ipt.NewChain(table, m.mainChainName); err != nil {
9292
return fmt.Errorf("failed to create main chain: %v", err)
9393
}
9494
}
9595

9696
// Remove any previous jump rule (just in case)
97-
_ = m.ipt.Delete("filter", "CNI-FORWARD", "-j", m.mainChainName)
97+
_ = m.ipt.Delete(table, "CNI-FORWARD", "-j", m.mainChainName)
9898

9999
// Insert jump to the main chain at the top of CNI-FORWARD.
100-
if err := m.ipt.Insert("filter", "CNI-FORWARD", 1, "-j", m.mainChainName); err != nil {
100+
if err := m.ipt.Insert(table, "CNI-FORWARD", 1, "-j", m.mainChainName); err != nil {
101101
return fmt.Errorf("failed to add jump to main chain in CNI-FORWARD: %v", err)
102102
}
103103
return nil
@@ -127,19 +127,19 @@ func (m *IPTablesManager) buildDefaultActionSpecs() [][]string {
127127

128128
// CreateContainerChain makes a new chain for a specific container and sets up default rules.
129129
func (m *IPTablesManager) CreateContainerChain(containerChain string) error {
130-
if err := m.ipt.NewChain("filter", containerChain); err != nil {
130+
if err := m.ipt.NewChain(table, containerChain); err != nil {
131131
return fmt.Errorf("failed to create container chain: %v", err)
132132
}
133133

134134
// Accept related and established connections first
135-
if err := m.ipt.Append("filter", containerChain,
135+
if err := m.ipt.Append(table, containerChain,
136136
"-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"); err != nil {
137137
return fmt.Errorf("failed to add RELATED,ESTABLISHED rule: %v", err)
138138
}
139139

140140
// Now append the default action rule(s)
141141
for _, spec := range m.buildDefaultActionSpecs() {
142-
if err := m.ipt.Append("filter", containerChain, spec...); err != nil {
142+
if err := m.ipt.Append(table, containerChain, spec...); err != nil {
143143
return fmt.Errorf("failed to set default action for container chain: %v", err)
144144
}
145145
}
@@ -182,7 +182,7 @@ func (m *IPTablesManager) AddRule(chainName string, rule OutboundRule) error {
182182

183183
// Insert each spec at position 1 in reverse order so they appear in the chain in the correct sequence
184184
for i := len(ruleSpecs) - 1; i >= 0; i-- {
185-
if err := m.ipt.Insert("filter", chainName, 1, ruleSpecs[i]...); err != nil {
185+
if err := m.ipt.Insert(table, chainName, 1, ruleSpecs[i]...); err != nil {
186186
return fmt.Errorf("failed to add rule: %v", err)
187187
}
188188
}
@@ -191,20 +191,20 @@ func (m *IPTablesManager) AddRule(chainName string, rule OutboundRule) error {
191191

192192
// AddJumpRule appends a jump from the main chain to the container chain for the given source IP.
193193
func (m *IPTablesManager) AddJumpRule(sourceIP, targetChain string) error {
194-
return m.ipt.Append("filter", m.mainChainName, "-s", sourceIP, "-j", targetChain)
194+
return m.ipt.Append(table, m.mainChainName, "-s", sourceIP, "-j", targetChain)
195195
}
196196

197197
// RemoveJumpRule deletes a jump rule referencing the targetChain for the given source IP.
198198
func (m *IPTablesManager) RemoveJumpRule(sourceIP, targetChain string) error {
199-
if err := m.ipt.Delete("filter", m.mainChainName, "-s", sourceIP, "-j", targetChain); err != nil {
199+
if err := m.ipt.Delete(table, m.mainChainName, "-s", sourceIP, "-j", targetChain); err != nil {
200200
return fmt.Errorf("failed to remove jump rule: %v", err)
201201
}
202202
return nil
203203
}
204204

205205
// RemoveJumpRuleByTargetChain does a more robust token-based matching to avoid partial strings.
206206
func (m *IPTablesManager) RemoveJumpRuleByTargetChain(targetChain string) error {
207-
rules, err := m.ipt.List("filter", m.mainChainName)
207+
rules, err := m.ipt.List(table, m.mainChainName)
208208
if err != nil {
209209
return fmt.Errorf("failed to list rules in main chain: %v", err)
210210
}
@@ -218,7 +218,7 @@ func (m *IPTablesManager) RemoveJumpRuleByTargetChain(targetChain string) error
218218
// Found the rule referencing the targetChain
219219
// We also skip the first two tokens ("-A" <chainname>) when calling Delete
220220
toDelete := tokens[2:]
221-
if err := m.ipt.Delete("filter", m.mainChainName, toDelete...); err != nil {
221+
if err := m.ipt.Delete(table, m.mainChainName, toDelete...); err != nil {
222222
return fmt.Errorf("failed to remove jump rule: %v", err)
223223
}
224224
return nil
@@ -231,23 +231,23 @@ func (m *IPTablesManager) RemoveJumpRuleByTargetChain(targetChain string) error
231231

232232
// ClearAndDeleteChain first clears all rules from the chain, then deletes it.
233233
func (m *IPTablesManager) ClearAndDeleteChain(chainName string) error {
234-
if err := m.ipt.ClearChain("filter", chainName); err != nil {
234+
if err := m.ipt.ClearChain(table, chainName); err != nil {
235235
return fmt.Errorf("failed to clear chain %s: %v", chainName, err)
236236
}
237-
if err := m.ipt.DeleteChain("filter", chainName); err != nil {
237+
if err := m.ipt.DeleteChain(table, chainName); err != nil {
238238
return fmt.Errorf("failed to delete chain %s: %v", chainName, err)
239239
}
240240
return nil
241241
}
242242

243243
// ChainExists checks whether the chain is present.
244244
func (m *IPTablesManager) ChainExists(chainName string) (bool, error) {
245-
return m.ipt.ChainExists("filter", chainName)
245+
return m.ipt.ChainExists(table, chainName)
246246
}
247247

248248
// VerifyRules verifies that each of the plugin's rules (and default actions) exist in iptables.
249249
func (m *IPTablesManager) VerifyRules(chainName string, rules []OutboundRule) error {
250-
existingRules, err := m.ipt.List("filter", chainName)
250+
existingRules, err := m.ipt.List(table, chainName)
251251
if err != nil {
252252
return err
253253
}

0 commit comments

Comments
 (0)