mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-08 05:52:37 +00:00
make name optional to delete all resources for the specified resource type (#517)
make name optional to delete all resources for the specified resource type SUMMARY closes #504 k8s module should allow deleting all namespace resources for the specified resource type. ISSUE TYPE Feature Pull Request COMPONENT NAME k8s ADDITIONAL INFORMATION Delete all Pods from namespace test - k8s: namespace: test kind: Pod api_version: v1 delete_all: true state: absent Reviewed-by: Gonéri Le Bouder <goneri@lebouder.net> Reviewed-by: Mike Graves <mgraves@redhat.com> Reviewed-by: Bikouo Aubin
This commit is contained in:
@@ -211,6 +211,30 @@ class K8sService:
|
||||
|
||||
return existing
|
||||
|
||||
def retrieve_all(
|
||||
self, resource: Resource, namespace: str, label_selectors: List[str] = None
|
||||
) -> List[Dict]:
|
||||
definitions: List[ResourceInstance] = []
|
||||
|
||||
try:
|
||||
params = dict(namespace=namespace)
|
||||
if label_selectors:
|
||||
params["label_selector"] = ",".join(label_selectors)
|
||||
resource_list = self.client.get(resource, **params)
|
||||
for item in resource_list.items:
|
||||
existing = self.client.get(
|
||||
resource, name=item.metadata.name, namespace=namespace
|
||||
)
|
||||
definitions.append(existing.to_dict())
|
||||
except (NotFoundError, MethodNotAllowedError):
|
||||
pass
|
||||
except Exception as e:
|
||||
reason = e.body if hasattr(e, "body") else e
|
||||
msg = "Failed to retrieve requested object: {0}".format(reason)
|
||||
raise CoreException(msg) from e
|
||||
|
||||
return definitions
|
||||
|
||||
def find(
|
||||
self,
|
||||
kind: str,
|
||||
|
||||
Reference in New Issue
Block a user