← Back to Pull requests

fix(module): preserve legacy Deployment selector labels for 3.5→3.6 upgrade #212

Closed
unknown closed this pull request without merging on Oct 22, 2026 18:49 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 14:48 UTC
Author

Summary

Fixes RHAI-10913: Spark Operator deployment selector immutable field error during RHOAI 3.5.x → 3.6 upgrades.

Updated approach based on @RobuRishabh's review feedback - uses surgical JSON6902 patches instead of includeSelectors: true to avoid breaking Spark job networking.

Problem

RHOAI 3.5.0 created spark-operator-controller and spark-operator-webhook Deployments with 6 selector labels. RHOAI 3.6.0 tries to apply only 4 labels. Since Kubernetes deployment selectors are immutable, the upgrade fails with:

Deployment.apps "spark-operator-controller" is invalid: 
spec.selector: Invalid value: ...: field is immutable

Root Cause

  • 3.5 (in-tree component): Used kustomize.WithLabel() which added app.kubernetes.io/part-of and app.opendatahub.io/sparkoperator to deployment selectors

  • 3.6 (out-of-tree module): Set includeSelectors: false, which excluded these labels from selectors

  • Result: Kubernetes rejects the selector change as immutable
  • Solution

    Use JSON6902 patches to surgically add legacy labels to:
    1. spec.selector.matchLabels - fixes upgrade (selector immutability)
    2. spec.template.metadata.labels - satisfies Kubernetes requirement (pod labels must be superset of selector)

    Why NOT includeSelectors: true?

    The initial fix used includeSelectors: true, but @RobuRishabh identified a critical flaw:

    Problem: includeSelectors: true adds labels to ALL selectors, including NetworkPolicy podSelector:

    # NetworkPolicy gets polluted:
    spec:
      podSelector:
        matchLabels:
          sparkoperator.k8s.io/launched-by-spark-operator: "true"  # Original
          app.kubernetes.io/part-of: sparkoperator                 # Added (breaks!)
          app.opendatahub.io/sparkoperator: "true"                 # Added (breaks!)
    

    Impact:

  • NetworkPolicy uses AND semantics - pods must match ALL labels

  • Spark driver/executor pods only have sparkoperator.k8s.io/launched-by-spark-operator

  • NetworkPolicy stops matching Spark pods → connectivity failure or security regression
  • The Correct Fix: JSON6902 Patches

    patches:
      - target:
          kind: Deployment
          name: spark-operator-controller
        patch: |-
          # Add to selector (fixes upgrade)
          - op: add
            path: /spec/selector/matchLabels/app.kubernetes.io~1part-of
            value: sparkoperator
          # Add to pod template (Kubernetes requirement)
          - op: add
            path: /spec/template/metadata/labels/app.kubernetes.io~1part-of
            value: sparkoperator
    

    This targets only Deployments, leaving NetworkPolicy untouched.

    Testing

    Regression Tests Added

    New test file: test/kustomize/overlay_selector_labels_test.go

    Three test functions:
    1. TestRHOAIOverlayPreserves35SelectorLabels - validates 6-label selectors AND pod labels
    2. TestODHOverlayPreserves2xSelectorLabels - validates 6-label selectors AND pod labels
    3. TestNetworkPolicySelectorsNotPolluted - prevents the includeSelectors: true regression

    All tests pass:

    $ go test ./test/kustomize/ -v -run Selector
    === RUN   TestRHOAIOverlayPreserves35SelectorLabels
    --- PASS: TestRHOAIOverlayPreserves35SelectorLabels (0.93s)
    === RUN   TestODHOverlayPreserves2xSelectorLabels
    --- PASS: TestODHOverlayPreserves2xSelectorLabels (0.46s)
    === RUN   TestNetworkPolicySelectorsNotPolluted
    --- PASS: TestNetworkPolicySelectorsNotPolluted (0.91s)
    PASS
    ok      github.com/kubeflow/spark-operator/v2/test/kustomize    2.310s
    

    Manual Validation

    Tested with the reproduction scripts from the bug report:

  • ✅ Fresh 3.5.0 install → 3.6.0 upgrade succeeds

  • ✅ SparkOperatorReady condition becomes True

  • ✅ No manual deployment deletion required

  • ✅ NetworkPolicy selectors unchanged (verified with kubectl kustomize)
  • Changes

    Modified Files


  • config/overlays/rhoai/kustomization.yaml - JSON6902 patches for RHOAI

  • config/overlays/odh/kustomization.yaml - JSON6902 patches for ODH

  • test/kustomize/overlay_selector_labels_test.go - regression tests (new file)
  • Diff Summary

    RHOAI overlay:

    patches:
      # Add legacy labels ONLY to Deployment selectors + pod templates
      - target:
          kind: Deployment
          name: spark-operator-controller
        patch: |-
          - op: add
            path: /spec/selector/matchLabels/app.kubernetes.io~1part-of
            value: sparkoperator
          - op: add
            path: /spec/selector/matchLabels/app.opendatahub.io~1sparkoperator
            value: "true"
          - op: add
            path: /spec/template/metadata/labels/app.kubernetes.io~1part-of
            value: sparkoperator
          - op: add
            path: /spec/template/metadata/labels/app.opendatahub.io~1sparkoperator
            value: "true"
      # (same for webhook)
    
    labels:
      - pairs:
          app.kubernetes.io/part-of: rhoai  # Changed back from "sparkoperator"
        includeSelectors: false  # Keeps NetworkPolicy safe
    

    Related Issues

  • Jira: RHAI-10913

  • Similar fixes:

  • - KubeRay: opendatahub-io/kuberay#247
    - TrustyAI: trustyai-explainability/trustyai-service-operator#1041

    Release Notes

    fix(module): Spark Operator deployments now preserve legacy selector and pod labels for smooth 3.5→3.6 upgrades without manual intervention or NetworkPolicy breakage
    

    🤖 Generated with Claude Code

    [RHAI-10913]: https://redhat.atlassian.net/browse/RHAI-10913?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: RobuRishabh Review duration: 1681m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #212"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    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: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    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: 29.7% · Safe to merge
    29.7%