From d3c53089b977d44515d62f5e37736ebd0ff7fe80 Mon Sep 17 00:00:00 2001 From: Ottavia Balducci Date: Wed, 31 Jul 2024 13:52:03 +0200 Subject: [PATCH] Make k8s_drain work when only one pod is present --- .../770-fix-k8s-drain-doesnt-wait-for-single-pod.yaml | 2 ++ plugins/modules/k8s_drain.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/770-fix-k8s-drain-doesnt-wait-for-single-pod.yaml diff --git a/changelogs/fragments/770-fix-k8s-drain-doesnt-wait-for-single-pod.yaml b/changelogs/fragments/770-fix-k8s-drain-doesnt-wait-for-single-pod.yaml new file mode 100644 index 0000000000..a063d79c81 --- /dev/null +++ b/changelogs/fragments/770-fix-k8s-drain-doesnt-wait-for-single-pod.yaml @@ -0,0 +1,2 @@ +bugfixes: + - k8s_drain - Fix k8s_drain does not wait for single pod (https://github.com/ansible-collections/kubernetes.core/issues/769). diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index 87cb268241..045d26f815 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -291,16 +291,17 @@ def _elapsed_time(): return (datetime.now() - start).seconds response = None - pod = pods.pop() + pod = None while (_elapsed_time() < wait_timeout or wait_timeout == 0) and pods: if not pod: - pod = pods.pop() + pod = pods[-1] try: response = self._api_instance.read_namespaced_pod( namespace=pod[0], name=pod[1] ) if not response: pod = None + del pods[-1] time.sleep(wait_sleep) except ApiException as exc: if exc.reason != "Not Found": @@ -308,6 +309,7 @@ def _elapsed_time(): msg="Exception raised: {0}".format(exc.reason) ) pod = None + del pods[-1] except Exception as e: self._module.fail_json(msg="Exception raised: {0}".format(to_native(e))) if not pods: