forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support driver pod kubernetes credentials mounting in V2 submission (a…
- Loading branch information
Showing
20 changed files
with
632 additions
and
181 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...rnetes/core/src/main/scala/org/apache/spark/deploy/kubernetes/KubernetesCredentials.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.deploy.kubernetes | ||
|
||
case class KubernetesCredentials( | ||
oauthTokenBase64: Option[String], | ||
caCertDataBase64: Option[String], | ||
clientKeyDataBase64: Option[String], | ||
clientCertDataBase64: Option[String]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
.../org/apache/spark/deploy/kubernetes/submit/v2/DriverPodKubernetesCredentialsMounter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.deploy.kubernetes.submit.v2 | ||
|
||
import io.fabric8.kubernetes.api.model.{PodBuilder, Secret, SecretBuilder} | ||
import scala.collection.JavaConverters._ | ||
|
||
import org.apache.spark.SparkConf | ||
import org.apache.spark.deploy.kubernetes.KubernetesCredentials | ||
import org.apache.spark.deploy.kubernetes.config._ | ||
import org.apache.spark.deploy.kubernetes.constants._ | ||
import org.apache.spark.internal.config.OptionalConfigEntry | ||
|
||
private[spark] trait DriverPodKubernetesCredentialsMounter { | ||
|
||
/** | ||
* Set fields on the Spark configuration that indicate where the driver pod is | ||
* to find its Kubernetes credentials for requesting executors. | ||
*/ | ||
def setDriverPodKubernetesCredentialLocations(sparkConf: SparkConf): SparkConf | ||
|
||
/** | ||
* Create the Kubernetes secret object that correspond to the driver's credentials | ||
* that have to be created and mounted into the driver pod. The single Secret | ||
* object contains all of the data entries for the driver pod's Kubernetes | ||
* credentials. Returns empty if no secrets are to be mounted. | ||
*/ | ||
def createCredentialsSecret(): Option[Secret] | ||
|
||
/** | ||
* Mount any Kubernetes credentials from the submitting machine's disk into the driver pod. The | ||
* secret that is passed in here should have been created from createCredentialsSecret so that | ||
* the implementation does not need to hold its state. | ||
*/ | ||
def mountDriverKubernetesCredentials( | ||
originalPodSpec: PodBuilder, | ||
driverContainerName: String, | ||
credentialsSecret: Option[Secret]): PodBuilder | ||
} | ||
|
||
private[spark] class DriverPodKubernetesCredentialsMounterImpl( | ||
kubernetesAppId: String, | ||
submitterLocalDriverPodKubernetesCredentials: KubernetesCredentials, | ||
maybeUserSpecifiedMountedClientKeyFile: Option[String], | ||
maybeUserSpecifiedMountedClientCertFile: Option[String], | ||
maybeUserSpecifiedMountedOAuthTokenFile: Option[String], | ||
maybeUserSpecifiedMountedCaCertFile: Option[String]) | ||
extends DriverPodKubernetesCredentialsMounter { | ||
|
||
override def setDriverPodKubernetesCredentialLocations(sparkConf: SparkConf): SparkConf = { | ||
val resolvedMountedClientKeyFile = resolveSecretLocation( | ||
maybeUserSpecifiedMountedClientKeyFile, | ||
submitterLocalDriverPodKubernetesCredentials.clientKeyDataBase64, | ||
DRIVER_CREDENTIALS_CLIENT_KEY_PATH) | ||
val resolvedMountedClientCertFile = resolveSecretLocation( | ||
maybeUserSpecifiedMountedClientCertFile, | ||
submitterLocalDriverPodKubernetesCredentials.clientCertDataBase64, | ||
DRIVER_CREDENTIALS_CLIENT_CERT_PATH) | ||
val resolvedMountedCaCertFile = resolveSecretLocation( | ||
maybeUserSpecifiedMountedCaCertFile, | ||
submitterLocalDriverPodKubernetesCredentials.caCertDataBase64, | ||
DRIVER_CREDENTIALS_CA_CERT_PATH) | ||
val resolvedMountedOAuthTokenFile = resolveSecretLocation( | ||
maybeUserSpecifiedMountedOAuthTokenFile, | ||
submitterLocalDriverPodKubernetesCredentials.oauthTokenBase64, | ||
DRIVER_CREDENTIALS_OAUTH_TOKEN_PATH) | ||
val sparkConfWithCredentialLocations = sparkConf.clone() | ||
.setOption(KUBERNETES_DRIVER_MOUNTED_CA_CERT_FILE, resolvedMountedCaCertFile) | ||
.setOption(KUBERNETES_DRIVER_MOUNTED_CLIENT_KEY_FILE, resolvedMountedClientKeyFile) | ||
.setOption(KUBERNETES_DRIVER_MOUNTED_CLIENT_CERT_FILE, resolvedMountedClientCertFile) | ||
.setOption(KUBERNETES_DRIVER_MOUNTED_OAUTH_TOKEN, resolvedMountedOAuthTokenFile) | ||
sparkConfWithCredentialLocations.get(KUBERNETES_DRIVER_OAUTH_TOKEN).foreach { _ => | ||
sparkConfWithCredentialLocations.set(KUBERNETES_DRIVER_OAUTH_TOKEN, "<present_but_redacted>") | ||
} | ||
sparkConfWithCredentialLocations.get(KUBERNETES_SUBMIT_OAUTH_TOKEN).foreach { _ => | ||
sparkConfWithCredentialLocations.set(KUBERNETES_SUBMIT_OAUTH_TOKEN, "<present_but_redacted>") | ||
} | ||
sparkConfWithCredentialLocations | ||
} | ||
|
||
override def createCredentialsSecret(): Option[Secret] = { | ||
val allSecretData = | ||
resolveSecretData( | ||
maybeUserSpecifiedMountedClientKeyFile, | ||
submitterLocalDriverPodKubernetesCredentials.clientKeyDataBase64, | ||
DRIVER_CREDENTIALS_CLIENT_KEY_SECRET_NAME) ++ | ||
resolveSecretData( | ||
maybeUserSpecifiedMountedClientCertFile, | ||
submitterLocalDriverPodKubernetesCredentials.clientCertDataBase64, | ||
DRIVER_CREDENTIALS_CLIENT_CERT_SECRET_NAME) ++ | ||
resolveSecretData( | ||
maybeUserSpecifiedMountedCaCertFile, | ||
submitterLocalDriverPodKubernetesCredentials.caCertDataBase64, | ||
DRIVER_CREDENTIALS_CA_CERT_SECRET_NAME) ++ | ||
resolveSecretData( | ||
maybeUserSpecifiedMountedOAuthTokenFile, | ||
submitterLocalDriverPodKubernetesCredentials.oauthTokenBase64, | ||
DRIVER_CREDENTIALS_OAUTH_TOKEN_SECRET_NAME) | ||
if (allSecretData.isEmpty) { | ||
None | ||
} else { | ||
Some(new SecretBuilder() | ||
.withNewMetadata().withName(s"$kubernetesAppId-kubernetes-credentials").endMetadata() | ||
.withData(allSecretData.asJava) | ||
.build()) | ||
} | ||
} | ||
|
||
override def mountDriverKubernetesCredentials( | ||
originalPodSpec: PodBuilder, | ||
driverContainerName: String, | ||
credentialsSecret: Option[Secret]): PodBuilder = { | ||
credentialsSecret.map { secret => | ||
originalPodSpec.editSpec() | ||
.addNewVolume() | ||
.withName(DRIVER_CREDENTIALS_SECRET_VOLUME_NAME) | ||
.withNewSecret().withSecretName(secret.getMetadata.getName).endSecret() | ||
.endVolume() | ||
.editMatchingContainer(new ContainerNameEqualityPredicate(driverContainerName)) | ||
.addNewVolumeMount() | ||
.withName(DRIVER_CREDENTIALS_SECRET_VOLUME_NAME) | ||
.withMountPath(DRIVER_CREDENTIALS_SECRETS_BASE_DIR) | ||
.endVolumeMount() | ||
.endContainer() | ||
.endSpec() | ||
}.getOrElse(originalPodSpec) | ||
} | ||
|
||
private def resolveSecretLocation( | ||
mountedUserSpecified: Option[String], | ||
valueMountedFromSubmitter: Option[String], | ||
mountedCanonicalLocation: String): Option[String] = { | ||
mountedUserSpecified.orElse(valueMountedFromSubmitter.map( _ => { | ||
mountedCanonicalLocation | ||
})) | ||
} | ||
|
||
private def resolveSecretData( | ||
mountedUserSpecified: Option[String], | ||
valueMountedFromSubmitter: Option[String], | ||
secretName: String): Map[String, String] = { | ||
mountedUserSpecified.map { _ => Map.empty[String, String]} | ||
.getOrElse { | ||
valueMountedFromSubmitter.map { valueBase64 => | ||
Map(secretName -> valueBase64) | ||
}.getOrElse(Map.empty[String, String]) | ||
} | ||
} | ||
|
||
private implicit def augmentSparkConf(sparkConf: SparkConf): OptionSettableSparkConf = { | ||
new OptionSettableSparkConf(sparkConf) | ||
} | ||
} | ||
|
||
private class OptionSettableSparkConf(sparkConf: SparkConf) { | ||
def setOption[T](configEntry: OptionalConfigEntry[T], option: Option[T]): SparkConf = { | ||
option.map( opt => { | ||
sparkConf.set(configEntry, opt) | ||
}).getOrElse(sparkConf) | ||
} | ||
} |
Oops, something went wrong.