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

   ## Why
   
   `airflow kubernetes cleanup-pods` is normally run unattended from a CronJob. 
When it cannot reach a kube config — no `~/.kube/config`, `in_cluster` set but 
not actually running in a pod, an expired `exec` credential plugin — 
`get_kube_client()` raises `kubernetes.config.ConfigException` and nothing 
catches it, so the job log gets a ten-frame traceback that reads like an 
Airflow crash instead of the environment problem it is:
   
   ```
   $ airflow kubernetes cleanup-pods --namespace default
   Traceback (most recent call last):
     ... 9 frames ...
   kubernetes.config.config_exception.ConfigException: Invalid kube-config 
file. No configuration found.
   ```
   
   The rest of the CLI already answers this class of user error with a single 
line (e.g. `dags test --conf` on invalid JSON), so this is just a gap.
   
   There is a second way the same exception escapes: `_delete_pod` called 
`get_kube_client()` itself, once per deleted pod, and the surrounding `except 
ApiException` does not cover `ConfigException`. A cleanup run can therefore die 
with the same raw traceback halfway through, after deleting some pods, if the 
kube config becomes unloadable mid-run (exec-credential plugin failure, token 
rotation).
   
   ## What
   
   
`providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py`:
   
   - `cleanup_pods` catches `ConfigException` from `get_kube_client()` and 
exits with `SystemExit(f"Could not load Kubernetes configuration: {e}")` — one 
line on stderr, exit code still 1, stdout untouched. Only config-loading 
failures are caught; API and network errors still surface as before.
   - `_delete_pod` takes the already-built client instead of calling 
`get_kube_client()` again. This leaves a single config-loading site in the 
command (the guarded one) and drops the kube config from being re-read once per 
deleted pod.
   
   Tests cover both: `test_unloadable_kube_config_exits_with_message` asserts 
the exact message, and 
`test_kube_config_is_loaded_once_and_reused_for_deletions` uses two pods so it 
fails (3 calls) without the `_delete_pod` change. Existing `_delete_pod` 
assertions were updated for the new argument.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Opus 5)
   
   Generated-by: Claude Code (Opus 5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


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