← Back to Pull requests

feat: Add webhook namespace-selector for multi-tenant deployments #2943

Closed
unknown closed this pull request without merging on Oct 07, 2026 07:06 UTC
View on GitHub
unknown unknown commented on May 13, 2026 15:14 UTC
Author

Enables running multiple spark-operator instances in one cluster, each scoped to a subset of namespaces by label.

Go:

  • Add --namespace-selector flag to the webhook, mirroring the controller.

  • Introduce namespaceFilteringDefaulter / namespaceFilteringValidator wrappers applied to every webhook handler at registration. Objects in unmatched namespaces skip the inner handler; non-namespaced objects pass through.

  • Drop the in-process namespace map in SparkPodDefaulter -- the wrapper is now the single source of truth.

  • Remove the dead --label-selector-filter flag.

  • Add corev1.Namespace to the webhook cache so the wrapper's MatchesWithClient label lookup goes through the informer.

  • Unit tests for both wrappers: explicit-namespace match/miss, label-selector match/miss with a fake client, NamespaceAll fast path, pass-through, and inner-error propagation.
  • Helm:

  • Add spark.webhookNamespaceSelector as a structured LabelSelector embedded into every MutatingWebhookConfiguration / ValidatingWebhookConfiguration webhook entry's namespaceSelector field.

  • Pass --namespace-selector to the webhook deployment from spark.jobNamespaceSelector.

  • Grant the webhook ClusterRole namespaces get/list/watch when jobNamespaceSelector is set, mirroring the controller's pattern.
  • <!-- Thanks for sending a pull request! Here are some tips for you:
    1. If this is your first time, check our contributor guidelines: https://www.kubeflow.org/docs/about/contributing
    2. To know more about how to develop with the Spark operator, check the developer guide: https://www.kubeflow.org/docs/components/spark-operator/developer-guide/
    3. If you want faster PR reviews, check how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
    4. Please open an issue to discuss significant work before you start. We appreciate your contributions and don't want your efforts to go to waste!
    -->

    Purpose of this PR

    At the moment the only way to install multiple spark-operators in the same Kubernetes cluster is by specifying the following attribute with all the Namespaces that it will be responsible for

    spark:
      jobNamespaces:
        - ns1
        - ns2
    

    The problem with this approach is that it is very static. Each time a new Namespace is created you have to helm upgrade. Additionally, all namespaces declared must already exist as the chart will attempt creating Roles and RoleBindings in these namespaces.

    Resolves https://github.com/kubeflow/spark-operator/issues/2913

    Proposed changes:

    A more dynamic approach would be for the controller and the webhook to filter events from responsible namespaces based on some labels. Luckily, https://github.com/kubeflow/spark-operator/pull/2808 already implements this but only for the controller. The webhook does not take the labels into consideration.

    Re-use the same util.NamespaceMatcher struct in the webhooks responsible for

  • SparkConnect

  • SparkApplication

  • ScheduledSparkApplication

  • Pod
  • In addition to the webhooks above use the same logic in the mutatingwebhookconfiguration and validatingwebhookconfiguration controllers.

    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

    https://github.com/kubeflow/spark-operator/issues/2913

    Checklist

    <!-- Before submitting your PR, please review the following: -->

  • 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

    Diff: 485 lines (+363 / -122) across 6 files Reviewer: vjanelle Review duration: 211192m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.71) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 485 lines diff across 6 files
    0.71
    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.370
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #2943"
    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: 70.5% (HIGH) · Higher = larger blast radius
    70.5%
    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: 27.9% · Safe to merge
    27.9%