This repository was archived by the owner on Jan 9, 2020. It is now read-only.
forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 117
Support driver pod kubernetes credentials mounting in V2 #246
Merged
ash211
merged 3 commits into
branch-2.1-kubernetes
from
submission-v2-kubernetes-credentials
May 18, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scala magic! I'm less familiar with it though -- this doesn't do anything outside this class does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
privatemodifier ensures that the implicit scope only applies to this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worthwhile to have a
setOptionAPI in the coreSparkConfthough.