Skip to content

Commit fa05d7a

Browse files
committed
Improve nftables connmark error handling and fix test typo
1 parent 9e07b46 commit fa05d7a

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

.github/workflows/pr-automated-tests.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
uses: codecov/codecov-action@79066c46f8dcdf8d7355f820dbac958c5b4cb9d3 # refs/tags/v4.5.0
4242
with:
4343
token: ${{ secrets.CODECOV_TOKEN }}
44-
docker-build:
44+
docker-build-arch:
4545
name: Build Docker images (${{ matrix.arch }})
4646
strategy:
4747
matrix:
@@ -58,7 +58,13 @@ jobs:
5858
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # refs/tags/v3.11.1
5959
- name: Build CNI image
6060
run: make docker
61-
docker-build-init:
61+
docker-build:
62+
name: Build Docker images
63+
needs: docker-build-arch
64+
runs-on: ubuntu-latest
65+
steps:
66+
- run: echo "All docker builds completed successfully"
67+
docker-build-init-arch:
6268
name: Build Docker init images (${{ matrix.arch }})
6369
strategy:
6470
matrix:
@@ -75,3 +81,9 @@ jobs:
7581
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # refs/tags/v3.11.1
7682
- name: Build CNI Init image
7783
run: make docker-init
84+
docker-build-init:
85+
name: Build Docker init images
86+
needs: docker-build-init-arch
87+
runs-on: ubuntu-latest
88+
steps:
89+
- run: echo "All docker init builds completed successfully"

pkg/networkutils/iptables_connmark.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (c *iptablesConnmark) Cleanup() error {
5959

6060
// Clear and delete chain
6161
_ = ipt.ClearChain("nat", "AWS-CONNMARK-CHAIN-0")
62-
//_ = ipt.DeleteChain("nat", "AWS-CONNMARK-CHAIN-0")
6362

6463
return nil
6564
}

pkg/networkutils/mocks/network_mocks.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/networkutils/network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ func TestSetupHostNetworkExcludedSNATCIDRsIdempotent(t *testing.T) {
848848
}, mockIptables.(*mock_iptables.MockIptables).DataplaneState)
849849
}
850850

851-
func TestUUpdateHostSNATRules(t *testing.T) {
851+
func TestUpdateHostSNATRules(t *testing.T) {
852852
ctrl, mockNetLink, _, mockNS, mockIptables := setup(t)
853853
defer ctrl.Finish()
854854

pkg/networkutils/nftables_connmark.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ func (c *nftConnmark) ensureBaseChain(table *nftables.Table) *nftables.Chain {
116116
}
117117
if existing == nil {
118118
priority := c.getDesiredPriority()
119+
policy := nftables.ChainPolicyAccept
119120
chain := c.nft.AddChain(&nftables.Chain{
120121
Name: nftBaseChainName,
121122
Table: table,
122123
Type: nftables.ChainTypeNAT,
123124
Hooknum: nftables.ChainHookPrerouting,
124125
Priority: &priority,
126+
Policy: &policy,
125127
})
126128
return chain
127129
}
@@ -276,7 +278,9 @@ func (c *nftConnmark) ensureConnmarkChainRules(table *nftables.Table, chain *nft
276278

277279
// Delete unknown rules
278280
for _, r := range unknownRules {
279-
c.nft.DelRule(r)
281+
if err := c.nft.DelRule(r); err != nil {
282+
log.Errorf("failed to delete unknown rule: %s, id: %d", err, r.Handle)
283+
}
280284
}
281285

282286
desiredCIDRs := make(map[string]bool)
@@ -287,13 +291,20 @@ func (c *nftConnmark) ensureConnmarkChainRules(table *nftables.Table, chain *nft
287291
// Delete stale CIDRs
288292
for cidr, rule := range currentCIDRs {
289293
if !desiredCIDRs[cidr] {
290-
c.nft.DelRule(rule)
294+
if err := c.nft.DelRule(rule); err != nil {
295+
log.Errorf("failed to delete stale cidr rule %s in nf table: %s, id: %d", cidr, err, rule.Handle)
296+
}
291297
}
292298
}
293299

294300
// Insert missing CIDRs (prepends - order doesn't matter for CIDR rules)
295-
for cidr := range desiredCIDRs {
296-
if _, exists := currentCIDRs[cidr]; !exists {
301+
for cidrStr := range desiredCIDRs {
302+
if _, exists := currentCIDRs[cidrStr]; !exists {
303+
_, cidr, err := net.ParseCIDR(cidrStr)
304+
if err != nil {
305+
log.Errorf("failed to insert cidr %s in nf table: %s", cidrStr, err)
306+
continue
307+
}
297308
c.insertCIDRReturnRule(table, chain, cidr) // InsertRule
298309
}
299310
}
@@ -489,11 +500,7 @@ func (c *nftConnmark) addSetMarkRule(table *nftables.Table, chain *nftables.Chai
489500
})
490501
}
491502

492-
func (c *nftConnmark) insertCIDRReturnRule(table *nftables.Table, chain *nftables.Chain, cidrStr string) {
493-
_, cidr, err := net.ParseCIDR(cidrStr)
494-
if err != nil {
495-
return
496-
}
503+
func (c *nftConnmark) insertCIDRReturnRule(table *nftables.Table, chain *nftables.Chain, cidr *net.IPNet) {
497504
c.nft.InsertRule(&nftables.Rule{
498505
Table: table,
499506
Chain: chain,

test/integration/cni/snat_kube_proxy_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var _ = Describe("test SNAT with kube-proxy modes", func() {
110110
By("detecting iptables backend on node")
111111
backend := detectIptablesBackend(primaryNode.Name)
112112
fmt.Fprintf(GinkgoWriter, "detected iptables backend: %s\n", backend)
113+
Expect(backend).To(Or(Equal("legacy"), Equal("nftables")))
113114

114115
By("verifying CNI connmark rules exist")
115116
verifyConnmarkRules(primaryNode.Name, backend)

0 commit comments

Comments
 (0)