From c83a5592ec24338d9588b43a6ead91e7e7a964d0 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Thu, 15 Jul 2021 10:33:48 -0400 Subject: [PATCH 1/3] Re-enable support for turbo mode This re-enables the ability to add turbo mode. It also adds a few more tests to cover some cases that had been broken in turbo mode previously. --- molecule/default/tasks/full.yml | 49 +++++++++++++++++++++++++++ plugins/module_utils/ansiblemodule.py | 8 ++++- requirements.yml | 3 ++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 requirements.yml diff --git a/molecule/default/tasks/full.yml b/molecule/default/tasks/full.yml index 65dfba013f..b60f231d5d 100644 --- a/molecule/default/tasks/full.yml +++ b/molecule/default/tasks/full.yml @@ -36,12 +36,30 @@ path: ~/.kube/config state: absent + - name: Try to create namespace without default kube config + kubernetes.core.k8s: + name: testing + kind: Namespace + ignore_errors: true + register: result + + - name: No default kube config should fail + assert: + that: result is not successful + - name: Using custom config location should succeed kubernetes.core.k8s: name: testing kind: Namespace kubeconfig: ~/.kube/customconfig + - name: Using an env var to set config location should succeed + kubernetes.core.k8s: + name: testing + kind: Namespace + environment: + K8S_AUTH_KUBECONFIG: ~/.kube/customconfig + always: - name: Return kubeconfig copy: @@ -365,6 +383,37 @@ register: k8s_info_testing6 failed_when: not k8s_info_testing6.resources or k8s_info_testing6.resources[0].status.phase != "Active" + - set_fact: + cmap_data: "{{ lookup('community.general.random_string', length=512000, base64=true) }}" + + - name: Create configmap with large value + k8s: + definition: + apiVersion: v1 + kind: ConfigMap + metadata: + name: testmap + namespace: testing + data: + testkey: "{{ cmap_data }}" + wait: true + register: result + + - assert: + that: + - result is changed + + - name: Retrieve configmap + k8s_info: + kind: ConfigMap + namespace: testing + name: testmap + register: result + + - assert: + that: + - result.resources[0].data.testkey == "{{ cmap_data }}" + always: - name: Delete all namespaces k8s: diff --git a/plugins/module_utils/ansiblemodule.py b/plugins/module_utils/ansiblemodule.py index a647b163f8..97fc7604d3 100644 --- a/plugins/module_utils/ansiblemodule.py +++ b/plugins/module_utils/ansiblemodule.py @@ -3,4 +3,10 @@ __metaclass__ = type -from ansible.module_utils.basic import AnsibleModule # noqa: F401 +try: + from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( + AnsibleTurboModule as AnsibleModule, + ) # noqa: F401 + AnsibleModule.collection_name = "kubernetes.core" +except ImportError: + from ansible.module_utils.basic import AnsibleModule # noqa: F401 diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000000..589c202719 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,3 @@ +# Test dependencies +collections: + - community.general From 76d4e9275a0bef1f30e43eb73c8014d9c9eba521 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Thu, 15 Jul 2021 10:46:11 -0400 Subject: [PATCH 2/3] Add changelog fragment --- changelogs/fragments/169-reenable-turbo-mode.yaml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/169-reenable-turbo-mode.yaml diff --git a/changelogs/fragments/169-reenable-turbo-mode.yaml b/changelogs/fragments/169-reenable-turbo-mode.yaml new file mode 100644 index 0000000000..e7ef90d948 --- /dev/null +++ b/changelogs/fragments/169-reenable-turbo-mode.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - re-enable turbo mode for collection (https://github.com/ansible-collections/kubernetes.core/pull/169). From 7f85d58604947981da620c092624ab2ee96d4117 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Mon, 2 Aug 2021 10:31:44 -0400 Subject: [PATCH 3/3] Use environment variable to enable turbo mode --- .../fragments/169-reenable-turbo-mode.yaml | 2 +- molecule/default/tasks/full.yml | 9 ++++---- plugins/module_utils/ansiblemodule.py | 22 ++++++++++++++----- requirements.yml | 4 ++-- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/changelogs/fragments/169-reenable-turbo-mode.yaml b/changelogs/fragments/169-reenable-turbo-mode.yaml index e7ef90d948..0df8f04792 100644 --- a/changelogs/fragments/169-reenable-turbo-mode.yaml +++ b/changelogs/fragments/169-reenable-turbo-mode.yaml @@ -1,3 +1,3 @@ --- minor_changes: - - re-enable turbo mode for collection (https://github.com/ansible-collections/kubernetes.core/pull/169). + - re-enable turbo mode for collection. The default is initially set to off (https://github.com/ansible-collections/kubernetes.core/pull/169). diff --git a/molecule/default/tasks/full.yml b/molecule/default/tasks/full.yml index b60f231d5d..554b3efb58 100644 --- a/molecule/default/tasks/full.yml +++ b/molecule/default/tasks/full.yml @@ -383,8 +383,9 @@ register: k8s_info_testing6 failed_when: not k8s_info_testing6.resources or k8s_info_testing6.resources[0].status.phase != "Active" - - set_fact: - cmap_data: "{{ lookup('community.general.random_string', length=512000, base64=true) }}" + - name: Create large configmap data + command: dd if=/dev/urandom bs=500K count=1 + register: cmap_data - name: Create configmap with large value k8s: @@ -395,7 +396,7 @@ name: testmap namespace: testing data: - testkey: "{{ cmap_data }}" + testkey: "{{ cmap_data.stdout | b64encode }}" wait: true register: result @@ -412,7 +413,7 @@ - assert: that: - - result.resources[0].data.testkey == "{{ cmap_data }}" + - result.resources[0].data.testkey == "{{ cmap_data.stdout | b64encode }}" always: - name: Delete all namespaces diff --git a/plugins/module_utils/ansiblemodule.py b/plugins/module_utils/ansiblemodule.py index 97fc7604d3..a983cad9f2 100644 --- a/plugins/module_utils/ansiblemodule.py +++ b/plugins/module_utils/ansiblemodule.py @@ -3,10 +3,22 @@ __metaclass__ = type +import os + +from ansible.module_utils.common.validation import check_type_bool + try: - from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( - AnsibleTurboModule as AnsibleModule, - ) # noqa: F401 - AnsibleModule.collection_name = "kubernetes.core" -except ImportError: + enable_turbo_mode = check_type_bool(os.environ.get("ENABLE_TURBO_MODE")) +except TypeError: + enable_turbo_mode = False + +if enable_turbo_mode: + try: + from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( + AnsibleTurboModule as AnsibleModule, + ) # noqa: F401 + AnsibleModule.collection_name = "kubernetes.core" + except ImportError: + from ansible.module_utils.basic import AnsibleModule # noqa: F401 +else: from ansible.module_utils.basic import AnsibleModule # noqa: F401 diff --git a/requirements.yml b/requirements.yml index 589c202719..1c4b370de5 100644 --- a/requirements.yml +++ b/requirements.yml @@ -1,3 +1,3 @@ -# Test dependencies collections: - - community.general + - name: cloud.common + version: ">=2.0.4"