You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "anmolxlight (via GitHub)" <gi...@apache.org> on 2026/05/10 11:15:12 UTC

[PR] Fix KubernetesPodTrigger pod terminal state handling [airflow]

anmolxlight opened a new pull request, #66650:
URL: https://github.com/apache/airflow/pull/66650

   ## Summary
   - Add pod-level terminal-state detection to `KubernetesPodTrigger`
   - Use pod phase to stop polling when Kubernetes marks the pod as `Failed` or `Succeeded`
   - Keep container-state fallback for non-terminal pod phases
   - Update deferrable operator pre-deferral terminal-state check to use pod state
   
   ## Tests
   - `uv run --project providers/cncf/kubernetes pytest providers/cncf/kubernetes/tests/unit/cncf/kubernetes/triggers/test_pod.py -q`
   - `uv run --project providers/cncf/kubernetes pytest --with-db-init providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py::TestKubernetesPodOperatorAsync::test_async_create_pod_should_execute_successfully -q`
   
   Closes #66624
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KubernetesPodTrigger pod terminal state handling [airflow]

Posted by "anmolxlight (via GitHub)" <gi...@apache.org>.
anmolxlight commented on code in PR #66650:
URL: https://github.com/apache/airflow/pull/66650#discussion_r3215047437


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -508,6 +508,18 @@ def define_container_state(self, pod: V1Pod) -> ContainerState:
                 return ContainerState.TERMINATED if state_obj.exit_code == 0 else ContainerState.FAILED
         return ContainerState.UNDEFINED
 
+    def define_pod_state(self, pod: V1Pod) -> ContainerState:

Review Comment:
   Renamed the method to `define_pod_container_state()` and renamed local variables to `pod_container_state` so the combined pod/container check is explicit.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -508,6 +508,18 @@ def define_container_state(self, pod: V1Pod) -> ContainerState:
                 return ContainerState.TERMINATED if state_obj.exit_code == 0 else ContainerState.FAILED
         return ContainerState.UNDEFINED
 
+    def define_pod_state(self, pod: V1Pod) -> ContainerState:
+        if pod.status is None:
+            return ContainerState.UNDEFINED
+
+        if pod.status.phase == PodPhase.SUCCEEDED:
+            return ContainerState.TERMINATED
+
+        if pod.status.phase == PodPhase.FAILED:
+            return ContainerState.FAILED
+
+        return self.define_container_state(pod)

Review Comment:
   Added explicit coverage for `Succeeded` pod phase and non-terminal pod phase fallback to `define_container_state()`.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -307,7 +307,7 @@ async def _wait_for_container_completion(self) -> TriggerEvent:
             time_get_more_logs = time_begin + datetime.timedelta(seconds=self.logging_interval)
         while True:
             pod = await self._get_pod()
-            container_state = self.define_container_state(pod)
+            container_state = self.define_pod_state(pod)
             if container_state == ContainerState.TERMINATED:
                 return TriggerEvent(
                     {

Review Comment:
   Renamed the loop variable to `pod_container_state` and updated the failure/debug wording to reflect pod-or-container state.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -925,7 +925,7 @@ def invoke_defer_method(
             logging_interval=self.logging_interval,
             trigger_kwargs=self.trigger_kwargs,
         )
-        container_state = trigger.define_container_state(self.pod) if self.pod else None
+        container_state = trigger.define_pod_state(self.pod) if self.pod else None
         if context and (
             container_state == ContainerState.TERMINATED or container_state == ContainerState.FAILED
         ):

Review Comment:
   Renamed the operator pre-deferral variable and related test mock to `pod_container_state`/`mocked_pod_container_state`; event message wording now also reflects pod-or-container state.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KubernetesPodTrigger pod terminal state handling [airflow]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #66650:
URL: https://github.com/apache/airflow/pull/66650#discussion_r3215019203


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -508,6 +508,18 @@ def define_container_state(self, pod: V1Pod) -> ContainerState:
                 return ContainerState.TERMINATED if state_obj.exit_code == 0 else ContainerState.FAILED
         return ContainerState.UNDEFINED
 
+    def define_pod_state(self, pod: V1Pod) -> ContainerState:
+        if pod.status is None:
+            return ContainerState.UNDEFINED
+
+        if pod.status.phase == PodPhase.SUCCEEDED:
+            return ContainerState.TERMINATED
+
+        if pod.status.phase == PodPhase.FAILED:
+            return ContainerState.FAILED
+
+        return self.define_container_state(pod)

Review Comment:
   `define_pod_state()` introduces new branching behavior (Succeeded/Failed short-circuit) but the unit tests only assert the `Failed` branch. Please add coverage for at least the `Succeeded` branch and a non-terminal phase that falls back to `define_container_state`, to ensure the new logic behaves as intended.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -307,7 +307,7 @@ async def _wait_for_container_completion(self) -> TriggerEvent:
             time_get_more_logs = time_begin + datetime.timedelta(seconds=self.logging_interval)
         while True:
             pod = await self._get_pod()
-            container_state = self.define_container_state(pod)
+            container_state = self.define_pod_state(pod)
             if container_state == ContainerState.TERMINATED:
                 return TriggerEvent(
                     {

Review Comment:
   This variable now represents the *pod* terminal state (via `define_pod_state()`), not just the base container state. Renaming `container_state` (and corresponding log/message wording where applicable) would make the control flow easier to follow and reduce misinterpretation during future changes.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -925,7 +925,7 @@ def invoke_defer_method(
             logging_interval=self.logging_interval,
             trigger_kwargs=self.trigger_kwargs,
         )
-        container_state = trigger.define_container_state(self.pod) if self.pod else None
+        container_state = trigger.define_pod_state(self.pod) if self.pod else None
         if context and (
             container_state == ContainerState.TERMINATED or container_state == ContainerState.FAILED
         ):

Review Comment:
   `container_state` is now computed via `define_pod_state(self.pod)` and can represent a pod-level terminal phase. Renaming this variable (and the related `mocked_container_state` in tests) to `pod_state`/`terminal_state` would better reflect what is being checked before deferring.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KubernetesPodTrigger pod terminal state handling [airflow]

Posted by "jscheffl (via GitHub)" <gi...@apache.org>.
jscheffl commented on code in PR #66650:
URL: https://github.com/apache/airflow/pull/66650#discussion_r3215015021


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/triggers/pod.py:
##########
@@ -508,6 +508,18 @@ def define_container_state(self, pod: V1Pod) -> ContainerState:
                 return ContainerState.TERMINATED if state_obj.exit_code == 0 else ContainerState.FAILED
         return ContainerState.UNDEFINED
 
+    def define_pod_state(self, pod: V1Pod) -> ContainerState:

Review Comment:
   Can you please rename the fucntion? I was reading the code three times and then was checking what the actual bug was to understand what it means. So what you actually want to make is to check the Pod and Container state and correlate?
   And the resulting variable is then not the "cotnainer_state" but the combined (workload?) state.
   
   We should take a naming (and naming is usually hard) that the code makes understandable what is being done.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KubernetesPodTrigger pod terminal state handling [airflow]

Posted by "jscheffl (via GitHub)" <gi...@apache.org>.
jscheffl merged PR #66650:
URL: https://github.com/apache/airflow/pull/66650


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org