Skip to content

is_delete_operator_pod=True and random_name_suffix=False can cause KubernetesPodOperator to delete the wrong pod #21169

Closed

Description

@SamWheating

Apache Airflow version

2.2.2

What happened

When running multiple KubernetesPodOperators with random_name_suffix=False and is_delete_pod_operator=True the following will happen:

  1. The first task will create the Pod my-pod
  2. The second task will attempt to create the pod, but fail with a 409 response from the API server (this is expected)
  3. The second task will delete my-pod, because it has is_delete_pod_operator=True and the Pod name is consistent between the two tasks. This is unexpected and will cause the first task to fail as well.

I understand that this is a rare circumstance, but I think its still worth fixing as anyone using random_name_suffix=False in an otherwise default KubernetesPodOperator may result in other pods being killed.

As a possible fix, we could find_pod before deleting, to ensure that the pod being deleted has the appropriate execution_date label:

def delete_pod(self, pod: V1Pod) -> None:
"""Deletes POD"""
try:
self._client.delete_namespaced_pod(
pod.metadata.name, pod.metadata.namespace, body=client.V1DeleteOptions()
)
except ApiException as e:
# If the pod is already deleted
if e.status != 404:
raise

Let me know if you have any other suggestions for how this could be fixed, or if this should just be considered expected behaviour when using fixed Kubernetes Pod IDs.

What you expected to happen

The second task should be able to fail without deleting the pod from the first task.

How to reproduce

Create a DAG with a single KubernetesPodOperator with random_name_suffix=False and is_delete_pod_operator=True and run it twice in parallel.

Operating System

Debian GNU/Linux 10 (buster)

Versions of Apache Airflow Providers

apache-airflow-providers-cncf-kubernetes=2.2.0

Deployment

Other 3rd-party Helm chart

Deployment details

No response

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    on Jan 27, 2022
  2. ferruzzi commented on Jan 27, 2022

    @ferruzzi
    Contributor

    Interesting combination of events. Another possible solution might be to check the failure cause before calling delete_pod, if it failed because the pod already existed, then don't delete it?

  3. added
    provider:cncf-kubernetes
    and removed on Jan 29, 2022
  4. SamWheating commented on Aug 24, 2022

    @SamWheating
    ContributorAuthor

    This was fixed in #22092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:providerskind:bugprovider:cncf-kubernetes

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions