diff --git a/changelogs/fragments/798-drain-pdb-error-message.yaml b/changelogs/fragments/798-drain-pdb-error-message.yaml new file mode 100644 index 0000000000..61b4a2573e --- /dev/null +++ b/changelogs/fragments/798-drain-pdb-error-message.yaml @@ -0,0 +1,2 @@ +minor_changes: + - k8s_drain - Improve error message for pod disruption budget when draining a node (https://github.com/ansible-collections/kubernetes.core/issues/797). diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index 9d8561e708..6544553b0d 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -143,6 +143,7 @@ """ import copy +import json import time import traceback from datetime import datetime @@ -187,6 +188,17 @@ HAS_EVICTION_API = False +def format_dynamic_api_exc(exc): + if exc.body: + if exc.headers and exc.headers.get("Content-Type") == "application/json": + message = json.loads(exc.body).get("message") + if message: + return message + return exc.body + else: + return "%s Reason: %s" % (exc.status, exc.reason) + + def filter_pods(pods, force, ignore_daemonset, delete_emptydir_data): k8s_kind_mirror = "kubernetes.io/config.mirror" daemonSet, unmanaged, mirror, localStorage, to_delete = [], [], [], [], [] @@ -338,7 +350,7 @@ def evict_pods(self, pods): if exc.reason != "Not Found": self._module.fail_json( msg="Failed to delete pod {0}/{1} due to: {2}".format( - namespace, name, exc.reason + namespace, name, to_native(format_dynamic_api_exc(exc)) ) ) except Exception as exc: