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


##########
kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KubernetesApplicationOperation.scala:
##########
@@ -295,16 +314,64 @@ object KubernetesApplicationOperation extends Logging {
 
   def toLabel(tag: String): String = s"label: $LABEL_KYUUBI_UNIQUE_KEY=$tag"
 
-  def toApplicationState(state: String): ApplicationState = state match {
-    // 
https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/types.go#L2396
-    // https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/
+  def toApplicationState(
+      pod: Pod,
+      appStateFromContainer: Boolean,
+      appStateContainer: String): ApplicationState = {
+    toApplicationStateAndError(pod, appStateFromContainer, 
appStateContainer)._1
+  }
+
+  def toApplicationStateAndError(
+      pod: Pod,
+      appStateFromContainer: Boolean,
+      appStateContainer: String): (ApplicationState, Option[String]) = {
+    val containerStateToBuildAppState = if (appStateFromContainer) {
+      pod.getStatus.getContainerStatuses.asScala.find(_.getState == 
appStateContainer).map(
+        _.getState)
+    } else {
+      None
+    }
+    val applicationState = 
containerStateToBuildAppState.map(containerStateToApplicationState)
+      .getOrElse(podStateToApplicationState(pod.getStatus.getPhase))
+    val applicationError = 
containerStateToBuildAppState.map(containerStateToApplicationError)
+      .getOrElse(Option(pod.getStatus.getReason))
+    applicationState -> applicationError
+  }
+
+  def containerStateToApplicationState(containerState: ContainerState): 
ApplicationState = {
+    // 
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-states
+    if (containerState.getWaiting != null) {
+      PENDING
+    } else if (containerState.getRunning != null) {
+      RUNNING
+    } else {
+      Option(containerState.getTerminated) match {
+        case Some(terminated) =>
+          if (terminated.getExitCode == 0) {
+            FINISHED
+          } else {
+            FAILED
+          }
+        case None => UNKNOWN
+      }
+    }

Review Comment:
   ```suggestion
       } else if (containerState.getTerminated == null) {
         UNKNOWN
       } else if (containerState.getTerminated.getExitCode == 0) {
         FINISHED
       } else {
         FAILED
       }
   ```



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