diff --git a/plugins/module_utils/k8s/runner.py b/plugins/module_utils/k8s/runner.py index bb1a8680d88..fd0656d4110 100644 --- a/plugins/module_utils/k8s/runner.py +++ b/plugins/module_utils/k8s/runner.py @@ -64,48 +64,47 @@ def run_module(module) -> None: name = module.params.get("name") state = module.params.get("state") - if ( - state == "absent" - and name is None - and resource_definition is None - and src is None - and select_all - ): - # Delete all resources in the namespace for the specified resource type - if module.params.get("kind") is None: - raise CoreException( - "'kind' option is required to sepcify the resource type." - ) + if select_all: + if ( + 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 module.params.get("kind") is None: + raise CoreException( + "'kind' option is required to sepcify the resource type." + ) - resource = svc.find_resource( - module.params.get("kind"), module.params.get("api_version"), fail=True - ) - definitions = svc.retrieve_all( - resource, - module.params.get("namespace"), - module.params.get("label_selectors"), - ) - elif ( - state == "patched" - and select_all - and len(definitions) == 1 - and definitions[0].get("metadata", {}).get("name") is None - ): - kind = definitions[0].get("kind") or module.params.get("kind") - api_version = module.params.get("apiVersion") or definitions[0].get( - "api_version" - ) - if kind is None: - raise CoreException( - "'kind' option is required to sepcify the resource type." + resource = svc.find_resource( + module.params.get("kind"), module.params.get("api_version"), fail=True ) - resource = svc.find_resource(kind, api_version, fail=True) - existing = svc.retrieve_all( - resource, - module.params.get("namespace"), - module.params.get("label_selectors"), - ) - definitions = [dict_merge(d, definitions[0]) for d in existing] + definitions = svc.retrieve_all( + resource, + module.params.get("namespace"), + module.params.get("label_selectors"), + ) + elif ( + state == "patched" + and len(definitions) == 1 + and definitions[0].get("metadata", {}).get("name") is None + ): + kind = definitions[0].get("kind") or module.params.get("kind") + api_version = module.params.get("apiVersion") or definitions[0].get( + "api_version" + ) + if kind is None: + raise CoreException( + "'kind' option is required to sepcify the resource type." + ) + resource = svc.find_resource(kind, api_version, fail=True) + existing = svc.retrieve_all( + resource, + module.params.get("namespace"), + module.params.get("label_selectors"), + ) + definitions = [dict_merge(d, definitions[0]) for d in existing] for definition in definitions: result = {"changed": False, "result": {}} diff --git a/tests/integration/targets/k8s_patched/meta/main.yml b/tests/integration/targets/k8s_patched/meta/main.yml index 54561c975c0..97e1601724a 100644 --- a/tests/integration/targets/k8s_patched/meta/main.yml +++ b/tests/integration/targets/k8s_patched/meta/main.yml @@ -1,2 +1,2 @@ -dependencies: -- remove_namespace +dependencies: [] +# - remove_namespace diff --git a/tests/integration/targets/k8s_patched/tasks/main.yml b/tests/integration/targets/k8s_patched/tasks/main.yml index f2cdbf5efd1..5d10438322d 100644 --- a/tests/integration/targets/k8s_patched/tasks/main.yml +++ b/tests/integration/targets/k8s_patched/tasks/main.yml @@ -129,6 +129,7 @@ metadata: labels: patched: "true" + select_all: true register: _patch - name: Ensure task is changed @@ -258,6 +259,7 @@ kind: Namespace name: "{{ item }}" state: absent + wait: yes with_items: - "{{ patch_only_namespace[0] }}" - "{{ patch_only_namespace[1] }}"