-
Notifications
You must be signed in to change notification settings - Fork 28.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-23529][K8s] Support mounting hostPath volumes #21095
Changes from all commits
51a877a
4ada724
17258a3
6fff716
f961b33
af4d9ba
13e1049
7a25d76
a5a277a
772128e
a393b92
b9b3dcb
81a7811
95d1b0d
facde97
ccdc799
7c1be8a
f482dfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.k8s | ||
|
||
import scala.collection.mutable.Map | ||
|
||
private[spark] case class KubernetesVolumeSpec( | ||
var mountPath: Option[String], | ||
var mountReadOnly: Option[Boolean], | ||
var optionsSpec: Map[String, String]) | ||
|
||
private[spark] object KubernetesVolumeSpec { | ||
def emptySpec(): KubernetesVolumeSpec = new KubernetesVolumeSpec(None, None, Map()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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.k8s | ||
|
||
import scala.collection.mutable.HashMap | ||
|
||
import io.fabric8.kubernetes.api.model._ | ||
|
||
import org.apache.spark.SparkConf | ||
import org.apache.spark.deploy.k8s.Config._ | ||
|
||
private[spark] object KubernetesVolumeUtils { | ||
|
||
/** | ||
* Given hostPath volume specs, add volume to pod and volume mount to container. | ||
* | ||
* @param pod original specification of the pod | ||
* @param container original specification of the container | ||
* @param sparkConf Spark configuration | ||
* @param prefix the prefix for volume configuration | ||
* @return a tuple of (pod with the volume(s) added, container with mount(s) added) | ||
*/ | ||
def addVolumes( | ||
pod: Pod, | ||
container: Container, | ||
sparkConf: SparkConf, | ||
prefix : String): (Pod, Container) = { | ||
val hostPathVolumeSpecs = parseHostPathVolumesWithPrefix(sparkConf, prefix) | ||
addHostPathVolumes(pod, container, hostPathVolumeSpecs) | ||
} | ||
|
||
/** | ||
* Extract Spark volume configuration properties with a given name prefix. | ||
* | ||
* @param sparkConf Spark configuration | ||
* @param prefix the given property name prefix | ||
* @param volumeTypeKey the given property name prefix | ||
* @return a Map storing with volume name as key and spec as value | ||
*/ | ||
def parseVolumesWithPrefix( | ||
sparkConf: SparkConf, | ||
prefix: String, | ||
volumeTypeKey: String): Map[String, KubernetesVolumeSpec] = { | ||
val volumes = HashMap[String, KubernetesVolumeSpec]() | ||
val properties = sparkConf.getAllWithPrefix(s"$prefix$volumeTypeKey.").toList | ||
// Extract volume names | ||
properties.foreach { | ||
k => | ||
val keys = k._1.split("\\.") | ||
if (keys.nonEmpty && !volumes.contains(keys(0))) { | ||
volumes.update(keys(0), KubernetesVolumeSpec.emptySpec()) | ||
} | ||
} | ||
// Populate spec | ||
volumes.foreach { | ||
case (name, spec) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
properties.foreach { | ||
k => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
k._1.split("\\.") match { | ||
case Array(`name`, KUBERNETES_VOLUMES_MOUNT_KEY, KUBERNETES_VOLUMES_PATH_KEY) => | ||
spec.mountPath = Some(k._2) | ||
case Array(`name`, KUBERNETES_VOLUMES_MOUNT_KEY, KUBERNETES_VOLUMES_READONLY_KEY) => | ||
spec.mountReadOnly = Some(k._2.toBoolean) | ||
case Array(`name`, KUBERNETES_VOLUMES_OPTIONS_KEY, option) => | ||
spec.optionsSpec.update(option, k._2) | ||
case _ => | ||
None | ||
} | ||
} | ||
} | ||
volumes.toMap | ||
} | ||
|
||
/** | ||
* Extract Spark hostPath volume configuration properties with a given name prefix and | ||
* return the result as a Map. | ||
* | ||
* @param sparkConf Spark configuration | ||
* @param prefix the given property name prefix | ||
* @return a Map storing with volume name as key and spec as value | ||
*/ | ||
def parseHostPathVolumesWithPrefix( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you don't really need this function as it's just a wrapper of |
||
sparkConf: SparkConf, | ||
prefix: String): Map[String, KubernetesVolumeSpec] = { | ||
parseVolumesWithPrefix(sparkConf, prefix, KUBERNETES_VOLUMES_HOSTPATH_KEY) | ||
} | ||
|
||
/** | ||
* Given hostPath volume specs, add volume to pod and volume mount to container. | ||
* | ||
* @param pod original specification of the pod | ||
* @param container original specification of the container | ||
* @param volumes list of named volume specs | ||
* @return a tuple of (pod with the volume(s) added, container with mount(s) added) | ||
*/ | ||
def addHostPathVolumes( | ||
pod: Pod, | ||
container: Container, | ||
volumes: Map[String, KubernetesVolumeSpec]): (Pod, Container) = { | ||
val podBuilder = new PodBuilder(pod).editOrNewSpec() | ||
val containerBuilder = new ContainerBuilder(container) | ||
volumes foreach { | ||
case (name, spec) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
var hostPath: Option[String] = None | ||
if (spec.optionsSpec.contains(KUBERNETES_VOLUMES_PATH_KEY)) { | ||
hostPath = Some(spec.optionsSpec(KUBERNETES_VOLUMES_PATH_KEY)) | ||
} | ||
if (hostPath.isDefined && spec.mountPath.isDefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this |
||
podBuilder.addToVolumes(new VolumeBuilder() | ||
.withHostPath(new HostPathVolumeSource(hostPath.get)) | ||
.withName(name) | ||
.build()) | ||
val volumeBuilder = new VolumeMountBuilder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.withMountPath(spec.mountPath.get) | ||
.withName(name) | ||
if (spec.mountReadOnly.isDefined) { | ||
containerBuilder | ||
.addToVolumeMounts(volumeBuilder | ||
.withReadOnly(spec.mountReadOnly.get) | ||
.build()) | ||
} else { | ||
containerBuilder.addToVolumeMounts(volumeBuilder.build()) | ||
} | ||
} | ||
} | ||
(podBuilder.endSpec().build(), containerBuilder.build()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,10 @@ package org.apache.spark.deploy.k8s.features | |
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable | ||
|
||
import io.fabric8.kubernetes.api.model.{ContainerBuilder, EnvVarBuilder, EnvVarSourceBuilder, HasMetadata, PodBuilder, QuantityBuilder} | ||
import io.fabric8.kubernetes.api.model._ | ||
|
||
import org.apache.spark.SparkException | ||
import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesUtils, SparkPod} | ||
import org.apache.spark.deploy.k8s._ | ||
import org.apache.spark.deploy.k8s.Config._ | ||
import org.apache.spark.deploy.k8s.Constants._ | ||
import org.apache.spark.internal.config._ | ||
|
@@ -109,7 +109,15 @@ private[spark] class BasicDriverFeatureStep( | |
.addToImagePullSecrets(conf.imagePullSecrets(): _*) | ||
.endSpec() | ||
.build() | ||
SparkPod(driverPod, driverContainer) | ||
|
||
val (driverPodWithVolumes, driverContainerVolumes) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of putting the logic of volume mounting in |
||
KubernetesVolumeUtils.addVolumes( | ||
driverPod, | ||
driverContainer, | ||
conf.sparkConf, | ||
KUBERNETES_DRIVER_VOLUMES_PREFIX) | ||
|
||
SparkPod(driverPodWithVolumes, driverContainerVolumes) | ||
} | ||
|
||
override def getAdditionalPodSystemProperties(): Map[String, String] = { | ||
|
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 Scaladoc should not mention hostPath as this function is not hostPath exclusively.