mirror of
https://github.com/hanzo-ml/spark-operator.git
synced 2026-10-30 20:39:07 +00:00
* feat: optional PodDisruptionBudget for Spark drivers (#2949) Refs: https://github.com/kubeflow/spark-operator/issues/2949 Add an opt-in PodDisruptionBudget for the driver pod so a voluntary eviction (e.g. `kubectl drain`) cannot kill the driver mid-job. The apiserver's eviction admission handler consults a matching PDB; without one the eviction is admitted and the operator only learns of it after the fact via its watch. A PDB is the only lever the operator has here that does not put it on the eviction hot path. Gated twice, off by default so upgrades are no-op: --enable-driver-pdb controller flag (default false) spec.driverPodDisruptionBudget per-app *bool (nil/false = no PDB) A bare *bool suffices: the driver is a singleton, so MinAvailable is fixed at 1 and a selector struct has nothing to grow into. Lifecycle (driver_pdb.go): - ensureDriverPDB converges PDB existence to spec on every Submitted/Running reconcile, so it recovers from operator restart with the gate flipped on, live spec edits, and transient API failures (retry.RetryOnConflict + controller-runtime backoff). - createDriverPDB/deleteDriverPDB read the cache first for idempotency; AlreadyExists/NotFound are treated as success. - The PDB carries a controller OwnerReference plus LabelLaunchedBySparkOperator + LabelSparkAppName, so kube GC reaps it on app deletion and the cache informer can filter to owned PDBs. - cleanUpOnTermination deletes the PDB on Completed/Failed; the terminal-app reconcile re-runs it to reap PDBs orphaned when the controller itself was drained mid-transition. - deleteSparkResources drops the PDB before the driver pod so an in-flight external drain does not block on a stale PDB. RBAC: the PDB entry in cache.Options.ByObject starts an informer, which needs list+watch even though the code only Get/Create/Deletes. Granted get;list;watch;create;delete on poddisruptionbudgets via the kubebuilder marker (regenerated config/rbac/role.yaml), the Helm chart helper, and the chart rbac unit test. Missing verbs would loop the informer on Forbidden and saturate the shared client rate limiter. Helm: controller.driverPodDisruptionBudget.enable (default false) emits --enable-driver-pdb=true; ci-values.yaml turns it on for e2e. Tests: envtest specs cover the gate matrix and the convergence cases ensureDriverPDB owns (restart-with-gate-on, spec false->true and true->false on a running app, transient Create failure retried via an interceptor.Funcs client). E2e submits real apps with the field set and omitted and asserts PDB creation, GC on deletion, and absence when not opted in. Makefile: kind-load-spark-image preloads the Spark runtime image and e2e-test pins IMAGE_TAG=local with the load targets as prerequisites, so `make deploy` and `make e2e-test` are self-contained on a local Kind cluster instead of hitting ImagePullBackOff. Signed-off-by: Vincent Janelle <[email protected]> * test: use events.FakeRecorder in driver PDB test NewReconciler takes an events.EventRecorder, not the legacy record.EventRecorder. Align driver_pdb_test.go with controller_test.go. Signed-off-by: Vincent Janelle <[email protected]> * allow for kustomize to patch pdb value; default to true in ci Signed-off-by: Vincent Janelle <[email protected]> --------- Signed-off-by: Vincent Janelle <[email protected]> Co-authored-by: Hanzo Dev <[email protected]>
147 lines
5.0 KiB
Go
147 lines
5.0 KiB
Go
/*
|
|
Copyright 2026 The Kubeflow authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package e2e_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
policyv1 "k8s.io/api/policy/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/apimachinery/pkg/util/yaml"
|
|
"k8s.io/utils/ptr"
|
|
|
|
"github.com/kubeflow/spark-operator/v2/api/v1beta2"
|
|
"github.com/kubeflow/spark-operator/v2/pkg/common"
|
|
"github.com/kubeflow/spark-operator/v2/pkg/util"
|
|
)
|
|
|
|
// loadSparkPi parses the canonical spark-pi example and gives the caller a
|
|
// fresh copy. We rename it per-test so two tests can run in the same
|
|
// namespace without colliding.
|
|
func loadSparkPi(name string) *v1beta2.SparkApplication {
|
|
app := &v1beta2.SparkApplication{}
|
|
path := filepath.Join("..", "..", "examples", "spark-pi.yaml")
|
|
file, err := os.Open(path)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
defer func() {
|
|
_ = file.Close()
|
|
}()
|
|
Expect(yaml.NewYAMLOrJSONDecoder(file, 100).Decode(app)).NotTo(HaveOccurred())
|
|
app.Name = name
|
|
app.ResourceVersion = ""
|
|
app.UID = ""
|
|
return app
|
|
}
|
|
|
|
func driverPDBKey(app *v1beta2.SparkApplication) types.NamespacedName {
|
|
return types.NamespacedName{Name: util.GetDriverPodName(app), Namespace: app.Namespace}
|
|
}
|
|
|
|
var _ = Describe("Driver PodDisruptionBudget", func() {
|
|
ctx := context.Background()
|
|
|
|
Context("when the SparkApplication opts in", func() {
|
|
var app *v1beta2.SparkApplication
|
|
|
|
BeforeEach(func() {
|
|
app = loadSparkPi("e2e-pdb-on")
|
|
app.Spec.DriverPodDisruptionBudget = ptr.To(true)
|
|
Expect(k8sClient.Create(ctx, app)).To(Succeed())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
|
|
if err := k8sClient.Get(ctx, key, app); err == nil {
|
|
_ = k8sClient.Delete(ctx, app)
|
|
}
|
|
})
|
|
|
|
It("creates a PDB while the driver is running, and the app completes", func() {
|
|
By("Waiting for the driver PDB to appear")
|
|
pdb := &policyv1.PodDisruptionBudget{}
|
|
Eventually(func() error {
|
|
return k8sClient.Get(ctx, driverPDBKey(app), pdb)
|
|
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
|
|
|
|
By("Verifying the PDB shape")
|
|
Expect(pdb.Spec.MinAvailable).ToNot(BeNil())
|
|
Expect(pdb.Spec.MinAvailable.IntValue()).To(Equal(1))
|
|
Expect(pdb.Spec.Selector.MatchLabels[common.LabelSparkAppName]).To(Equal(app.Name))
|
|
Expect(pdb.Spec.Selector.MatchLabels[common.LabelSparkRole]).To(Equal(common.SparkRoleDriver))
|
|
Expect(pdb.OwnerReferences).ToNot(BeEmpty())
|
|
Expect(pdb.OwnerReferences[0].Name).To(Equal(app.Name))
|
|
Expect(pdb.OwnerReferences[0].Kind).To(Equal("SparkApplication"))
|
|
|
|
By("Waiting for SparkApplication to complete")
|
|
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
|
|
Expect(waitForSparkApplicationCompleted(ctx, key)).NotTo(HaveOccurred())
|
|
})
|
|
|
|
It("garbage-collects the PDB when the SparkApplication is deleted", func() {
|
|
By("Waiting for the driver PDB to appear")
|
|
Eventually(func() error {
|
|
pdb := &policyv1.PodDisruptionBudget{}
|
|
return k8sClient.Get(ctx, driverPDBKey(app), pdb)
|
|
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
|
|
|
|
By("Deleting the SparkApplication")
|
|
Expect(k8sClient.Delete(ctx, app)).To(Succeed())
|
|
|
|
By("Waiting for the PDB to be garbage-collected")
|
|
Eventually(func() bool {
|
|
pdb := &policyv1.PodDisruptionBudget{}
|
|
err := k8sClient.Get(ctx, driverPDBKey(app), pdb)
|
|
return apierrors.IsNotFound(err)
|
|
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
|
|
})
|
|
})
|
|
|
|
Context("when the SparkApplication does not opt in", func() {
|
|
var app *v1beta2.SparkApplication
|
|
|
|
AfterEach(func() {
|
|
if app == nil {
|
|
return
|
|
}
|
|
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
|
|
if err := k8sClient.Get(ctx, key, app); err == nil {
|
|
_ = k8sClient.Delete(ctx, app)
|
|
}
|
|
})
|
|
|
|
It("does not create a PDB when the field is omitted, and the app completes", func() {
|
|
app = loadSparkPi("e2e-pdb-omitted")
|
|
Expect(k8sClient.Create(ctx, app)).To(Succeed())
|
|
|
|
By("Waiting for SparkApplication to complete (proves no PDB was needed for normal lifecycle)")
|
|
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
|
|
Expect(waitForSparkApplicationCompleted(ctx, key)).NotTo(HaveOccurred())
|
|
|
|
By("Confirming no PDB was ever created")
|
|
pdb := &policyv1.PodDisruptionBudget{}
|
|
err := k8sClient.Get(ctx, driverPDBKey(app), pdb)
|
|
Expect(apierrors.IsNotFound(err)).To(BeTrue(), "expected NotFound but got: err=%v pdb=%+v", err, pdb)
|
|
})
|
|
})
|
|
})
|