Read and write CNI file only once - #2115
Conversation
The rest of the codebase only uses the exported methods, and doesn't need to access those fields. Unexporting them makes it easier to reason about it. Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
It's used outside the utils package, so it makes sense to have a type that's nameable outside this package. Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
Retain the parsed CNI config throughout NRC's Run method. Postpone writing the CNI file until the kube-bridge is set up. By this time, router will have collected all the required information, including the correct MTU. This ensures that the node's MTU is read only once and that the CNI configuration is read, parsed, and written only once. Note that kube-router will no longer terminate when the node MTU cannot be determined and both --enable-cni and --auto-mtu are true. Instead, an error is logged, and the MTU won't be set. This makes the behavior consistent with when --enable-cni is false. Moreover, a malformed CNI file will cause kube-router to terminate with a proper message, instead of panicking due to a nil pointer access. Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
6473f19 to
89c5637
Compare
| if cniNetConf != nil { | ||
| if err := cniNetConf.WriteCNIConfig(); err != nil { | ||
| klog.Fatalf("failed to write CNI file: %v", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
CNI written even when bridge setup fails
WriteCNIConfig is called unconditionally after the bridge-setup block, even if kubeBridgeIf was never successfully obtained (e.g., both the initial LinkByName and the creation-retry failed with errors). In that case, LinkSetMTU would have been called on a nil interface (likely panicking), but if it somehow returned an error instead, the MTU in the CNI config would still reflect the desired value rather than the actual bridge MTU. This is a pre-existing hazard, but the new unconditional write path makes the CNI file a source of truth that diverges from the actual bridge state on failure.
There was a problem hiding this comment.
Indeed, noticed that myself, but decided not to fix it here. I might submit another PR for this.
|
Just a small nit, in the future please match the repo's conventional commit messages standard |
Alright 🫡 (I usually don't do conventional commits and try to avoid them unless something or someone requests them 😬) |
What type of PR is this?
cleanup
feature
What this PR does / why we need it:
Retain the parsed CNI config throughout NRC's Run method. Postpone writing the CNI file until the kube-bridge is set up. By this time, router will have collected all the required information, including the correct MTU. This ensures that the node's MTU is read only once and that the CNI configuration is read, parsed, and written only once.
Note that kube-router will no longer terminate when the node MTU cannot be determined and both
--enable-cniand--auto-mtuaretrue. Instead, an error is logged, and the MTU won't be set. This makes the behavior consistent with when--enable-cniisfalse. Moreover, a malformed CNI file will cause kube-router to terminate with a proper message, instead of panicking due to a nil pointer access.Which issue(s) this PR is related to:
Was AI used during the creation of this PR?
Yes, for local code review.
What, if any, amount of integration testing was done with this change in a Kubernetes environment?
None yet. I'll probably trigger a k0s CI run, but that requires me to build a custom kube-router OCI image first.
Does this PR introduce a breaking change?
I hope not. There is a difference in behavior, as described above.
Anything else the reviewer should know that wasn't already covered?
I'm currently trying to tackle the race window between when the init container puts the CNI template on the node's file system and when it actually becomes usable after kube-router has filled in the missing parts. This is an intermediate step.
That race window triggers all sorts of fun stuff 😅, like this: