Skip to content

Commit 9fb8346

Browse files
author
Marcelo Vanzin
committed
[SPARK-24894][k8s] Make sure valid host names are created for executors.
Since the host name is derived from the app name, which can contain arbitrary characters, it needs to be sanitized so that only valid characters are allowed. On top of that, take extra care that truncation doesn't leave characters that are valid except at the start of a host name.
1 parent a829234 commit 9fb8346

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/features/BasicExecutorFeatureStep.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ private[spark] class BasicExecutorFeatureStep(
8383
// name as the hostname. This preserves uniqueness since the end of name contains
8484
// executorId
8585
val hostname = name.substring(Math.max(0, name.length - 63))
86+
// Remove non-word characters from the start of the hostname
87+
.replaceAll("^[^\\w]+", "")
88+
// Replace dangerous characters in the remaining string with a safe alternative.
89+
.replaceAll("[^\\w-]+", "_")
90+
8691
val executorMemoryQuantity = new QuantityBuilder(false)
8792
.withAmount(s"${executorMemoryTotal}Mi")
8893
.build()

resource-managers/kubernetes/core/src/test/scala/org/apache/spark/deploy/k8s/features/BasicExecutorFeatureStepSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.nio.file.Files
2222

2323
import scala.collection.JavaConverters._
2424

25+
import com.google.common.net.InternetDomainName
2526
import io.fabric8.kubernetes.api.model._
2627
import org.scalatest.BeforeAndAfter
2728

@@ -124,6 +125,16 @@ class BasicExecutorFeatureStepSuite extends SparkFunSuite with BeforeAndAfter {
124125
assert(step.configurePod(SparkPod.initialPod()).pod.getSpec.getHostname.length === 63)
125126
}
126127

128+
test("hostname truncation generates valid host names") {
129+
val invalidPrefix = "abcdef-*_/[]{}+==.,;'\"-----------------------------------------------"
130+
131+
baseConf.set(KUBERNETES_EXECUTOR_POD_NAME_PREFIX, invalidPrefix)
132+
val step = new BasicExecutorFeatureStep(newExecutorConf(), new SecurityManager(baseConf))
133+
val hostname = step.configurePod(SparkPod.initialPod()).pod.getSpec().getHostname()
134+
assert(hostname.length <= 63)
135+
assert(InternetDomainName.isValid(hostname))
136+
}
137+
127138
test("classpath and extra java options get translated into environment variables") {
128139
baseConf.set(config.EXECUTOR_JAVA_OPTIONS, "foo=bar")
129140
baseConf.set(config.EXECUTOR_CLASS_PATH, "bar=baz")

0 commit comments

Comments
 (0)