← Back to Pull requests

feat(utils): sort and unify map iteration in NodeSelector and affinity generation sites #6234

Closed
unknown closed this pull request without merging on Nov 12, 2026 04:59 UTC
View on GitHub
unknown unknown commented on Nov 08, 2026 07:01 UTC
Author

Ⅰ. Describe what this PR does

Direct follow-up to #6217 and fixes #6220.

In #6216 and #6217, MergeNodeSelectorAndNodeAffinity was updated to sort nodeSelector keys alphabetically to ensure deterministic output order and prevent spurious status updates. During review of #6217, several other sites were identified where map iteration without key sorting caused non-deterministic ordering:

1. Volume creation sites:
- pkg/utils/dataset/volume/create.go: CreatePersistentVolumeForRuntime iterated over nodeSelector map directly when building PV node affinity MatchExpressions.
- pkg/ddc/efc/create_volume.go: createPersistentVolumeForRuntime iterated over nodeSelector map directly when building PV node affinity MatchExpressions.
2. Webhook sites:
- pkg/webhook/plugins/requirenodewithfuse/require_node_with_fuse.go: getRequiredSchedulingTerm iterated over selectors map directly to append MatchExpressions.
- pkg/webhook/plugins/nodeaffinitywithcache/node_affinity_with_cache.go: getTieredLocalityPreferredSchedulingTerms and getTieredLocalityNodeSelectorTerms iterated over preferredRuntimes and runtimeInfos maps directly, producing non-deterministic term order in mutated pod specs for pods bound to multiple datasets/runtimes.

This PR addresses all these sites:

  • Introduces utils.ToNodeSelectorRequirements(selectors map[string]string) []corev1.NodeSelectorRequirement in pkg/utils/affinity.go, which converts selector maps into []corev1.NodeSelectorRequirement sorted alphabetically by key.

  • Refactors MergeNodeSelectorAndNodeAffinity (pkg/utils/kubeclient/pod.go), CreatePersistentVolumeForRuntime (pkg/utils/dataset/volume/create.go), createPersistentVolumeForRuntime (pkg/ddc/efc/create_volume.go), and RequireNodeWithFuse webhook (pkg/webhook/plugins/requirenodewithfuse) to use utils.ToNodeSelectorRequirements.

  • Refactors NodeAffinityWithCache webhook (pkg/webhook/plugins/nodeaffinitywithcache) to iterate over utils.OrderedKeys(preferredRuntimes) and utils.OrderedKeys(runtimeInfos), guaranteeing deterministic term order.
  • Ⅱ. Does this pull request fix one issue?


    fixes #6220

    Ⅲ. List the added test cases (unit test/integration test) if any, please explain if no tests are needed.


  • pkg/utils/affinity_test.go:

  • - Added unit test TestToNodeSelectorRequirements covering nil map, empty map, single entry, multi-entry sorted, and 100 repeated iterations asserting deterministic ordering.
  • pkg/webhook/plugins/requirenodewithfuse/require_node_with_fuse_test.go:

  • - Added test case for multi-label fuse node selector verifying sorted MatchExpressions across 50 iterations.
  • pkg/utils/dataset/volume/create_test.go:

  • - Added test case verifying PV node affinity match expressions are sorted alphabetically for multi-label selectors.
  • pkg/ddc/efc/create_volume_test.go:

  • - Added test TestEFCEngine_createFusePersistentVolumeWithNodeSelector verifying PV node affinity match expressions are sorted alphabetically.
  • pkg/webhook/plugins/nodeaffinitywithcache/node_affinity_with_cache_test.go:

  • - Added test TestTieredLocalityDeterministicOrdering verifying that both preferred and required scheduling terms for multi-runtime pods are strictly sorted and deterministic across 50 iterations.

    Ⅳ. Describe how to verify it


    go test -v -gcflags="all=-N -l" ./pkg/utils ./pkg/utils/kubeclient ./pkg/webhook/plugins/requirenodewithfuse ./pkg/utils/dataset/volume ./pkg/ddc/efc ./pkg/webhook/plugins/nodeaffinitywithcache
    go vet ./pkg/utils ./pkg/utils/kubeclient ./pkg/webhook/plugins/requirenodewithfuse ./pkg/utils/dataset/volume ./pkg/ddc/efc ./pkg/webhook/plugins/nodeaffinitywithcache
    gofmt -l pkg/utils/ pkg/ddc/efc/ pkg/webhook/plugins/
    

    Ⅴ. Special notes for reviews


    None.

    Diff: 479 lines (+359 / -120) across 5 files Reviewer: cheyang Review duration: 5637m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 479 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #6234"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.8% (MEDIUM) · Higher = larger blast radius
    45.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.1% · Safe to merge
    18.1%