Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -250,12 +250,17 @@ private[spark] object KubernetesConf {
s"spark-${UUID.randomUUID().toString.replaceAll("-", "")}"

def getResourceNamePrefix(appName: String): String = {
// Most resource types require a name that can be used as a DNS subdomain name, the name must
// contain only lowercase alphanumeric characters, '-' or '.', and start with an alphanumeric
// character.
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
val id = KubernetesUtils.uniqueID()
s"$appName-$id"
.trim
.toLowerCase(Locale.ROOT)
.replaceAll("[^a-z0-9\\-]", "-")
.replaceAll("-+", "-")
.stripPrefix("-")
}

def getAppNameLabel(appName: String): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,12 @@ class KubernetesConfSuite extends SparkFunSuite {
assert(KubernetesConf.getAppNameLabel("a" * 62 + "-aaa") === "a" * 62)
assert(KubernetesConf.getAppNameLabel("-" + "a" * 63) === "a" * 62)
}

test("SPARK-40997: K8s resource name prefix should start w/ alphanumeric") {
// scalastyle:off nonascii
Seq("-hello-", "测试").foreach { appName =>
// scalastyle:on nonascii
assert(KubernetesConf.getResourceNamePrefix(appName).matches("^[a-z0-9][a-z0-9\\-]*"))
}
}
}