← Back to Pull requests

fix(webhook): fall back to admission request namespace when pod namespace is empty (Kubernetes 1.22 compatibility) #3057

Open
unknown opened this pull request with 571 lines diff across 7 files
View on GitHub
unknown unknown commented on Jul 28, 2026 04:47 UTC
Author

Purpose of this PR

Fixes silent no-op of the pod mutating webhook on older Kubernetes API servers (verified on 1.22): driver/executor pods created by spark-submit are admitted without any mutation (no volumes, tolerations, initContainers, etc. from the SparkApplication spec), with no error and no log line.

Root cause

SparkPodDefaulter.Default() filters pods by pod.Namespace:

namespace := pod.Namespace
if !d.isSparkJobNamespace(namespace) {
    return nil
}

spark-submit (fabric8 client) POSTs pods without metadata.namespace in the request body (the namespace is only in the URL path). On Kubernetes 1.22, the API server does not populate metadata.namespace on the object sent to admission webhooks in this case, so pod.Namespace is an empty string inside the webhook, isSparkJobNamespace("") returns false, and the defaulter returns without mutating. Newer API servers populate the namespace before calling webhooks, which is why this does not reproduce there.

The result is hard to diagnose: the webhook is called (controller_runtime_webhook_requests_total{webhook="/mutate--v1-pod",code="200"} increments, code="500" stays 0), failurePolicy: Fail does not trigger, and nothing is logged.

Proposed changes:

  • SparkPodDefaulter.Default(): when pod.Namespace is empty, fall back to the namespace of the admission request (admission.RequestFromContext), which is always populated by the API server from the request URL.

  • Unit test covering the fallback (empty pod namespace + request namespace in watched/unwatched namespaces).
  • Change Category

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

    Environment where this was hit:

  • Kubernetes v1.22.9 (on-premises), 3 API servers

  • spark-operator 2.4.0 (ghcr.io/kubeflow/spark-operator/controller:2.4.0, digest sha256:067ee917...), Helm chart 2.4.0, webhook enabled, spark.jobNamespaces set to an explicit list. Also reproduced on 2.5.1.

  • The same image digest on EKS v1.35.6 (v1.35.6-eks-8f14419) mutates the same SparkApplication correctly.
  • Observed behavior on 1.22:

  • Webhook log over 4 days contains 9037 Defaulting SparkApplication lines and 0 Mutating Spark pod lines, while /mutate--v1-pod served 30k+ requests, all HTTP 200, zero 500.

  • Driver pod is created with all operator labels present at creation time (single fabric8-kubernetes-client entry in managedFields containing sparkoperator.k8s.io/app-name, .../launched-by-spark-operator), scheduled, and runs — but without the tolerations/volumes/initContainers declared in spec.driver.

  • Re-creating the same driver pod manifest with kubectl create (which puts metadata.namespace into the body) is mutated correctly by the same webhook instance, on the same cluster, in the same namespace.
  • Minimal reproduction independent of Spark, against a 1.22 API server with the webhook watching spark-test:

    # metadata.namespace omitted from the body (as fabric8 does) — silently accepted, no mutation:
    $ kubectl create --raw "/api/v1/namespaces/spark-test/pods?dryRun=All" -f pod-without-namespace.json
    {"kind":"Pod", ...}
    
    # identical pod with metadata.namespace in the body — webhook processes it
    # (denied here because the referenced SparkApplication does not exist):
    $ kubectl create --raw "/api/v1/namespaces/spark-test/pods?dryRun=All" -f pod-with-namespace.json
    Error from server (Forbidden): admission webhook "mutate--v1-pod.sparkoperator.k8s.io" denied the request:
    failed to get SparkApplication spark-test/nonexistent: SparkApplication.sparkoperator.k8s.io "nonexistent" not found
    

    Note that setting spark.jobNamespaces to include the empty string is not a workaround: isSparkJobNamespace("") would pass, but the subsequent client.Get with an empty namespace fails and, with failurePolicy: Fail, rejects every matching pod.

    Current workaround in our cluster until this is fixed: we keep the legacy 1.x operator (chart 1.4.6, image v1beta2-1.6.2-3.5.0) running purely as the pod mutation engine — its webhook uses a label-based namespaceSelector and no in-process namespace re-check, so it is not affected — while the 2.x operator handles reconciliation. This "2.x controller + 1.x webhook" split works but blocks retiring the 1.x deployment.

    Checklist

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

  • I have updated documentation accordingly (code comment).

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

  • Existing unit tests pass locally with my changes.
  • Diff: 571 lines (+428 / -143) across 7 files Reviewer: shepherd44 Review duration: 152169m
    All Vouch review confidence checks passed
    Residual risk 0.47 = Change Risk (0.80) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.80 · 571 lines diff across 7 files
    0.80
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    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.380
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.080
    Real PR Conversation Timeline
    1 comments
    "Review active on #3057"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 40.9% 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: 80.0% (HIGH) · Higher = larger blast radius
    80.0%
    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: 40.9% · Lower = rushed or shallow review
    40.9%
    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: 47.2% · Moderate risk
    47.2%