|
| 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.kubernetes.submit.v2 |
| 18 | + |
| 19 | +import io.fabric8.kubernetes.api.model.{PodBuilder, Secret, SecretBuilder} |
| 20 | +import scala.collection.JavaConverters._ |
| 21 | + |
| 22 | +import org.apache.spark.SparkConf |
| 23 | +import org.apache.spark.deploy.kubernetes.KubernetesCredentials |
| 24 | +import org.apache.spark.deploy.kubernetes.config._ |
| 25 | +import org.apache.spark.deploy.kubernetes.constants._ |
| 26 | +import org.apache.spark.internal.config.OptionalConfigEntry |
| 27 | + |
| 28 | +private[spark] trait DriverPodKubernetesCredentialsMounter { |
| 29 | + |
| 30 | + /** |
| 31 | + * Set fields on the Spark configuration that indicate where the driver pod is |
| 32 | + * to find its Kubernetes credentials for requesting executors. |
| 33 | + */ |
| 34 | + def setDriverPodKubernetesCredentialLocations(sparkConf: SparkConf): SparkConf |
| 35 | + |
| 36 | + /** |
| 37 | + * Create the Kubernetes secret object that correspond to the driver's credentials |
| 38 | + * that have to be created and mounted into the driver pod. The single Secret |
| 39 | + * object contains all of the data entries for the driver pod's Kubernetes |
| 40 | + * credentials. Returns empty if no secrets are to be mounted. |
| 41 | + */ |
| 42 | + def createCredentialsSecret(): Option[Secret] |
| 43 | + |
| 44 | + /** |
| 45 | + * Mount any Kubernetes credentials from the submitting machine's disk into the driver pod. The |
| 46 | + * secret that is passed in here should have been created from createCredentialsSecret so that |
| 47 | + * the implementation does not need to hold its state. |
| 48 | + */ |
| 49 | + def mountDriverKubernetesCredentials( |
| 50 | + originalPodSpec: PodBuilder, |
| 51 | + driverContainerName: String, |
| 52 | + credentialsSecret: Option[Secret]): PodBuilder |
| 53 | +} |
| 54 | + |
| 55 | +private[spark] class DriverPodKubernetesCredentialsMounterImpl( |
| 56 | + kubernetesAppId: String, |
| 57 | + submitterLocalDriverPodKubernetesCredentials: KubernetesCredentials, |
| 58 | + maybeUserSpecifiedMountedClientKeyFile: Option[String], |
| 59 | + maybeUserSpecifiedMountedClientCertFile: Option[String], |
| 60 | + maybeUserSpecifiedMountedOAuthTokenFile: Option[String], |
| 61 | + maybeUserSpecifiedMountedCaCertFile: Option[String]) |
| 62 | + extends DriverPodKubernetesCredentialsMounter { |
| 63 | + |
| 64 | + override def setDriverPodKubernetesCredentialLocations(sparkConf: SparkConf): SparkConf = { |
| 65 | + val resolvedMountedClientKeyFile = resolveSecretLocation( |
| 66 | + maybeUserSpecifiedMountedClientKeyFile, |
| 67 | + submitterLocalDriverPodKubernetesCredentials.clientKeyDataBase64, |
| 68 | + DRIVER_CREDENTIALS_CLIENT_KEY_PATH) |
| 69 | + val resolvedMountedClientCertFile = resolveSecretLocation( |
| 70 | + maybeUserSpecifiedMountedClientCertFile, |
| 71 | + submitterLocalDriverPodKubernetesCredentials.clientCertDataBase64, |
| 72 | + DRIVER_CREDENTIALS_CLIENT_CERT_PATH) |
| 73 | + val resolvedMountedCaCertFile = resolveSecretLocation( |
| 74 | + maybeUserSpecifiedMountedCaCertFile, |
| 75 | + submitterLocalDriverPodKubernetesCredentials.caCertDataBase64, |
| 76 | + DRIVER_CREDENTIALS_CA_CERT_PATH) |
| 77 | + val resolvedMountedOAuthTokenFile = resolveSecretLocation( |
| 78 | + maybeUserSpecifiedMountedOAuthTokenFile, |
| 79 | + submitterLocalDriverPodKubernetesCredentials.oauthTokenBase64, |
| 80 | + DRIVER_CREDENTIALS_OAUTH_TOKEN_PATH) |
| 81 | + val sparkConfWithCredentialLocations = sparkConf.clone() |
| 82 | + .setOption(KUBERNETES_DRIVER_MOUNTED_CA_CERT_FILE, resolvedMountedCaCertFile) |
| 83 | + .setOption(KUBERNETES_DRIVER_MOUNTED_CLIENT_KEY_FILE, resolvedMountedClientKeyFile) |
| 84 | + .setOption(KUBERNETES_DRIVER_MOUNTED_CLIENT_CERT_FILE, resolvedMountedClientCertFile) |
| 85 | + .setOption(KUBERNETES_DRIVER_MOUNTED_OAUTH_TOKEN, resolvedMountedOAuthTokenFile) |
| 86 | + sparkConfWithCredentialLocations.get(KUBERNETES_DRIVER_OAUTH_TOKEN).foreach { _ => |
| 87 | + sparkConfWithCredentialLocations.set(KUBERNETES_DRIVER_OAUTH_TOKEN, "<present_but_redacted>") |
| 88 | + } |
| 89 | + sparkConfWithCredentialLocations.get(KUBERNETES_SUBMIT_OAUTH_TOKEN).foreach { _ => |
| 90 | + sparkConfWithCredentialLocations.set(KUBERNETES_SUBMIT_OAUTH_TOKEN, "<present_but_redacted>") |
| 91 | + } |
| 92 | + sparkConfWithCredentialLocations |
| 93 | + } |
| 94 | + |
| 95 | + override def createCredentialsSecret(): Option[Secret] = { |
| 96 | + val allSecretData = |
| 97 | + resolveSecretData( |
| 98 | + maybeUserSpecifiedMountedClientKeyFile, |
| 99 | + submitterLocalDriverPodKubernetesCredentials.clientKeyDataBase64, |
| 100 | + DRIVER_CREDENTIALS_CLIENT_KEY_SECRET_NAME) ++ |
| 101 | + resolveSecretData( |
| 102 | + maybeUserSpecifiedMountedClientCertFile, |
| 103 | + submitterLocalDriverPodKubernetesCredentials.clientCertDataBase64, |
| 104 | + DRIVER_CREDENTIALS_CLIENT_CERT_SECRET_NAME) ++ |
| 105 | + resolveSecretData( |
| 106 | + maybeUserSpecifiedMountedCaCertFile, |
| 107 | + submitterLocalDriverPodKubernetesCredentials.caCertDataBase64, |
| 108 | + DRIVER_CREDENTIALS_CA_CERT_SECRET_NAME) ++ |
| 109 | + resolveSecretData( |
| 110 | + maybeUserSpecifiedMountedOAuthTokenFile, |
| 111 | + submitterLocalDriverPodKubernetesCredentials.oauthTokenBase64, |
| 112 | + DRIVER_CREDENTIALS_OAUTH_TOKEN_SECRET_NAME) |
| 113 | + if (allSecretData.isEmpty) { |
| 114 | + None |
| 115 | + } else { |
| 116 | + Some(new SecretBuilder() |
| 117 | + .withNewMetadata().withName(s"$kubernetesAppId-kubernetes-credentials").endMetadata() |
| 118 | + .withData(allSecretData.asJava) |
| 119 | + .build()) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + override def mountDriverKubernetesCredentials( |
| 124 | + originalPodSpec: PodBuilder, |
| 125 | + driverContainerName: String, |
| 126 | + credentialsSecret: Option[Secret]): PodBuilder = { |
| 127 | + credentialsSecret.map { secret => |
| 128 | + originalPodSpec.editSpec() |
| 129 | + .addNewVolume() |
| 130 | + .withName(DRIVER_CREDENTIALS_SECRET_VOLUME_NAME) |
| 131 | + .withNewSecret().withSecretName(secret.getMetadata.getName).endSecret() |
| 132 | + .endVolume() |
| 133 | + .editMatchingContainer(new ContainerNameEqualityPredicate(driverContainerName)) |
| 134 | + .addNewVolumeMount() |
| 135 | + .withName(DRIVER_CREDENTIALS_SECRET_VOLUME_NAME) |
| 136 | + .withMountPath(DRIVER_CREDENTIALS_SECRETS_BASE_DIR) |
| 137 | + .endVolumeMount() |
| 138 | + .endContainer() |
| 139 | + .endSpec() |
| 140 | + }.getOrElse(originalPodSpec) |
| 141 | + } |
| 142 | + |
| 143 | + private def resolveSecretLocation( |
| 144 | + mountedUserSpecified: Option[String], |
| 145 | + valueMountedFromSubmitter: Option[String], |
| 146 | + mountedCanonicalLocation: String): Option[String] = { |
| 147 | + mountedUserSpecified.orElse(valueMountedFromSubmitter.map( _ => { |
| 148 | + mountedCanonicalLocation |
| 149 | + })) |
| 150 | + } |
| 151 | + |
| 152 | + private def resolveSecretData( |
| 153 | + mountedUserSpecified: Option[String], |
| 154 | + valueMountedFromSubmitter: Option[String], |
| 155 | + secretName: String): Map[String, String] = { |
| 156 | + mountedUserSpecified.map { _ => Map.empty[String, String]} |
| 157 | + .getOrElse { |
| 158 | + valueMountedFromSubmitter.map { valueBase64 => |
| 159 | + Map(secretName -> valueBase64) |
| 160 | + }.getOrElse(Map.empty[String, String]) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private implicit def augmentSparkConf(sparkConf: SparkConf): OptionSettableSparkConf = { |
| 165 | + new OptionSettableSparkConf(sparkConf) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +private class OptionSettableSparkConf(sparkConf: SparkConf) { |
| 170 | + def setOption[T](configEntry: OptionalConfigEntry[T], option: Option[T]): SparkConf = { |
| 171 | + option.map( opt => { |
| 172 | + sparkConf.set(configEntry, opt) |
| 173 | + }).getOrElse(sparkConf) |
| 174 | + } |
| 175 | +} |
0 commit comments