-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32971][K8S] Support dynamic PVC creation/deletion for K8s executors #29846
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
Changes from all commits
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,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.k8s | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata | ||
|
|
||
| private[spark] case class KubernetesExecutorSpec( | ||
| pod: SparkPod, | ||
| executorKubernetesResources: Seq[HasMetadata]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,10 @@ private[spark] sealed trait KubernetesVolumeSpecificConf | |
| private[spark] case class KubernetesHostPathVolumeConf(hostPath: String) | ||
| extends KubernetesVolumeSpecificConf | ||
|
|
||
| private[spark] case class KubernetesPVCVolumeConf(claimName: String) | ||
| private[spark] case class KubernetesPVCVolumeConf( | ||
| claimName: String, | ||
| storageClass: Option[String] = None, | ||
| size: Option[String] = None) | ||
|
Comment on lines
+26
to
+27
Member
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. Do
Member
Author
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. Yes. We need both. In this PR, if one of them is missing, the fallback operation is the existing PVC mounting behavior. So, it doesn't try to create PVC and assume the PVC exists with the given PVC name. |
||
| extends KubernetesVolumeSpecificConf | ||
|
|
||
| private[spark] case class KubernetesEmptyDirVolumeConf( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,19 @@ | |
| */ | ||
| package org.apache.spark.deploy.k8s.features | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import io.fabric8.kubernetes.api.model._ | ||
|
|
||
| import org.apache.spark.deploy.k8s._ | ||
| import org.apache.spark.deploy.k8s.Constants.ENV_EXECUTOR_ID | ||
|
|
||
| private[spark] class MountVolumesFeatureStep(conf: KubernetesConf) | ||
| extends KubernetesFeatureConfigStep { | ||
| import MountVolumesFeatureStep._ | ||
|
|
||
| val additionalResources = ArrayBuffer.empty[HasMetadata] | ||
|
|
||
| override def configurePod(pod: SparkPod): SparkPod = { | ||
| val (volumeMounts, volumes) = constructVolumes(conf.volumes).unzip | ||
|
|
@@ -43,7 +49,7 @@ private[spark] class MountVolumesFeatureStep(conf: KubernetesConf) | |
| private def constructVolumes( | ||
| volumeSpecs: Iterable[KubernetesVolumeSpec] | ||
| ): Iterable[(VolumeMount, Volume)] = { | ||
| volumeSpecs.map { spec => | ||
| volumeSpecs.zipWithIndex.map { case (spec, i) => | ||
| val volumeMount = new VolumeMountBuilder() | ||
| .withMountPath(spec.mountPath) | ||
| .withReadOnly(spec.mountReadOnly) | ||
|
|
@@ -57,10 +63,32 @@ private[spark] class MountVolumesFeatureStep(conf: KubernetesConf) | |
| new VolumeBuilder() | ||
| .withHostPath(new HostPathVolumeSource(hostPath, "")) | ||
|
|
||
| case KubernetesPVCVolumeConf(claimNameTemplate) => | ||
| case KubernetesPVCVolumeConf(claimNameTemplate, storageClass, size) => | ||
| val claimName = conf match { | ||
| case c: KubernetesExecutorConf => | ||
| claimNameTemplate.replaceAll(ENV_EXECUTOR_ID, c.executorId) | ||
| val claimName = claimNameTemplate | ||
| .replaceAll(PVC_ON_DEMAND, | ||
| s"${conf.resourceNamePrefix}-exec-${c.executorId}$PVC_POSTFIX-$i") | ||
| .replaceAll(ENV_EXECUTOR_ID, c.executorId) | ||
|
|
||
| if (storageClass.isDefined && size.isDefined) { | ||
| additionalResources.append(new PersistentVolumeClaimBuilder() | ||
| .withKind(PVC) | ||
| .withApiVersion("v1") | ||
| .withNewMetadata() | ||
| .withName(claimName) | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| .withStorageClassName(storageClass.get) | ||
| .withAccessModes(PVC_ACCESS_MODE) | ||
| .withResources(new ResourceRequirementsBuilder() | ||
| .withRequests(Map("storage" -> new Quantity(size.get)).asJava).build()) | ||
| .endSpec() | ||
| .build()) | ||
| } | ||
|
|
||
| claimName | ||
|
|
||
| case _ => | ||
| claimNameTemplate | ||
| } | ||
|
|
@@ -84,4 +112,15 @@ private[spark] class MountVolumesFeatureStep(conf: KubernetesConf) | |
| (volumeMount, volume) | ||
| } | ||
| } | ||
|
|
||
| override def getAdditionalKubernetesResources(): Seq[HasMetadata] = { | ||
| additionalResources | ||
| } | ||
| } | ||
|
|
||
| private[spark] object MountVolumesFeatureStep { | ||
| val PVC_ON_DEMAND = "OnDemand" | ||
| val PVC = "PersistentVolumeClaim" | ||
| val PVC_POSTFIX = "-pvc" | ||
| val PVC_ACCESS_MODE = "ReadWriteOnce" | ||
|
Comment on lines
+122
to
+125
Contributor
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. Would it make sense to have the OnDemand, postfix, and access mode configurable?
Member
Author
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. Ya, it's possible, but I didn't do that in this PR because of the followings.
For |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,18 +20,20 @@ import java.io.StringWriter | |
| import java.util.{Collections, UUID} | ||
| import java.util.Properties | ||
|
|
||
| import scala.collection.mutable | ||
| import scala.util.control.Breaks._ | ||
| import scala.util.control.NonFatal | ||
|
|
||
| import io.fabric8.kubernetes.api.model._ | ||
| import io.fabric8.kubernetes.client.{KubernetesClient, Watch} | ||
| import io.fabric8.kubernetes.client.Watcher.Action | ||
| import scala.collection.mutable | ||
| import scala.util.control.NonFatal | ||
| import util.control.Breaks._ | ||
|
|
||
| import org.apache.spark.SparkConf | ||
| import org.apache.spark.deploy.SparkApplication | ||
| import org.apache.spark.deploy.k8s._ | ||
| import org.apache.spark.deploy.k8s.Config._ | ||
| import org.apache.spark.deploy.k8s.Constants._ | ||
| import org.apache.spark.deploy.k8s.KubernetesUtils.addOwnerReference | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.util.Utils | ||
|
|
||
|
|
@@ -134,7 +136,7 @@ private[spark] class Client( | |
| val createdDriverPod = kubernetesClient.pods().create(resolvedDriverPod) | ||
| try { | ||
| val otherKubernetesResources = resolvedDriverSpec.driverKubernetesResources ++ Seq(configMap) | ||
| addDriverOwnerReference(createdDriverPod, otherKubernetesResources) | ||
| addOwnerReference(createdDriverPod, otherKubernetesResources) | ||
| kubernetesClient.resourceList(otherKubernetesResources: _*).createOrReplace() | ||
| } catch { | ||
| case NonFatal(e) => | ||
|
|
@@ -163,22 +165,6 @@ private[spark] class Client( | |
| } | ||
| } | ||
|
|
||
| // Add a OwnerReference to the given resources making the driver pod an owner of them so when | ||
| // the driver pod is deleted, the resources are garbage collected. | ||
| private def addDriverOwnerReference(driverPod: Pod, resources: Seq[HasMetadata]): Unit = { | ||
| val driverPodOwnerReference = new OwnerReferenceBuilder() | ||
| .withName(driverPod.getMetadata.getName) | ||
| .withApiVersion(driverPod.getApiVersion) | ||
| .withUid(driverPod.getMetadata.getUid) | ||
| .withKind(driverPod.getKind) | ||
| .withController(true) | ||
| .build() | ||
| resources.foreach { resource => | ||
| val originalMetadata = resource.getMetadata | ||
| originalMetadata.setOwnerReferences(Collections.singletonList(driverPodOwnerReference)) | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
-166
to
-181
Contributor
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. Thank you for unifying this code :) |
||
| // Build a Config Map that will house spark conf properties in a single file for spark-submit | ||
| private def buildConfigMap(configMapName: String, conf: Map[String, String]): ConfigMap = { | ||
| val properties = new Properties() | ||
|
|
||
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.
I like this :)
Initially I was thinking about asking about re-using the PVCs because I know that in some systems PVC creating is slow (for dynamic scaling), but it's probably better to return the resources and not do some weird storage pool like I was thinking of.