From c11a255026ede82be38f8cc5cb5903068187b9e2 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Wed, 16 Jun 2021 11:28:46 -0400 Subject: [PATCH] Fix apply on Ansible 2.9 (#135) * Fix apply on Ansible 2.9 For some reason the apply function can't be correctly imported in Ansible 2.9. This just renames it to get it to import. I've also added molecule testing on multiple Ansible versions. * Add changelog fragment --- .github/workflows/ci.yml | 3 ++- changelogs/fragments/135-rename-apply-function.yml | 3 +++ molecule/default/tasks/full.yml | 2 +- molecule/default/tasks/template.yml | 4 ++-- plugins/action/helm.py | 1 + plugins/action/helm_info.py | 1 + plugins/action/helm_plugin.py | 1 + plugins/action/helm_plugin_info.py | 1 + plugins/action/helm_repository.py | 1 + plugins/action/k8s.py | 1 + plugins/action/k8s_cluster_info.py | 1 + plugins/action/k8s_exec.py | 1 + plugins/action/k8s_log.py | 1 + plugins/action/k8s_rollback.py | 1 + plugins/action/k8s_scale.py | 1 + plugins/action/k8s_service.py | 1 + plugins/action/ks8_json_patch.py | 1 + plugins/module_utils/apply.py | 2 +- plugins/module_utils/k8sdynamicclient.py | 4 ++-- 19 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/135-rename-apply-function.yml create mode 120000 plugins/action/helm.py create mode 120000 plugins/action/helm_info.py create mode 120000 plugins/action/helm_plugin.py create mode 120000 plugins/action/helm_plugin_info.py create mode 120000 plugins/action/helm_repository.py create mode 120000 plugins/action/k8s.py create mode 120000 plugins/action/k8s_cluster_info.py create mode 120000 plugins/action/k8s_exec.py create mode 120000 plugins/action/k8s_log.py create mode 120000 plugins/action/k8s_rollback.py create mode 120000 plugins/action/k8s_scale.py create mode 120000 plugins/action/k8s_service.py create mode 120000 plugins/action/ks8_json_patch.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6682b318..c316c36b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,6 +75,7 @@ jobs: strategy: matrix: python_version: ['3.7'] + ansible_version: ['==2.9.*', '==2.10.*', ''] steps: - name: Check out code uses: actions/checkout@v2 @@ -92,7 +93,7 @@ jobs: # The 3.3.0 release of molecule introduced a breaking change. See # https://github.com/ansible-community/molecule/issues/3083 - name: Install molecule and kubernetes dependencies - run: pip install ansible "molecule<3.3.0" yamllint kubernetes flake8 jsonpatch + run: pip install ansible${{ matrix.ansible_version }} "molecule<3.3.0" yamllint kubernetes flake8 jsonpatch # The latest release doesn't work with Molecule currently. # See: https://github.com/ansible-community/molecule/issues/2757 diff --git a/changelogs/fragments/135-rename-apply-function.yml b/changelogs/fragments/135-rename-apply-function.yml new file mode 100644 index 00000000..965cd6f9 --- /dev/null +++ b/changelogs/fragments/135-rename-apply-function.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - rename the apply function to fix broken imports in Ansible 2.9 (https://github.com/ansible-collections/kubernetes.core/pull/135). diff --git a/molecule/default/tasks/full.yml b/molecule/default/tasks/full.yml index 2e3268ed..65dfba01 100644 --- a/molecule/default/tasks/full.yml +++ b/molecule/default/tasks/full.yml @@ -219,7 +219,7 @@ ### https://github.com/ansible-collections/community.kubernetes/issues/111 - set_fact: - api_groups: "{{ lookup('k8s', cluster_info='api_groups') }}" + api_groups: "{{ lookup('kubernetes.core.k8s', cluster_info='api_groups') }}" - debug: var: api_groups diff --git a/molecule/default/tasks/template.yml b/molecule/default/tasks/template.yml index cb5b91ac..d7cba2dc 100644 --- a/molecule/default/tasks/template.yml +++ b/molecule/default/tasks/template.yml @@ -236,8 +236,8 @@ assert: that: - resource is successful - - resource.result.results | selectattr('changed') | length == 1 - - resource.result.results | selectattr('error', 'defined') | length == 1 + - resource.result.results | selectattr('changed') | list | length == 1 + - resource.result.results | selectattr('error', 'defined') | list | length == 1 - name: Remove Pod (Cleanup) kubernetes.core.k8s: diff --git a/plugins/action/helm.py b/plugins/action/helm.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/helm.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/helm_info.py b/plugins/action/helm_info.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/helm_info.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/helm_plugin.py b/plugins/action/helm_plugin.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/helm_plugin.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/helm_plugin_info.py b/plugins/action/helm_plugin_info.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/helm_plugin_info.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/helm_repository.py b/plugins/action/helm_repository.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/helm_repository.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s.py b/plugins/action/k8s.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_cluster_info.py b/plugins/action/k8s_cluster_info.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_cluster_info.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_exec.py b/plugins/action/k8s_exec.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_exec.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_log.py b/plugins/action/k8s_log.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_log.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_rollback.py b/plugins/action/k8s_rollback.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_rollback.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_scale.py b/plugins/action/k8s_scale.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_scale.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/k8s_service.py b/plugins/action/k8s_service.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/k8s_service.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/action/ks8_json_patch.py b/plugins/action/ks8_json_patch.py new file mode 120000 index 00000000..667980ed --- /dev/null +++ b/plugins/action/ks8_json_patch.py @@ -0,0 +1 @@ +k8s_info.py \ No newline at end of file diff --git a/plugins/module_utils/apply.py b/plugins/module_utils/apply.py index 87c3e2d6..486d9a5a 100644 --- a/plugins/module_utils/apply.py +++ b/plugins/module_utils/apply.py @@ -110,7 +110,7 @@ def apply_object(resource, definition): return apply_patch(actual.to_dict(), definition) -def apply(resource, definition): +def k8s_apply(resource, definition): existing, desired = apply_object(resource, definition) if not existing: return resource.create(body=desired, namespace=definition['metadata'].get('namespace')) diff --git a/plugins/module_utils/k8sdynamicclient.py b/plugins/module_utils/k8sdynamicclient.py index 7a7b6cdd..0827009c 100644 --- a/plugins/module_utils/k8sdynamicclient.py +++ b/plugins/module_utils/k8sdynamicclient.py @@ -19,7 +19,7 @@ __metaclass__ = type from kubernetes.dynamic import DynamicClient -from ansible_collections.kubernetes.core.plugins.module_utils.apply import apply +from ansible_collections.kubernetes.core.plugins.module_utils.apply import k8s_apply from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import ApplyException @@ -33,7 +33,7 @@ class K8SDynamicClient(DynamicClient): if resource.namespaced: body['metadata']['namespace'] = super().ensure_namespace(resource, namespace, body) try: - return apply(resource, body) + return k8s_apply(resource, body) except ApplyException as e: raise ValueError("Could not apply strategic merge to %s/%s: %s" % (body['kind'], body['metadata']['name'], e))