You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "dongjoon-hyun (via GitHub)" <gi...@apache.org> on 2026/09/14 08:20:29 UTC

[PR] [SPARK-59490] Add `KueueWorkloadFactory` for SparkApplication and SparkCluster [spark-kubernetes-operator]

dongjoon-hyun opened a new pull request, #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823

   ### What changes were proposed in this pull request?
   
   This PR introduces `KueueWorkloadFactory` and unit tests in the `spark-operator` module to construct Kueue `Workload` (`kueue.x-k8s.io/v1beta1`) resources from Spark custom resources:
   
   | Custom Resource | PodSet Name | Count (`count`) | Min Count (`minCount`) | Resource Calculation |
   | :--- | :--- | :--- | :--- | :--- |
   | **`SparkApplication`** | `driver` | 1 | - | CPU, Memory (+ overheads, off-heap, PySpark), GPU |
   | **`SparkApplication`** | `executor` | Instances / Max | Min (Dynamic Allocation) | CPU, Memory (+ overheads), GPU |
   | **`SparkCluster`** | `master` | StatefulSet Replicas | - | Master StatefulSet PodTemplate |
   | **`SparkCluster`** | `worker` | StatefulSet Replicas / HPA Max | HPA Min (if configured) | Worker StatefulSet PodTemplate |
   
   - **Common Features**: Links OwnerReference, sets `queueName` from label, and syncs `active = !spec.isSuspend()`.
   - **Unit Tests** (`KueueWorkloadFactoryTest`): Verifies resource calculations, dynamic allocation/HPA bounds, and suspend status for both resources.
   
   ### Why are the changes needed?
   
   Following the introduction of Kueue v1beta1 CRD models in [SPARK-59486](https://github.com/apache/spark-kubernetes-operator/pull/822), this PR adds the resource calculation and workload construction logic for both `SparkApplication` and `SparkCluster` as a modular, reviewable step.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   Pass the CIs.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Antigravity (Gemini 3.8 Flash)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4007740479


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,431 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));

Review Comment:
   Thank you. Fixed in a3badb2. When `spark.kubernetes.driver.master` starts with `local`, only the `driver` PodSet is built. `testBuildWorkloadDriverOnly` uses the `sparkConf` of `examples/pi-with-one-pod.yaml` and asserts a single `driver` PodSet.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,431 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)
+            .build());
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_DRIVER,
+        1,
+        spec.getDriverSpec(),
+        sparkConf,
+        Constants.DRIVER_SPARK_CONTAINER_PROP_KEY,
+        calculateDriverMemoryMiB(sparkConf, isNonJvmApp(spec)));
+  }
+
+  static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_EXECUTOR,
+        parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES),
+        spec.getExecutorSpec(),
+        sparkConf,
+        Constants.EXECUTOR_SPARK_CONTAINER_PROP_KEY,
+        calculateExecutorMemoryMiB(sparkConf, isNonJvmApp(spec), isPythonApp(spec)));
+  }
+
+  /**
+   * Builds a PodSet for a Spark role (`driver` or `executor`) from the pod template of the
+   * SparkApplication and the resource configurations in the same way as Spark does.
+   */
+  private static PodSet buildPodSet(
+      final String role,
+      final int count,
+      final BaseApplicationTemplateSpec roleSpec,
+      final Map<String, String> sparkConf,
+      final String containerNameKey,
+      final long memoryMiB) {
+    PodTemplateSpec templateSpec =
+        roleSpec != null && roleSpec.getPodTemplateSpec() != null
+            ? ReconcilerUtils.clone(roleSpec.getPodTemplateSpec())
+            : new PodTemplateSpecBuilder().build();
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    PodSpec podSpec = templateSpec.getSpec();
+    if (podSpec.getContainers() == null) {
+      podSpec.setContainers(new ArrayList<>());
+    }
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes." + role + ".request.cores",
+            sparkConf.getOrDefault("spark." + role + ".cores", DEFAULT_CORES));
+    Container container =
+        selectContainer(podSpec, sparkConf.get(containerNameKey), "spark-kubernetes-" + role);
+    decorateContainerResources(
+        container, sparkConf, "spark." + role + ".resource.", cpu, memoryMiB);
+    decorateNodeSelector(podSpec, sparkConf, "spark.kubernetes." + role + ".node.selector.");
+
+    return PodSet.builder().name(role).count(count).template(templateSpec).build();
+  }
+
+  /** Calculates total driver memory in MiB including overhead. */
+  static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      long offHeapMiB =
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+      if (offHeapMiB <= 0) {
+        throw new IllegalArgumentException(
+            "spark.memory.offHeap.size must be at least 1MiB when "
+                + "spark.memory.offHeap.enabled == true");
+      }
+      total += offHeapMiB;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  /**
+   * Like Spark's KubernetesUtils.selectSparkContainer, selects the container by name and falls
+   * back to the first container. A new container is added if the template has no container.
+   */
+  private static Container selectContainer(
+      final PodSpec podSpec, final String containerName, final String defaultContainerName) {
+    List<Container> containers = podSpec.getContainers();
+    if (containers.isEmpty()) {
+      Container container = new ContainerBuilder().withName(defaultContainerName).build();
+      containers.add(container);
+      return container;
+    }
+    if (containerName != null) {
+      for (Container container : containers) {
+        if (containerName.equals(container.getName())) {
+          return container;
+        }
+      }
+      log.warn(
+          "Specified container {} not found on pod template, falling back to taking the first "
+              + "container",
+          containerName);
+    }
+    return containers.get(0);
+  }
+
+  private static void decorateContainerResources(
+      final Container container,
+      final Map<String, String> sparkConf,
+      final String resourcePrefix,
+      final String cpu,
+      final long memoryMiB) {
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    // Like Spark's KubernetesConf.buildKubernetesResourceName,
+    // `spark.<role>.resource.<name>.amount` and `.vendor` become `<vendor>/<name>`,
+    // e.g., `nvidia.com/gpu`.
+    for (Map.Entry<String, String> e : sparkConf.entrySet()) {
+      if (!e.getKey().startsWith(resourcePrefix) || !e.getKey().endsWith(".amount")) {
+        continue;
+      }
+      String name =
+          e.getKey().substring(resourcePrefix.length(), e.getKey().length() - ".amount".length());
+      String vendor = sparkConf.get(resourcePrefix + name + ".vendor");

Review Comment:
   Fixed. A non-positive amount is skipped before the vendor lookup like Spark's `ResourceUtils`, while a malformed amount still throws via `parseInt`. `testBuildWorkloadWithZeroResourceAmount` covers the zero amount with and without a vendor.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5668702729

   Thank you, @peter-toth. Finding 23 is addressed in f83e218, and the PR description lists `podTemplateFile` under Unsupported Features.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4007670154


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,431 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));

Review Comment:
   **Finding 21.** A driver-only application still gets an `executor` PodSet.
   
   `spark.kubernetes.driver.master` is documented as the API server address "to be used for driver to request executors or `local[*]` for driver-only mode" (`Config.scala:50-57`). When it starts with `local`, `KubernetesClusterManager.createSchedulerBackend` returns a `LocalSchedulerBackend` and returns early (`KubernetesClusterManager.scala:40-72`). No executor pod is ever created, and `spark.executor.instances` is ignored.
   
   Measured on `d7cb986` with the `sparkConf` of `examples/pi-with-one-pod.yaml` verbatim:
   
   ```
   podSet=driver   count=1  requests={cpu=5, memory=1408Mi}
   podSet=executor count=2  requests={cpu=1, memory=1408Mi}
   ```
   
   So Kueue reserves 2 cpu and 2816Mi for executors that never exist, for as long as the app runs. `examples/word-count-celeborn.yaml` and `tests/benchmark/sparkapps.sh` set the same conf.
   
   This is the case dynamic allocation is rejected for, except here the count is knowable: it is zero. Next to the guard at line 93:
   
   ```java
   boolean driverOnly =
       sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
   List<PodSet> podSets =
       driverOnly
           ? List.of(buildDriverPodSet(app, sparkConf))
           : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
   ```
   
   A test over `pi-with-one-pod`'s conf asserting a single PodSet named `driver` would pin it.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,431 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)
+            .build());
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_DRIVER,
+        1,
+        spec.getDriverSpec(),
+        sparkConf,
+        Constants.DRIVER_SPARK_CONTAINER_PROP_KEY,
+        calculateDriverMemoryMiB(sparkConf, isNonJvmApp(spec)));
+  }
+
+  static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_EXECUTOR,
+        parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES),
+        spec.getExecutorSpec(),
+        sparkConf,
+        Constants.EXECUTOR_SPARK_CONTAINER_PROP_KEY,
+        calculateExecutorMemoryMiB(sparkConf, isNonJvmApp(spec), isPythonApp(spec)));
+  }
+
+  /**
+   * Builds a PodSet for a Spark role (`driver` or `executor`) from the pod template of the
+   * SparkApplication and the resource configurations in the same way as Spark does.
+   */
+  private static PodSet buildPodSet(
+      final String role,
+      final int count,
+      final BaseApplicationTemplateSpec roleSpec,
+      final Map<String, String> sparkConf,
+      final String containerNameKey,
+      final long memoryMiB) {
+    PodTemplateSpec templateSpec =
+        roleSpec != null && roleSpec.getPodTemplateSpec() != null
+            ? ReconcilerUtils.clone(roleSpec.getPodTemplateSpec())
+            : new PodTemplateSpecBuilder().build();
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    PodSpec podSpec = templateSpec.getSpec();
+    if (podSpec.getContainers() == null) {
+      podSpec.setContainers(new ArrayList<>());
+    }
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes." + role + ".request.cores",
+            sparkConf.getOrDefault("spark." + role + ".cores", DEFAULT_CORES));
+    Container container =
+        selectContainer(podSpec, sparkConf.get(containerNameKey), "spark-kubernetes-" + role);
+    decorateContainerResources(
+        container, sparkConf, "spark." + role + ".resource.", cpu, memoryMiB);
+    decorateNodeSelector(podSpec, sparkConf, "spark.kubernetes." + role + ".node.selector.");
+
+    return PodSet.builder().name(role).count(count).template(templateSpec).build();
+  }
+
+  /** Calculates total driver memory in MiB including overhead. */
+  static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      long offHeapMiB =
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+      if (offHeapMiB <= 0) {
+        throw new IllegalArgumentException(
+            "spark.memory.offHeap.size must be at least 1MiB when "
+                + "spark.memory.offHeap.enabled == true");
+      }
+      total += offHeapMiB;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  /**
+   * Like Spark's KubernetesUtils.selectSparkContainer, selects the container by name and falls
+   * back to the first container. A new container is added if the template has no container.
+   */
+  private static Container selectContainer(
+      final PodSpec podSpec, final String containerName, final String defaultContainerName) {
+    List<Container> containers = podSpec.getContainers();
+    if (containers.isEmpty()) {
+      Container container = new ContainerBuilder().withName(defaultContainerName).build();
+      containers.add(container);
+      return container;
+    }
+    if (containerName != null) {
+      for (Container container : containers) {
+        if (containerName.equals(container.getName())) {
+          return container;
+        }
+      }
+      log.warn(
+          "Specified container {} not found on pod template, falling back to taking the first "
+              + "container",
+          containerName);
+    }
+    return containers.get(0);
+  }
+
+  private static void decorateContainerResources(
+      final Container container,
+      final Map<String, String> sparkConf,
+      final String resourcePrefix,
+      final String cpu,
+      final long memoryMiB) {
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    // Like Spark's KubernetesConf.buildKubernetesResourceName,
+    // `spark.<role>.resource.<name>.amount` and `.vendor` become `<vendor>/<name>`,
+    // e.g., `nvidia.com/gpu`.
+    for (Map.Entry<String, String> e : sparkConf.entrySet()) {
+      if (!e.getKey().startsWith(resourcePrefix) || !e.getKey().endsWith(".amount")) {
+        continue;
+      }
+      String name =
+          e.getKey().substring(resourcePrefix.length(), e.getKey().length() - ".amount".length());
+      String vendor = sparkConf.get(resourcePrefix + name + ".vendor");

Review Comment:
   **Finding 22.** A zero amount is a request here, but not in Spark.
   
   `ResourceUtils.parseAllResourceRequests` ends with `.filter(_.amount > 0)` (`ResourceUtils.scala:163-169`), and `KubernetesUtils.buildResourcesQuantities` only checks the vendor on what survives that filter (`KubernetesUtils.scala:254-268`). So an amount of `0` produces no entry at all on the real pod, with or without a vendor.
   
   Measured on `d7cb986`:
   
   ```
   spark.executor.resource.gpu.amount=0, spark.executor.resource.gpu.vendor=nvidia.com
     -> executor requests={cpu=1, memory=1408Mi, nvidia.com/gpu=0}, limits={nvidia.com/gpu=0}
   ```
   
   The pod asks for nothing while the Workload names the resource. Kueue then needs the ClusterQueue to cover `nvidia.com/gpu` for the workload to be admissible. I did not run Kueue to confirm that last step; the divergence from Spark is what I measured.
   
   Skipping before the vendor lookup matches Spark's order, and `parseInt` keeps throwing on a malformed amount the way Spark's `.toInt` does:
   
   ```java
   if (parseInt(e.getValue(), 0) <= 0) {
     continue;
   }
   String vendor = sparkConf.get(resourcePrefix + name + ".vendor");
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5667924393

   Thank you, @peter-toth. Findings 21 and 22 are addressed in a3badb2, and the PR description is updated for the driver-only mode and the positive-amount filter.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4008566905


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    checkNoPodTemplateFile(
+        sparkConf, Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getDriverSpec());
+    if (!driverOnly) {
+      checkNoPodTemplateFile(
+          sparkConf, Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getExecutorSpec());
+    }
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  /**
+   * A pod template file is fetched by Spark, not by the operator, so its node selectors,
+   * tolerations and extra containers cannot be reflected in the PodSet template. The file is
+   * ignored by Spark if the pod template is set in the SparkApplication spec.
+   */
+  private static void checkNoPodTemplateFile(
+      final Map<String, String> sparkConf,
+      final String templateFileKey,
+      final BaseApplicationTemplateSpec roleSpec) {
+    if (sparkConf.containsKey(templateFileKey)
+        && (roleSpec == null || roleSpec.getPodTemplateSpec() == null)) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support "
+              + templateFileKey
+              + " yet. Set the pod template in the SparkApplication spec instead.");
+    }
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)

Review Comment:
   Thank you. I agree that the priority should be propagated, but I'd like to defer both fields together to the wiring PR rather than setting `priorityClassName` alone here. Kueue's Workload CRD has a CEL rule on `WorkloadSpec` (`apis/kueue/v1beta1/workload_types.go`):
   
   ```
   // +kubebuilder:validation:XValidation:rule="has(self.priorityClassName) ? has(self.priority) : true", message="priority should not be nil when priorityClassName is set"
   ```
   
   So a Workload with `priorityClassName` but no `priority` is rejected by the API server, and resolving `priority` needs a `PriorityClass` lookup with a `KubernetesClient`, which this factory doesn't take. I listed it under "Deferred Features" in the PR description.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    checkNoPodTemplateFile(
+        sparkConf, Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getDriverSpec());
+    if (!driverOnly) {
+      checkNoPodTemplateFile(
+          sparkConf, Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getExecutorSpec());
+    }
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  /**
+   * A pod template file is fetched by Spark, not by the operator, so its node selectors,
+   * tolerations and extra containers cannot be reflected in the PodSet template. The file is
+   * ignored by Spark if the pod template is set in the SparkApplication spec.
+   */
+  private static void checkNoPodTemplateFile(
+      final Map<String, String> sparkConf,
+      final String templateFileKey,
+      final BaseApplicationTemplateSpec roleSpec) {
+    if (sparkConf.containsKey(templateFileKey)
+        && (roleSpec == null || roleSpec.getPodTemplateSpec() == null)) {

Review Comment:
   Fixed in ab7cfb8. `checkNoPodTemplateFile` takes the result of `ModelUtils.overrideDriverTemplateEnabled` / `overrideExecutorTemplateEnabled` now, and the Javadoc is corrected as you suggested.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4007507900


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(

Review Comment:
   Thank you. Fixed in db33ec0. `decorateNodeSelector` applies `spark.kubernetes.node.selector.*` first and `spark.kubernetes.{driver,executor}.node.selector.*` last, like Spark, while keeping the selectors of the pod template. `testBuildWorkloadWithNodeSelector` covers the precedence.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),
+        "spark-executor",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES))
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  public static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      total +=
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String containerName,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      // Like Spark's KubernetesUtils.selectSparkContainer, select the container by name and
+      // fall back to the first container.
+      container =
+          podSpec.getContainers().stream()
+              .filter(c -> containerName != null && containerName.equals(c.getName()))
+              .findFirst()
+              .orElse(podSpec.getContainers().get(0));
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (StringUtils.isEmpty(gpuVendor)) {
+        throw new IllegalArgumentException(
+            "Resource: gpu was requested, but vendor was not specified.");
+      }
+      // Like Spark's KubernetesConf.buildKubernetesResourceName, e.g., `nvidia.com/gpu`.
+      String gpuResourceName = gpuVendor + "/gpu";

Review Comment:
   Fixed. Every `spark.{driver,executor}.resource.<name>.amount` is converted into `<vendor>/<name>` requests and limits, and the `gpu`-specific reads in the callers are gone. The test now covers `xilinx.com/fpga` together with `nvidia.com/gpu`.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),
+        "spark-executor",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES))
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  public static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      total +=
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String containerName,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      // Like Spark's KubernetesUtils.selectSparkContainer, select the container by name and
+      // fall back to the first container.
+      container =
+          podSpec.getContainers().stream()
+              .filter(c -> containerName != null && containerName.equals(c.getName()))
+              .findFirst()
+              .orElse(podSpec.getContainers().get(0));
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (StringUtils.isEmpty(gpuVendor)) {
+        throw new IllegalArgumentException(
+            "Resource: gpu was requested, but vendor was not specified.");
+      }
+      // Like Spark's KubernetesConf.buildKubernetesResourceName, e.g., `nvidia.com/gpu`.
+      String gpuResourceName = gpuVendor + "/gpu";
+      requests.put(gpuResourceName, new Quantity(gpuAmount));
+      Map<String, Quantity> limits = resources.getLimits();
+      if (limits == null) {
+        limits = new HashMap<>();
+        resources.setLimits(limits);
+      }
+      limits.put(gpuResourceName, new Quantity(gpuAmount));
+    }
+  }
+
+  private static int parseInt(final String str, final int defaultValue) {

Review Comment:
   Fixed. `parseInt` / `parseDouble` let `NumberFormatException` propagate now, and only an empty value falls back to the default. `testBuildWorkloadWithMalformedNumbers` covers both `spark.executor.instances` and `spark.driver.memoryOverheadFactor`. In the same spirit, `spark.memory.offHeap.enabled=true` without a positive `spark.memory.offHeap.size` is rejected like Spark's `Utils.checkOffHeapEnabled`.
   



##########
spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactoryTest.java:
##########
@@ -0,0 +1,497 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpecBuilder;
+import org.junit.jupiter.api.Test;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.ClusterTolerations;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.RuntimeVersions;
+import org.apache.spark.k8s.operator.spec.WorkerInstanceConfig;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+
+class KueueWorkloadFactoryTest {
+
+  @Test
+  void testCalculateDriverMemoryMiB() {
+    // 1g memory -> 1024MiB. Overhead factor 0.10 -> 102MiB -> min 384MiB.
+    // Total = 1024 + 384 = 1408
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.driver.memory", "1g");
+    assertEquals(1408L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Explicit overhead 512m -> 1024 + 512 = 1536
+    conf.put("spark.driver.memoryOverhead", "512m");
+    assertEquals(1536L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Custom overhead factor 0.50 -> 1024 * 0.50 = 512 -> 1024 + 512 = 1536
+    conf.remove("spark.driver.memoryOverhead");
+    conf.put("spark.driver.memoryOverheadFactor", "0.50");
+    assertEquals(1536L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Custom minimum overhead 1g -> 1024 + max(512, 1024) = 2048
+    conf.put("spark.driver.minMemoryOverhead", "1g");
+    assertEquals(2048L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+  }
+
+  @Test
+  void testCalculateDriverMemoryMiBForNonJvmApp() {
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.driver.memory", "8g");
+    // Non-JVM: 8192 + 0.40 * 8192 = 8192 + 3276 = 11468
+    assertEquals(11468L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, true));
+    // JVM: 8192 + 0.10 * 8192 = 8192 + 819 = 9011
+    assertEquals(9011L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // The deprecated `spark.kubernetes.memoryOverheadFactor` 0.20 -> 8192 + 1638 = 9830
+    conf.put("spark.kubernetes.memoryOverheadFactor", "0.20");
+    assertEquals(9830L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, true));
+    assertEquals(9830L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+  }
+
+  @Test
+  void testCalculateExecutorMemoryMiB() {
+    Map<String, String> conf = new HashMap<>();
+    // 2048MiB. Overhead: max(204, 384) = 384. Total = 2432
+    conf.put("spark.executor.memory", "2g");
+    assertEquals(2432L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // PySpark memory is added only for Python applications
+    conf.put("spark.executor.pyspark.memory", "1g");
+    assertEquals(2432L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+    // Non-JVM overhead: max(819, 384) = 819. Total = 2048 + 819 + 1024 = 3891
+    assertEquals(3891L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, true, true));
+  }
+
+  @Test
+  void testCalculateExecutorMemoryMiBWithOffHeap() {
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.executor.memory", "4g");
+    conf.put("spark.memory.offHeap.size", "4g");
+    // Off-heap memory is ignored if disabled. 4096 + 409 = 4505
+    assertEquals(4505L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // 4096 + 409 + 4096 = 8601
+    conf.put("spark.memory.offHeap.enabled", "true");
+    assertEquals(8601L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // `spark.memory.offHeap.size` is in bytes unless otherwise specified
+    conf.put("spark.memory.offHeap.size", "4294967296");
+    assertEquals(8601L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+  }
+
+  @Test
+  void testCalculateMemoryWithUnits() {
+    // 2048 + 384 = 2432
+    assertEquals(
+        2432L,
+        KueueWorkloadFactory.calculateExecutorMemoryMiB(
+            Map.of("spark.executor.memory", "2Gi"), false, false));
+    assertThrows(
+        NumberFormatException.class,
+        () ->
+            KueueWorkloadFactory.calculateDriverMemoryMiB(
+                Map.of("spark.driver.memory", "10zz"), false));
+    assertThrows(
+        NumberFormatException.class,
+        () ->
+            KueueWorkloadFactory.calculateExecutorMemoryMiB(
+                Map.of("spark.executor.memory", "1.5g"), false, false));
+  }
+
+  @Test
+  void testHasAndGetQueueName() {
+    SparkApplication app = new SparkApplication();
+    assertFalse(KueueWorkloadFactory.hasQueueName(app));
+    assertNull(KueueWorkloadFactory.getQueueName(app));
+
+    // From label
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "test-queue"))
+            .build());
+    assertTrue(KueueWorkloadFactory.hasQueueName(app));
+    assertEquals("test-queue", KueueWorkloadFactory.getQueueName(app));
+
+    // Empty label
+    SparkApplication app2 = new SparkApplication();
+    app2.setMetadata(
+        new ObjectMetaBuilder()
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, ""))
+            .build());
+    assertFalse(KueueWorkloadFactory.hasQueueName(app2));
+    assertNull(KueueWorkloadFactory.getQueueName(app2));
+  }
+
+  @Test
+  void testBuildWorkloadStaticAllocation() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-pi")
+            .withNamespace("spark-jobs")
+            .withUid("app-uid-123")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "team-a-queue"))
+            .build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setSparkConf(
+        Map.of(
+            "spark.driver.cores", "2",
+            "spark.driver.memory", "2g",
+            "spark.executor.cores", "4",
+            "spark.executor.memory", "4g",
+            "spark.executor.instances", "3"));
+    app.setSpec(spec);
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    assertNotNull(workload);
+    assertEquals("sparkapplication-spark-pi", workload.getMetadata().getName());
+    assertEquals("spark-jobs", workload.getMetadata().getNamespace());
+    assertEquals("team-a-queue", workload.getSpec().getQueueName());
+    assertTrue(workload.getSpec().getActive());
+    assertEquals(1, workload.getMetadata().getOwnerReferences().size());
+    assertEquals("spark-pi", workload.getMetadata().getOwnerReferences().get(0).getName());
+    assertTrue(workload.getMetadata().getOwnerReferences().get(0).getController());
+
+    assertEquals(2, workload.getSpec().getPodSets().size());
+
+    PodSet driverPodSet = workload.getSpec().getPodSets().get(0);
+    assertEquals("driver", driverPodSet.getName());
+    assertEquals(1, driverPodSet.getCount());
+    assertNull(driverPodSet.getMinCount());
+    Container driverContainer =
+        driverPodSet.getTemplate().getSpec().getContainers().get(0);
+    assertEquals(new Quantity("2"), driverContainer.getResources().getRequests().get("cpu"));
+    assertEquals(
+        new Quantity("2432Mi"),
+        driverContainer.getResources().getRequests().get("memory")); // 2048 + 384 = 2432
+
+    PodSet executorPodSet = workload.getSpec().getPodSets().get(1);
+    assertEquals("executor", executorPodSet.getName());
+    assertEquals(3, executorPodSet.getCount());
+    assertNull(executorPodSet.getMinCount());
+    Container executorContainer =
+        executorPodSet.getTemplate().getSpec().getContainers().get(0);
+    assertEquals(new Quantity("4"), executorContainer.getResources().getRequests().get("cpu"));
+    assertEquals(
+        new Quantity("4505Mi"),
+        executorContainer.getResources().getRequests().get("memory")); // 4096 + 409 = 4505
+  }
+
+  @Test
+  void testBuildWorkloadDefaultExecutorInstances() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder().withName("spark-default").withNamespace("default").build());
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    PodSet executorPodSet = workload.getSpec().getPodSets().get(1);
+    assertEquals(2, executorPodSet.getCount());
+    assertNull(executorPodSet.getMinCount());
+  }
+
+  @Test
+  void testBuildWorkloadDynamicAllocation() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-elastic")
+            .withNamespace("default")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "elastic-queue"))
+            .build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setSparkConf(
+        Map.of(
+            "spark.dynamicAllocation.enabled", "true",
+            "spark.dynamicAllocation.minExecutors", "2",
+            "spark.dynamicAllocation.maxExecutors", "10"));
+    app.setSpec(spec);
+
+    assertThrows(
+        UnsupportedOperationException.class, () -> KueueWorkloadFactory.buildWorkload(app));
+  }
+
+  @Test
+  void testBuildWorkloadForPythonApp() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder().withName("pi-python").withNamespace("default").build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setPyFiles("local:///opt/spark/examples/src/main/python/pi.py");
+    spec.setSparkConf(
+        Map.of(
+            "spark.driver.memory", "8g",
+            "spark.executor.memory", "8g",
+            "spark.executor.pyspark.memory", "1g"));
+    app.setSpec(spec);
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    Container driverContainer =
+        workload.getSpec().getPodSets().get(0).getTemplate().getSpec().getContainers().get(0);
+    assertEquals(
+        new Quantity("11468Mi"),
+        driverContainer.getResources().getRequests().get("memory")); // 8192 + 3276 = 11468
+    Container executorContainer =
+        workload.getSpec().getPodSets().get(1).getTemplate().getSpec().getContainers().get(0);
+    assertEquals(
+        new Quantity("12492Mi"),
+        executorContainer.getResources().getRequests().get("memory")); // 8192 + 3276 + 1024
+  }
+
+  @Test
+  void testBuildWorkloadWithGpuAndTemplateSpec() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-gpu")
+            .withNamespace("default")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "gpu-queue"))
+            .build());
+
+    PodTemplateSpec driverTemplate =
+        new PodTemplateSpecBuilder()
+            .withNewSpec()
+            .withContainers(
+                new ContainerBuilder()
+                    .withName("sidecar")
+                    .withResources(
+                        new ResourceRequirementsBuilder()
+                            .withRequests(Map.of("cpu", new Quantity("100m")))
+                            .build())
+                    .build(),
+                new ContainerBuilder()
+                    .withName("custom-driver")
+                    .withResources(
+                        new ResourceRequirementsBuilder()
+                            .withRequests(Map.of("cpu", new Quantity("3")))
+                            .build())
+                    .build())
+            .endSpec()
+            .build();
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setDriverSpec(new BaseApplicationTemplateSpec(driverTemplate));
+    spec.setSparkConf(
+        Map.of(
+            Constants.DRIVER_SPARK_CONTAINER_PROP_KEY, "custom-driver",

Review Comment:
   Added `testBuildWorkloadWithExecutorTemplateSpecAndRequestCores`, which selects `custom-executor` behind a leading sidecar via `spark.kubernetes.executor.podTemplateContainerName`, checks that the sidecar's requests are untouched, and covers `spark.kubernetes.{driver,executor}.request.cores`. In addition, a warning is logged like Spark's `selectSparkContainer` when the specified container is not found.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),

Review Comment:
   Added `Constants.EXECUTOR_SPARK_CONTAINER_PROP_KEY` and used it here.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",

Review Comment:
   Fixed. The default names are `spark-kubernetes-driver` and `spark-kubernetes-executor` now.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {

Review Comment:
   Fixed. The four methods are package-private now, and the dynamic allocation check moved to the top of `buildWorkload(SparkApplication)` next to the `SparkCluster` HPA rejection. In d7cb986, I also merged the duplicated driver / executor logic into a single role-based `buildPodSet` and the duplicated Workload assembly into one helper.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4008502984


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    checkNoPodTemplateFile(
+        sparkConf, Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getDriverSpec());
+    if (!driverOnly) {
+      checkNoPodTemplateFile(
+          sparkConf, Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getExecutorSpec());
+    }
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  /**
+   * A pod template file is fetched by Spark, not by the operator, so its node selectors,
+   * tolerations and extra containers cannot be reflected in the PodSet template. The file is
+   * ignored by Spark if the pod template is set in the SparkApplication spec.
+   */
+  private static void checkNoPodTemplateFile(
+      final Map<String, String> sparkConf,
+      final String templateFileKey,
+      final BaseApplicationTemplateSpec roleSpec) {
+    if (sparkConf.containsKey(templateFileKey)
+        && (roleSpec == null || roleSpec.getPodTemplateSpec() == null)) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support "
+              + templateFileKey
+              + " yet. Set the pod template in the SparkApplication spec instead.");
+    }
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)

Review Comment:
   **Finding 24.** The Workload never carries a priority, so Kueue orders every Spark workload the same.
   
   `WorkloadSpec` has `priorityClassName` and `priority` — #822 added both and `WorkloadTest:74` exercises them — and this builder sets neither. Kueue reads `spec.priority` and nothing else: `priority.Priority(w)` is `ptr.Deref(w.Spec.Priority, constants.DefaultPriority)`, and the Workload webhook's `Default` only drops `minCount`s, it does not resolve a priority. So a driver pod template with `priorityClassName: high-priority` sits in the ClusterQueue at the default priority, and queue ordering and preemption inside the cohort cannot tell it apart from an unprioritized app.
   
   Kueue's own integrations do this in `jobframework.ExtractPriority`: the `kueue.x-k8s.io/priority-class` label wins, otherwise it takes `priorityClassName` from the first PodSet template that has one (`extractPriorityFromPodSets`) and resolves it through `GetPriorityFromPriorityClass`, which also picks up the cluster's `globalDefault` PriorityClass when nothing is named. Then it writes both fields back.
   
   The first half is local here — read `podSets.get(0).getTemplate().getSpec().getPriorityClassName()` and put it in `spec.priorityClassName`. The numeric `priority` needs a `PriorityClass` GET, so it needs a `KubernetesClient` this factory does not take, and that half fits the wiring PR better.
   
   I could not run Kueue here, so this is read off Kueue's source rather than observed. The experiment that settles it is a Workload created with `spec.priority` unset, queued behind a higher-priority one: if Spark's never preempts, it is at the default.
   
   If leaving it out is deliberate, it belongs in the description's Unsupported Features list next to `podTemplateFile` — today it fails silently.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    checkNoPodTemplateFile(
+        sparkConf, Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getDriverSpec());
+    if (!driverOnly) {
+      checkNoPodTemplateFile(
+          sparkConf, Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getExecutorSpec());
+    }
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  /**
+   * A pod template file is fetched by Spark, not by the operator, so its node selectors,
+   * tolerations and extra containers cannot be reflected in the PodSet template. The file is
+   * ignored by Spark if the pod template is set in the SparkApplication spec.
+   */
+  private static void checkNoPodTemplateFile(
+      final Map<String, String> sparkConf,
+      final String templateFileKey,
+      final BaseApplicationTemplateSpec roleSpec) {
+    if (sparkConf.containsKey(templateFileKey)
+        && (roleSpec == null || roleSpec.getPodTemplateSpec() == null)) {

Review Comment:
   **Finding 25.** This is a second copy of a rule `ModelUtils` already owns.
   
   `ModelUtils.overrideDriverTemplateEnabled` / `overrideExecutorTemplateEnabled` (`spark-operator-api/.../ModelUtils.java:132-148`) are exactly `roleSpec != null && roleSpec.getPodTemplateSpec() != null`, and they are what `SparkAppResourceSpecFactory.overrideDependencyConf` consults when it decides whether to overwrite the `podTemplateFile` key. The two agree today. If the precedence rule ever changes there, this guard diverges silently — it either rejects an app whose file the operator would have replaced, or admits one whose file Spark really does fetch.
   
   `ModelUtils` is already imported here for `buildOwnerReferenceTo`, so passing the boolean in is small:
   
   ```java
       checkNoPodTemplateFile(
           sparkConf,
           Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY,
           ModelUtils.overrideDriverTemplateEnabled(spec));
       if (!driverOnly) {
         checkNoPodTemplateFile(
             sparkConf,
             Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY,
             ModelUtils.overrideExecutorTemplateEnabled(spec));
       }
   ```
   
   ```java
     /**
      * A pod template file is fetched by Spark, not by the operator, so its node selectors,
      * tolerations and extra containers cannot be reflected in the PodSet template. The operator
      * overwrites the file key when the pod template is set in the SparkApplication spec, so the
      * spec wins in that case.
      */
     private static void checkNoPodTemplateFile(
         final Map<String, String> sparkConf,
         final String templateFileKey,
         final boolean specTemplateWins) {
       if (sparkConf.containsKey(templateFileKey) && !specTemplateWins) {
   ```
   
   The Javadoc rewrite is part of the same edit. It is the operator, in `SparkAppResourceSpecFactory.getOrCreateLocalFileForSpec`, that replaces the key with a generated temp file — Spark itself reads whatever `spark.kubernetes.driver.podTemplateFile` says.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for SparkApplication and SparkCluster [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4006715774


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())

Review Comment:
   Fixed. The Workload name is `<lower-cased kind>-<name>` now, e.g., `sparkapplication-foo` and `sparkcluster-foo`.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);

Review Comment:
   Fixed. The Spark container is selected by `spark.kubernetes.{driver,executor}.podTemplateContainerName` with a fallback to the first container. The test now covers a driver template with a leading sidecar.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {

Review Comment:
   Thank you for the suggestion. I took the minimal approach in this PR.
   - Memory strings are parsed with `JavaUtils.byteStringAsMb` (from `spark-common-utils-java` in Spark 4.2), and the overhead rounding follows Spark (e.g., `4505Mi`).
   - The `SparkCluster` PodSets come from the StatefulSets built by `SparkClusterResourceSpec`.
   - For the GPU name, I used `<vendor>/gpu` directly because `spark-kubernetes` is not on the compile classpath of `spark-operator`.
   
   Moving the factory into `spark-submission-worker` would require moving the Kueue model classes too, because they live in `spark-operator`. I'd like to revisit deriving the PodSets from Spark's own computation separately.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {

Review Comment:
   Removed. Only the `HasMetadata` variants remain.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);

Review Comment:
   Fixed. `parseMemoryToMiB` is removed in favor of `JavaUtils.byteStringAsMb`, which throws `NumberFormatException` for an invalid unit like `10zz` or a fractional value like `1.5g`.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5667563671

   Thank you for the second round, @peter-toth. I addressed findings 13-20 in db33ec0, and d7cb986 is a follow-up cleanup (deduplicating the driver / executor and Workload assembly code, dropping dead null guards, and narrowing the public constants). The PR description is updated for the node selectors and generic custom resources.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4008253849


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,438 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)
+            .build());
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_DRIVER,
+        1,
+        spec.getDriverSpec(),
+        sparkConf,
+        Constants.DRIVER_SPARK_CONTAINER_PROP_KEY,
+        calculateDriverMemoryMiB(sparkConf, isNonJvmApp(spec)));
+  }
+
+  static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_EXECUTOR,
+        parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES),
+        spec.getExecutorSpec(),
+        sparkConf,
+        Constants.EXECUTOR_SPARK_CONTAINER_PROP_KEY,
+        calculateExecutorMemoryMiB(sparkConf, isNonJvmApp(spec), isPythonApp(spec)));
+  }
+
+  /**
+   * Builds a PodSet for a Spark role (`driver` or `executor`) from the pod template of the
+   * SparkApplication and the resource configurations in the same way as Spark does.
+   */
+  private static PodSet buildPodSet(
+      final String role,
+      final int count,
+      final BaseApplicationTemplateSpec roleSpec,
+      final Map<String, String> sparkConf,
+      final String containerNameKey,
+      final long memoryMiB) {
+    PodTemplateSpec templateSpec =
+        roleSpec != null && roleSpec.getPodTemplateSpec() != null
+            ? ReconcilerUtils.clone(roleSpec.getPodTemplateSpec())
+            : new PodTemplateSpecBuilder().build();

Review Comment:
   Thank you. Fixed in f83e218. `spark.kubernetes.{driver,executor}.podTemplateFile` without a pod template in the spec is rejected with `UnsupportedOperationException`, next to the dynamic allocation check. The executor key is not checked in driver-only mode. `testBuildWorkloadWithPodTemplateFile` covers the rejection, the driver-only case, and the precedence of the pod template spec. Reading the file in the operator can be a follow-up.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #823: [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster`
URL: https://github.com/apache/spark-kubernetes-operator/pull/823


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4007335410


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(

Review Comment:
   **Finding 13.** The PodSet template omits the node selectors Spark injects, so Kueue sees a less constrained pod than the one that will actually be created.
   
   `BasicDriverFeatureStep.configurePod` does `.addToNodeSelector(conf.nodeSelector).addToNodeSelector(conf.driverNodeSelector)` (`BasicDriverFeatureStep.scala:153-154` on `branch-4.2`), where those two maps are `spark.kubernetes.node.selector.*` and `spark.kubernetes.driver.node.selector.*` with the prefix stripped (`KubernetesConf.scala:69` and `:101`). `BasicExecutorFeatureStep` does the same with the executor prefix (`BasicExecutorFeatureStep.scala:310-311`). Here only what the user wrote into `podTemplateSpec` survives.
   
   Kueue's own field doc on `PodSet.template` says the template's `nodeSelector` and required node affinity are matched against a `ResourceFlavor`'s node labels during admission to filter the eligible flavors. A pod with no `nodeSelector` conflicts with no flavor, so it matches all of them.
   
   Measured on `0ddd85a` with `spark.kubernetes.node.selector.karpenter.sh/nodepool=gpu` plus `spark.kubernetes.{driver,executor}.node.selector.node.kubernetes.io/instance-type=p4d.24xlarge`:
   
       DRIVER nodeSelector = {}
       EXEC   nodeSelector = {}
   
   Both should carry all three entries. Fix shape, as a sibling of `decorateTemplateResources`:
   
   ```java
     private static void decorateTemplateNodeSelector(
         final PodTemplateSpec templateSpec,
         final Map<String, String> sparkConf,
         final String rolePrefix) {
       PodSpec podSpec = templateSpec.getSpec();
       if (podSpec.getNodeSelector() == null) {
         podSpec.setNodeSelector(new HashMap<>());
       }
       // Like Spark, the role-specific prefix is applied last so that it wins.
       putPrefixedKeyValuePairs(podSpec.getNodeSelector(), sparkConf, NODE_SELECTOR_PREFIX);
       putPrefixedKeyValuePairs(podSpec.getNodeSelector(), sparkConf, rolePrefix);
     }
   ```
   
   Tolerations and affinity are fine as they are, since Spark only ever takes those from the pod template.
   
   If you would rather defer this, please list it under Unsupported Features in the description. Dropping it silently produces a plausible-looking Workload rather than an error, so nothing surfaces it.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),
+        "spark-executor",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES))
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  public static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      total +=
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String containerName,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      // Like Spark's KubernetesUtils.selectSparkContainer, select the container by name and
+      // fall back to the first container.
+      container =
+          podSpec.getContainers().stream()
+              .filter(c -> containerName != null && containerName.equals(c.getName()))
+              .findFirst()
+              .orElse(podSpec.getContainers().get(0));
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (StringUtils.isEmpty(gpuVendor)) {
+        throw new IllegalArgumentException(
+            "Resource: gpu was requested, but vendor was not specified.");
+      }
+      // Like Spark's KubernetesConf.buildKubernetesResourceName, e.g., `nvidia.com/gpu`.
+      String gpuResourceName = gpuVendor + "/gpu";

Review Comment:
   **Finding 14.** `gpu` is not the only resource name Spark accepts here. `spark.{driver,executor}.resource.<resourceName>.amount` / `.vendor` is generic, and Spark turns each pair into `<vendor>/<resourceName>` (`KubernetesUtils.buildResourcesQuantities` for the driver, `BasicExecutorFeatureStep.buildExecutorResourcesQuantities` for the executor, both via `KubernetesConf.buildKubernetesResourceName`).
   
   Measured on `0ddd85a`, with `spark.executor.resource.gpu.{amount,vendor} = 1, nvidia.com` and `spark.executor.resource.fpga.{amount,vendor} = 2, xilinx.com`:
   
       EXEC requests = {cpu=1, memory=1408Mi, nvidia.com/gpu=1}
   
   The executor pod that runs requests `xilinx.com/fpga: 2` as well, so Kueue admits against a quota that never accounts for it.
   
   The generic form needs no new dependency, and it also lets the two `spark.{driver,executor}.resource.gpu.*` reads in the callers go away:
   
   ```java
       // Like Spark's `KubernetesConf.buildKubernetesResourceName`, e.g., `nvidia.com/gpu`.
       String prefix = rolePrefix + ".resource."; // e.g., `spark.executor.resource.`
       for (Map.Entry<String, String> e : sparkConf.entrySet()) {
         if (!e.getKey().startsWith(prefix) || !e.getKey().endsWith(".amount")) {
           continue;
         }
         String name =
             e.getKey().substring(prefix.length(), e.getKey().length() - ".amount".length());
         String vendor = sparkConf.get(prefix + name + ".vendor");
         if (StringUtils.isEmpty(vendor)) {
           throw new IllegalArgumentException(
               "Resource: " + name + " was requested, but vendor was not specified.");
         }
         requests.put(vendor + "/" + name, new Quantity(e.getValue()));
         limits.put(vendor + "/" + name, new Quantity(e.getValue()));
       }
   ```
   



##########
spark-operator/src/test/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactoryTest.java:
##########
@@ -0,0 +1,497 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpecBuilder;
+import org.junit.jupiter.api.Test;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.ClusterTolerations;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.RuntimeVersions;
+import org.apache.spark.k8s.operator.spec.WorkerInstanceConfig;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+
+class KueueWorkloadFactoryTest {
+
+  @Test
+  void testCalculateDriverMemoryMiB() {
+    // 1g memory -> 1024MiB. Overhead factor 0.10 -> 102MiB -> min 384MiB.
+    // Total = 1024 + 384 = 1408
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.driver.memory", "1g");
+    assertEquals(1408L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Explicit overhead 512m -> 1024 + 512 = 1536
+    conf.put("spark.driver.memoryOverhead", "512m");
+    assertEquals(1536L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Custom overhead factor 0.50 -> 1024 * 0.50 = 512 -> 1024 + 512 = 1536
+    conf.remove("spark.driver.memoryOverhead");
+    conf.put("spark.driver.memoryOverheadFactor", "0.50");
+    assertEquals(1536L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // Custom minimum overhead 1g -> 1024 + max(512, 1024) = 2048
+    conf.put("spark.driver.minMemoryOverhead", "1g");
+    assertEquals(2048L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+  }
+
+  @Test
+  void testCalculateDriverMemoryMiBForNonJvmApp() {
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.driver.memory", "8g");
+    // Non-JVM: 8192 + 0.40 * 8192 = 8192 + 3276 = 11468
+    assertEquals(11468L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, true));
+    // JVM: 8192 + 0.10 * 8192 = 8192 + 819 = 9011
+    assertEquals(9011L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+
+    // The deprecated `spark.kubernetes.memoryOverheadFactor` 0.20 -> 8192 + 1638 = 9830
+    conf.put("spark.kubernetes.memoryOverheadFactor", "0.20");
+    assertEquals(9830L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, true));
+    assertEquals(9830L, KueueWorkloadFactory.calculateDriverMemoryMiB(conf, false));
+  }
+
+  @Test
+  void testCalculateExecutorMemoryMiB() {
+    Map<String, String> conf = new HashMap<>();
+    // 2048MiB. Overhead: max(204, 384) = 384. Total = 2432
+    conf.put("spark.executor.memory", "2g");
+    assertEquals(2432L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // PySpark memory is added only for Python applications
+    conf.put("spark.executor.pyspark.memory", "1g");
+    assertEquals(2432L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+    // Non-JVM overhead: max(819, 384) = 819. Total = 2048 + 819 + 1024 = 3891
+    assertEquals(3891L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, true, true));
+  }
+
+  @Test
+  void testCalculateExecutorMemoryMiBWithOffHeap() {
+    Map<String, String> conf = new HashMap<>();
+    conf.put("spark.executor.memory", "4g");
+    conf.put("spark.memory.offHeap.size", "4g");
+    // Off-heap memory is ignored if disabled. 4096 + 409 = 4505
+    assertEquals(4505L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // 4096 + 409 + 4096 = 8601
+    conf.put("spark.memory.offHeap.enabled", "true");
+    assertEquals(8601L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+
+    // `spark.memory.offHeap.size` is in bytes unless otherwise specified
+    conf.put("spark.memory.offHeap.size", "4294967296");
+    assertEquals(8601L, KueueWorkloadFactory.calculateExecutorMemoryMiB(conf, false, false));
+  }
+
+  @Test
+  void testCalculateMemoryWithUnits() {
+    // 2048 + 384 = 2432
+    assertEquals(
+        2432L,
+        KueueWorkloadFactory.calculateExecutorMemoryMiB(
+            Map.of("spark.executor.memory", "2Gi"), false, false));
+    assertThrows(
+        NumberFormatException.class,
+        () ->
+            KueueWorkloadFactory.calculateDriverMemoryMiB(
+                Map.of("spark.driver.memory", "10zz"), false));
+    assertThrows(
+        NumberFormatException.class,
+        () ->
+            KueueWorkloadFactory.calculateExecutorMemoryMiB(
+                Map.of("spark.executor.memory", "1.5g"), false, false));
+  }
+
+  @Test
+  void testHasAndGetQueueName() {
+    SparkApplication app = new SparkApplication();
+    assertFalse(KueueWorkloadFactory.hasQueueName(app));
+    assertNull(KueueWorkloadFactory.getQueueName(app));
+
+    // From label
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "test-queue"))
+            .build());
+    assertTrue(KueueWorkloadFactory.hasQueueName(app));
+    assertEquals("test-queue", KueueWorkloadFactory.getQueueName(app));
+
+    // Empty label
+    SparkApplication app2 = new SparkApplication();
+    app2.setMetadata(
+        new ObjectMetaBuilder()
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, ""))
+            .build());
+    assertFalse(KueueWorkloadFactory.hasQueueName(app2));
+    assertNull(KueueWorkloadFactory.getQueueName(app2));
+  }
+
+  @Test
+  void testBuildWorkloadStaticAllocation() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-pi")
+            .withNamespace("spark-jobs")
+            .withUid("app-uid-123")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "team-a-queue"))
+            .build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setSparkConf(
+        Map.of(
+            "spark.driver.cores", "2",
+            "spark.driver.memory", "2g",
+            "spark.executor.cores", "4",
+            "spark.executor.memory", "4g",
+            "spark.executor.instances", "3"));
+    app.setSpec(spec);
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    assertNotNull(workload);
+    assertEquals("sparkapplication-spark-pi", workload.getMetadata().getName());
+    assertEquals("spark-jobs", workload.getMetadata().getNamespace());
+    assertEquals("team-a-queue", workload.getSpec().getQueueName());
+    assertTrue(workload.getSpec().getActive());
+    assertEquals(1, workload.getMetadata().getOwnerReferences().size());
+    assertEquals("spark-pi", workload.getMetadata().getOwnerReferences().get(0).getName());
+    assertTrue(workload.getMetadata().getOwnerReferences().get(0).getController());
+
+    assertEquals(2, workload.getSpec().getPodSets().size());
+
+    PodSet driverPodSet = workload.getSpec().getPodSets().get(0);
+    assertEquals("driver", driverPodSet.getName());
+    assertEquals(1, driverPodSet.getCount());
+    assertNull(driverPodSet.getMinCount());
+    Container driverContainer =
+        driverPodSet.getTemplate().getSpec().getContainers().get(0);
+    assertEquals(new Quantity("2"), driverContainer.getResources().getRequests().get("cpu"));
+    assertEquals(
+        new Quantity("2432Mi"),
+        driverContainer.getResources().getRequests().get("memory")); // 2048 + 384 = 2432
+
+    PodSet executorPodSet = workload.getSpec().getPodSets().get(1);
+    assertEquals("executor", executorPodSet.getName());
+    assertEquals(3, executorPodSet.getCount());
+    assertNull(executorPodSet.getMinCount());
+    Container executorContainer =
+        executorPodSet.getTemplate().getSpec().getContainers().get(0);
+    assertEquals(new Quantity("4"), executorContainer.getResources().getRequests().get("cpu"));
+    assertEquals(
+        new Quantity("4505Mi"),
+        executorContainer.getResources().getRequests().get("memory")); // 4096 + 409 = 4505
+  }
+
+  @Test
+  void testBuildWorkloadDefaultExecutorInstances() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder().withName("spark-default").withNamespace("default").build());
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    PodSet executorPodSet = workload.getSpec().getPodSets().get(1);
+    assertEquals(2, executorPodSet.getCount());
+    assertNull(executorPodSet.getMinCount());
+  }
+
+  @Test
+  void testBuildWorkloadDynamicAllocation() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-elastic")
+            .withNamespace("default")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "elastic-queue"))
+            .build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setSparkConf(
+        Map.of(
+            "spark.dynamicAllocation.enabled", "true",
+            "spark.dynamicAllocation.minExecutors", "2",
+            "spark.dynamicAllocation.maxExecutors", "10"));
+    app.setSpec(spec);
+
+    assertThrows(
+        UnsupportedOperationException.class, () -> KueueWorkloadFactory.buildWorkload(app));
+  }
+
+  @Test
+  void testBuildWorkloadForPythonApp() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder().withName("pi-python").withNamespace("default").build());
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setPyFiles("local:///opt/spark/examples/src/main/python/pi.py");
+    spec.setSparkConf(
+        Map.of(
+            "spark.driver.memory", "8g",
+            "spark.executor.memory", "8g",
+            "spark.executor.pyspark.memory", "1g"));
+    app.setSpec(spec);
+
+    Workload workload = KueueWorkloadFactory.buildWorkload(app);
+    Container driverContainer =
+        workload.getSpec().getPodSets().get(0).getTemplate().getSpec().getContainers().get(0);
+    assertEquals(
+        new Quantity("11468Mi"),
+        driverContainer.getResources().getRequests().get("memory")); // 8192 + 3276 = 11468
+    Container executorContainer =
+        workload.getSpec().getPodSets().get(1).getTemplate().getSpec().getContainers().get(0);
+    assertEquals(
+        new Quantity("12492Mi"),
+        executorContainer.getResources().getRequests().get("memory")); // 8192 + 3276 + 1024
+  }
+
+  @Test
+  void testBuildWorkloadWithGpuAndTemplateSpec() {
+    SparkApplication app = new SparkApplication();
+    app.setMetadata(
+        new ObjectMetaBuilder()
+            .withName("spark-gpu")
+            .withNamespace("default")
+            .withLabels(Map.of(Constants.LABEL_QUEUE_NAME, "gpu-queue"))
+            .build());
+
+    PodTemplateSpec driverTemplate =
+        new PodTemplateSpecBuilder()
+            .withNewSpec()
+            .withContainers(
+                new ContainerBuilder()
+                    .withName("sidecar")
+                    .withResources(
+                        new ResourceRequirementsBuilder()
+                            .withRequests(Map.of("cpu", new Quantity("100m")))
+                            .build())
+                    .build(),
+                new ContainerBuilder()
+                    .withName("custom-driver")
+                    .withResources(
+                        new ResourceRequirementsBuilder()
+                            .withRequests(Map.of("cpu", new Quantity("3")))
+                            .build())
+                    .build())
+            .endSpec()
+            .build();
+
+    ApplicationSpec spec = new ApplicationSpec();
+    spec.setDriverSpec(new BaseApplicationTemplateSpec(driverTemplate));
+    spec.setSparkConf(
+        Map.of(
+            Constants.DRIVER_SPARK_CONTAINER_PROP_KEY, "custom-driver",

Review Comment:
   **Finding 16.** This covers the driver key only. The executor path reads a bare `"spark.kubernetes.executor.podTemplateContainerName"` literal (`KueueWorkloadFactory.java:308`) with no constant and no test, so a typo there falls back to `containers.get(0)` and the whole executor request lands on a sidecar.
   
   Mirroring this test for the executor is a few lines: an `executorSpec` template with `sidecar` first and `custom-executor` second, `spark.kubernetes.executor.podTemplateContainerName: custom-executor`, then assert the computed `cpu` / `memory` landed on `custom-executor` and the sidecar's own `requests` are untouched.
   
   `spark.kubernetes.{driver,executor}.request.cores` is untested too, and it is the other branch of the same `getOrDefault` chain.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),
+        "spark-executor",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES))
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY));
+    return memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.driver", memMiB, isNonJvm);
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead, off-heap memory and PySpark
+   * memory.
+   */
+  public static long calculateExecutorMemoryMiB(
+      final Map<String, String> sparkConf, final boolean isNonJvm, final boolean isPython) {
+    long memMiB =
+        JavaUtils.byteStringAsMb(sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY));
+    long total =
+        memMiB + calculateMemoryOverheadMiB(sparkConf, "spark.executor", memMiB, isNonJvm);
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.memory.offHeap.enabled"))) {
+      // `spark.memory.offHeap.size` is in bytes unless otherwise specified.
+      total +=
+          JavaUtils.byteStringAsBytes(sparkConf.getOrDefault("spark.memory.offHeap.size", "0"))
+              / 1024
+              / 1024;
+    }
+    if (isPython && sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += JavaUtils.byteStringAsMb(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  private static long calculateMemoryOverheadMiB(
+      final Map<String, String> sparkConf,
+      final String prefix,
+      final long memMiB,
+      final boolean isNonJvm) {
+    String overhead = sparkConf.get(prefix + ".memoryOverhead");
+    if (StringUtils.isNotEmpty(overhead)) {
+      return JavaUtils.byteStringAsMb(overhead);
+    }
+    // Like Spark's BasicDriverFeatureStep, the deprecated `spark.kubernetes.memoryOverheadFactor`
+    // or the default factor (0.4 for non-JVM applications) is used if not set explicitly.
+    double defaultFactor =
+        parseDouble(
+            sparkConf.get("spark.kubernetes.memoryOverheadFactor"),
+            isNonJvm ? NON_JVM_MEMORY_OVERHEAD_FACTOR : DEFAULT_MEMORY_OVERHEAD_FACTOR);
+    double factor = parseDouble(sparkConf.get(prefix + ".memoryOverheadFactor"), defaultFactor);
+    long minOverheadMiB =
+        JavaUtils.byteStringAsMb(
+            sparkConf.getOrDefault(prefix + ".minMemoryOverhead", DEFAULT_MIN_MEMORY_OVERHEAD));
+    return Math.max((int) (factor * memMiB), minOverheadMiB);
+  }
+
+  /** Follows the main application resource selection of SparkAppSubmissionWorker. */
+  private static boolean isPythonApp(final ApplicationSpec spec) {
+    return StringUtils.isEmpty(spec.getJars())
+        && ("org.apache.spark.deploy.PythonRunner".equals(spec.getMainClass())
+            || StringUtils.isNotEmpty(spec.getPyFiles()));
+  }
+
+  private static boolean isNonJvmApp(final ApplicationSpec spec) {
+    return isPythonApp(spec)
+        || (StringUtils.isEmpty(spec.getJars()) && StringUtils.isNotEmpty(spec.getSparkRFiles()));
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String containerName,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      // Like Spark's KubernetesUtils.selectSparkContainer, select the container by name and
+      // fall back to the first container.
+      container =
+          podSpec.getContainers().stream()
+              .filter(c -> containerName != null && containerName.equals(c.getName()))
+              .findFirst()
+              .orElse(podSpec.getContainers().get(0));
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    // Like Spark, overwrite the requests of the pod template.
+    requests.put("cpu", new Quantity(cpu));
+    requests.put("memory", new Quantity(memoryMiB + "Mi"));
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (StringUtils.isEmpty(gpuVendor)) {
+        throw new IllegalArgumentException(
+            "Resource: gpu was requested, but vendor was not specified.");
+      }
+      // Like Spark's KubernetesConf.buildKubernetesResourceName, e.g., `nvidia.com/gpu`.
+      String gpuResourceName = gpuVendor + "/gpu";
+      requests.put(gpuResourceName, new Quantity(gpuAmount));
+      Map<String, Quantity> limits = resources.getLimits();
+      if (limits == null) {
+        limits = new HashMap<>();
+        resources.setLimits(limits);
+      }
+      limits.put(gpuResourceName, new Quantity(gpuAmount));
+    }
+  }
+
+  private static int parseInt(final String str, final int defaultValue) {

Review Comment:
   **Finding 15.** Both helpers swallow a malformed value and return a plausible number, which is the failure mode finding 12 removed from the memory path.
   
   Measured on `0ddd85a`:
   
       spark.executor.instances=not-a-number          -> executor PodSet count 2
       spark.driver.memoryOverheadFactor=not-a-number -> 9011Mi (factor silently 0.1)
   
   Spark rejects both: `spark.executor.instances` is an `intConf` and `spark.driver.memoryOverheadFactor` is a `doubleConf` with a `> 0` check, so the driver never starts. Meanwhile `spark.driver.memory=10zz` throws here, and `testCalculateMemoryWithUnits` pins that.
   
   Letting `Integer.parseInt` / `Double.parseDouble` throw makes all three consistent. The `StringUtils.isEmpty` guard already covers the only input that legitimately needs a default.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",

Review Comment:
   **Finding 18.** Spark's defaults are `spark-kubernetes-driver` and `spark-kubernetes-executor` (`Constants.scala:106-107` on `branch-4.2`); `BasicDriverFeatureStep` applies the first via `Option(pod.container.getName).getOrElse(DEFAULT_DRIVER_CONTAINER_NAME)`. The comment at line 412 says this block follows `KubernetesUtils.selectSparkContainer`, so the name it synthesizes should follow too. It is what shows up in the Workload a user reads, and what any later PodSet-update or topology code would match on.
   
   ```suggestion
           "spark-kubernetes-driver",
   ```
   
   Line 309 needs the same change to `"spark-kubernetes-executor"`.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {

Review Comment:
   **Finding 19.** `buildDriverPodSet`, `buildExecutorPodSet`, `calculateDriverMemoryMiB` and `calculateExecutorMemoryMiB` are `public` only so `KueueWorkloadFactoryTest` can reach them, and that test is in the same package — package-private is enough, and it also settles the missing `@param` / `@return` tags on those four.
   
   That public surface is also why this guard sits here rather than at the top of `buildWorkload`. As written it runs after `buildDriverPodSet` has already built a PodSet, and `buildDriverPodSet` called on its own skips the check entirely. Moving it to the first line of `buildWorkload(SparkApplication)` puts it alongside the `SparkCluster` HPA rejection at line 148, so both unsupported cases are rejected in the same place.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(app))
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()));
+    podSets.add(buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(cluster))
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf, isNonJvmApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.driver.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY),
+        "spark-driver",
+        cpu,
+        memoryMiB,
+        gpuAmount,
+        gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB =
+        calculateExecutorMemoryMiB(
+            sparkConf, isNonJvmApp(app.getSpec()), isPythonApp(app.getSpec()));
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor = sparkConf.get("spark.executor.resource.gpu.vendor");
+
+    decorateTemplateResources(
+        templateSpec,
+        sparkConf.get("spark.kubernetes.executor.podTemplateContainerName"),

Review Comment:
   **Finding 17.** The driver reads this from `Constants.DRIVER_SPARK_CONTAINER_PROP_KEY` at line 261. `Constants` already carries the driver and executor `podTemplateFile` keys as a pair (`spark-operator-api/src/main/java/org/apache/spark/k8s/operator/Constants.java:93-99`), so the executor container-name key is the one member missing from that set.
   
   ```java
     /** The property key for the executor Spark container name. */
     public static final String EXECUTOR_SPARK_CONTAINER_PROP_KEY =
         "spark.kubernetes.executor.podTemplateContainerName";
   ```
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);

