eladkal commented on code in PR #28161:
URL: https://github.com/apache/airflow/pull/28161#discussion_r1080630500


##########
airflow/executors/kubernetes_executor.py:
##########
@@ -773,6 +775,57 @@ def _change_state(self, key: TaskInstanceKey, state: str | 
None, pod_id: str, na
             # do this once, so only do it when we remove the task from running
             self.event_buffer[key] = state, None
 
+    @staticmethod
+    def _get_pod_namespace(ti: TaskInstance):
+        pod_override = ti.executor_config.get("pod_override")
+        namespace = None
+        with suppress(Exception):
+            namespace = pod_override.metadata.namespace
+        return namespace or conf.get("kubernetes_executor", "namespace", 
fallback="default")
+
+    def get_task_log(self, ti: TaskInstance, log: str = "") -> str | 
tuple[str, dict[str, bool]]:
+
+        try:
+            from airflow.kubernetes.pod_generator import PodGenerator
+
+            client = get_kube_client()
+
+            log += f"*** Trying to get logs (last 100 lines) from worker pod 
{ti.hostname} ***\n\n"
+            selector = PodGenerator.build_selector_for_k8s_executor_pod(
+                dag_id=ti.dag_id,
+                task_id=ti.task_id,
+                try_number=ti.try_number,
+                map_index=ti.map_index,
+                run_id=ti.run_id,
+                airflow_worker=ti.queued_by_job_id,
+            )
+            namespace = self._get_pod_namespace(ti)
+            pod_list = client.list_namespaced_pod(
+                namespace=namespace,
+                label_selector=selector,
+            ).items
+            if not pod_list:
+                raise RuntimeError("Cannot find pod for ti %s", ti)
+            elif len(pod_list) > 1:
+                raise RuntimeError("Found multiple pods for ti %s: %s", ti, 
pod_list)

Review Comment:
   Not really part of this PR but feels like the right place to ask.
   
   Why do we raise these exceptions and not write the issue to the log and 
return it? (Like lines 285-287)
   
   ```
           except Exception as f:
               log += f"*** Unable to fetch logs from worker pod {ti.hostname} 
***\n{str(f)}\n\n"
               return log, {"end_of_log": True}
   ```
   
   I wonder if this is the reason users sometimes don't see the task log and it 
makes them harder to find the root cause like in 
https://github.com/apache/airflow/issues/29025 ?



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