You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2026/09/24 17:50:32 UTC
(spark-kubernetes-operator) branch main updated: [SPARK-59766] Make a pending Kueue `Workload` follow the changed or removed queue label
This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-kubernetes-operator.git
The following commit(s) were added to refs/heads/main by this push:
new 5c2ccf6a [SPARK-59766] Make a pending Kueue `Workload` follow the changed or removed queue label
5c2ccf6a is described below
commit 5c2ccf6af2516f695cac1527f205ca633bd57a4d
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Thu Sep 24 10:50:25 2026 -0700
[SPARK-59766] Make a pending Kueue `Workload` follow the changed or removed queue label
### What changes were proposed in this pull request?
This PR aims to make a pending Kueue `Workload` follow the changed or removed `kueue.x-k8s.io/queue-name` label.
- If the label changes before the `Workload` reserves quota, move it to the new queue in place, like Kueue's `QueueNameChange`.
- If the label is removed, delete the pending `Workload` before the driver or master is created.
After the quota is reserved, Kueue freezes the queue name, so the `Workload` keeps its queue. An admitted `Workload` is kept in both cases.
### Why are the changes needed?
Currently, a pending `Workload` stays in the old queue after the label changes. If the label is removed, the `Workload` is left behind, and Kueue may admit it later into quota that nothing uses.
### Does this PR introduce _any_ user-facing change?
Yes, but only when `spark.kubernetes.operator.kueue.enabled` is true. It is disabled by default.
### How was this patch tested?
Pass the CIs with the newly added test cases.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #881 from dongjoon-hyun/SPARK-59766.
Authored-by: Dongjoon Hyun <do...@apache.org>
Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
docs/spark_custom_resources.md | 7 +-
.../spark/k8s/operator/context/BaseContext.java | 18 ++++
.../k8s/operator/kueue/KueueWorkloadUtils.java | 74 +++++++++++++--
.../reconciler/reconcilesteps/AppInitStep.java | 6 +-
.../reconciler/reconcilesteps/ClusterInitStep.java | 6 +-
.../operator/context/SparkClusterContextTest.java | 28 ++++++
.../k8s/operator/kueue/KueueWorkloadUtilsTest.java | 105 +++++++++++++++++++++
.../reconciler/reconcilesteps/AppInitStepTest.java | 76 +++++++++++++++
.../reconcilesteps/ClusterInitStepTest.java | 32 ++++++-
9 files changed, 338 insertions(+), 14 deletions(-)
diff --git a/docs/spark_custom_resources.md b/docs/spark_custom_resources.md
index 70c6b56c..3351d133 100644
--- a/docs/spark_custom_resources.md
+++ b/docs/spark_custom_resources.md
@@ -635,7 +635,12 @@ spec:
the status is not there to fall back on, the pending event is republished while the `Workload`
waits, so that it outlives the event retention of the API server. Use `kubectl get workload` to
see the admission status. If the spec changes while waiting, the
- `Workload` is recreated with the new resource requests.
+ `Workload` is recreated with the new resource requests. Like Kueue, if the
+ `kueue.x-k8s.io/queue-name` label changes before the `Workload` reserves quota, the `Workload` is
+ moved to the new queue in place. After that Kueue freezes the queue name, so the change is
+ ignored and the resource stays in the old queue. If the label is removed while waiting, the
+ `Workload` is deleted, so that Kueue does not admit it later into quota which nothing uses, and
+ the resource starts without Kueue.
* A queued resource spends one more reconciliation on the admission itself. With the default rate
limiter (5 reconciliations per 15 seconds), an application that finishes within the first 15
seconds may be observed only after its driver completed. It then reports its terminal state up
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/context/BaseContext.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/context/BaseContext.java
index 5df681d7..b963a93b 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/context/BaseContext.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/context/BaseContext.java
@@ -19,7 +19,10 @@
package org.apache.spark.k8s.operator.context;
+import static org.apache.spark.k8s.operator.config.SparkOperatorConf.KUEUE_ENABLED;
+
import java.util.Map;
+import java.util.Optional;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.event.ResourceEventRecorder;
@@ -27,6 +30,7 @@ import io.javaoperatorsdk.operator.api.reconciler.Context;
import org.apache.spark.k8s.operator.BaseResource;
import org.apache.spark.k8s.operator.kueue.KueuePodSetFlavor;
+import org.apache.spark.k8s.operator.kueue.v1beta2.Workload;
/**
* Base class for context objects.
@@ -86,6 +90,20 @@ public abstract class BaseContext<CR extends BaseResource<?, ?, ?, ?, ?>> {
return josdkContext.getClient();
}
+ /**
+ * Returns the Kueue Workload of the resource associated with this context from the informer
+ * cache, which is registered only with the Kueue integration, so that a resource which was never
+ * queued costs no request to find out.
+ *
+ * @return The cached Workload, or empty if there is none or the Kueue integration is disabled.
+ */
+ public Optional<Workload> getCachedKueueWorkload() {
+ if (!KUEUE_ENABLED.getValue()) {
+ return Optional.empty();
+ }
+ return josdkContext.getSecondaryResource(Workload.class);
+ }
+
/**
* Returns the event recorder bound to the resource associated with this context. Recording an
* event is best effort: the recorder drops the event when publishing is disabled, and logs and
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtils.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtils.java
index 2676f19b..ecfe1c41 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtils.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtils.java
@@ -165,6 +165,22 @@ public final class KueueWorkloadUtils {
return new AdmissionResponse(AdmissionResult.STALE, workload);
}
WorkloadSpec spec = workload.getSpec();
+ boolean changed = false;
+ String desiredQueueName = desired.getSpec().getQueueName();
+ boolean quotaReserved = workload.getStatus() != null && workload.getStatus().isQuotaReserved();
+ if (!quotaReserved && !Objects.equals(spec.getQueueName(), desiredQueueName)) {
+ // Like Kueue, a queue label changed before the quota is reserved moves the Workload to the
+ // new queue in place. After that, the Workload CEL rules freeze its queue name.
+ log.info(
+ "Moving the pending Kueue Workload {} to queue {}.",
+ workload.getMetadata().getName(),
+ desiredQueueName);
+ spec.setQueueName(desiredQueueName);
+ Map<String, String> labels = new HashMap<>(workload.getMetadata().getLabels());
+ labels.put(Constants.LABEL_QUEUE_NAME, desiredQueueName);
+ workload.getMetadata().setLabels(labels);
+ changed = true;
+ }
PriorityClassRef desiredPriorityClassRef = desired.getSpec().getPriorityClassRef();
if (desired.getSpec().getPriority() != null
&& !Objects.equals(spec.getPriorityClassRef(), desiredPriorityClassRef)
@@ -177,6 +193,9 @@ public final class KueueWorkloadUtils {
workload.getMetadata().getName());
spec.setPriorityClassRef(desiredPriorityClassRef);
spec.setPriority(desired.getSpec().getPriority());
+ changed = true;
+ }
+ if (changed) {
client.resource(workload).update();
}
return new AdmissionResponse(AdmissionResult.PENDING, workload);
@@ -251,14 +270,10 @@ public final class KueueWorkloadUtils {
// Like Kueue, a node selector conflict is permanent, so the quota is released before the
// caller fails the resource, which this step does not reach again. The release itself is
// retried, since a Workload left behind holds the quota of a resource that never starts.
- try {
- deleteWorkloadOf(context.getClient(), context.getResource());
- } catch (KubernetesClientException releaseFailure) {
- return Optional.of(
- retryAfterRequestFailure(
- context,
- releaseFailure,
- "Failed to release the Kueue Workload of a rejected admission"));
+ Optional<ReconcileProgress> retry =
+ releaseOrRetry(context, "Failed to release the Kueue Workload of a rejected admission");
+ if (retry.isPresent()) {
+ return retry;
}
throw e;
}
@@ -365,6 +380,49 @@ public final class KueueWorkloadUtils {
return ReconcileProgress.completeAndDefaultRequeue();
}
+ /**
+ * Releases the pending Workload of a resource whose queue label was removed while it waited for
+ * the admission. The resource now starts without Kueue, so Kueue would otherwise admit the
+ * Workload later into quota which nothing uses. An admitted Workload is kept, since the
+ * resources it was admitted for may be running already, and it is released with them like the
+ * Workload of a queued resource. Like the admission request, a failed release is retried before
+ * the resources are requested.
+ *
+ * @param context The context of the resource without a queue name label.
+ * @return The progress to return while the release fails, or empty to proceed.
+ */
+ public static Optional<ReconcileProgress> releaseDequeuedWorkload(
+ final BaseContext<?> context) {
+ Optional<Workload> workload = context.getCachedKueueWorkload();
+ if (workload.isEmpty() || isAdmitted(workload.get())) {
+ return Optional.empty();
+ }
+ log.info(
+ "Deleting the pending Kueue Workload {} whose owner was removed from the queue.",
+ workload.get().getMetadata().getName());
+ return releaseOrRetry(
+ context, "Failed to release the Kueue Workload of a resource removed from its queue");
+ }
+
+ /**
+ * Deletes the Workload of the resource of the given context before its resources are requested
+ * or it fails, and reports the progress to retry a failed deletion with, like a failed admission
+ * request.
+ *
+ * @param context The context of the resource owning the Workload.
+ * @param what What failed, as named in the event and the log.
+ * @return The progress to return while the deletion fails, or empty once it succeeded.
+ */
+ private static Optional<ReconcileProgress> releaseOrRetry(
+ final BaseContext<?> context, final String what) {
+ try {
+ deleteWorkloadOf(context.getClient(), context.getResource());
+ } catch (KubernetesClientException e) {
+ return Optional.of(retryAfterRequestFailure(context, e, what));
+ }
+ return Optional.empty();
+ }
+
/**
* Reports that the Kueue queue name label of the resource of the given context is ignored, since
* the Kueue integration is disabled. The label is set by the author of the resource, who may not
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStep.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStep.java
index ad2be904..d7b4cc99 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStep.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStep.java
@@ -177,7 +177,9 @@ public final class AppInitStep extends AppReconcileStep {
* assigned to it are applied again then.
* An unsupported spec fails to build the Workload, which the caller turns into SchedulingFailure.
* Unlike the driver resources, an API failure of the admission request is retried, see {@link
- * KueueWorkloadUtils#holdForAdmission}.
+ * KueueWorkloadUtils#holdForAdmission}. An application without the label releases the Workload
+ * left pending from before the label was removed instead, see {@link
+ * KueueWorkloadUtils#releaseDequeuedWorkload}.
*
* @param context The SparkAppContext for the application.
* @param app The SparkApplication.
@@ -186,7 +188,7 @@ public final class AppInitStep extends AppReconcileStep {
private Optional<ReconcileProgress> holdForKueueAdmission(
SparkAppContext context, SparkApplication app) {
if (!KueueWorkloadFactory.hasQueueName(app)) {
- return Optional.empty();
+ return KueueWorkloadUtils.releaseDequeuedWorkload(context);
}
if (!KUEUE_ENABLED.getValue()) {
KueueWorkloadUtils.warnQueueNameIgnored(context);
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStep.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStep.java
index cb50dbe8..1c632193 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStep.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStep.java
@@ -214,7 +214,9 @@ public final class ClusterInitStep extends ClusterReconcileStep {
* assigned to it are applied again then. An
* unsupported spec fails to build the Workload, which the caller turns into SchedulingFailure.
* SchedulingFailure is terminal for a cluster, so an API failure of the admission request is
- * retried instead, see {@link KueueWorkloadUtils#holdForAdmission}.
+ * retried instead, see {@link KueueWorkloadUtils#holdForAdmission}. A cluster without the label
+ * releases the Workload left pending from before the label was removed instead, see {@link
+ * KueueWorkloadUtils#releaseDequeuedWorkload}.
*
* @param context The SparkClusterContext for the cluster.
* @param cluster The SparkCluster.
@@ -223,7 +225,7 @@ public final class ClusterInitStep extends ClusterReconcileStep {
private Optional<ReconcileProgress> holdForKueueAdmission(
SparkClusterContext context, SparkCluster cluster) {
if (!KueueWorkloadFactory.hasQueueName(cluster)) {
- return Optional.empty();
+ return KueueWorkloadUtils.releaseDequeuedWorkload(context);
}
if (!KUEUE_ENABLED.getValue()) {
KueueWorkloadUtils.warnQueueNameIgnored(context);
diff --git a/spark-operator/src/test/java/org/apache/spark/k8s/operator/context/SparkClusterContextTest.java b/spark-operator/src/test/java/org/apache/spark/k8s/operator/context/SparkClusterContextTest.java
index 91938cb0..ebbde062 100644
--- a/spark-operator/src/test/java/org/apache/spark/k8s/operator/context/SparkClusterContextTest.java
+++ b/spark-operator/src/test/java/org/apache/spark/k8s/operator/context/SparkClusterContextTest.java
@@ -20,9 +20,12 @@
package org.apache.spark.k8s.operator.context;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
@@ -36,13 +39,16 @@ import org.junit.jupiter.api.Test;
import org.apache.spark.k8s.operator.Constants;
import org.apache.spark.k8s.operator.SparkCluster;
import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.config.SparkOperatorConf;
import org.apache.spark.k8s.operator.kueue.KueuePodSetFlavor;
import org.apache.spark.k8s.operator.kueue.KueueWorkloadFactory;
+import org.apache.spark.k8s.operator.kueue.v1beta2.Workload;
import org.apache.spark.k8s.operator.spec.ClusterSpec;
import org.apache.spark.k8s.operator.spec.ClusterTolerations;
import org.apache.spark.k8s.operator.spec.RuntimeVersions;
import org.apache.spark.k8s.operator.spec.WorkerInstanceConfig;
import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.TestUtils;
class SparkClusterContextTest {
@@ -77,6 +83,28 @@ class SparkClusterContextTest {
Assertions.assertEquals(clusterSpec, asJson(cluster.getSpec()));
}
+ @Test
+ @SuppressWarnings("unchecked")
+ void cachedKueueWorkloadIsReadOnlyWithKueueIntegration() {
+ SparkCluster cluster = buildCluster();
+ Context<SparkCluster> josdkContext = mock(Context.class);
+ Workload workload = KueueWorkloadFactory.buildWorkload(cluster);
+ when(josdkContext.getSecondaryResource(Workload.class)).thenReturn(Optional.of(workload));
+ SparkClusterContext context =
+ new SparkClusterContext(cluster, josdkContext, new SparkClusterSubmissionWorker());
+
+ // Without the integration, no Workload informer is registered to read from
+ Assertions.assertEquals(Optional.empty(), context.getCachedKueueWorkload());
+ verifyNoInteractions(josdkContext);
+
+ TestUtils.setConfigKey(SparkOperatorConf.KUEUE_ENABLED, true);
+ try {
+ Assertions.assertEquals(Optional.of(workload), context.getCachedKueueWorkload());
+ } finally {
+ TestUtils.setConfigKey(SparkOperatorConf.KUEUE_ENABLED, false);
+ }
+ }
+
private static String asJson(Object value) {
try {
return ModelUtils.objectMapper.writeValueAsString(value);
diff --git a/spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtilsTest.java b/spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtilsTest.java
index b9a4e3f3..54a077d3 100644
--- a/spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtilsTest.java
+++ b/spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadUtilsTest.java
@@ -165,6 +165,68 @@ class KueueWorkloadUtilsTest {
Assertions.assertEquals(5, getWorkload().getSpec().getPodSets().get(0).getCount());
}
+ @Test
+ void admittedWorkloadIsKeptEvenIfQueueChanged() {
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, workload("owner-uid-1", 1));
+ admitWorkload();
+
+ Workload moved = workload("owner-uid-1", 1);
+ moved.getSpec().setQueueName("another-queue");
+ Assertions.assertEquals(
+ AdmissionResult.ADMITTED,
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, moved).result());
+ Assertions.assertEquals("test-queue", getWorkload().getSpec().getQueueName());
+ }
+
+ @Test
+ void pendingWorkloadOfDequeuedResourceIsReleased() {
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, workload("owner-uid-1", 1));
+ SparkAppContext context = context(kubernetesClient);
+ when(context.getCachedKueueWorkload()).thenReturn(Optional.of(getWorkload()));
+
+ // The queue label was removed while the Workload waited, so the resource starts without Kueue
+ // and the Workload would be admitted later into quota which nothing uses
+ Assertions.assertEquals(Optional.empty(), KueueWorkloadUtils.releaseDequeuedWorkload(context));
+ Assertions.assertNull(getWorkload());
+ }
+
+ @Test
+ void admittedWorkloadOfDequeuedResourceIsKept() {
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, workload("owner-uid-1", 1));
+ admitWorkload();
+ SparkAppContext context = context(kubernetesClient);
+ when(context.getCachedKueueWorkload()).thenReturn(Optional.of(getWorkload()));
+
+ // The resources it was admitted for may be running already
+ Assertions.assertEquals(Optional.empty(), KueueWorkloadUtils.releaseDequeuedWorkload(context));
+ Assertions.assertNotNull(getWorkload());
+ }
+
+ @Test
+ void resourceWithoutCachedWorkloadIsNotReleased() {
+ KubernetesClient client = mock(KubernetesClient.class);
+ SparkAppContext context = context(client);
+
+ // A resource which was never queued costs no request
+ Assertions.assertEquals(Optional.empty(), KueueWorkloadUtils.releaseDequeuedWorkload(context));
+ verifyNoInteractions(client);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ void failedReleaseOfDequeuedWorkloadIsRetried() {
+ Resource<Workload> workloadResource = mock(Resource.class);
+ when(workloadResource.delete())
+ .thenThrow(new KubernetesClientException("forbidden", 403, null));
+ SparkAppContext context = context(clientReturning(workloadResource));
+ when(context.getCachedKueueWorkload()).thenReturn(Optional.of(workload("owner-uid-1", 1)));
+
+ // The resources are not requested while the Workload may still be admitted for nothing
+ Assertions.assertEquals(
+ Optional.of(ReconcileProgress.completeAndDefaultRequeue()),
+ KueueWorkloadUtils.releaseDequeuedWorkload(context));
+ }
+
@Test
void workloadOwnedByAnotherResourceIsDeleted() {
KueueWorkloadUtils.requestAdmission(kubernetesClient, workload("stale-owner-uid", 1));
@@ -843,6 +905,30 @@ class KueueWorkloadUtilsTest {
Assertions.assertEquals(1000, updated.getSpec().getPriority());
}
+ @Test
+ void pendingWorkloadIsMovedToAnotherQueueInPlace() {
+ createWorkloadPriorityClass("low", 10);
+ createWorkloadPriorityClass("high", 1000);
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, workloadWithPriorityClass("low"));
+ String uid = getWorkload().getMetadata().getUid();
+
+ // The queue label changed while waiting for the admission, e.g. along with a suspension which
+ // was cleared before the operator released the Workload. Like Kueue, the Workload is updated
+ // rather than recreated, since it holds no quota yet, along with its priority class.
+ Workload moved = workloadWithPriorityClass("high");
+ moved.getSpec().setQueueName("another-queue");
+ Assertions.assertEquals(
+ AdmissionResult.PENDING,
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, moved).result());
+ Workload updated = getWorkload();
+ Assertions.assertEquals(uid, updated.getMetadata().getUid());
+ Assertions.assertEquals("another-queue", updated.getSpec().getQueueName());
+ Assertions.assertEquals(
+ "another-queue", updated.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME));
+ Assertions.assertEquals("high", updated.getSpec().getPriorityClassRef().getName());
+ Assertions.assertEquals(1000, updated.getSpec().getPriority());
+ }
+
@Test
void pendingWorkloadKeepsPriorityOfTheSameClass() {
createWorkloadPriorityClass("low", 10);
@@ -878,6 +964,25 @@ class KueueWorkloadUtilsTest {
Assertions.assertEquals(10, getWorkload().getSpec().getPriority());
}
+ @Test
+ void quotaReservedWorkloadKeepsItsQueue() {
+ createWorkloadPriorityClass("low", 10);
+ createWorkloadPriorityClass("high", 1000);
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, workloadWithPriorityClass("low"));
+ reserveQuota();
+
+ // Kueue freezes the queue name once the quota is reserved, while the name of a
+ // WorkloadPriorityClass stays mutable, so only the priority follows in the same update
+ Workload moved = workloadWithPriorityClass("high");
+ moved.getSpec().setQueueName("another-queue");
+ Assertions.assertEquals(
+ AdmissionResult.PENDING,
+ KueueWorkloadUtils.requestAdmission(kubernetesClient, moved).result());
+ Assertions.assertEquals("test-queue", getWorkload().getSpec().getQueueName());
+ Assertions.assertEquals("high", getWorkload().getSpec().getPriorityClassRef().getName());
+ Assertions.assertEquals(1000, getWorkload().getSpec().getPriority());
+ }
+
@Test
void quotaReservedWorkloadFollowsWorkloadPriorityClassChange() {
createWorkloadPriorityClass("low", 10);
diff --git a/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStepTest.java b/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStepTest.java
index 8f284798..2e578833 100644
--- a/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStepTest.java
+++ b/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStepTest.java
@@ -908,6 +908,82 @@ class AppInitStepTest {
events.get(1).message());
}
+ @Test
+ void suspendingQueuedAppReleasesKueueWorkloadEvenIfQueueLabelIsRemoved() {
+ AppInitStep appInitStep = new AppInitStep();
+ SparkAppContext mockContext = mock(SparkAppContext.class);
+ SparkAppStatusRecorder recorder = mock(SparkAppStatusRecorder.class);
+ SparkApplication application = new SparkApplication();
+ application.setMetadata(kueueApplicationMetadata);
+ when(mockContext.getResource()).thenReturn(application);
+ when(mockContext.getClient()).thenReturn(kubernetesClient);
+ when(mockContext.getEventRecorder()).thenReturn(eventRecorder);
+
+ // Queued: the Workload waits for the admission
+ Assertions.assertEquals(
+ ReconcileProgress.completeAndDefaultRequeue(),
+ appInitStep.reconcile(mockContext, recorder));
+ Assertions.assertNotNull(getWorkload());
+
+ // Suspended and removed from the queue in one update: the Workload is deleted all the same,
+ // since Kueue would admit it later and hold the quota for an app which never uses it
+ application.getSpec().setSuspend(true);
+ application.getMetadata().setLabels(Map.of());
+ Assertions.assertEquals(
+ SUSPEND_HOLD_PROGRESS,
+ appInitStep.reconcile(mockContext, recorder));
+ Assertions.assertNull(getWorkload());
+ List<EventRecord> events = captureEvents(2);
+ Assertions.assertEquals(EventUtils.REASON_SUSPEND_HELD, events.get(1).reason());
+ Assertions.assertTrue(
+ events
+ .get(1)
+ .message()
+ .endsWith(
+ "It holds no Kueue Workload while suspended, so an earlier "
+ + EventUtils.REASON_KUEUE_ADMISSION_PENDING
+ + " event no longer applies."),
+ events.get(1).message());
+ }
+
+ @Test
+ void queueLabelRemovedWhileQueuedReleasesPendingKueueWorkload() {
+ AppInitStep appInitStep = new AppInitStep();
+ SparkAppContext mockContext = mock(SparkAppContext.class);
+ SparkAppStatusRecorder recorder = mock(SparkAppStatusRecorder.class);
+ SparkApplication application = new SparkApplication();
+ application.setMetadata(kueueApplicationMetadata);
+ when(mockContext.getResource()).thenReturn(application);
+ when(mockContext.getDriverPreResourcesSpec()).thenReturn(List.of());
+ when(mockContext.getDriverPodSpec()).thenReturn(driverPodSpec);
+ when(mockContext.getDriverResourcesSpec()).thenReturn(List.of());
+ when(mockContext.getClient()).thenReturn(kubernetesClient);
+ when(mockContext.getEventRecorder()).thenReturn(eventRecorder);
+ when(recorder.persistStatus(any(), any()))
+ .thenAnswer(
+ invocation -> {
+ application.setStatus(invocation.getArgument(1));
+ return true;
+ });
+
+ // Queued: the Workload waits for the admission
+ Assertions.assertEquals(
+ ReconcileProgress.completeAndDefaultRequeue(),
+ appInitStep.reconcile(mockContext, recorder));
+ Assertions.assertNotNull(getWorkload());
+
+ // Removed from the queue while waiting: the driver is requested without Kueue, and the pending
+ // Workload is released, which Kueue would otherwise admit later into quota which nothing uses
+ application.getMetadata().setLabels(Map.of());
+ when(mockContext.getCachedKueueWorkload()).thenReturn(Optional.of(getWorkload()));
+ Assertions.assertEquals(
+ ReconcileProgress.completeAndDefaultRequeue(),
+ appInitStep.reconcile(mockContext, recorder));
+ Assertions.assertNull(getWorkload());
+ Assertions.assertNotNull(
+ kubernetesClient.pods().inNamespace("default").withName("driver-pod").get());
+ }
+
@Test
void queueLabelIsIgnoredWhenKueueIsDisabled() {
TestUtils.setConfigKey(SparkOperatorConf.KUEUE_ENABLED, false);
diff --git a/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStepTest.java b/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStepTest.java
index ce40ca3d..23eba11e 100644
--- a/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStepTest.java
+++ b/spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/ClusterInitStepTest.java
@@ -610,6 +610,35 @@ class ClusterInitStepTest {
captor.getValue().getCurrentState().getCurrentStateSummary());
}
+ @Test
+ void failedReleaseOfPendingKueueWorkloadHoldsDequeuedCluster() {
+ ClusterInitStep clusterInitStep = new ClusterInitStep();
+ SparkClusterContext mockContext = mock(SparkClusterContext.class);
+ SparkClusterStatusRecorder recorder = mock(SparkClusterStatusRecorder.class);
+ // Queued before, and removed from the queue while its Workload waited for the admission
+ SparkCluster cluster = buildCluster();
+ KubernetesClient mockClient = mock(KubernetesClient.class, RETURNS_DEEP_STUBS);
+ Resource<Workload> workload = stubWorkloadRead(mockClient, null);
+ when(workload.delete()).thenThrow(new KubernetesClientException("forbidden", 403, null));
+ when(mockClient.resource(masterStatefulSetSpec).get()).thenReturn(null);
+ when(mockContext.getResource()).thenReturn(cluster);
+ when(mockContext.getClient()).thenReturn(mockClient);
+ when(mockContext.getMasterStatefulSetSpec()).thenReturn(masterStatefulSetSpec);
+ when(mockContext.getEventRecorder()).thenReturn(eventRecorder);
+ when(mockContext.getCachedKueueWorkload())
+ .thenReturn(Optional.of(KueueWorkloadFactory.buildWorkload(buildKueueCluster())));
+
+ // The master is not requested while the pending Workload may still be admitted for nothing
+ Assertions.assertEquals(
+ ReconcileProgress.completeAndDefaultRequeue(),
+ clusterInitStep.reconcile(mockContext, recorder));
+ verify(workload).delete();
+ verify(mockContext, never()).getMasterServiceSpec();
+ verifyNoInteractions(recorder);
+ EventRecord event = captureEvents(1).get(0);
+ Assertions.assertEquals(EventUtils.REASON_KUEUE_ADMISSION_REQUEST_FAILED, event.reason());
+ }
+
@Test
@SuppressWarnings("unchecked")
void queueLabelIsIgnoredWhenKueueIsDisabled() {
@@ -1105,7 +1134,7 @@ class ClusterInitStepTest {
}
@SuppressWarnings("unchecked")
- private static void stubWorkloadRead(KubernetesClient client, Workload workload) {
+ private static Resource<Workload> stubWorkloadRead(KubernetesClient client, Workload workload) {
MixedOperation<Workload, KubernetesResourceList<Workload>, Resource<Workload>> workloads =
mock(MixedOperation.class);
NonNamespaceOperation<Workload, KubernetesResourceList<Workload>, Resource<Workload>>
@@ -1115,6 +1144,7 @@ class ClusterInitStepTest {
when(workloads.inNamespace("default")).thenReturn(namespaced);
when(namespaced.withName("sparkcluster-cluster1")).thenReturn(resource);
when(resource.get()).thenReturn(workload);
+ return resource;
}
private static WorkloadStatus admittedStatus(Map<String, Map<String, String>> podSetFlavors) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org