Review Comment:
   **Finding 20.** This `put` cannot change anything. `queueName` was read from `Constants.LABEL_QUEUE_NAME` on this same object (line 87 into line 217), and line 105 already copied every one of the CR's labels. Line 166 is the same for `SparkCluster`. Dropping both leaves `labels` identical.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4007509295


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  public static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);

Review Comment:
   Removed both.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for SparkApplication and SparkCluster [spark-kubernetes-operator]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4003965631


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;

Review Comment:
   **Finding 1.** These aren't the fields the operator uses to size a cluster.
   
   `SparkClusterResourceSpec` builds the worker StatefulSet with `withReplicas(initWorkers)` where `initWorkers` is `spec.getClusterTolerations().getInstanceConfig().getInitWorkers()` (`spark-submission-worker/src/main/java/org/apache/spark/k8s/operator/SparkClusterResourceSpec.java:112` and `:310`), and the HPA with `withMinReplicas(instanceConfig.getMinWorkers()).withMaxReplicas(instanceConfig.getMaxWorkers())` (`:422`), overriding whatever `workerSpec.horizontalPodAutoscalerSpec` carries. The master StatefulSet is hardcoded to `withReplicas(1)` (`:229`), so `masterSpec.statefulSetSpec.replicas` at line 205 is never what runs.
   
   None of the shipped examples set `statefulSetSpec.replicas`. Running `buildWorkload` over them:
   
   - `examples/cluster.yaml` (`initWorkers: 3, minWorkers: 3, maxWorkers: 3`) gives `master {count: 1, template: {}}` and `worker {count: 1, template: {}}`. Three worker pods run, the Workload reserves one, and there is no container spec for Kueue to compute any quota from.
   - `examples/cluster-with-hpa.yaml` (`initWorkers: 3, minWorkers: 1, maxWorkers: 3`) gives `worker {count: 1}` with no `minCount`. It should be `count: 3, minCount: 1`.
   
   Suggested shape: take the counts from `clusterTolerations.instanceConfig` (`maxWorkers` as `count` and `minWorkers` as `minCount` when `minWorkers < maxWorkers`, else `initWorkers`), pin master to 1, and build the templates from the StatefulSets `SparkClusterResourceSpec` already produces so the PodSet carries the same `master` / `worker` container the real pod has.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;

Review Comment:
   **Finding 4.** `BasicDriverFeatureStep` uses `NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.4` when the main app resource is non-JVM and no explicit factor is set. The operator already makes that classification: `SparkAppSubmissionWorker.buildDriverConf` builds a `PythonMainAppResource` when `mainClass` is `org.apache.spark.deploy.PythonRunner` or `pyFiles` is set, and an `RMainAppResource` for `sparkRFiles` (`spark-submission-worker/src/main/java/org/apache/spark/k8s/operator/SparkAppSubmissionWorker.java:150-165`).
   
   For an 8g PySpark driver, Spark requests `8192 + max(0.4*8192, 384) = 11468Mi` and this computes `8192 + max(0.1*8192, 384) = 9011Mi`, 21% under-reserved. `examples/pi-python.yaml` is the shape that hits it.
   
   Two smaller gaps at the same constants: the deprecated `spark.kubernetes.memoryOverheadFactor` is Spark's fallback before the built-in default and is ignored, and `MIN_MEMORY_OVERHEAD_MIB` hardcodes 384 while `spark.driver.minMemoryOverhead` / `spark.executor.minMemoryOverhead` are configurable.
   
   Deciding the non-JVM case needs the `ApplicationSpec`, not just `sparkConf`, so `calculateDriverMemoryMiB` / `calculateExecutorMemoryMiB` would have to take the app or a pre-resolved factor.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;

Review Comment:
   **Finding 3.** Spark's executor request is `executorMemory + overhead + offHeap + pyspark` (`ResourceProfile.getResourcesForClusterManager`, `core/src/main/scala/org/apache/spark/resource/ResourceProfile.scala:610`), where `offHeap` is `spark.memory.offHeap.size` when `spark.memory.offHeap.enabled` is true. The off-heap term is missing here.
   
   With `spark.executor.memory=4g`, `spark.memory.offHeap.enabled=true`, `spark.memory.offHeap.size=4g` the factory emits `memory: 4506Mi`, while the executor pod Spark creates requests `8601Mi`. Kueue would admit roughly twice the executors that fit.
   
   ```java
       long total = memMiB + overheadMiB;
       if (Boolean.parseBoolean(sparkConf.getOrDefault("spark.memory.offHeap.enabled", "false"))) {
         total += offHeapMiB(sparkConf.get("spark.memory.offHeap.size"));
       }
   ```
   
   `parseMemoryToMiB` is the wrong parser for that value: `spark.memory.offHeap.size` is `bytesConf(ByteUnit.BYTE)`, so a bare `4294967296` means bytes, not MiB.
   
   Separately, `spark.executor.pyspark.memory` is added unconditionally here while Spark only counts it for a Python app (`isPythonApp` in the same method).
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    if (!requests.containsKey("cpu")) {
+      requests.put("cpu", new Quantity(cpu));
+    }
+    if (!requests.containsKey("memory")) {
+      requests.put("memory", new Quantity(memoryMiB + "Mi"));
+    }
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (!requests.containsKey(gpuVendor)) {
+        requests.put(gpuVendor, new Quantity(gpuAmount));

Review Comment:
   **Finding 2.** `spark.{driver,executor}.resource.gpu.vendor` holds the vendor *domain*, not the full resource name. Spark composes the Kubernetes name as `vendor/resourceName` (`KubernetesConf.buildKubernetesResourceName`), so the documented `spark.executor.resource.gpu.vendor: nvidia.com` yields `nvidia.com/gpu` on the executor pod. Here the vendor value becomes the key directly, so that same config produces:
   
   ```yaml
   requests:
     nvidia.com: "2"
   limits:
     nvidia.com: "2"
   ```
   
   No node advertises `nvidia.com`, so Kueue charges a resource that isn't in any quota and the GPU need is invisible. `testBuildWorkloadWithGpuAndTemplateSpec` hides this by setting the vendor to `nvidia.com/gpu`.
   
   ```java
       if (StringUtils.isNotEmpty(gpuAmount)) {
         String gpuResource = gpuVendor + "/gpu";
         if (!requests.containsKey(gpuResource)) {
           requests.put(gpuResource, new Quantity(gpuAmount));
         }
         ...
         if (!limits.containsKey(gpuResource)) {
           limits.put(gpuResource, new Quantity(gpuAmount));
         }
       }
   ```
   
   `DEFAULT_GPU_VENDOR` at line 77 needs to become `nvidia.com` to match. Worth considering dropping the default entirely: Spark throws `Resource: gpu was requested, but vendor was not specified` when the amount is set without a vendor, so defaulting here quietly accounts a config that would fail at submission.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    if (!requests.containsKey("cpu")) {

Review Comment:
   **Finding 6.** Spark overwrites the pod template's cpu/memory requests rather than deferring to them. `BasicDriverFeatureStep.configurePod` does `.editOrNewResources().addToRequests("cpu", driverCpuQuantity).addToRequests("memory", driverMemoryQuantity)`, and `addToRequests` replaces an existing key. So for a driver template carrying `cpu: 3` with no `spark.driver.cores`, the pod that runs requests `cpu: 1`.
   
   `testBuildWorkloadWithGpuAndTemplateSpec` asserts the opposite — `assertEquals(new Quantity("3"), driverContainer.getResources().getRequests().get("cpu"))` — so the test pins the divergence rather than catching it. The mirror case under-reserves: template `cpu: 1` with `spark.driver.cores=4` gives a PodSet of 1 against a pod of 4.
   
   Dropping the `!requests.containsKey(...)` guards for `cpu` and `memory` matches Spark. The same applies to the GPU keys below, since `addToLimits` overwrites the template too.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {

Review Comment:
   **Finding 10.** Findings 2 to 6 are one shape: this class re-implements `BasicDriverFeatureStep`, `BasicExecutorFeatureStep` and `ResourceProfile.getResourcesForClusterManager` from raw conf strings, and drifts from each. They're individually fixable, but the drift returns on every Spark upgrade and nothing in CI compares the two. Even the rounding differs — `Math.ceil` here versus Spark's `(overheadFactor * mem).toInt`, which is why `testBuildWorkloadStaticAllocation` pins `4506Mi` where the real executor pod asks for `4505Mi`.
   
   `spark-kubernetes` (and transitively `spark-core` / `spark-network-common`) is bundled into the operator through `spark-submission-worker`, and `spark-operator` already has `compileOnly(libs.spark.core)`. So part of this is a direct swap today:
   
   - `parseMemoryToMiB` becomes `org.apache.spark.network.util.JavaUtils.byteStringAsMb`, which is what `spark.driver.memory` and friends are actually parsed with. That also settles finding 12's unit handling, and `spark.memory.offHeap.size` gets `byteStringAsBytes` since it defaults to bytes.
   - The GPU key becomes `KubernetesConf.buildKubernetesResourceName(vendor, "gpu")` (finding 2).
   - The SparkCluster PodSets come from the StatefulSets `SparkClusterResourceSpec` already builds (finding 1), which resolve the replica counts and inject the `master` / `worker` container.
   
   The direction I'd push toward is stronger: the driver PodSet template *is* the driver pod that `SparkAppSubmissionWorker.getResourceSpec` already computes, and the executor PodSet comes from `ResourceProfile.getResourcesForClusterManager`. Then the Workload can't disagree with the pods by construction.
   
   The counter-argument is real. `getResourcesForClusterManager` is `private[spark]`, so calling it from Java means a small shim in `spark-submission-worker` or a `MODULE$` call; and building the driver spec needs a `KubernetesClient` and runs the whole feature-step chain, which is more work than a reconcile that only wants a resource total. A middle ground is to keep this factory but move it into `spark-submission-worker`, where Spark's API is already a first-class dependency, and call the Spark helpers from there.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);

Review Comment:
   **Finding 5.** Spark on Kubernetes launches `DEFAULT_NUMBER_EXECUTORS = 2` when `spark.executor.instances` is unset (`ExecutorPodsAllocator.scala:56`, `SchedulerBackendUtils.scala:24`), so the default here should be 2. 36 of the 40 files under `examples/` leave the conf unset, so this is the common path, not the edge: each of those apps would reserve one executor and run two.
   
   Line 394 has the same problem from the other end. With dynamic allocation on and no `spark.dynamicAllocation.maxExecutors`, Spark's ceiling is `Int.MaxValue`, and the fallback chain lands on 1. Reserving 1 for a job that can scale without bound is the least safe guess available; `spark.dynamicAllocation.initialExecutors` plus a warn log would at least be visible.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {

Review Comment:
   **Finding 11.** This overload and the three like it are unreachable as distinct methods. `getQueueName(SparkApplication)` and `getQueueName(SparkCluster)` (line 290) only cast to `HasMetadata` and delegate, and Java resolves `getQueueName(app)` to the `HasMetadata` overload at line 262 on its own. Same for `hasQueueName` at lines 310 and 320 against line 300. Deleting all four leaves every call site, including the tests, compiling unchanged.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())

Review Comment:
   **Finding 8.** The Workload takes the CR's bare name, here and at line 122 for the application. A `SparkApplication` and a `SparkCluster` can both be named `foo` in the same namespace — nothing forbids it — and both then map to `Workload/foo`. Whichever reconciles second either fails with `AlreadyExists` on every attempt or adopts a Workload whose `ownerReferences` already names the other CR as controller with `blockOwnerDeletion: true`.
   
   Kueue's own integrations avoid this by deriving the name from the owner: `GetWorkloadNameForOwnerWithGVK` produces `<kind-prefix>-<name>-<uid-hash>`. A kind prefix alone would be enough here, e.g. `sparkapp-<name>` and `sparkcluster-<name>`, and this repo already has `SparkAppSubmissionWorker.generateHashBasedId` if you want the collision-proof version.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);

Review Comment:
   **Finding 9.** The Spark container isn't always first. Spark selects it by `spark.kubernetes.driver.podTemplateContainerName` and only falls back to the first container (`KubernetesUtils.selectSparkContainer`, `KubernetesUtils.scala:117`). This repo already carries the key as `Constants.DRIVER_SPARK_CONTAINER_PROP_KEY`, used by `ModelUtils.findDriverMainContainerStatus`.
   
   `examples/cluster-with-template.yaml` shows the main-plus-sidecar shape users write. With the sidecar first, the driver's computed cpu/memory land on the sidecar. Worse, combined with the guards at line 523, a sidecar that declares its own `cpu`/`memory` requests means the driver's own request is never added at all, and the PodSet under-reserves by the entire driver.
   
   Selecting by `sparkConf.get(Constants.DRIVER_SPARK_CONTAINER_PROP_KEY)` for the driver and `spark.kubernetes.executor.podTemplateContainerName` for the executor, falling back to index 0, matches Spark.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);

Review Comment:
   **Finding 12.** An unrecognised unit silently becomes MiB. `parseMemoryToMiB("10zz")` returns `10` with no warning, because the regex at line 80 accepts `[a-zA-Z]*` and this arm swallows whatever it captured. A string that fails the regex outright does warn (line 463), so the two failure modes are handled inconsistently, and the quieter one is the one that produces a plausible-looking wrong number.
   
   Either warn here the way line 463 does, or take `JavaUtils.byteStringAsMb` per finding 10 and drop the switch — Spark rejects the unit rather than guessing.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5669213794

   Thank you so much again, @peter-toth !


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for SparkApplication and SparkCluster [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4006713947


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;

Review Comment:
   Thank you. Fixed in 0ddd85a. `buildWorkload(SparkCluster)` now builds the PodSets from the StatefulSets produced by `SparkClusterResourceSpecFactory.buildResourceSpec`, the same as the reconciler. So, `master` is `1` and `worker` is `initWorkers`, and both templates carry the injected `master` / `worker` containers. A `SparkCluster` with HPA (`minWorkers < maxWorkers`) is rejected with `UnsupportedOperationException` because it's not supported with Kueue yet.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    if (!requests.containsKey("cpu")) {
+      requests.put("cpu", new Quantity(cpu));
+    }
+    if (!requests.containsKey("memory")) {
+      requests.put("memory", new Quantity(memoryMiB + "Mi"));
+    }
+
+    if (StringUtils.isNotEmpty(gpuAmount)) {
+      if (!requests.containsKey(gpuVendor)) {
+        requests.put(gpuVendor, new Quantity(gpuAmount));

Review Comment:
   Fixed. The resource name is `<vendor>/gpu` now, e.g., `nvidia.com/gpu`, and `DEFAULT_GPU_VENDOR` is removed. Like Spark, `IllegalArgumentException` is thrown when the amount is set without a vendor.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;

Review Comment:
   Fixed. When `spark.memory.offHeap.enabled` is true, `spark.memory.offHeap.size` is added after parsing it with `JavaUtils.byteStringAsBytes`. `spark.executor.pyspark.memory` is added only for Python applications now.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;

Review Comment:
   Fixed. The non-JVM factor `0.4` is used for Python and R applications, which are classified in the same way as the main resource selection of `SparkAppSubmissionWorker`. Since the driver propagates this factor to executors via `spark.kubernetes.memoryOverheadFactor`, it's applied to the executor PodSet too. The deprecated `spark.kubernetes.memoryOverheadFactor` and `spark.{driver,executor}.minMemoryOverhead` are respected as well.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);

Review Comment:
   Fixed. The default number of executors is `2` now. For dynamic allocation, instead of guessing the bounds, `UnsupportedOperationException` is thrown because it's not supported with Kueue yet.
   



##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSetSpec;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.ClusterSpec;
+import org.apache.spark.k8s.operator.spec.MasterSpec;
+import org.apache.spark.k8s.operator.spec.WorkerSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  public static final String DEFAULT_CORES = "1";
+  public static final String DEFAULT_MEMORY = "1g";
+  public static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  public static final long MIN_MEMORY_OVERHEAD_MIB = 384L;
+  public static final String DEFAULT_GPU_VENDOR = "nvidia.com/gpu";
+
+  private static final Pattern MEMORY_PATTERN =
+      Pattern.compile("^(\\d+)(?:[\\.,](\\d+))?\\s*([a-zA-Z]*)$");
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    String queueName = getQueueName(app);
+    ApplicationSpec appSpec = app.getSpec();
+    Map<String, String> sparkConf =
+        appSpec != null && appSpec.getSparkConf() != null
+            ? appSpec.getSparkConf()
+            : Map.of();
+
+    PodSet driverPodSet = buildDriverPodSet(app, sparkConf);
+    PodSet executorPodSet = buildExecutorPodSet(app, sparkConf);
+
+    List<PodSet> podSets = new ArrayList<>();
+    podSets.add(driverPodSet);
+    podSets.add(executorPodSet);
+
+    boolean active = appSpec == null || !appSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (app.getMetadata().getLabels() != null) {
+      labels.putAll(app.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_APPLICATION_NAME, app.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(app);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(app.getMetadata().getName())
+            .withNamespace(app.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    String queueName = getQueueName(cluster);
+    ClusterSpec clusterSpec = cluster.getSpec();
+
+    List<PodSet> podSets = new ArrayList<>();
+    PodSet masterPodSet = buildMasterPodSet(cluster);
+    if (masterPodSet != null) {
+      podSets.add(masterPodSet);
+    }
+    PodSet workerPodSet = buildWorkerPodSet(cluster);
+    if (workerPodSet != null) {
+      podSets.add(workerPodSet);
+    }
+
+    boolean active = clusterSpec == null || !clusterSpec.isSuspend();
+
+    Map<String, String> labels = new HashMap<>();
+    if (cluster.getMetadata().getLabels() != null) {
+      labels.putAll(cluster.getMetadata().getLabels());
+    }
+    labels.put(Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getMetadata().getName());
+    if (StringUtils.isNotEmpty(queueName)) {
+      labels.put(Constants.LABEL_QUEUE_NAME, queueName);
+    }
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(cluster);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(cluster.getMetadata().getName())
+            .withNamespace(cluster.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(queueName)
+            .active(active)
+            .podSets(podSets)
+            .build());
+
+    return workload;
+  }
+
+  /**
+   * Builds the master PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The master PodSet.
+   */
+  public static PodSet buildMasterPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getMasterSpec() == null) {
+      return null;
+    }
+    MasterSpec masterSpec = cluster.getSpec().getMasterSpec();
+    StatefulSetSpec ssSpec = masterSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    return PodSet.builder()
+        .name(PODSET_MASTER)
+        .count(count)
+        .template(template)
+        .build();
+  }
+
+  /**
+   * Builds the worker PodSet for a SparkCluster.
+   *
+   * @param cluster The SparkCluster.
+   * @return The worker PodSet.
+   */
+  public static PodSet buildWorkerPodSet(final SparkCluster cluster) {
+    if (cluster == null
+        || cluster.getSpec() == null
+        || cluster.getSpec().getWorkerSpec() == null) {
+      return null;
+    }
+    WorkerSpec workerSpec = cluster.getSpec().getWorkerSpec();
+    StatefulSetSpec ssSpec = workerSpec.getStatefulSetSpec();
+    int count = ssSpec != null && ssSpec.getReplicas() != null ? ssSpec.getReplicas() : 1;
+    Integer minCount = null;
+
+    if (workerSpec.getHorizontalPodAutoscalerSpec() != null) {
+      minCount = workerSpec.getHorizontalPodAutoscalerSpec().getMinReplicas();
+      count = workerSpec.getHorizontalPodAutoscalerSpec().getMaxReplicas();
+    }
+
+    PodTemplateSpec template =
+        ssSpec != null && ssSpec.getTemplate() != null
+            ? ReconcilerUtils.clone(ssSpec.getTemplate())
+            : new PodTemplateSpec();
+
+    PodSet.PodSetBuilder builder =
+        PodSet.builder()
+            .name(PODSET_WORKER)
+            .count(count)
+            .template(template);
+    if (minCount != null) {
+      builder.minCount(minCount);
+    }
+    return builder.build();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkApplication metadata.
+   *
+   * @param app The SparkApplication.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkApplication app) {
+    return getQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Extracts the Kueue queue name from the SparkCluster metadata.
+   *
+   * @param cluster The SparkCluster.
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final SparkCluster cluster) {
+    return getQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  /**
+   * Checks whether the SparkApplication is configured to use Kueue.
+   *
+   * @param app The SparkApplication.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkApplication app) {
+    return hasQueueName((HasMetadata) app);
+  }
+
+  /**
+   * Checks whether the SparkCluster is configured to use Kueue.
+   *
+   * @param cluster The SparkCluster.
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final SparkCluster cluster) {
+    return hasQueueName((HasMetadata) cluster);
+  }
+
+  /**
+   * Builds the driver PodSet.
+   */
+  public static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getDriverSpec() != null
+        && app.getSpec().getDriverSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getDriverSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.driver.request.cores",
+            sparkConf.getOrDefault("spark.driver.cores", DEFAULT_CORES));
+    long memoryMiB = calculateDriverMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.driver.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.driver.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(templateSpec, "spark-driver", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    return PodSet.builder()
+        .name(PODSET_DRIVER)
+        .count(1)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Builds the executor PodSet.
+   */
+  public static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    PodTemplateSpec templateSpec = null;
+    if (app.getSpec() != null
+        && app.getSpec().getExecutorSpec() != null
+        && app.getSpec().getExecutorSpec().getPodTemplateSpec() != null) {
+      templateSpec = ReconcilerUtils.clone(app.getSpec().getExecutorSpec().getPodTemplateSpec());
+    }
+    if (templateSpec == null) {
+      templateSpec = new PodTemplateSpecBuilder().build();
+    }
+    ensurePodSpec(templateSpec);
+
+    String cpu =
+        sparkConf.getOrDefault(
+            "spark.kubernetes.executor.request.cores",
+            sparkConf.getOrDefault("spark.executor.cores", DEFAULT_CORES));
+    long memoryMiB = calculateExecutorMemoryMiB(sparkConf);
+    String gpuAmount = sparkConf.get("spark.executor.resource.gpu.amount");
+    String gpuVendor =
+        sparkConf.getOrDefault("spark.executor.resource.gpu.vendor", DEFAULT_GPU_VENDOR);
+
+    decorateTemplateResources(
+        templateSpec, "spark-executor", cpu, memoryMiB, gpuAmount, gpuVendor);
+
+    boolean dynamicAllocation =
+        Boolean.parseBoolean(sparkConf.getOrDefault("spark.dynamicAllocation.enabled", "false"));
+    int count;
+    Integer minCount = null;
+    if (dynamicAllocation) {
+      int maxExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.maxExecutors"),
+              parseInt(sparkConf.get("spark.executor.instances"), 1));
+      int minExecutors =
+          parseInt(
+              sparkConf.get("spark.dynamicAllocation.minExecutors"),
+              parseInt(sparkConf.get("spark.dynamicAllocation.initialExecutors"), 0));
+      count = maxExecutors;
+      minCount = minExecutors;
+    } else {
+      count = parseInt(sparkConf.get("spark.executor.instances"), 1);
+    }
+
+    return PodSet.builder()
+        .name(PODSET_EXECUTOR)
+        .count(count)
+        .minCount(minCount)
+        .template(templateSpec)
+        .build();
+  }
+
+  /**
+   * Calculates total driver memory in MiB including overhead.
+   */
+  public static long calculateDriverMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.driver.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.driver.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.driver.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.driver.memoryOverheadFactor"), DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    return memMiB + overheadMiB;
+  }
+
+  /**
+   * Calculates total executor memory in MiB including overhead and optional PySpark memory.
+   */
+  public static long calculateExecutorMemoryMiB(final Map<String, String> sparkConf) {
+    String memStr = sparkConf.getOrDefault("spark.executor.memory", DEFAULT_MEMORY);
+    long memMiB = parseMemoryToMiB(memStr);
+    long overheadMiB;
+    if (sparkConf.containsKey("spark.executor.memoryOverhead")) {
+      overheadMiB = parseMemoryToMiB(sparkConf.get("spark.executor.memoryOverhead"));
+    } else {
+      double factor =
+          parseDouble(
+              sparkConf.get("spark.executor.memoryOverheadFactor"),
+              DEFAULT_MEMORY_OVERHEAD_FACTOR);
+      overheadMiB = Math.max((long) Math.ceil(memMiB * factor), MIN_MEMORY_OVERHEAD_MIB);
+    }
+    long total = memMiB + overheadMiB;
+    if (sparkConf.containsKey("spark.executor.pyspark.memory")) {
+      total += parseMemoryToMiB(sparkConf.get("spark.executor.pyspark.memory"));
+    }
+    return total;
+  }
+
+  /**
+   * Parses memory string like '1g', '512m', '2048Mi' to MiB.
+   */
+  public static long parseMemoryToMiB(final String memoryStr) {
+    if (StringUtils.isEmpty(memoryStr)) {
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    Matcher matcher = MEMORY_PATTERN.matcher(memoryStr.trim());
+    if (!matcher.matches()) {
+      log.warn("Unable to parse memory string '{}', using default {}", memoryStr, DEFAULT_MEMORY);
+      return parseMemoryToMiB(DEFAULT_MEMORY);
+    }
+    long integerPart = Long.parseLong(matcher.group(1));
+    String fractionPart = matcher.group(2);
+    String unit = matcher.group(3).toLowerCase(Locale.ROOT);
+
+    double value = integerPart;
+    if (fractionPart != null && !fractionPart.isEmpty()) {
+      value += Double.parseDouble("0." + fractionPart);
+    }
+
+    return switch (unit) {
+      case "b" -> (long) Math.ceil(value / (1024.0 * 1024.0));
+      case "k", "kb", "ki", "kib" -> (long) Math.ceil(value / 1024.0);
+      case "m", "mb", "mi", "mib", "" -> (long) Math.ceil(value);
+      case "g", "gb", "gi", "gib" -> (long) Math.ceil(value * 1024.0);
+      case "t", "tb", "ti", "tib" -> (long) Math.ceil(value * 1024.0 * 1024.0);
+      case "p", "pb", "pi", "pib" -> (long) Math.ceil(value * 1024.0 * 1024.0 * 1024.0);
+      default -> (long) Math.ceil(value);
+    };
+  }
+
+  private static void ensurePodSpec(final PodTemplateSpec templateSpec) {
+    if (templateSpec.getSpec() == null) {
+      templateSpec.setSpec(new PodSpecBuilder().build());
+    }
+    if (templateSpec.getSpec().getContainers() == null) {
+      templateSpec.getSpec().setContainers(new ArrayList<>());
+    }
+  }
+
+  private static void decorateTemplateResources(
+      final PodTemplateSpec templateSpec,
+      final String defaultContainerName,
+      final String cpu,
+      final long memoryMiB,
+      final String gpuAmount,
+      final String gpuVendor) {
+    PodSpec podSpec = templateSpec.getSpec();
+    Container container;
+    if (podSpec.getContainers().isEmpty()) {
+      container = new ContainerBuilder().withName(defaultContainerName).build();
+      podSpec.getContainers().add(container);
+    } else {
+      container = podSpec.getContainers().get(0);
+    }
+
+    ResourceRequirements resources = container.getResources();
+    if (resources == null) {
+      resources = new ResourceRequirementsBuilder().build();
+      container.setResources(resources);
+    }
+
+    Map<String, Quantity> requests = resources.getRequests();
+    if (requests == null) {
+      requests = new HashMap<>();
+      resources.setRequests(requests);
+    }
+
+    if (!requests.containsKey("cpu")) {

Review Comment:
   Fixed. The `cpu`, `memory`, and GPU values of the pod template are overwritten like Spark. `testBuildWorkloadWithGpuAndTemplateSpec` is updated accordingly.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5669312906

   Merged to main


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for SparkApplication and SparkCluster [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5666342104

   Thank you for the review, @peter-toth. I addressed the comments in 0ddd85a. For finding 7, the PR description is updated to match the code.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#discussion_r4008176560


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/kueue/KueueWorkloadFactory.java:
##########
@@ -0,0 +1,438 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://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 org.apache.spark.k8s.operator.kueue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.apps.StatefulSet;
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.spark.k8s.operator.Constants;
+import org.apache.spark.k8s.operator.SparkApplication;
+import org.apache.spark.k8s.operator.SparkCluster;
+import org.apache.spark.k8s.operator.SparkClusterResourceSpec;
+import org.apache.spark.k8s.operator.SparkClusterSubmissionWorker;
+import org.apache.spark.k8s.operator.kueue.v1beta1.PodSet;
+import org.apache.spark.k8s.operator.kueue.v1beta1.Workload;
+import org.apache.spark.k8s.operator.kueue.v1beta1.WorkloadSpec;
+import org.apache.spark.k8s.operator.reconciler.SparkClusterResourceSpecFactory;
+import org.apache.spark.k8s.operator.spec.ApplicationSpec;
+import org.apache.spark.k8s.operator.spec.BaseApplicationTemplateSpec;
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
+import org.apache.spark.k8s.operator.utils.StringUtils;
+import org.apache.spark.network.util.JavaUtils;
+
+/**
+ * Factory for creating Kueue Workload resources from Spark custom resources.
+ * This factory supports both {@link SparkApplication} (driver and executor pod sets)
+ * and {@link SparkCluster} (master and worker pod sets).
+ */
+@Slf4j
+@SuppressWarnings("PMD.GodClass")
+public final class KueueWorkloadFactory {
+
+  public static final String PODSET_DRIVER = "driver";
+  public static final String PODSET_EXECUTOR = "executor";
+  public static final String PODSET_MASTER = "master";
+  public static final String PODSET_WORKER = "worker";
+
+  private static final String DEFAULT_CORES = "1";
+  private static final String DEFAULT_MEMORY = "1g";
+  private static final String DEFAULT_MIN_MEMORY_OVERHEAD = "384m";
+  private static final double DEFAULT_MEMORY_OVERHEAD_FACTOR = 0.10;
+  private static final double NON_JVM_MEMORY_OVERHEAD_FACTOR = 0.40;
+  private static final int DEFAULT_EXECUTOR_INSTANCES = 2;
+
+  private static final String NODE_SELECTOR_PREFIX = "spark.kubernetes.node.selector.";
+
+  private KueueWorkloadFactory() {}
+
+  /**
+   * Builds a Kueue Workload from a SparkApplication resource.
+   *
+   * @param app The SparkApplication.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkApplication app) {
+    ApplicationSpec spec = app.getSpec();
+    Map<String, String> sparkConf = spec.getSparkConf();
+    if ("true".equalsIgnoreCase(sparkConf.get("spark.dynamicAllocation.enabled"))) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkApplication with dynamic allocation "
+              + "(spark.dynamicAllocation.enabled=true) yet.");
+    }
+    // Like Spark's KubernetesClusterManager, `local[*]` runs the driver only without executors.
+    boolean driverOnly =
+        sparkConf.getOrDefault("spark.kubernetes.driver.master", "").startsWith("local");
+    List<PodSet> podSets =
+        driverOnly
+            ? List.of(buildDriverPodSet(app, sparkConf))
+            : List.of(buildDriverPodSet(app, sparkConf), buildExecutorPodSet(app, sparkConf));
+    return buildWorkload(app, Constants.LABEL_SPARK_APPLICATION_NAME, spec.isSuspend(), podSets);
+  }
+
+  /**
+   * Builds a Kueue Workload from a SparkCluster resource.
+   *
+   * @param cluster The SparkCluster.
+   * @return The constructed Kueue Workload.
+   */
+  public static Workload buildWorkload(final SparkCluster cluster) {
+    // Use the same StatefulSets which the operator creates for the cluster.
+    SparkClusterResourceSpec resourceSpec =
+        SparkClusterResourceSpecFactory.buildResourceSpec(
+            cluster, new SparkClusterSubmissionWorker());
+    if (resourceSpec.getHorizontalPodAutoscaler().isPresent()) {
+      throw new UnsupportedOperationException(
+          "Kueue does not support SparkCluster with HorizontalPodAutoscaler "
+              + "(minWorkers < maxWorkers) yet.");
+    }
+    List<PodSet> podSets =
+        List.of(
+            buildPodSet(PODSET_MASTER, resourceSpec.getMasterStatefulSet()),
+            buildPodSet(PODSET_WORKER, resourceSpec.getWorkerStatefulSet()));
+    return buildWorkload(
+        cluster, Constants.LABEL_SPARK_CLUSTER_NAME, cluster.getSpec().isSuspend(), podSets);
+  }
+
+  private static Workload buildWorkload(
+      final HasMetadata owner,
+      final String nameLabelKey,
+      final boolean suspend,
+      final List<PodSet> podSets) {
+    Map<String, String> labels = new HashMap<>();
+    if (owner.getMetadata().getLabels() != null) {
+      labels.putAll(owner.getMetadata().getLabels());
+    }
+    labels.put(nameLabelKey, owner.getMetadata().getName());
+
+    OwnerReference ownerReference = ModelUtils.buildOwnerReferenceTo(owner);
+    ownerReference.setController(true);
+
+    Workload workload = new Workload();
+    workload.setMetadata(
+        new ObjectMetaBuilder()
+            .withName(getWorkloadName(owner))
+            .withNamespace(owner.getMetadata().getNamespace())
+            .withLabels(labels)
+            .withOwnerReferences(ownerReference)
+            .build());
+    workload.setSpec(
+        WorkloadSpec.builder()
+            .queueName(getQueueName(owner))
+            .active(!suspend)
+            .podSets(podSets)
+            .build());
+    return workload;
+  }
+
+  private static PodSet buildPodSet(final String name, final StatefulSet statefulSet) {
+    return PodSet.builder()
+        .name(name)
+        .count(statefulSet.getSpec().getReplicas())
+        .template(statefulSet.getSpec().getTemplate())
+        .build();
+  }
+
+  /**
+   * Returns the Workload name prefixed with the lower-cased kind of the owner resource, like Kueue
+   * built-in integrations, to avoid name collisions between SparkApplication and SparkCluster.
+   */
+  private static String getWorkloadName(final HasMetadata resource) {
+    return resource.getKind().toLowerCase(Locale.ROOT) + "-" + resource.getMetadata().getName();
+  }
+
+  /**
+   * Extracts the Kueue queue name from the resource metadata labels.
+   *
+   * @param resource The Kubernetes resource (e.g. SparkApplication or SparkCluster).
+   * @return The queue name, or null if not found.
+   */
+  public static String getQueueName(final HasMetadata resource) {
+    if (resource != null
+        && resource.getMetadata() != null
+        && resource.getMetadata().getLabels() != null) {
+      String queue = resource.getMetadata().getLabels().get(Constants.LABEL_QUEUE_NAME);
+      if (StringUtils.isNotEmpty(queue)) {
+        return queue;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Checks whether the resource is configured to use Kueue.
+   *
+   * @param resource The Kubernetes resource (SparkApplication or SparkCluster).
+   * @return true if a Kueue queue name is specified.
+   */
+  public static boolean hasQueueName(final HasMetadata resource) {
+    return StringUtils.isNotEmpty(getQueueName(resource));
+  }
+
+  static PodSet buildDriverPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_DRIVER,
+        1,
+        spec.getDriverSpec(),
+        sparkConf,
+        Constants.DRIVER_SPARK_CONTAINER_PROP_KEY,
+        calculateDriverMemoryMiB(sparkConf, isNonJvmApp(spec)));
+  }
+
+  static PodSet buildExecutorPodSet(
+      final SparkApplication app, final Map<String, String> sparkConf) {
+    ApplicationSpec spec = app.getSpec();
+    return buildPodSet(
+        PODSET_EXECUTOR,
+        parseInt(sparkConf.get("spark.executor.instances"), DEFAULT_EXECUTOR_INSTANCES),
+        spec.getExecutorSpec(),
+        sparkConf,
+        Constants.EXECUTOR_SPARK_CONTAINER_PROP_KEY,
+        calculateExecutorMemoryMiB(sparkConf, isNonJvmApp(spec), isPythonApp(spec)));
+  }
+
+  /**
+   * Builds a PodSet for a Spark role (`driver` or `executor`) from the pod template of the
+   * SparkApplication and the resource configurations in the same way as Spark does.
+   */
+  private static PodSet buildPodSet(
+      final String role,
+      final int count,
+      final BaseApplicationTemplateSpec roleSpec,
+      final Map<String, String> sparkConf,
+      final String containerNameKey,
+      final long memoryMiB) {
+    PodTemplateSpec templateSpec =
+        roleSpec != null && roleSpec.getPodTemplateSpec() != null
+            ? ReconcilerUtils.clone(roleSpec.getPodTemplateSpec())
+            : new PodTemplateSpecBuilder().build();

Review Comment:
   **Finding 23.** A pod template supplied as a file never reaches the PodSet.
   
   `docs/spark_custom_resources.md:85-99` lists two supported ways to set a pod template, and option 2 is `spark.kubernetes.[driver/executor].podTemplateFile`. Option 1 wins when both are set, because `SparkAppResourceSpecFactory.overrideDependencyConf` writes that key only when `ModelUtils.overrideDriverTemplateEnabled` holds, i.e. when `driverSpec.podTemplateSpec != null` (`SparkAppResourceSpecFactory.java:96-103`, `ModelUtils.java:132-136`). With option 2 alone the user's value in `spec.sparkConf` is passed through untouched (`SparkAppSubmissionWorker.java:135-143`), Spark fetches the file and builds the pod from it, and this ternary falls to the empty-template branch.
   
   Measured on `a3badb2` with only the two `podTemplateFile` keys in `sparkConf`:
   
   ```
   podSet=driver   count=1  nodeSelector={}  tolerations=[]  initContainers=[]
                   containers=[spark-kubernetes-driver: requests={cpu=1, memory=1408Mi}]
   podSet=executor count=2  nodeSelector={}  tolerations=[]  initContainers=[]
                   containers=[spark-kubernetes-executor: requests={cpu=1, memory=1408Mi}]
   ```
   
   Two consequences, both of the kind finding 13 was about. Kueue matches a `ResourceFlavor` against an empty `nodeSelector`, so it can admit the Workload into a flavor whose nodes the real pod cannot land on, and the quota stays consumed while the pod sits Pending. Sidecar and init containers declared in the file are also absent from the sum Kueue computes for the PodSet, so the reservation is smaller than the pod.
   
   The operator could read the file. The docs already require it to have access to the remote location in order to create the driver. That is a bigger change than this PR, though, so the in-scope fix is to reject the configuration the way dynamic allocation and the HPA already are:
   
   ```java
       checkNoPodTemplateFile(
           sparkConf, Constants.DRIVER_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getDriverSpec());
       if (!driverOnly) {
         checkNoPodTemplateFile(
             sparkConf, Constants.EXECUTOR_SPARK_TEMPLATE_FILE_PROP_KEY, spec.getExecutorSpec());
       }
   ```
   
   ```java
     /**
      * A pod template file is fetched by Spark, not by the operator, so its node selectors,
      * tolerations and extra containers cannot be reflected in the PodSet template.
      */
     private static void checkNoPodTemplateFile(
         final Map<String, String> sparkConf,
         final String templateFileKey,
         final BaseApplicationTemplateSpec roleSpec) {
       if (sparkConf.containsKey(templateFileKey)
           && (roleSpec == null || roleSpec.getPodTemplateSpec() == null)) {
         throw new UnsupportedOperationException(
             "Kueue does not support " + templateFileKey + " yet. Set the pod template in the "
                 + "SparkApplication spec instead.");
       }
     }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59490] Add `KueueWorkloadFactory` for `SparkApplication` and `SparkCluster` [spark-kubernetes-operator]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #823:
URL: https://github.com/apache/spark-kubernetes-operator/pull/823#issuecomment-5669183037

   Thank you, @peter-toth. Finding 25 is addressed in ab7cfb8. For finding 24, I deferred both `priorityClassName` and `priority` to the wiring PR because Kueue's CRD requires them to be set together; the PR description has a new "Deferred Features" note for it.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org