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_r263797923
 
 

 ##########
 File path: 
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/submit/K8sSubmitOps.scala
 ##########
 @@ -0,0 +1,178 @@
+/*
+ * 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.{SparkSubmitArguments, 
SparkSubmitOperationsClient}
+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[spark] sealed trait K8sSubmitOp extends CommandLineLoggingUtils {
+  def executeOnPod(pName: String, ns: String, gracePeriodSeconds: Option[Long])
+      (implicit client: KubernetesClient): Unit
+  def executeOnGlob(pods: List[Pod], ns: String, gracePeriodSeconds: 
Option[Long])
+      (implicit client: KubernetesClient): Unit
+}
+
+private[spark] class KillApplication extends K8sSubmitOp  {
+  override def executeOnPod(pName: String, ns: String, gracePeriodSeconds: 
Option[Long])
+      (implicit client: KubernetesClient): Unit = {
+    val podToDelete = client
+      .pods
+      .inNamespace(ns)
+      .withName(pName)
+      gracePeriodSeconds match {
+        case Some(period) => podToDelete.withGracePeriod(period).delete()
+        case _ => podToDelete.delete()
+      }
+  }
+
+  override def executeOnGlob(pods: List[Pod], ns: String, gracePeriodSeconds: 
Option[Long])
+      (implicit client: KubernetesClient): Unit = {
+    if (pods.nonEmpty) {
+      pods.foreach { pod => printMessage(s"Deleting driver pod: 
${pod.getMetadata.getName}.") }
+      val nClient = client.pods.inNamespace(ns)
+      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[spark] class ListStatus extends K8sSubmitOp {
+  override def executeOnPod(pName: String, ns: String, gracePerio: 
Option[Long])
+      (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, gracePeriod: 
Option[Long])
+      (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 K8sSparkSubmitOperationsClient extends 
SparkSubmitOperationsClient
+  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((ns, pName))
+      case Array(pName) => Some(("default", pName))
 
 Review comment:
   This is broken on minikube btw: 
https://github.com/kubernetes/minikube/issues/2117.
   AFAIK from the integration tests and the `KubeConfigBackend`  logic, the ns 
used if none is specified from fabric8io  client is the "default".
   Otherwise I would need to load the config then check if namespace exists 
etc. Right now I just re-use the code at the backend, which will pick the 
.kub/conf config by default and then override the ns value with whatever I 
pass. I am using `SparkKubernetesClientFactory` so the user can set 
`spark.kubernetes.context` and the namespace will be picked up depending the 
context but it will be overridden again by the submitId ns. I can relax this 
but will have to detect if I am reading from some specific context or not. 

----------------------------------------------------------------
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]

Reply via email to