You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2026/04/09 06:19:24 UTC
(spark-kubernetes-operator) branch main updated: [SPARK-56408] Introduce `OwnerResourceDecorator` and use it instead `(Driver|Cluster)Decorator`
This is an automated email from the ASF dual-hosted git repository.
dongjoon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-kubernetes-operator.git
The following commit(s) were added to refs/heads/main by this push:
new fd055f1 [SPARK-56408] Introduce `OwnerResourceDecorator` and use it instead `(Driver|Cluster)Decorator`
fd055f1 is described below
commit fd055f13c4c37f1272051ffb2f279121f5040b26
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Wed Apr 8 23:19:17 2026 -0700
[SPARK-56408] Introduce `OwnerResourceDecorator` and use it instead `(Driver|Cluster)Decorator`
### What changes were proposed in this pull request?
This PR refactors `DriverDecorator` and `ClusterDecorator` into a unified `OwnerResourceDecorator` class. Both decorators had nearly identical `decorate()` logic — adding owner references, labels, and namespace to Kubernetes resources — differing only in the owner type and label generation function.
**Before:**
- `DriverDecorator(SparkApplication)` → used `sparkAppResourceLabels(app)`
- `ClusterDecorator(SparkCluster)` → used `sparkClusterResourceLabels(cluster)`
**After:**
- `OwnerResourceDecorator(HasMetadata owner, Map<String, String> labels)`
### Why are the changes needed?
The two decorator classes contained duplicated code that only differed in the type of the owner resource and the label map. Consolidating them into a single class reduces code duplication and simplifies future maintenance.
### 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: Claude Code (claude-opus-4-6)
Closes #623 from dongjoon-hyun/SPARK-56408.
Authored-by: Dongjoon Hyun <do...@apache.org>
Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
.../k8s/operator/decorators/DriverDecorator.java | 59 ----------------------
...rDecorator.java => OwnerResourceDecorator.java} | 28 +++++-----
.../reconciler/SparkAppResourceSpecFactory.java | 4 +-
.../SparkClusterResourceSpecFactory.java | 7 ++-
4 files changed, 19 insertions(+), 79 deletions(-)
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/DriverDecorator.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/DriverDecorator.java
deleted file mode 100644
index 719f9d9..0000000
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/DriverDecorator.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.decorators;
-
-import static org.apache.spark.k8s.operator.utils.ModelUtils.buildOwnerReferenceTo;
-import static org.apache.spark.k8s.operator.utils.Utils.sparkAppResourceLabels;
-
-import io.fabric8.kubernetes.api.model.HasMetadata;
-import io.fabric8.kubernetes.api.model.ObjectMeta;
-import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
-import lombok.RequiredArgsConstructor;
-
-import org.apache.spark.k8s.operator.SparkApplication;
-
-/**
- * Decorates driver (pod) to make sure its metadata matches event source Also adds owner reference
- * to the owner SparkApplication for garbage collection
- */
-@RequiredArgsConstructor
-public class DriverDecorator implements ResourceDecorator {
-
- private final SparkApplication app;
-
- /**
- * Decorates a Kubernetes resource with owner references and labels from the SparkApplication.
- *
- * @param resource The resource to decorate.
- * @param <T> The type of the resource, extending HasMetadata.
- * @return The decorated resource.
- */
- @Override
- public <T extends HasMetadata> T decorate(T resource) {
- ObjectMeta metaData =
- new ObjectMetaBuilder(resource.getMetadata())
- .addToOwnerReferences(buildOwnerReferenceTo(app))
- .addToLabels(sparkAppResourceLabels(app))
- .withNamespace(app.getMetadata().getNamespace())
- .build();
- resource.setMetadata(metaData);
- return resource;
- }
-}
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/ClusterDecorator.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/OwnerResourceDecorator.java
similarity index 65%
rename from spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/ClusterDecorator.java
rename to spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/OwnerResourceDecorator.java
index 09d075b..c877f2a 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/ClusterDecorator.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/OwnerResourceDecorator.java
@@ -20,35 +20,31 @@
package org.apache.spark.k8s.operator.decorators;
import static org.apache.spark.k8s.operator.utils.ModelUtils.buildOwnerReferenceTo;
-import static org.apache.spark.k8s.operator.utils.Utils.sparkClusterResourceLabels;
+
+import java.util.Map;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import lombok.RequiredArgsConstructor;
-import org.apache.spark.k8s.operator.SparkCluster;
-
-/** Decorates Spark cluster resources like statefulsets. */
+/**
+ * Decorates Kubernetes resources with owner references and labels from the owner resource. Adds
+ * owner reference to the owner resource for garbage collection.
+ */
@RequiredArgsConstructor
-public class ClusterDecorator implements ResourceDecorator {
+public class OwnerResourceDecorator implements ResourceDecorator {
- private final SparkCluster cluster;
+ private final HasMetadata owner;
+ private final Map<String, String> labels;
- /**
- * Decorates a Kubernetes resource with owner references and labels from the SparkCluster.
- *
- * @param resource The resource to decorate.
- * @param <T> The type of the resource, extending HasMetadata.
- * @return The decorated resource.
- */
@Override
public <T extends HasMetadata> T decorate(T resource) {
ObjectMeta metaData =
new ObjectMetaBuilder(resource.getMetadata())
- .addToOwnerReferences(buildOwnerReferenceTo(cluster))
- .addToLabels(sparkClusterResourceLabels(cluster))
- .withNamespace(cluster.getMetadata().getNamespace())
+ .addToOwnerReferences(buildOwnerReferenceTo(owner))
+ .addToLabels(labels)
+ .withNamespace(owner.getMetadata().getNamespace())
.build();
resource.setMetadata(metaData);
return resource;
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkAppResourceSpecFactory.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkAppResourceSpecFactory.java
index e137faa..735d38c 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkAppResourceSpecFactory.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkAppResourceSpecFactory.java
@@ -41,7 +41,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.spark.k8s.operator.SparkAppResourceSpec;
import org.apache.spark.k8s.operator.SparkAppSubmissionWorker;
import org.apache.spark.k8s.operator.SparkApplication;
-import org.apache.spark.k8s.operator.decorators.DriverDecorator;
+import org.apache.spark.k8s.operator.decorators.OwnerResourceDecorator;
import org.apache.spark.k8s.operator.utils.ModelUtils;
/** Factory for creating SparkAppResourceSpec objects. */
@@ -65,7 +65,7 @@ public final class SparkAppResourceSpecFactory {
Map<String, String> confOverrides = overrideDependencyConf(app);
SparkAppResourceSpec resourceSpec = worker.getResourceSpec(app, client, confOverrides);
cleanUpTempResourcesForApp(app, confOverrides);
- DriverDecorator decorator = new DriverDecorator(app);
+ OwnerResourceDecorator decorator = new OwnerResourceDecorator(app, sparkAppResourceLabels(app));
decorator.decorate(resourceSpec.getConfiguredPod());
return resourceSpec;
}
diff --git a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkClusterResourceSpecFactory.java b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkClusterResourceSpecFactory.java
index 161234c..7ac8f49 100644
--- a/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkClusterResourceSpecFactory.java
+++ b/spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/SparkClusterResourceSpecFactory.java
@@ -19,6 +19,8 @@
package org.apache.spark.k8s.operator.reconciler;
+import static org.apache.spark.k8s.operator.utils.Utils.sparkClusterResourceLabels;
+
import java.util.HashMap;
import java.util.Map;
@@ -27,7 +29,7 @@ import lombok.extern.slf4j.Slf4j;
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.decorators.ClusterDecorator;
+import org.apache.spark.k8s.operator.decorators.OwnerResourceDecorator;
/** Factory for creating SparkClusterResourceSpec objects. */
@Slf4j
@@ -46,7 +48,8 @@ public final class SparkClusterResourceSpecFactory {
final SparkCluster cluster, final SparkClusterSubmissionWorker worker) {
Map<String, String> confOverrides = new HashMap<>();
SparkClusterResourceSpec spec = worker.getResourceSpec(cluster, confOverrides);
- ClusterDecorator decorator = new ClusterDecorator(cluster);
+ OwnerResourceDecorator decorator =
+ new OwnerResourceDecorator(cluster, sparkClusterResourceLabels(cluster));
decorator.decorate(spec.getMasterService());
decorator.decorate(spec.getWorkerService());
decorator.decorate(spec.getMasterStatefulSet());
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org