Skip to content

Commit 41ce338

Browse files
committed
Improve error message for pod disruption budget when draining a node
1 parent cd68631 commit 41ce338

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- k8s_drain - Improve error message for pod disruption budget when draining a node (https://github.com/ansible-collections/kubernetes.core/issues/797).

plugins/modules/k8s_drain.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"""
144144

145145
import copy
146+
import json
146147
import time
147148
import traceback
148149
from datetime import datetime
@@ -187,6 +188,17 @@
187188
HAS_EVICTION_API = False
188189

189190

191+
def format_dynamic_api_exc(exc):
192+
if exc.body:
193+
if exc.headers and exc.headers.get("Content-Type") == "application/json":
194+
message = json.loads(exc.body).get("message")
195+
if message:
196+
return message
197+
return exc.body
198+
else:
199+
return "%s Reason: %s" % (exc.status, exc.reason)
200+
201+
190202
def filter_pods(pods, force, ignore_daemonset, delete_emptydir_data):
191203
k8s_kind_mirror = "kubernetes.io/config.mirror"
192204
daemonSet, unmanaged, mirror, localStorage, to_delete = [], [], [], [], []
@@ -336,7 +348,7 @@ def evict_pods(self, pods):
336348
if exc.reason != "Not Found":
337349
self._module.fail_json(
338350
msg="Failed to delete pod {0}/{1} due to: {2}".format(
339-
namespace, name, exc.reason
351+
namespace, name, to_native(format_dynamic_api_exc(exc))
340352
)
341353
)
342354
except Exception as exc:

0 commit comments

Comments
 (0)