From f10462ecf0501e73b6c0d02e5f473aaae69a04e6 Mon Sep 17 00:00:00 2001 From: abikouo Date: Tue, 28 Mar 2023 17:23:20 +0200 Subject: [PATCH] add commit action --- .github/workflows/test.yml | 6 ++++ plugins/module_utils/k8s/runner.py | 51 ++++++++++++++++++++++++----- plugins/module_utils/k8s/service.py | 27 +++++++++++++-- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50330ed568..6be8440d18 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -68,3 +68,9 @@ jobs: git status shell: bash if: steps.black_check.outcome != 'success' + + - name: commit and push changes + id: commit + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "automated changes for changelog and/or black formatting" diff --git a/plugins/module_utils/k8s/runner.py b/plugins/module_utils/k8s/runner.py index 629c4a40be..49cbbb46bf 100644 --- a/plugins/module_utils/k8s/runner.py +++ b/plugins/module_utils/k8s/runner.py @@ -46,16 +46,51 @@ def _prepend_resource_info(resource, msg): return [_prepend_resource_info(resource, msg) for msg in warnings + errors] +def get_definitions(svc, params): + try: + definitions = create_definitions(params) + except Exception as e: + msg = "Failed to load resource definition: {0}".format(e) + raise CoreException(msg) from e + + delete_all = params.get("delete_all") + src = params.get("src") + resource_definition = params.get("resource_definition") + name = params.get("name") + state = params.get("state") + + if ( + delete_all + and state == "absent" + and name is None + and resource_definition is None + and src is None + ): + # Delete all resources in the namespace for the specified resource type + if params.get("kind") is None: + raise CoreException( + "'kind' option is required to specify the resource type." + ) + + resource = svc.find_resource( + params.get("kind"), params.get("api_version"), fail=True + ) + definitions = svc.retrieve_all( + resource, + params.get("namespace"), + params.get("label_selectors"), + ) + + return definitions + + def run_module(module) -> None: results = [] changed = False client = get_api_client(module) svc = K8sService(client, module) - try: - definitions = create_definitions(module.params) - except Exception as e: - msg = "Failed to load resource definition: {0}".format(e) - raise CoreException(msg) from e + + definitions = get_definitions(svc, module.params) for definition in definitions: result = {"changed": False, "result": {}} @@ -130,9 +165,9 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict: result["msg"] = ( "resource 'kind={kind},name={name},namespace={namespace}' " "filtered by label_selectors.".format( - kind=kind, - name=origin_name, - namespace=namespace, + kind=kind, + name=origin_name, + namespace=namespace, ) ) return result diff --git a/plugins/module_utils/k8s/service.py b/plugins/module_utils/k8s/service.py index a835914213..71d0a6904c 100644 --- a/plugins/module_utils/k8s/service.py +++ b/plugins/module_utils/k8s/service.py @@ -211,6 +211,30 @@ def retrieve(self, resource: Resource, definition: Dict) -> ResourceInstance: 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, @@ -231,8 +255,7 @@ def find( return dict( resources=[], msg='Failed to find API for resource with apiVersion "{0}" and kind "{1}"'.format( - api_version, - kind, + api_version, kind ), api_found=False, )