o-nikolas commented on code in PR #72511:
URL: https://github.com/apache/airflow/pull/72511#discussion_r4675783020


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -1306,3 +1319,63 @@ def _emit_container_logs(self, logs: list[str], now: 
DateTime, container_name: s
                 else:
                     level = _parse_log_level(message_to_log)
                     self.log.log(level, "[%s] %s", container_name, 
message_to_log)
+
+    def get_init_container_names(self, pod: V1Pod) -> list[str]:
+        """
+        Return init container names from the pod spec.
+
+        :meta private:
+        """
+        return [container_spec.name for container_spec in 
pod.spec.init_containers or []]
+
+    async def fetch_requested_init_container_logs(
+        self,
+        pod: V1Pod,
+        init_containers: Iterable[str] | str | Literal[True] | None,
+        poll_interval: float = 1,
+    ) -> None:
+        """
+        Stream logs for the requested init containers, one at a time, until 
each completes.
+
+        Init containers run sequentially before the base container starts, so 
they are
+        processed in ``spec.initContainers`` order, mirroring the sync 
``PodManager``'s
+        ``fetch_requested_init_container_logs``.
+
+        :meta private:
+        """
+        all_containers = self.get_init_container_names(pod)
+        containers_to_log = reconcile_requested_log_containers(
+            self.log, init_containers, all_containers, pod.metadata.name
+        )
+        containers_to_log = sorted(containers_to_log, key=lambda cn: 
all_containers.index(cn))
+        for container_name in containers_to_log:
+            await self._await_init_container_start(pod, container_name)
+            await self._stream_init_container_logs_until_completion(pod, 
container_name, poll_interval)
+
+    async def _await_init_container_start(self, pod: V1Pod, container_name: 
str) -> None:
+        while True:
+            remote_pod = await self.read_pod(pod)
+            if (
+                remote_pod.status is not None
+                and remote_pod.status.phase != PodPhase.PENDING
+                and get_container_status(remote_pod, container_name) is not 
None
+                and not container_is_wait(remote_pod, container_name)
+            ):
+                return
+            await asyncio.sleep(1)

Review Comment:
   I think this loop can run forever. KPO pods default to `restartPolicy: 
Never`, so if an earlier init container fails, the pod goes to `Failed` and the 
later init containers stay `waiting` (`PodInitializing`) for good. With 
`init_container_logs=True` and no `execution_timeout`, the task then stays 
deferred indefinitely, where today the trigger reports the failure. The sync 
helper has the same loop, but this is new for the deferrable path.
   
   Could we return here once the pod phase is `Failed` or `Succeeded`, so the 
trigger falls through to `define_pod_container_state`, and add a test for that 
case?



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

Reply via email to