Repository navigation
fix(utils): inject node selector requirements into existing terms - #6276
BahaaMattar wants to merge 2 commits into
Conversation
InjectNodeSelectorRequirements ranged over NodeSelectorTerms by value, so the requirements were appended to a copy of each term and then dropped. When a data operation already had its own required node affinity, the affinity that runAfter.affinityStrategy asks for (policy: Require) was silently not added to the operation's pod. Append through the slice index instead, and treat an empty (non-nil) NodeSelectorTerms slice like a missing one so the requirement is not lost there either. Fixes fluid-cloudnative#6275 Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the affinity-constraint fix.
No open issues 🎉
What changed in this PR
Fixes #6275 by ensuring runAfter.affinityStrategy with policy: Require adds constraints even when a data operation already has required node affinity.
Changes:
- Updates existing selector terms by slice index so every ORed term receives the injected requirements.
- Handles empty, non-nil term slices by creating a new term.
- Adds regression tests for multiple existing terms, empty slices, and dataflow operations with existing required affinity.
| File | Description |
|---|---|
pkg/utils/affinity.go |
Fixes requirement injection into existing and empty selector terms. |
pkg/utils/affinity_test.go |
Adds regression coverage for multiple terms and empty, non-nil slices. |
pkg/dataflow/affinity_test.go |
Tests combining an operation’s required affinity with the preceding operation’s hostname. |
Review effort: Balanced
cheyang
left a comment
There was a problem hiding this comment.
Both reviewers independently reproduced issue #6275 on the base commit and confirmed the fix on this head: with runAfter.affinityStrategy.policy: Require and an operation that already has its own required node affinity, the old range-loop appended to a copy and silently dropped the injected requirement — including the spare-capacity and empty-but-non-nil-terms variants. The index-based fix lands the requirement in every existing term, which is correct since terms are ORed. Beyond the unit and fake-client entry-point differentials both harnesses ran, Codex proved the merged affinity passes real kube-apiserver validation (envtest, k8s 1.31) while the base-produced empty-terms shape is rejected outright — and that layer was re-verified against the upstream validation source. No blocking issues were raised by either reviewer. What remains is polish: a stale doc comment on InjectNodeSelectorRequirements (both reviewers hit it independently — one-line fix), a latent robustness nit on injectRequiredAffinity's NodeSelectorTerms[0] read (its producer, GenerateNodeAffinity, emits exactly one term from the succeeded pod's actual node labels, so there is no present-day bug — but the assumption is undocumented), the in-place mutation aliasing edge (traced safe today across all engine callers), and the repo-wide absence of e2e coverage for dataflow affinity (pre-existing gap, deferrable to a dedicated e2e PR). Reviewer C (Copilot) reviewed this head and raised no findings. The one meaningful disagreement — whether the [0] read can mis-pin pods today — was settled by the producer trace in Codex's favor. CI is green at the reviewed head (rollup plus live DCO/SonarCloud checks).
Next round
[MUST] (none — merge is not gated on anything from this review)
[SHOULD] Reword the stale doc comment on InjectNodeSelectorRequirements (pkg/utils/affinity.go:31); document or defensively loop the single-term assumption in injectRequiredAffinity's NodeSelectorTerms[0] read (pkg/dataflow/affinity.go:152); consider a DeepCopy or an explicit aliasing note for the in-place mutation contract; e2e coverage for dataflow Require affinity can land in a dedicated PR.
[Filed] none
Non-blocking observations (4) — not part of this round's ask; fix now, follow up, or file an issue as you see fit
- pkg/dataflow/affinity.go:152 [nit] — pre-existing, not introduced by this PR — injectRequiredAffinity reads only NodeSelectorTerms[0] of the preceding operation — undocumented single-term producer/consumer assumption
F1 (disputed, mechanism rebutted by Codex; adjudicated down from minor to nit): injectRequiredAffinity unconditionally reads NodeSelectorTerms[0] of the preceding op's affinity (comment: "currently, only has one element"; the same [0] pattern also exists at :107-108 in injectPreferredAffinity). Claude's original wrong/over-constraining-pinning scenario was refuted: the consumed value is precedingOpStatus.NodeAffinity, populated only via dataflow.GenerateNodeAffinity, which constructs exactly one term from the succeeded pod's actual node labels — webhook-built multi-term (Cartesian-product) affinities live in pod specs and never reach status.nodeAffinity, and status is controller-owned. What stands is a robustness nit: if GenerateNodeAffinity ever emits multiple terms, the [0] read silently drops the rest. Worth a defensive loop or a comment documenting the assumption; non-gating, pre-existing. - pkg/utils/affinity.go:32 [nit] — pre-existing, not introduced by this PR — In-place mutation of the caller's affinity is a sharp edge, newly observable via this PR's existing-terms branch
F4 (consensus — raised by Claude, confirmed and extended by Codex): InjectNodeSelectorRequirements mutates the passed affinity in place, and callers pass spec.Affinity pointers into the CR object. The existing-terms branch was a no-op on base, so this PR newly makes the aliasing observable (the aliasing contract itself is pre-existing). Traced safe today across all engine callers: utils.GetDataLoad-style client.Get into a per-reconcile deepcopy (controller-runtime v0.17.5, cache not disabled), and after Operate only the status subresource is written back. A DeepCopy at the dataflow entry point, or an explicit aliasing note on the function, would remove the hazard for future refactors. Non-gating. - integration:1 [minor] — pre-existing, not introduced by this PR — No e2e scenario for Require-policy dataflow affinity anywhere in the repo
F3 (consensus on substance; classification adjudicated per Codex: the gap predates this PR, so introducedByPR is false — the PR adds focused unit and fake-client entry-point tests, which are appropriate for a pure-function fix, but does not close the repo-wide e2e gap): no e2e or integration test references AffinityStrategy/affinityStrategy/runAfter, so the user-visible contract — the DataLoad/DataMigrate/DataProcess Job pod actually landing on the preceding operation's node (and legitimately staying Pending on conflict) — is never exercised against a real scheduler. Codex's envtest layer covers apiserver validity of the merged spec but not placement. Minor, deferrable to a dedicated dataflow-affinity e2e PR; non-gating. - pkg/utils/affinity.go:31 [nit] — pre-existing, not introduced by this PR — Stale doc comment on InjectNodeSelectorRequirements now contradicts the (fixed) append semantics
F2 (consensus — Claude's F2 = Codex's O1, found independently by both): the function comment still says "injects(not append) a node selector term to affinity's nodeAffinity", but the PR's whole point is that the injected requirements are now appended to every existing term (and a term is synthesized only when none exist). The PR updated the inline comment but left the doc line. One-line fix; the PR already reworks this function, so rewording here is free. Pre-existing, non-gating.
Verified review delta
- F1: open (preexisting)
- F4: open (preexisting)
- F3: open (preexisting)
- F2: open (preexisting)
Code: clear. CI: success; attribution: unknown. Required CI checks still apply.
Reword the InjectNodeSelectorRequirements comment: it appends the requirements to every existing term and changes a non-nil affinity in place. Explain why injectRequiredAffinity and injectPreferredAffinity read only the first term of the preceding operation, which GenerateNodeAffinity builds as a single term. Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
|
Thanks @cheyang for the thorough review! Pushed ff3923c with the comment changes:
I agree the e2e coverage for dataflow |
|



