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_r253884633
########## File path: resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/submit/K8sSubmitOps.scala ########## @@ -0,0 +1,134 @@ +/* + * 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 io.fabric8.kubernetes.api.model.Pod +import io.fabric8.kubernetes.client.KubernetesClient + +import org.apache.spark.SparkConf +import org.apache.spark.deploy.k8s.{Constants, KubernetesUtils, SparkKubernetesClientFactory} +import org.apache.spark.deploy.k8s.Config.KUBERNETES_AUTH_SUBMISSION_CONF_PREFIX +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 [spark] sealed trait K8sSubmitOp extends CommandLineLoggingUtils { + def executeOnPod(pName: String, ns: String)(implicit client: KubernetesClient): Unit + def executeOnGlob(pods: List[Pod], ns: String)(implicit client: KubernetesClient): Unit +} + +private [spark] class KillApplication extends K8sSubmitOp { + override def executeOnPod(pName: String, ns: String) + (implicit client: KubernetesClient): Unit = { + client + .pods + .inNamespace(ns) + .withName(pName) + .delete() + } + + override def executeOnGlob(pods: List[Pod], ns: String) + (implicit client: KubernetesClient): Unit = { + if (pods.nonEmpty) { + pods.foreach(pod => printMessage(s"Deleting driver pod: ${pod.getMetadata.getName}.")) + client + .pods + .inNamespace(ns) + .delete(pods.asJava) + } else { + printMessage("No applications found.") + } + } +} + +private [spark] class ListStatus extends K8sSubmitOp { + override def executeOnPod(pName: String, ns: String) + (implicit client: KubernetesClient): Unit = { + val pod = client + .pods + .inNamespace(ns) + .withName(pName) + .get() + + printMessage("Application status (driver): " + + Option(pod).map(formatPodState).getOrElse("unknown.")) + } + + override def executeOnGlob(pods: List[Pod], ns: String) + (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] object K8sSubmitOps extends 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((ns, pName)) + case Array(pName) => Some(("default", pName)) + case _ => None + } + + sIdParts match { + case Some((ns, pName)) => + Utils.tryWithResource(SparkKubernetesClientFactory.createKubernetesClient( + master, + Some(ns), + KUBERNETES_AUTH_SUBMISSION_CONF_PREFIX, + sparkConf, + None, + None)) { kubernetesClient => + implicit val client: KubernetesClient = kubernetesClient + if (isGlob(pName)) { + val pods = { + kubernetesClient + .pods + .inNamespace(ns) + .list() + .getItems + .asScala + .filter { pod => + val meta = pod.getMetadata + meta.getName.startsWith(pName.stripSuffix("*")) && + meta + .getLabels + .asScala + .exists(_.equals((SPARK_ROLE_LABEL, SPARK_POD_DRIVER_ROLE))) Review comment: getLabels returns a java map so it is not easy to compare both the key and the value at the same statement. I turn it to a scala map so I can check for existence. Contains on the scala map uses the key only. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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]
