From 61d298dcf12a5b6c77bd863886adbd6fa6a375de Mon Sep 17 00:00:00 2001 From: Ottavia Balducci <62117827+OttaviaB@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:35:07 +0100 Subject: [PATCH] Fix k8s_drain runs into timeout with pods from stateful sets. (#793) SUMMARY Fixes #792 . The function wait_for_pod_deletion in k8s_drain never checks on which node a pod is actually running: try: response = self._api_instance.read_namespaced_pod( namespace=pod[0], name=pod[1] ) if not response: pod = None time.sleep(wait_sleep) This means that if a pod is successfully evicted and restarted with the same name on a new node, k8s_drain does not notice and thinks that the original pod is still running. This is the case for pods which are part of a stateful set. ISSUE TYPE Bugfix Pull Request COMPONENT NAME k8s_drain Reviewed-by: Mike Graves (cherry picked from commit fca0dc0485bf3748b61ac547957617e1e66573be) --- changelogs/fragments/793-fix-k8s-drain-runs-into-timeout.yaml | 2 ++ plugins/module_utils/helm.py | 1 - plugins/modules/k8s_drain.py | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/793-fix-k8s-drain-runs-into-timeout.yaml diff --git a/changelogs/fragments/793-fix-k8s-drain-runs-into-timeout.yaml b/changelogs/fragments/793-fix-k8s-drain-runs-into-timeout.yaml new file mode 100644 index 0000000000..26b8b8407c --- /dev/null +++ b/changelogs/fragments/793-fix-k8s-drain-runs-into-timeout.yaml @@ -0,0 +1,2 @@ +bugfixes: + - k8s_drain - Fix k8s_drain runs into a timeout when evicting a pod which is part of a stateful set (https://github.com/ansible-collections/kubernetes.core/issues/792). diff --git a/plugins/module_utils/helm.py b/plugins/module_utils/helm.py index edc48f6b0c..4318ff0851 100644 --- a/plugins/module_utils/helm.py +++ b/plugins/module_utils/helm.py @@ -77,7 +77,6 @@ def write_temp_kubeconfig(server, validate_certs=True, ca_cert=None, kubeconfig= class AnsibleHelmModule(object): - """ An Ansible module class for Kubernetes.core helm modules """ diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index 87cb268241..539f3e8df0 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -299,7 +299,9 @@ def _elapsed_time(): response = self._api_instance.read_namespaced_pod( namespace=pod[0], name=pod[1] ) - if not response: + if not response or response.spec.node_name != self._module.params.get( + "name" + ): pod = None time.sleep(wait_sleep) except ApiException as exc: