amoghrajesh opened a new pull request, #69914:
URL: https://github.com/apache/airflow/pull/69914
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull
request.
Write a good git commit message following this guide:
http://chris.beams.io/posts/git-commit/
Please make sure that your code changes are covered with tests.
And in case of new features or big changes remember to adjust the
documentation.
For user-facing UI changes, please attach before/after screenshots (or a
short
screen recording) so reviewers can assess the visual impact.
Feel free to ping (in general) for the review if you do not see reaction for
a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we
sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change below checkbox to `[X]` followed by the name of the tool, uncomment
the "Generated-by".
-->
- [x] Yes Sonnet 4.5
<!--
Generated-by: [Tool Name] following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
-->
### Why?
KPO's existing `reattach_on_restart` relies on a label search on every
retry, which can raise `FoundMoreThanOnePodFailure` when more than one matching
pod exists, and gives no way to reconnect to a specific pod unambiguously. On
Airflow 3.3+, persisting the running pod's identity to task state store lets a
retry reconnect directly to that exact pod instead, removing the ambiguity
window entirely. `reattach_on_restart` is deprecated in favor of a new durable
flag that supersedes it; existing configurations map onto durable with zero
behavior change, and pre-3.3 environments keep today's label-search behavior
unchanged.
### Summary
- Adds a `durable` parameter to `KubernetesPodOperator` (default `True`),
superseding the
deprecated `reattach_on_restart` parameter.
- On Airflow 3.3+, `durable=True` persists the running pod's identity (name,
namespace, uid)
to task state store before the operator waits on it. On retry, the
operator reconnects
directly to that pod via a direct `read_namespaced_pod` lookup instead of
a label search --
removing the `FoundMoreThanOnePodFailure` ambiguity window a label search
can hit when more
than one matching pod exists.
- The existing label-search mechanism (`find_pod()`) is retained as a
one-time bootstrap
fallback: a pod created by a pre-upgrade provider version (no persisted
identity yet) is
still found via labels on the first retry after upgrading, and that retry
starts persisting
state for every subsequent one. No in-flight task or existing pod breaks
across an upgrade.
- `reattach_on_restart` still works, mapping its value onto `durable` with
zero behavior
change; passing it emits `AirflowProviderDeprecationWarning` on Airflow
3.3+ only (below
3.3, `durable`/task state store don't functionally exist yet, so warning
would be
premature).
- `durable=False` opts out of task state store entirely, preserving today's
`reattach_on_restart=False`
behavior exactly, including its pre-existing incidental-cleanup quirk (an
orphaned pod from
a killed prior attempt can still get found and deleted via unconditional
`find_pod()`
refetch calls elsewhere in `execute_sync`/`execute_async` -- unrelated to
and unchanged by
this work).
- Docs: added a "Durable execution" section to KPO's operator docs,
cross-linking task state
store.
### Testing
Using this dag:
```python
from datetime import datetime, timedelta
from airflow.providers.cncf.kubernetes.operators.pod import
KubernetesPodOperator
from airflow.sdk import DAG
with DAG(
dag_id="try_kpo",
schedule=None,
start_date=datetime(2024, 1, 1),
catchup=False,
) as dag:
run_pod = KubernetesPodOperator(
task_id="run_pod",
name="try-kpo",
namespace="default",
image="ubuntu:24.04",
cmds=["bash", "-cx"],
arguments=["sleep 60 && echo done"],
in_cluster=False,
do_xcom_push=False,
retries=2,
retry_delay=timedelta(seconds=5),
)
```
### Before my changes:
Worker killed:
<img width="1985" height="914" alt="image"
src="https://github.com/user-attachments/assets/d69f6788-3f07-4959-8506-22198966c15d"
/>
But pod still exists:
<img width="2540" height="202" alt="image"
src="https://github.com/user-attachments/assets/91236ea0-a6b6-4f4b-8e2e-c5145669f0e4"
/>
Reconnected when worker came back up:
<img width="1985" height="914" alt="image"
src="https://github.com/user-attachments/assets/61a4422c-6ce0-43c3-84dd-2838a6f0d021"
/>
### After my changes (changing to pass durable=True)
```python
run_pod = KubernetesPodOperator(
task_id="run_pod",
name="try-kpo",
namespace="default",
image="ubuntu:24.04",
cmds=["bash", "-cx"],
arguments=["sleep 60 && echo done"],
in_cluster=False,
do_xcom_push=False,
retries=2,
retry_delay=timedelta(seconds=5),
durable=True,
)
```
WOrker killed:
<img width="2544" height="988" alt="image"
src="https://github.com/user-attachments/assets/e0df6342-4b97-44b3-8a29-255bd02373df"
/>
Task store persisted:
<img width="2544" height="988" alt="image"
src="https://github.com/user-attachments/assets/761d9cdf-ce3b-4014-9ad8-902f37322c58"
/>
```shell
[Breeze:3.10.20] root@0014bf761b80:/opt/airflow$ cat
/tmp/airflow_state/ti_run_pod/pod_identifier.json
{"name": "try-kpo-vlap9a9c", "namespace": "default", "uid":
"878b321f-fb21-4779-94c4-1811f7ced720"}[Breeze:3.10.20]
```
Pod present and reconnected to via state store:
<img width="2544" height="988" alt="image"
src="https://github.com/user-attachments/assets/07a07bec-df4d-43ce-88db-901950848666"
/>
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#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](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
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]