Skip to content

Commit 52f2cb5

Browse files
authored
Improve error message for pod disruption budget when draining a node (ansible-collections#798)
SUMMARY Closes ansible-collections#797 . The error message "Too Many Requests" is confusing and is changed to a more meaningful message: TASK [Drain node] ************************************************************************* Montag 25 November 2024 09:20:28 +0100 (0:00:00.014) 0:00:00.014 ******* fatal: [host -> localhost]: FAILED! => {"changed": false, "msg": "Failed to delete pod kube-public/draintest-6b84677b99-9jf7m due to: Cannot evict pod as it would violate the pod's disruption budget."} The new task output would allow to deal with a pod disruption budget with the retries/until logic in a more controlled way: --- - hosts: "{{ target }}" serial: 1 gather_facts: false tasks: - name: Drain node kubernetes.core.k8s_drain: kubeconfig: "{{ kubeconfig_path }}" name: "{{ inventory_hostname }}" delete_options: ignore_daemonsets: true delete_emptydir_data: true wait_timeout: 100 disable_eviction: false wait_sleep: 1 delegate_to: localhost retries: 10 delay: 5 until: drain_result is success or 'disruption budget' not in drain_result.msg register: drain_result ISSUE TYPE Feature Pull Request COMPONENT NAME k8s_drain Reviewed-by: Mike Graves <[email protected]>
1 parent 513ff66 commit 52f2cb5

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 = [], [], [], [], []
@@ -338,7 +350,7 @@ def evict_pods(self, pods):
338350
if exc.reason != "Not Found":
339351
self._module.fail_json(
340352
msg="Failed to delete pod {0}/{1} due to: {2}".format(
341-
namespace, name, exc.reason
353+
namespace, name, to_native(format_dynamic_api_exc(exc))
342354
)
343355
)
344356
except Exception as exc:

0 commit comments

Comments
 (0)