pan3793 commented on code in PR #4625:
URL: https://github.com/apache/kyuubi/pull/4625#discussion_r1152712617


##########
kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KubernetesApplicationOperation.scala:
##########
@@ -127,22 +137,68 @@ class KubernetesApplicationOperation extends 
ApplicationOperation with Logging {
     }
   }
 
-  private def findDriverPodByTag(tag: String): util.List[Pod] = {
-    val podList = kubernetesClient.pods()
-      .withLabel(KubernetesApplicationOperation.LABEL_KYUUBI_UNIQUE_KEY, 
tag).list().getItems
-    val size = podList.size()
-    if (size != 1) {
-      warn(s"Get Tag: ${tag} Driver Pod In Kubernetes size: ${size}, we expect 
1")
+  override def stop(): Unit = {
+    try {
+      if (driverInformer != null) {
+        driverInformer.stop()
+      }
+      if (kubernetesClient != null) {
+        kubernetesClient.close()
+      }
+      if (deletedAppInfoCache != null) {
+        deletedAppInfoCache.cleanUp()
+      }
+    } catch {
+      case e: Exception => error(e.getMessage)
     }
-    podList
   }
 
-  override def stop(): Unit = {
-    if (kubernetesClient != null) {
-      try {
-        kubernetesClient.close()
-      } catch {
-        case e: Exception => error(e.getMessage)
+  private class SparkEnginePodEventHandler extends ResourceEventHandler[Pod] {
+    private def isSparkEnginePod(pod: Pod): Boolean = {
+      pod.getMetadata.getLabels.containsKey(LABEL_KYUUBI_UNIQUE_KEY)
+    }
+
+    private def updateState(pod: Pod): Unit = {
+      val metaData = pod.getMetadata
+      val state = toApplicationState(pod.getStatus.getPhase)
+      debug(s"Driver Informer change pod: ${metaData.getName} state: $state")
+      appInfoStore.put(
+        metaData.getLabels.get(LABEL_KYUUBI_UNIQUE_KEY),
+        ApplicationInfo(
+          id = metaData.getLabels.get(SPARK_APP_ID_LABEL),
+          name = metaData.getName,
+          state = state,
+          error = Option(pod.getStatus.getReason)))
+    }
+
+    private def markDeleted(pod: Pod): Unit = {
+      deletedAppInfoCache.put(
+        pod.getMetadata.getLabels.get(LABEL_KYUUBI_UNIQUE_KEY),
+        toApplicationState(pod.getStatus.getPhase))
+    }
+
+    override def onAdd(pod: Pod): Unit = {
+      if (isSparkEnginePod(pod)) {
+        updateState(pod)
+      }
+    }
+
+    override def onUpdate(oldPod: Pod, newPod: Pod): Unit = {
+      if (isSparkEnginePod(newPod)) {
+        updateState(newPod)
+        toApplicationState(newPod.getStatus.getPhase) match {
+          case FINISHED | FAILED | UNKNOWN =>
+            markDeleted(newPod)

Review Comment:
   And from the K8s docs description about Pod state, "Unknown" sounds like a 
transient error
   
   > Unknown 
   >
   > For some reason the state of the Pod could not be obtained. This phase 
typically occurs due to an error in communicating with the node where the Pod 
should be running.
   
   
   



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to