← Back to Pull requests

fix: stop adding SparkApplication owner reference to executor pods #3152

Closed
unknown closed this pull request without merging on Sep 14, 2026 04:45 UTC
View on GitHub
unknown unknown commented on Sep 08, 2026 02:34 UTC
Author

Purpose of this PR

Proposed changes:

  • Stop adding the SparkApplication as a second (non-controller) ownerReference on executor pod templates at submission time, so that executor pods are garbage-collected as soon as their controller owner (the driver pod) is deleted.

  • Update the expectations in TestExecutorPodTemplateContents and TestSubmitPodTemplates so executor templates carry only user-supplied owner references; driver templates keep the controller reference to the SparkApplication (TestDriverPodTemplateContents is unchanged).
  • Problem

    Since #2670 every executor pod has two owners: the driver pod (controller: true, set by Spark itself) and the SparkApplication (controller unset, added by the operator). Kubernetes garbage collection only deletes a dependent once all of its owners are gone, so deleting the driver pod (manually, or via spec.suspend=true, e.g. when Kueue preempts the workload) leaves the executor pods behind until the SparkApplication itself is deleted. Across Kueue requeue cycles this accumulates orphaned executor pods.

    Root cause

    internal/controller/sparkapplication/submission.go:1199-1209 (buildExecutorPodTemplate) appends util.GetOwnerReference(app) with Controller/BlockOwnerDeletion cleared to the executor pod template. That extra owner is what blocks the cascade deletion from the driver pod. The webhook path (internal/webhook/sparkpod_defaulter.go addOwnerReference) already returns early for non-driver pods, so submission is the only place that adds it.

    Fix

    Remove the executor ownerReference block in buildExecutorPodTemplate. Executor pods keep their controller reference to the driver pod, and the driver pod keeps its controller reference to the SparkApplication, so deleting the SparkApplication still cascades to the driver and then to the executors, while deleting only the driver now cascades to the executors as well. This follows the approach suggested in the issue discussion (drop the executor's SparkApplication reference and rely on GC cascade deletion instead of adding operator-managed deletion of executor pods).

    The original motivation for the extra reference was to let other controllers (e.g. Kueue) recognize executor pods as children of the SparkApplication. Kueue's ownership walk (FindAncestorJobManagedByKueue) and its IsOwnerManagedByKueueForObject check both use metav1.GetControllerOf, i.e. only controller references, and its SparkApplication integration identifies executor pods by the spark-role label and reads readiness from status.executorState, so the non-controller reference is not consumed there.

    How tested

    Unit tests (no cluster required) plus an e2e assertion in the existing suspend/resume test.

    Before the fix, with the updated expectations, three subtests fail. Two are shown below; the third, TestSubmitPodTemplates/sends_default_templates_with_owner_ref_when_not_specified (internal/controller/sparkapplication/rest_submission_test.go:203), fails the same way because the submitted executor template still carries the SparkApplication owner reference (Empty assertion on OwnerReferences) and is only absent from the output because of the -run filter. The "pod template already has owner references" case is unchanged and passes both before and after, because user-supplied owner references are preserved as-is.

    $ go test ./internal/controller/sparkapplication/ -run 'TestExecutorPodTemplateContents|TestDriverPodTemplateContents' -count=1
    --- FAIL: TestExecutorPodTemplateContents (0.00s)
        --- FAIL: TestExecutorPodTemplateContents/with_pod_template (0.00s)
            submission_test.go:645:
                	Diff:
                	--- Expected
                	+++ Actual
                	@@ -6,2 +6,6 @@
                	     uid: ""
                	+  - apiVersion: sparkoperator.k8s.io/v1beta2
                	+    kind: SparkApplication
                	+    name: test
                	+    uid: 2d2d2c08-fcc9-4e41-9d93-13540605f24e
                	 spec:
        --- FAIL: TestExecutorPodTemplateContents/no_pod_template (0.00s)
            submission_test.go:645:
                	Diff:
                	--- Expected
                	+++ Actual
                	@@ -1,2 +1,7 @@
                	-metadata: {}
                	+metadata:
                	+  ownerReferences:
                	+  - apiVersion: sparkoperator.k8s.io/v1beta2
                	+    kind: SparkApplication
                	+    name: test
                	+    uid: 2d2d2c08-fcc9-4e41-9d93-13540605f24e
                	 spec:
    FAIL
    FAIL	github.com/kubeflow/spark-operator/v2/internal/controller/sparkapplication	0.031s
    

    After the fix:

    $ go test ./internal/controller/sparkapplication/ -run 'TestExecutorPodTemplateContents|TestDriverPodTemplateContents' -count=1 -v
    --- PASS: TestDriverPodTemplateContents (0.00s)
    --- PASS: TestExecutorPodTemplateContents (0.00s)
    ok  	github.com/kubeflow/spark-operator/v2/internal/controller/sparkapplication	0.030s
    
    $ KUBEBUILDER_ASSETS=... go test ./internal/controller/sparkapplication/... ./internal/webhook/... -count=1
    ok  	github.com/kubeflow/spark-operator/v2/internal/controller/sparkapplication
    ok  	github.com/kubeflow/spark-operator/v2/internal/webhook
    

    gofmt -l, go vet, go build ./... and golangci-lint run ./internal/controller/sparkapplication/... are clean.

    e2e (added on request of the reviewers): the spark-pi: Suspend and Resume case in test/e2e/sparkapplication_test.go now waits for an executor pod to exist before suspending and, once the application reaches Suspended, asserts that no pod labelled sparkoperator.k8s.io/app-name=spark-pi remains (driver or executor). Without this fix the executor pod survives the driver deletion because the SparkApplication is still an owner, so the assertion fails; with the fix the executors are cascade-deleted with the driver. gofmt -l and go vet ./... in test/e2e are clean; the CI e2e matrix runs it.

    Links

  • Issue: https://github.com/kubeflow/spark-operator/issues/2961

  • Change that introduced the executor owner reference: https://github.com/kubeflow/spark-operator/pull/2670

  • Related Kueue report: https://github.com/kubernetes-sigs/kueue/issues/11675
  • Change Category

  • Bugfix (non-breaking change which fixes an issue)

  • Feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that could affect existing functionality)

  • Documentation update
  • Rationale

    Covered above. Executor pods created by an operator built before this change keep their existing owner references; only newly submitted applications are affected.

    Checklist

  • I have conducted a self-review of my own code.

  • I have updated documentation accordingly.

  • I have added tests that prove my changes are effective or that my feature works.

  • Existing unit tests pass locally with my changes.
  • Additional Notes

    This change was prepared with an AI agent operated by KR-Ravindra, who reviewed and tested it.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: vara-bonthu Review duration: 8770m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    56%
    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
    Real PR Conversation Timeline
    1 comments
    "Review active on #3152"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 22.5% · Safe to merge
    22.5%