Skip to content

Retry KubernetesPodOperator pod launch when preempted before it starts - #70274

Open
imrichardwu wants to merge 2 commits into
apache:mainfrom
imrichardwu:fix/k8s-pod-operator-404
Open

Retry KubernetesPodOperator pod launch when preempted before it starts#70274
imrichardwu wants to merge 2 commits into
apache:mainfrom
imrichardwu:fix/k8s-pod-operator-404

Conversation

@imrichardwu

Copy link
Copy Markdown
Contributor

On autoscaling Kubernetes clusters (for example, Karpenter), a KubernetesPodOperator task pod can be preempted by a higher-priority DaemonSet before it ever starts running. Today, that surfaces as a raw ApiException(404) and fails the task immediately.

This PR adds an opt-in retry_on_pod_not_found flag (with pod_not_found_max_retries) to relaunch the pod only if it disappeared before the base container could have started, making retries safe because no user workload has executed. If the base container may have started, the task behaves exactly as it does today and the pod is never recreated.

The retry logic works in both synchronous and deferrable (deferrable=True) execution modes with exponential backoff between attempts.

This PR also fixes AsyncKubernetesHook.get_pod() to raise PodNotFoundException on HTTP 404, matching the synchronous implementation.

related: #59626


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@boring-cyborg boring-cyborg Bot added area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues labels Jul 23, 2026
@imrichardwu
imrichardwu force-pushed the fix/k8s-pod-operator-404 branch from 090255f to 8c91190 Compare July 23, 2026 16:02
@github-actions

Copy link
Copy Markdown
Contributor

uv.lock on main just moved via #70652 ("Review and update constraint dependencies"), commit 443b919 and this PR currently conflicts.

Quickest fix:

git fetch upstream main && git rebase upstream/main
rm uv.lock && uv lock
git add uv.lock && git rebase --continue
git push --force-with-lease

Automated nudge — ignore if you're not ready to rebase. This comment is updated in place on future uv.lock bumps.

- Added `retry_on_pod_not_found` parameter to `KubernetesPodOperator` to allow automatic relaunching of pods that disappear before their base container starts, addressing issues with pod preemption or eviction.
- Introduced `pod_not_found_max_retries` parameter to control the maximum number of relaunch attempts.
- Updated `AsyncKubernetesHook` to raise `PodNotFoundException` for 404 errors, enabling better error handling.
- Enhanced unit tests to cover new retry behavior and validate correct exception handling for pod not found scenarios.
- Introduced optional dependency group "standard" in both `uv.lock` and `pyproject.toml` to include `apache-airflow-providers-standard`.
- Updated documentation to reflect the new optional dependencies and installation instructions for users.
- Modified unit tests to ensure compatibility with the new dependency structure.
@imrichardwu
imrichardwu force-pushed the fix/k8s-pod-operator-404 branch from 88ebd0c to b793741 Compare July 29, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant