skonto commented on a change in pull request #23599: [SPARK-24793][K8s] Enhance spark-submit for app management URL: https://github.com/apache/spark/pull/23599#discussion_r266704535
########## File path: resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/submit/K8sSubmitOps.scala ########## @@ -0,0 +1,192 @@ +/* + * 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.submit + +import scala.collection.JavaConverters._ + +import K8SSparkSubmitOperation.getGracePeriod +import io.fabric8.kubernetes.api.model.Pod +import io.fabric8.kubernetes.client.KubernetesClient + +import org.apache.spark.SparkConf +import org.apache.spark.deploy.SparkSubmitOperation +import org.apache.spark.deploy.k8s.{KubernetesUtils, SparkKubernetesClientFactory} +import org.apache.spark.deploy.k8s.Config.{KUBERNETES_AUTH_SUBMISSION_CONF_PREFIX, KUBERNETES_SUBMIT_GRACE_PERIOD} +import org.apache.spark.deploy.k8s.Constants.{SPARK_POD_DRIVER_ROLE, SPARK_ROLE_LABEL} +import org.apache.spark.deploy.k8s.KubernetesUtils.formatPodState +import org.apache.spark.util.{CommandLineLoggingUtils, Utils} + +private sealed trait K8sSubmitOp extends CommandLineLoggingUtils { + def executeOnPod(pName: String, namespace: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit + def executeOnGlob(pods: List[Pod], ns: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit +} + +private class KillApplication extends K8sSubmitOp { + override def executeOnPod(pName: String, namespace: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit = { + val gracePeriodSeconds = getGracePeriod(sparkConf) + val podToDelete = { + namespace match { + case Some(ns) => client.pods.inNamespace(ns) + case None => client.pods + } + }.withName(pName) + + gracePeriodSeconds match { + case Some(period) => podToDelete.withGracePeriod(period).delete() + case _ => podToDelete.delete() + } + } + + override def executeOnGlob(pods: List[Pod], namespace: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit = { + if (pods.nonEmpty) { + pods.foreach { pod => printMessage(s"Deleting driver pod: ${pod.getMetadata.getName}.") } + val nClient = { + namespace match { + case Some(ns) => client.pods.inNamespace(ns) + case None => client.pods + } + } + + val gracePeriodSeconds = getGracePeriod(sparkConf) + gracePeriodSeconds match { + case Some(period) => + // this is not using the batch api because no option is provided + // when using the grace period. + pods.foreach { pod => + nClient + .withName(pod.getMetadata.getName) + .withGracePeriod(period) + .delete() + } + case _ => nClient.delete(pods.asJava) + } + } else { + printMessage("No applications found.") + } + } +} + +private class ListStatus extends K8sSubmitOp { + override def executeOnPod(pName: String, namespace: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit = { + val pod = { + namespace match { + case Some(ns) => client.pods.inNamespace(ns) + case None => client.pods + } + }.withName(pName) + .get() + + printMessage("Application status (driver): " + + Option(pod).map(formatPodState).getOrElse("unknown.")) + } + + override def executeOnGlob(pods: List[Pod], ns: Option[String], sparkConf: SparkConf) + (implicit client: KubernetesClient): Unit = { + if (pods.nonEmpty) { + for (pod <- pods) { + printMessage("Application status (driver): " + + Option(pod).map(formatPodState).getOrElse("unknown.")) + } + } else { + printMessage("No applications found.") + } + } +} + +private[spark] class K8SSparkSubmitOperation extends SparkSubmitOperation + with CommandLineLoggingUtils { + + private def isGlob(name: String): Boolean = { + name.last == '*' + } + + def execute(submissionId: String, sparkConf: SparkConf, op: K8sSubmitOp): Unit = { + val master = KubernetesUtils.parseMasterUrl(sparkConf.get("spark.master")) + val sIdParts = submissionId.split(":") match { + case Array(ns, pName) => Some((Some(ns), pName)) + case Array(pName) => Some((None, pName)) + case _ => None + } + + sIdParts match { + case Some((namespace, pName)) => + Utils.tryWithResource(SparkKubernetesClientFactory.createKubernetesClient( + master, + namespace, + KUBERNETES_AUTH_SUBMISSION_CONF_PREFIX, + SparkKubernetesClientFactory.ClientType.Submission, + sparkConf, + None, + None) + ) { kubernetesClient => + implicit val client: KubernetesClient = kubernetesClient + if (isGlob(pName)) { + val ops = namespace match { + case Some(ns) => + kubernetesClient + .pods + .inNamespace(ns) + case None => + kubernetesClient + .pods + } + val pods = ops + .list() + .getItems + .asScala + .filter { pod => + val meta = pod.getMetadata + meta.getName.startsWith(pName.stripSuffix("*")) && + meta.getLabels.get(SPARK_ROLE_LABEL) == SPARK_POD_DRIVER_ROLE + }.toList + op.executeOnGlob(pods, namespace, sparkConf) + } else { + op.executeOnPod(pName, namespace, sparkConf) + } + } + case _ => printErrorAndExit(s"submissionId: {$submissionId} is invalid.") + } + } + + override def kill(submissionToKill: String, conf: SparkConf): Unit = { + printMessage(s"Submitting a request to kill submission " + + s"${submissionToKill} in ${conf.get("spark.master")}. " + + s"Grace period in secs: ${getGracePeriod(conf).getOrElse("not set.")}") + execute(submissionToKill, conf, new KillApplication) + } + + override def printSubmissionStatus(submissionToPrintStatusFor: String, conf: SparkConf): Unit = { Review comment: Fixed that. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
