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

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 <None>
This commit is contained in:
Bikouo Aubin
2022-04-25 15:37:20 +02:00
committed by GitHub
parent 67c808d934
commit f2f4b66d77
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: