Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static org.apache.spark.k8s.operator.Constants.*;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

import scala.Tuple2;
Expand Down Expand Up @@ -117,8 +117,13 @@ private static Service buildMasterService(
.endMetadata()
.withNewSpecLike(serviceSpec)
.withClusterIP("None")
.withClusterIP("None")
.withSelector(
Collections.singletonMap(LABEL_SPARK_ROLE_NAME, LABEL_SPARK_ROLE_MASTER_VALUE))
Map.of(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why we need to use Map.of instead of Collections.singletonMap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dongjoon-hyun, I'm afraid, I'm not quire sure what you are suggesting. Collections.singletonMap only creates a map with a single element.
I changed my initial commit to below approach. Do you think it would be more suitable?

        .addToSelector(Collections.singletonMap(LABEL_SPARK_CLUSTER_NAME, name))
        .addToSelector(
            Collections.singletonMap(LABEL_SPARK_ROLE_NAME, LABEL_SPARK_ROLE_MASTER_VALUE))

LABEL_SPARK_CLUSTER_NAME,
name,
LABEL_SPARK_ROLE_NAME,
LABEL_SPARK_ROLE_MASTER_VALUE))
.addNewPort()
.withName("web")
.withPort(8080)
Expand Down Expand Up @@ -150,7 +155,11 @@ private static Service buildWorkerService(
.withNewSpecLike(serviceSpec)
.withClusterIP("None")
.withSelector(
Collections.singletonMap(LABEL_SPARK_ROLE_NAME, LABEL_SPARK_ROLE_WORKER_VALUE))
Map.of(
LABEL_SPARK_CLUSTER_NAME,
name,
LABEL_SPARK_ROLE_NAME,
LABEL_SPARK_ROLE_WORKER_VALUE))
.addNewPort()
.withName("web")
.withPort(8081)
Expand Down Expand Up @@ -186,6 +195,7 @@ private static StatefulSet buildMasterStatefulSet(
.editOrNewTemplate()
.editOrNewMetadata()
.addToLabels(LABEL_SPARK_ROLE_NAME, LABEL_SPARK_ROLE_MASTER_VALUE)
.addToLabels(LABEL_SPARK_CLUSTER_NAME, name)
.addToLabels(LABEL_SPARK_VERSION_NAME, version)
.endMetadata()
.editOrNewSpec()
Expand Down Expand Up @@ -253,6 +263,7 @@ private static StatefulSet buildWorkerStatefulSet(
.editOrNewTemplate()
.editOrNewMetadata()
.addToLabels(LABEL_SPARK_ROLE_NAME, LABEL_SPARK_ROLE_WORKER_VALUE)
.addToLabels(LABEL_SPARK_CLUSTER_NAME, name)
.addToLabels(LABEL_SPARK_VERSION_NAME, version)
.endMetadata()
.editOrNewSpec()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

package org.apache.spark.k8s.operator;

import static org.apache.spark.k8s.operator.Constants.LABEL_SPARK_VERSION_NAME;
import static org.apache.spark.k8s.operator.Constants.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Map;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.ObjectMeta;
Expand Down Expand Up @@ -129,6 +130,13 @@ void testWorkerServiceWithTemplate() {
assertEquals("bar", service1.getMetadata().getLabels().get("foo"));
assertEquals("4.0.0", service1.getMetadata().getLabels().get(LABEL_SPARK_VERSION_NAME));
assertEquals("foo", service1.getSpec().getExternalName());
assertEquals(
Map.of(
LABEL_SPARK_CLUSTER_NAME,
"cluster-name",
LABEL_SPARK_ROLE_NAME,
LABEL_SPARK_ROLE_WORKER_VALUE),
service1.getSpec().getSelector());
}

@Test
Expand All @@ -151,6 +159,13 @@ void testMasterServiceWithTemplate() {
assertEquals("bar", service1.getMetadata().getLabels().get("foo"));
assertEquals("4.0.0", service1.getMetadata().getLabels().get(LABEL_SPARK_VERSION_NAME));
assertEquals("foo", service1.getSpec().getExternalName());
assertEquals(
Map.of(
LABEL_SPARK_CLUSTER_NAME,
"cluster-name",
LABEL_SPARK_ROLE_NAME,
LABEL_SPARK_ROLE_MASTER_VALUE),
service1.getSpec().getSelector());
}

@Test
Expand All @@ -172,6 +187,17 @@ void testMasterStatefulSet() {
SparkClusterResourceSpec spec2 = new SparkClusterResourceSpec(cluster, sparkConf);
StatefulSet statefulSet2 = spec2.getMasterStatefulSet();
assertEquals("other-namespace", statefulSet2.getMetadata().getNamespace());
assertEquals(
"cluster-name",
statefulSet2
.getSpec()
.getTemplate()
.getMetadata()
.getLabels()
.get(LABEL_SPARK_CLUSTER_NAME));
assertEquals(
LABEL_SPARK_ROLE_MASTER_VALUE,
statefulSet2.getSpec().getTemplate().getMetadata().getLabels().get(LABEL_SPARK_ROLE_NAME));
}

@Test
Expand Down Expand Up @@ -236,6 +262,17 @@ void testWorkerStatefulSet() {
SparkClusterResourceSpec spec2 = new SparkClusterResourceSpec(cluster, sparkConf);
StatefulSet statefulSet2 = spec2.getWorkerStatefulSet();
assertEquals("other-namespace", statefulSet2.getMetadata().getNamespace());
assertEquals(
"cluster-name",
statefulSet2
.getSpec()
.getTemplate()
.getMetadata()
.getLabels()
.get(LABEL_SPARK_CLUSTER_NAME));
assertEquals(
LABEL_SPARK_ROLE_WORKER_VALUE,
statefulSet2.getSpec().getTemplate().getMetadata().getLabels().get(LABEL_SPARK_ROLE_NAME));
}

@Test
Expand Down
Loading