diff --git a/plugins/module_utils/copy.py b/plugins/module_utils/copy.py index 0b7b58a0..3a335f6b 100644 --- a/plugins/module_utils/copy.py +++ b/plugins/module_utils/copy.py @@ -24,6 +24,9 @@ from abc import ABCMeta, abstractmethod import tarfile # from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( + CoreException, +) from ansible.module_utils._text import to_native try: @@ -376,12 +379,17 @@ class K8SCopyToPod(K8SCopy): ) -def check_pod(k8s_ansible_mixin, module): - resource = k8s_ansible_mixin.find_resource("Pod", None, True) +def check_pod(svc): + module = svc.module namespace = module.params.get("namespace") name = module.params.get("pod") container = module.params.get("container") + try: + resource = svc.find_resource("Pod", None, True) + except CoreException as e: + module.fail_json(msg=to_native(e)) + def _fail(exc): arg = {} if hasattr(exc, "body"): @@ -398,7 +406,7 @@ def check_pod(k8s_ansible_mixin, module): module.fail_json(msg=msg, **arg) try: - result = resource.get(name=name, namespace=namespace) + result = svc.client.get(resource, name=name, namespace=namespace) containers = [ c["name"] for c in result.to_dict()["status"]["containerStatuses"] ] diff --git a/plugins/module_utils/k8s/core.py b/plugins/module_utils/k8s/core.py index 2cff9563..0deca7eb 100644 --- a/plugins/module_utils/k8s/core.py +++ b/plugins/module_utils/k8s/core.py @@ -15,6 +15,7 @@ class AnsibleK8SModule: default_settings = { "check_k8s": True, + "check_pyyaml": True, "module_class": AnsibleModule, } @@ -33,6 +34,9 @@ class AnsibleK8SModule: self.requires("kubernetes") self.has_at_least("kubernetes", "12.0.0", warn=True) + if self.settings["check_pyyaml"]: + self.requires("pyyaml") + @property def check_mode(self): return self._module.check_mode diff --git a/plugins/modules/k8s_cp.py b/plugins/modules/k8s_cp.py index 11111a1d..c14b918d 100644 --- a/plugins/modules/k8s_cp.py +++ b/plugins/modules/k8s_cp.py @@ -142,6 +142,16 @@ import copy from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( + get_api_client, +) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( + AnsibleK8SModule, +) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( + K8sService, +) + from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_SPEC, ) @@ -171,23 +181,9 @@ def argspec(): def execute_module(module): - - from ansible_collections.kubernetes.core.plugins.module_utils.common import ( - K8sAnsibleMixin, - get_api_client, - ) - - k8s_ansible_mixin = K8sAnsibleMixin(module, pyyaml_required=False) - k8s_ansible_mixin.check_library_version() - - k8s_ansible_mixin.module = module - k8s_ansible_mixin.argspec = module.argument_spec - k8s_ansible_mixin.params = k8s_ansible_mixin.module.params - k8s_ansible_mixin.fail_json = k8s_ansible_mixin.module.fail_json - k8s_ansible_mixin.fail = k8s_ansible_mixin.module.fail_json - - k8s_ansible_mixin.client = get_api_client(module=module) - containers = check_pod(k8s_ansible_mixin, module) + client = get_api_client(module=module) + svc = K8sService(client, module) + containers = check_pod(svc) if len(containers) > 1 and module.params.get("container") is None: module.fail_json( msg="Pod contains more than 1 container, option 'container' should be set" @@ -195,9 +191,9 @@ def execute_module(module): state = module.params.get("state") if state == "to_pod": - k8s_copy = K8SCopyToPod(module, k8s_ansible_mixin.client) + k8s_copy = K8SCopyToPod(module, client) else: - k8s_copy = K8SCopyFromPod(module, k8s_ansible_mixin.client) + k8s_copy = K8SCopyFromPod(module, client) try: k8s_copy.run() @@ -206,8 +202,10 @@ def execute_module(module): def main(): - module = AnsibleModule( + module = AnsibleK8SModule( + module_class=AnsibleModule, argument_spec=argspec(), + check_pyyaml=False, mutually_exclusive=[("local_path", "content")], required_if=[("state", "from_pod", ["local_path"])], required_one_of=[["local_path", "content"]],