kaxil commented on code in PR #77197:
URL: https://github.com/apache/airflow/pull/77197#discussion_r4701852335
##########
providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py:
##########
@@ -2870,6 +2881,32 @@ def
test_durable_reconnect_terminal_state_runs_existing_handling(
self.create_mock.assert_called_once_with(pod=mock_pod_request_obj)
assert result == mock_pod_request_obj
+ @patch(f"{KPO_MODULE}.KubernetesPodOperator.process_pod_deletion")
+ def test_durable_reconnect_reuses_succeeded_pod_without_xcom(self,
mock_process_pod_deletion):
+ k = KubernetesPodOperator(
+ image="ubuntu:16.04",
+ cmds=["bash", "-cx"],
+ arguments=["echo 10"],
+ task_id="task",
+ name="hello",
+ )
+ context = create_context(k)
+ task_state_store = MagicMock()
+ task_state_store.get.return_value = {"name": "prior-pod", "namespace":
"default"}
+ context["task_state_store"] = task_state_store
+
+ succeeded_pod = MagicMock()
+ succeeded_pod.status.phase = PodPhase.SUCCEEDED
+ succeeded_pod.status.reason = None
+ succeeded_pod.metadata.name = "prior-pod"
+ k.hook.get_pod = MagicMock(return_value=succeeded_pod)
+
+ result = k.get_or_create_pod(pod_request_obj=MagicMock(),
context=context)
Review Comment:
Without the fix this fails with a yaml `RepresenterError`, because the bare
`MagicMock()` request reaches `yaml.safe_dump`, not on the
`process_pod_deletion` assert. Setting `to_dict.return_value` like the test
above would make it fail for the right reason.
##########
providers/cncf/kubernetes/docs/changelog.rst:
##########
@@ -27,6 +27,13 @@
Changelog
---------
+.. note::
+ When a retry reattaches to the pod of a previous try and finds it
``Succeeded``,
+ ``KubernetesPodOperator`` now treats that pod as the finished run instead
of deleting it and
+ starting a new pod. This applies only with ``do_xcom_push=False``; with
XCom on, the pod is still
+ deleted and recreated because its result can no longer be read. Set
``reattach_on_restart=False``
+ (or ``durable=False`` on Airflow 3.3+) to always start a new pod on retry.
Review Comment:
Could this get a second note for the timeout change? With
`on_kill_action="keep_pod"` and retries left, a timed-out pod now keeps running
and the retry reattaches to it, which existing `keep_pod` users will see on
upgrade. Setting `on_kill_action="delete_pod"` or `durable=False` gets the old
behaviour back.
##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -932,6 +946,16 @@ def execute_sync(self, context: Context):
self.remote_pod = self.pod_manager.await_pod_completion(
self.pod, istio_enabled, self.base_container_name,
self.do_xcom_push
)
+ except AirflowTaskTimeout:
+ # The task runner calls on_kill only after this propagates, so
cleanup has to know now
+ # that the pod must be left running for the next try to reattach.
+ ti = context["ti"]
+ self._keep_pod_after_timeout = (
+ self.reattach_on_restart
+ and self.on_kill_action == OnKillAction.KEEP_POD
+ and ti.try_number <= ti.max_tries
Review Comment:
That check was my suggestion, and it misses a case on 3.3+: a `retry_policy`
that returns `RetryAction.FAIL` for the timeout fails the TI after `on_kill`
even with tries left, so the kept pod keeps running and nothing reattaches to
it or cleans it up (before this PR `cleanup` deleted it). The docs line "On the
last try no retry follows, so the pod is cleaned up as usual" is then wrong
too. Could the keep also require `getattr(self, "retry_policy", None) is None`
(the attribute only exists on 3.3+), or should the docs say the pod is left
running when a retry policy is set?
--
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]