Skip to content

fix(NPC): stop copying the filter table buffer once per pod in syncPodFirewallChains - #2192

Merged
aauren merged 1 commit into
cloudnativelabs:masterfrom
spicysharkco:fix/npc-pod-firewall-chain-string-copy
Oct 12, 2026
Merged

aauren merged 1 commit into
cloudnativelabs:masterfrom
spicysharkco:fix/npc-pod-firewall-chain-string-copy

Conversation

@spicysharkco

@spicysharkco spicysharkco commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

bug

What this PR does / why we need it:

syncPodFirewallChains calls strings.Contains(filterTableRules.String(), rule) for every local pod and IP family. bytes.Buffer.String() copies the whole buffer, so each full sync copies and scans the iptables-save output once per pod. See #2191.

This removes the check, because it can never match. The log rule text is unique to one pod in one family in one sync: it holds the pod's chain name, which hashes the namespace, the pod name and the sync version, and a <namespace>/<name> log-drop comment. Rules from an earlier sync carry the old version in their chain name, and pods whose chain names collide (the hash joins namespace and name without a separator, e.g. a/bc and ab/c; see #2193) still differ in the comment.

BenchmarkSyncPodFirewallChains (80 pods, one family, 1 MiB of existing rules, median of -count 5):

ns/op B/op allocs/op
master 152,895,797 90,489,076 2115
this PR 477,097 205,066 1954

Which issue(s) this PR is related to:

Fixes #2191

Was AI used during the creation of this PR?

What, if any, amount of integration testing was done with this change in a Kubernetes environment?

None; unit tests and the benchmark only. The new tests in pod_test.go pin the exact rules written per chain and family (single-stack, dual-stack, colliding chain names). They pass on master and on this branch. go vet and the Makefile's golangci-lint v2.12.2 are clean.

Does this PR introduce a breaking change?

NONE

@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR removes a per-pod scan of the filter-table buffer when writing NFLOG rules, addressing the reported full-sync copying cost. It also updates the pod test lister to match production and removes two tests tied to the previous deduplication check.

Reviews (2) · Last reviewed commit: "fix(NPC): drop the log-drop check that c..." · Reviewed by Greptile

greptile-apps[bot]
greptile-apps Bot previously approved these changes Oct 5, 2026

@aauren aauren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting feedback given the comment

"--nflog-group", "100", "-m", "limit", "--limit", "10/minute", "--limit-burst", "10", "\n"}
// This used to be AppendUnique when we were using iptables directly, this checks to make sure we didn't drop
// unmarked for this chain already
if strings.Contains(filterTableRules.String(), strings.Join(args, " ")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given what you said in your description:

The set is not seeded from the buffer, because rules from an earlier sync could never match the old check: the chain name contains the sync version, and iptables-save prints the rule differently (10/min instead of 10/minute, different match order; checked against a real iptables-save).

I would argue that we should just drop this check altogether, as far as I can tell this is dead code and never has the potential of actually coming back with a match, unless you see it differently?

The rule text is unique to one pod in one family in one sync. The string being checked includes:

  • the chain name, which is sha256(namespace + podName + version)
  • the comment "/ log-drop"
  • the per-family buffer it's written into

So a second write within a sync would need the same pod, with the same namespace and name, to come through the allLocalPods loop twice in the same family. Which from what I can tell is not possible in a real k8s cluster.

Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I don't see a way for it to match either. The two cases I kept the set for don't need it: rules from an earlier sync carry the old version in their chain name, and pods whose chain names collide (a/bc and ab/c, now #2193) still differ in the log-drop comment.

Updated: the check and the set are gone, along with the two tests that only covered them (a pod listed twice, a second sync over the first one's rules). TestSyncPodFirewallChainsDropRules still pins the exact rules per chain and family, and passes on master and on this branch. go vet and golangci-lint v2.12.2 are clean. The benchmark is unchanged: about 0.48 ms and 0.2 MB per sync, against about 150 ms and 90 MB on master.

syncPodFirewallChains called strings.Contains(filterTableRules.String(), rule)
for every local pod and IP family before writing the pod's log-drop rule.
bytes.Buffer.String() copies the whole buffer, so a full sync copied and
scanned the iptables-save output plus the generated rules once per pod.

The check could never match, so remove it. The rule text is unique to one
pod in one family in one sync: it holds the pod's chain name, which hashes
the namespace, the pod name and the sync version, and a
"<namespace>/<name> log-drop" comment. Rules from an earlier sync carry the
old version in their chain name, and two pods whose chain names collide
still differ in the comment.

For 80 pods and a 1 MiB buffer, BenchmarkSyncPodFirewallChains goes from
about 150 ms and 90 MB per sync to about 0.5 ms and 0.2 MB.
@spicysharkco
spicysharkco force-pushed the fix/npc-pod-firewall-chain-string-copy branch from 41908b0 to 5764f1e Compare October 12, 2026 18:11
@greptile-apps
greptile-apps Bot dismissed their stale review October 12, 2026 18:11

Dismissed because a newer commit was pushed; Greptile will re-review the current head.

@aauren aauren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Thanks @spicysharkco !

@aauren
aauren merged commit ecf5008 into cloudnativelabs:master Oct 12, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants