Repository navigation
fix(NPC): stop copying the filter table buffer once per pod in syncPodFirewallChains - #2192
Conversation
|
aauren
left a comment
There was a problem hiding this comment.
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, " ")) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
41908b0 to
5764f1e
Compare
Dismissed because a newer commit was pushed; Greptile will re-review the current head.
aauren
left a comment
There was a problem hiding this comment.
LGTM!
Thanks @spicysharkco !
What type of PR is this?
bug
What this PR does / why we need it:
syncPodFirewallChainscallsstrings.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-dropcomment. 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/bcandab/c; see #2193) still differ in the comment.BenchmarkSyncPodFirewallChains(80 pods, one family, 1 MiB of existing rules, median of-count 5):Which issue(s) this PR is related to:
Fixes #2191
Was AI used during the creation of this PR?
strings.Contains(filterTableRules.String(), ...)), about 175 ms CPU and 100 MB of allocation per full sync on a node with 84 pods #2191).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.gopin the exact rules written per chain and family (single-stack, dual-stack, colliding chain names). They pass on master and on this branch.go vetand the Makefile's golangci-lint v2.12.2 are clean.Does this PR introduce a breaking change?