Skip to content

Commit d26dc22

Browse files
Patch only single label when marking KPO checked (#29279)
Instead of sending over a whole pod, we can simply send over the label to add to the pod instead. This is less work for us, and also avoids any issues like #24015. Fixes #24015
1 parent 05fb80e commit d26dc22

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,11 @@ def _set_name(name: str | None) -> str | None:
711711
def patch_already_checked(self, pod: k8s.V1Pod, *, reraise=True):
712712
"""Add an "already checked" annotation to ensure we don't reattach on retries"""
713713
with _optionally_suppress(reraise=reraise):
714-
pod.metadata.labels[self.POD_CHECKED_KEY] = "True"
715-
body = PodGenerator.serialize_pod(pod)
716-
self.client.patch_namespaced_pod(pod.metadata.name, pod.metadata.namespace, body)
714+
self.client.patch_namespaced_pod(
715+
name=pod.metadata.name,
716+
namespace=pod.metadata.namespace,
717+
body={"metadata": {"labels": {self.POD_CHECKED_KEY: "True"}}},
718+
)
717719

718720
def on_kill(self) -> None:
719721
if self.pod:

tests/providers/cncf/kubernetes/operators/test_kubernetes_pod.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,18 @@ def test_mark_checked_if_not_deleted(self, mock_patch_already_checked, mock_dele
999999
mock_patch_already_checked.assert_called_once()
10001000
mock_delete_pod.assert_not_called()
10011001

1002+
@patch(HOOK_CLASS, new=MagicMock)
1003+
def test_patch_already_checked(self):
1004+
"""Make sure we patch the pods with the right label"""
1005+
k = KubernetesPodOperator(task_id="task")
1006+
pod = k.build_pod_request_obj()
1007+
k.patch_already_checked(pod)
1008+
k.client.patch_namespaced_pod.assert_called_once_with(
1009+
name=pod.metadata.name,
1010+
namespace=pod.metadata.namespace,
1011+
body={"metadata": {"labels": {"already_checked": "True"}}},
1012+
)
1013+
10021014
def test_task_id_as_name(self):
10031015
k = KubernetesPodOperator(
10041016
task_id=".hi.-_09HI",

0 commit comments

Comments
 (0)