fix: use StrategicMergePatchType for pod condition updates to avoid TOCTOU race - #2756
Open
WorrierKhushal wants to merge 1 commit into
Open
fix: use StrategicMergePatchType for pod condition updates to avoid TOCTOU race#2756WorrierKhushal wants to merge 1 commit into
WorrierKhushal wants to merge 1 commit into
Conversation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What type of PR is this?
/kind bug
What this PR does / why we need it:
Background
The
PullPodImagehandler inpkg/yurthub/otaupdate/ota.goinjects aPodImageReadycondition onto a Pod during OTA image pull. It fetched thecurrent Pod, locally mutated the full
Status.Conditionsarray viapodutil.UpdatePodCondition, and patched the Pod with the entire arrayusing
types.MergePatchType.The Bug
MergePatchType(RFC 7386 JSON Merge Patch) does not support arraymerging — it replaces arrays wholesale. If the Kubelet or another
controller updated a Pod condition (e.g.
Ready=True) in the windowbetween this handler's
getPod()call and itsPatch()call, thatupdate was silently overwritten by the stale array this handler had
fetched earlier. This could leave a Pod stuck reporting as not-ready on
the control plane even after the Kubelet had confirmed it was running.
Verification of the fix approach
Checked
k8s.io/api v0.34.0'sPodStatus.Conditionsfield definitiondirectly — it carries
patchStrategy:"merge" patchMergeKey:"type",confirming the API server natively supports a strategic merge patch that
merges condition entries by their
Typefield. Also confirmed the fakeclientset used in this package's tests fully supports
StrategicMergePatchType.The Fix
podutil.UpdatePodConditionfull-array mutation(and the now-unused
podutilimport).instead of the full conditions array.
types.MergePatchTypetotypes.StrategicMergePatchType, letting the API server perform themerge natively by condition
Type, eliminating the TOCTOU windowentirely.
Added
TestPullPodImageStrategicMergePatch, verifying the patch sent isStrategicMergePatchTypeand contains exactly one condition.Which issue(s) this PR fixes:
Fixes #2754
Special notes for your reviewer:
go test -v ./pkg/yurthub/otaupdate/...— all tests pass (verified on Linux/WSL), including the new testgo build/go veton the affected package — cleanPullPodImageDoes this PR introduce a user-facing change?
other Note