k8s - fix issue when try to delete resources using label_selectors option (#434) (#444)

[backport/2.3] k8s - fix issue when try to delete resources using label_selectors op…

Depends-On: #446
k8s - fix issue when try to delete resources using label_selectors
SUMMARY
The kubernetes dynamic client has label_selector parameter for the delete method, however based on the documentation of REST API we cannot delete resources using labelSelector option, this fix update the way the resources are deleted. The list of resources are deleted one after another like in the kubectl go client.
Fixes #428
ISSUE TYPE
Bugfix Pull Request
Reviewed-by: Abhijeet Kasurde 
(cherry picked from commit f2f4b66)
This commit is contained in:
Mike Graves
2022-05-02 11:43:19 -04:00
committed by GitHub
parent edf104d687
commit e2dec91460
3 changed files with 61 additions and 2 deletions

View File

@@ -995,6 +995,7 @@ class K8sAnsibleMixin(object):
if self.check_mode and not self.supports_dry_run:
return result
else:
params = {"namespace": namespace}
if delete_options:
body = {
"apiVersion": "v1",
@@ -1005,8 +1006,18 @@ class K8sAnsibleMixin(object):
if self.check_mode:
params["dry_run"] = "All"
try:
k8s_obj = resource.delete(**params)
result["result"] = k8s_obj.to_dict()
if existing.kind.endswith("List"):
result["result"] = []
for item in existing.items:
origin_name = item.metadata.name
params["name"] = origin_name
k8s_obj = resource.delete(**params)
result["result"].append(k8s_obj.to_dict())
else:
origin_name = existing.metadata.name
params["name"] = origin_name
k8s_obj = resource.delete(**params)
result["result"] = k8s_obj.to_dict()
except DynamicApiError as exc:
msg = "Failed to delete object: {0}".format(exc.body)
if continue_on_error: