|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.deploy.k8s |
| 18 | + |
| 19 | +import io.fabric8.kubernetes.api.model.{LocalObjectReference, LocalObjectReferenceBuilder, Pod} |
| 20 | + |
| 21 | +import org.apache.spark.SparkConf |
| 22 | +import org.apache.spark.deploy.k8s.Config._ |
| 23 | +import org.apache.spark.deploy.k8s.Constants._ |
| 24 | +import org.apache.spark.deploy.k8s.submit.{JavaMainAppResource, MainAppResource} |
| 25 | +import org.apache.spark.internal.config.ConfigEntry |
| 26 | + |
| 27 | +private[spark] sealed trait KubernetesRoleSpecificConf |
| 28 | + |
| 29 | +/* |
| 30 | + * Structure containing metadata for Kubernetes logic that builds a Spark driver. |
| 31 | + */ |
| 32 | +private[spark] case class KubernetesDriverSpecificConf( |
| 33 | + mainAppResource: Option[MainAppResource], |
| 34 | + mainClass: String, |
| 35 | + appName: String, |
| 36 | + appArgs: Seq[String]) extends KubernetesRoleSpecificConf |
| 37 | + |
| 38 | +/* |
| 39 | + * Structure containing metadata for Kubernetes logic that builds a Spark executor. |
| 40 | + */ |
| 41 | +private[spark] case class KubernetesExecutorSpecificConf( |
| 42 | + executorId: String, |
| 43 | + driverPod: Pod) |
| 44 | + extends KubernetesRoleSpecificConf |
| 45 | + |
| 46 | +/** |
| 47 | + * Structure containing metadata for Kubernetes logic to build Spark pods. |
| 48 | + */ |
| 49 | +private[spark] case class KubernetesConf[T <: KubernetesRoleSpecificConf]( |
| 50 | + sparkConf: SparkConf, |
| 51 | + roleSpecificConf: T, |
| 52 | + appResourceNamePrefix: String, |
| 53 | + appId: String, |
| 54 | + roleLabels: Map[String, String], |
| 55 | + roleAnnotations: Map[String, String], |
| 56 | + roleSecretNamesToMountPaths: Map[String, String], |
| 57 | + roleEnvs: Map[String, String]) { |
| 58 | + |
| 59 | + def namespace(): String = sparkConf.get(KUBERNETES_NAMESPACE) |
| 60 | + |
| 61 | + def sparkJars(): Seq[String] = sparkConf |
| 62 | + .getOption("spark.jars") |
| 63 | + .map(str => str.split(",").toSeq) |
| 64 | + .getOrElse(Seq.empty[String]) |
| 65 | + |
| 66 | + def sparkFiles(): Seq[String] = sparkConf |
| 67 | + .getOption("spark.files") |
| 68 | + .map(str => str.split(",").toSeq) |
| 69 | + .getOrElse(Seq.empty[String]) |
| 70 | + |
| 71 | + def imagePullPolicy(): String = sparkConf.get(CONTAINER_IMAGE_PULL_POLICY) |
| 72 | + |
| 73 | + def imagePullSecrets(): Seq[LocalObjectReference] = { |
| 74 | + sparkConf |
| 75 | + .get(IMAGE_PULL_SECRETS) |
| 76 | + .map(_.split(",")) |
| 77 | + .getOrElse(Array.empty[String]) |
| 78 | + .map(_.trim) |
| 79 | + .map { secret => |
| 80 | + new LocalObjectReferenceBuilder().withName(secret).build() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + def nodeSelector(): Map[String, String] = |
| 85 | + KubernetesUtils.parsePrefixedKeyValuePairs(sparkConf, KUBERNETES_NODE_SELECTOR_PREFIX) |
| 86 | + |
| 87 | + def get[T](config: ConfigEntry[T]): T = sparkConf.get(config) |
| 88 | + |
| 89 | + def get(conf: String): String = sparkConf.get(conf) |
| 90 | + |
| 91 | + def get(conf: String, defaultValue: String): String = sparkConf.get(conf, defaultValue) |
| 92 | + |
| 93 | + def getOption(key: String): Option[String] = sparkConf.getOption(key) |
| 94 | +} |
| 95 | + |
| 96 | +private[spark] object KubernetesConf { |
| 97 | + def createDriverConf( |
| 98 | + sparkConf: SparkConf, |
| 99 | + appName: String, |
| 100 | + appResourceNamePrefix: String, |
| 101 | + appId: String, |
| 102 | + mainAppResource: Option[MainAppResource], |
| 103 | + mainClass: String, |
| 104 | + appArgs: Array[String]): KubernetesConf[KubernetesDriverSpecificConf] = { |
| 105 | + val sparkConfWithMainAppJar = sparkConf.clone() |
| 106 | + mainAppResource.foreach { |
| 107 | + case JavaMainAppResource(res) => |
| 108 | + val previousJars = sparkConf |
| 109 | + .getOption("spark.jars") |
| 110 | + .map(_.split(",")) |
| 111 | + .getOrElse(Array.empty) |
| 112 | + if (!previousJars.contains(res)) { |
| 113 | + sparkConfWithMainAppJar.setJars(previousJars ++ Seq(res)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + val driverCustomLabels = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 118 | + sparkConf, KUBERNETES_DRIVER_LABEL_PREFIX) |
| 119 | + require(!driverCustomLabels.contains(SPARK_APP_ID_LABEL), "Label with key " + |
| 120 | + s"$SPARK_APP_ID_LABEL is not allowed as it is reserved for Spark bookkeeping " + |
| 121 | + "operations.") |
| 122 | + require(!driverCustomLabels.contains(SPARK_ROLE_LABEL), "Label with key " + |
| 123 | + s"$SPARK_ROLE_LABEL is not allowed as it is reserved for Spark bookkeeping " + |
| 124 | + "operations.") |
| 125 | + val driverLabels = driverCustomLabels ++ Map( |
| 126 | + SPARK_APP_ID_LABEL -> appId, |
| 127 | + SPARK_ROLE_LABEL -> SPARK_POD_DRIVER_ROLE) |
| 128 | + val driverAnnotations = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 129 | + sparkConf, KUBERNETES_DRIVER_ANNOTATION_PREFIX) |
| 130 | + val driverSecretNamesToMountPaths = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 131 | + sparkConf, KUBERNETES_DRIVER_SECRETS_PREFIX) |
| 132 | + val driverEnvs = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 133 | + sparkConf, KUBERNETES_DRIVER_ENV_PREFIX) |
| 134 | + |
| 135 | + KubernetesConf( |
| 136 | + sparkConfWithMainAppJar, |
| 137 | + KubernetesDriverSpecificConf(mainAppResource, mainClass, appName, appArgs), |
| 138 | + appResourceNamePrefix, |
| 139 | + appId, |
| 140 | + driverLabels, |
| 141 | + driverAnnotations, |
| 142 | + driverSecretNamesToMountPaths, |
| 143 | + driverEnvs) |
| 144 | + } |
| 145 | + |
| 146 | + def createExecutorConf( |
| 147 | + sparkConf: SparkConf, |
| 148 | + executorId: String, |
| 149 | + appId: String, |
| 150 | + driverPod: Pod): KubernetesConf[KubernetesExecutorSpecificConf] = { |
| 151 | + val executorCustomLabels = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 152 | + sparkConf, KUBERNETES_EXECUTOR_LABEL_PREFIX) |
| 153 | + require( |
| 154 | + !executorCustomLabels.contains(SPARK_APP_ID_LABEL), |
| 155 | + s"Custom executor labels cannot contain $SPARK_APP_ID_LABEL as it is reserved for Spark.") |
| 156 | + require( |
| 157 | + !executorCustomLabels.contains(SPARK_EXECUTOR_ID_LABEL), |
| 158 | + s"Custom executor labels cannot contain $SPARK_EXECUTOR_ID_LABEL as it is reserved for" + |
| 159 | + " Spark.") |
| 160 | + require( |
| 161 | + !executorCustomLabels.contains(SPARK_ROLE_LABEL), |
| 162 | + s"Custom executor labels cannot contain $SPARK_ROLE_LABEL as it is reserved for Spark.") |
| 163 | + val executorLabels = Map( |
| 164 | + SPARK_EXECUTOR_ID_LABEL -> executorId, |
| 165 | + SPARK_APP_ID_LABEL -> appId, |
| 166 | + SPARK_ROLE_LABEL -> SPARK_POD_EXECUTOR_ROLE) ++ |
| 167 | + executorCustomLabels |
| 168 | + val executorAnnotations = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 169 | + sparkConf, KUBERNETES_EXECUTOR_ANNOTATION_PREFIX) |
| 170 | + val executorSecrets = KubernetesUtils.parsePrefixedKeyValuePairs( |
| 171 | + sparkConf, KUBERNETES_EXECUTOR_SECRETS_PREFIX) |
| 172 | + val executorEnv = sparkConf.getExecutorEnv.toMap |
| 173 | + |
| 174 | + KubernetesConf( |
| 175 | + sparkConf.clone(), |
| 176 | + KubernetesExecutorSpecificConf(executorId, driverPod), |
| 177 | + sparkConf.get(KUBERNETES_EXECUTOR_POD_NAME_PREFIX), |
| 178 | + appId, |
| 179 | + executorLabels, |
| 180 | + executorAnnotations, |
| 181 | + executorSecrets, |
| 182 | + executorEnv) |
| 183 | + } |
| 184 | +} |
0 commit comments