fix(webhook): fall back to admission request namespace when pod namespace is empty (Kubernetes 1.22 compatibility) #3057
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
shepherd44
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 bypod.Namespace:spark-submit (fabric8 client) POSTs pods without
metadata.namespacein the request body (the namespace is only in the URL path). On Kubernetes 1.22, the API server does not populatemetadata.namespaceon the object sent to admission webhooks in this case, sopod.Namespaceis 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: Faildoes not trigger, and nothing is logged.Proposed changes:
SparkPodDefaulter.Default(): whenpod.Namespaceis empty, fall back to the namespace of the admission request (admission.RequestFromContext), which is always populated by the API server from the request URL.Change Category
Rationale
Environment where this was hit:
ghcr.io/kubeflow/spark-operator/controller:2.4.0, digestsha256:067ee917...), Helm chart 2.4.0, webhook enabled,spark.jobNamespacesset to an explicit list. Also reproduced on 2.5.1.v1.35.6-eks-8f14419) mutates the same SparkApplication correctly.Observed behavior on 1.22:
Defaulting SparkApplicationlines and 0Mutating Spark podlines, while/mutate--v1-podserved 30k+ requests, all HTTP 200, zero 500.fabric8-kubernetes-cliententry inmanagedFieldscontainingsparkoperator.k8s.io/app-name,.../launched-by-spark-operator), scheduled, and runs — but without the tolerations/volumes/initContainers declared inspec.driver.kubectl create(which putsmetadata.namespaceinto 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:Note that setting
spark.jobNamespacesto include the empty string is not a workaround:isSparkJobNamespace("")would pass, but the subsequentclient.Getwith an empty namespace fails and, withfailurePolicy: 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-basednamespaceSelectorand 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