Ⅰ. Describe what this PR does
InjectNodeSelectorRequirementsis howrunAfter.affinityStrategywithpolicy: Requireadds the node (or zone, region, custom label) of the preceding data operation to the pod affinity of the next one.When the operation already had its own required node affinity, the function looped with
for _, nodeSelectorTerm := range ...NodeSelectorTermsand appended to the loop variable.rangehands out a copy of each term, so the appended requirements were discarded and the pod was not tied to the preceding operation's node.This PR appends through the slice index so that every existing term gets the requirement (node selector terms are ORed, so each one needs it). It also treats an empty but non-nil
NodeSelectorTermsslice like a nil one, so the requirement is added as a new term instead of being lost there as well.Ⅱ. Does this pull request fix one issue?
fixes #6275
Ⅲ. List the added test cases (unit test/integration test) if any, please explain if no tests are needed.
pkg/utils,TestInjectNodeSelectorTermsToAffinity: "requirements are appended to every existing term" (two existing terms) and "empty but non-nil terms get a new term".pkg/dataflow,TestInjectAffinityByRunAfterOp: "require policy, operation already has its own required affinity" (a GPU term plus the hostname of the preceding operation).All three cases fail on master and pass with this change. The existing cases still pass.
Ⅳ. Describe how to verify it
go test -gcflags=all=-l ./pkg/utils/ -run TestInjectNodeSelectorTermsToAffinity
go test -gcflags=all=-l ./pkg/dataflow/ -run TestInjectAffinityByRunAfterOp
Ⅴ. Special notes for reviews
The function still edits the affinity it is given in place and returns it, as before. Operations that have their own required affinity and use
Requirewill now also carry the requirement, so if the two cannot be satisfied together the pod stays Pending, which is whatRequireasks for. ThePreferpath and the webhook'sInjectNodeSelectorTermsare untouched.