-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Use labels instead of pod name for pod log read in k8s exec #28546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import logging | ||
| import os | ||
| import warnings | ||
| from contextlib import suppress | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any | ||
| from urllib.parse import urljoin | ||
|
|
@@ -191,19 +192,32 @@ def _read(self, ti: TaskInstance, try_number: int, metadata: dict[str, Any] | No | |
| log += f"*** {str(e)}\n" | ||
| return log, {"end_of_log": True} | ||
| elif self._should_check_k8s(ti.queue): | ||
| pod_override = ti.executor_config.get("pod_override") | ||
| if pod_override and pod_override.metadata and pod_override.metadata.namespace: | ||
| namespace = pod_override.metadata.namespace | ||
| else: | ||
| namespace = conf.get("kubernetes_executor", "namespace") | ||
| try: | ||
| from airflow.kubernetes.kube_client import get_kube_client | ||
| from airflow.kubernetes.pod_generator import PodGenerator | ||
|
|
||
| kube_client = get_kube_client() | ||
| client = get_kube_client() | ||
|
|
||
| log += f"*** Trying to get logs (last 100 lines) from worker pod {ti.hostname} ***\n\n" | ||
| res = kube_client.read_namespaced_pod_log( | ||
| name=ti.hostname, | ||
| selector = PodGenerator.build_selector_for_k8s_executor_pod( | ||
dstandish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
| res = client.read_namespaced_pod_log( | ||
| name=pod_list[0].metadata.name, | ||
dstandish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| namespace=namespace, | ||
| container="base", | ||
| follow=False, | ||
|
|
@@ -272,6 +286,14 @@ def _read(self, ti: TaskInstance, try_number: int, metadata: dict[str, Any] | No | |
|
|
||
| return log, {"end_of_log": end_of_log, "log_pos": log_pos} | ||
|
|
||
| @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") | ||
|
|
||
| @staticmethod | ||
| def _get_log_retrieval_url(ti: TaskInstance, log_relative_path: str) -> str: | ||
| url = urljoin( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.