From 7d4e0ee4a9b8038b2a4855350bec515d75b50b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Tue, 27 Jul 2021 13:39:12 +0200 Subject: [PATCH] allow turbo mode, but disabled by default Depends-On: https://github.com/ansible-collections/cloud.common/pull/73 Expose a new parameter called enable_session_cache to enable the feature. --- .../fragments/add_enable_session_cache.yaml | 3 +++ galaxy.yml | 2 ++ plugins/doc_fragments/k8s_turbo_options.py | 21 +++++++++++++++++++ plugins/module_utils/ansiblemodule.py | 8 ++++++- plugins/module_utils/args_common.py | 7 +++++++ plugins/modules/k8s.py | 4 +++- plugins/modules/k8s_cluster_info.py | 4 +++- plugins/modules/k8s_cp.py | 1 + plugins/modules/k8s_exec.py | 6 ++++-- plugins/modules/k8s_info.py | 4 +++- plugins/modules/k8s_json_patch.py | 6 +++++- plugins/modules/k8s_log.py | 4 +++- plugins/modules/k8s_rollback.py | 4 +++- plugins/modules/k8s_scale.py | 4 +++- plugins/modules/k8s_service.py | 4 +++- 15 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/add_enable_session_cache.yaml create mode 100644 plugins/doc_fragments/k8s_turbo_options.py diff --git a/changelogs/fragments/add_enable_session_cache.yaml b/changelogs/fragments/add_enable_session_cache.yaml new file mode 100644 index 00000000000..7a931ac8bdf --- /dev/null +++ b/changelogs/fragments/add_enable_session_cache.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- add ``enable_session_cache`` parameter to turn on the session cache. The option is disabled by default. diff --git a/galaxy.yml b/galaxy.yml index e2d8c6da5a8..22d82f12f92 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -8,6 +8,8 @@ authors: - willthames (https://github.com/willthames) - mmazur (https://github.com/mmazur) - jamescassell (https://github.com/jamescassell) +dependencies: + cloud.common: '>=2.0.5' description: Kubernetes Collection for Ansible. documentation: '' homepage: '' diff --git a/plugins/doc_fragments/k8s_turbo_options.py b/plugins/doc_fragments/k8s_turbo_options.py new file mode 100644 index 00000000000..d48594aa135 --- /dev/null +++ b/plugins/doc_fragments/k8s_turbo_options.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +# Copyright: (c) 2018, Red Hat | Ansible +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Options for selecting or identifying a specific K8s object + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +class ModuleDocFragment(object): + + DOCUMENTATION = r''' +options: + enable_session_cache: + description: + - Enable the caching of the HTTP sessions. + type: bool + default: false +''' diff --git a/plugins/module_utils/ansiblemodule.py b/plugins/module_utils/ansiblemodule.py index a647b163f8a..97fc7604d33 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/plugins/module_utils/args_common.py b/plugins/module_utils/args_common.py index 67c183db74d..b70a51adbd1 100644 --- a/plugins/module_utils/args_common.py +++ b/plugins/module_utils/args_common.py @@ -142,3 +142,10 @@ def list_dict_str(value): } } } + +TURBO_ARG_SPEC = { + 'enable_session_cache': { + 'type': 'bool', + 'default': False, + }, +} diff --git a/plugins/modules/k8s.py b/plugins/modules/k8s.py index e7bbd7e46ec..1242a75f3b7 100644 --- a/plugins/modules/k8s.py +++ b/plugins/modules/k8s.py @@ -35,6 +35,7 @@ - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_wait_options - kubernetes.core.k8s_delete_options + - kubernetes.core.k8s_turbo_options options: state: @@ -326,7 +327,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, WAIT_ARG_SPEC, NAME_ARG_SPEC, RESOURCE_ARG_SPEC, DELETE_OPTS_ARG_SPEC) + AUTH_ARG_SPEC, WAIT_ARG_SPEC, NAME_ARG_SPEC, RESOURCE_ARG_SPEC, DELETE_OPTS_ARG_SPEC, TURBO_ARG_SPEC) def validate_spec(): @@ -342,6 +343,7 @@ def argspec(): argument_spec.update(copy.deepcopy(RESOURCE_ARG_SPEC)) argument_spec.update(copy.deepcopy(AUTH_ARG_SPEC)) argument_spec.update(copy.deepcopy(WAIT_ARG_SPEC)) + argument_spec.update(copy.deepcopy(TURBO_ARG_SPEC)) argument_spec['merge_type'] = dict(type='list', elements='str', choices=['json', 'merge', 'strategic-merge']) argument_spec['validate'] = dict(type='dict', default=None, options=validate_spec()) argument_spec['append_hash'] = dict(type='bool', default=False) diff --git a/plugins/modules/k8s_cluster_info.py b/plugins/modules/k8s_cluster_info.py index b17f07bec06..aeff10ca8aa 100644 --- a/plugins/modules/k8s_cluster_info.py +++ b/plugins/modules/k8s_cluster_info.py @@ -31,6 +31,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" @@ -155,7 +156,7 @@ from ansible.module_utils.basic import missing_required_lib from ansible.module_utils.parsing.convert_bool import boolean from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, TURBO_ARG_SPEC) def execute_module(module, client): @@ -196,6 +197,7 @@ def execute_module(module, client): def argspec(): spec = copy.deepcopy(AUTH_ARG_SPEC) + spec.update(copy.deepcopy(TURBO_ARG_SPEC)) spec['invalidate_cache'] = dict(type='bool', default=True) return spec diff --git a/plugins/modules/k8s_cp.py b/plugins/modules/k8s_cp.py index 463757b7956..18f1c49a3df 100644 --- a/plugins/modules/k8s_cp.py +++ b/plugins/modules/k8s_cp.py @@ -23,6 +23,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" diff --git a/plugins/modules/k8s_exec.py b/plugins/modules/k8s_exec.py index b72df56e603..769fbd0455f 100644 --- a/plugins/modules/k8s_exec.py +++ b/plugins/modules/k8s_exec.py @@ -24,6 +24,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" @@ -120,8 +121,8 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible.module_utils._text import to_native -from ansible_collections.kubernetes.core.plugins.module_utils.common import ( - AUTH_ARG_SPEC +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( + AUTH_ARG_SPEC, TURBO_ARG_SPEC ) try: @@ -134,6 +135,7 @@ def argspec(): spec = copy.deepcopy(AUTH_ARG_SPEC) + spec.update(TURBO_ARG_SPEC) spec['namespace'] = dict(type='str', required=True) spec['pod'] = dict(type='str', required=True) spec['container'] = dict(type='str') diff --git a/plugins/modules/k8s_info.py b/plugins/modules/k8s_info.py index 50059e4124f..6ef94c61965 100644 --- a/plugins/modules/k8s_info.py +++ b/plugins/modules/k8s_info.py @@ -47,6 +47,7 @@ - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_name_options - kubernetes.core.k8s_wait_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" @@ -149,7 +150,7 @@ import copy from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, WAIT_ARG_SPEC) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, WAIT_ARG_SPEC, TURBO_ARG_SPEC) def execute_module(module, k8s_ansible_mixin): @@ -170,6 +171,7 @@ def execute_module(module, k8s_ansible_mixin): def argspec(): args = copy.deepcopy(AUTH_ARG_SPEC) + args.update(TURBO_ARG_SPEC) args.update(WAIT_ARG_SPEC) args.update( dict( diff --git a/plugins/modules/k8s_json_patch.py b/plugins/modules/k8s_json_patch.py index c38e2eb6b5a..a8b7db30d87 100644 --- a/plugins/modules/k8s_json_patch.py +++ b/plugins/modules/k8s_json_patch.py @@ -60,6 +60,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_wait_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" @@ -130,7 +131,9 @@ from ansible.module_utils.basic import missing_required_lib from ansible.module_utils._text import to_native from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC, WAIT_ARG_SPEC +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( + AUTH_ARG_SPEC, WAIT_ARG_SPEC, TURBO_ARG_SPEC +) from ansible_collections.kubernetes.core.plugins.module_utils.common import ( get_api_client, K8sAnsibleMixin) @@ -267,6 +270,7 @@ def build_error_msg(kind, name, msg): def main(): args = copy.deepcopy(AUTH_ARG_SPEC) args.update(copy.deepcopy(WAIT_ARG_SPEC)) + args.update(copy.deepcopy(TURBO_ARG_SPEC)) args.update(JSON_PATCH_ARGS) module = AnsibleModule(argument_spec=args, supports_check_mode=True) k8s_module = K8sAnsibleMixin(module) diff --git a/plugins/modules/k8s_log.py b/plugins/modules/k8s_log.py index 0c07ce7077d..f59ce72aad4 100644 --- a/plugins/modules/k8s_log.py +++ b/plugins/modules/k8s_log.py @@ -27,6 +27,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_name_options + - kubernetes.core.k8s_turbo_options options: kind: description: @@ -121,11 +122,12 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible.module_utils.six import PY2 -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, NAME_ARG_SPEC) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, NAME_ARG_SPEC, TURBO_ARG_SPEC) def argspec(): args = copy.deepcopy(AUTH_ARG_SPEC) + args.update(TURBO_ARG_SPEC) args.update(NAME_ARG_SPEC) args.update( dict( diff --git a/plugins/modules/k8s_rollback.py b/plugins/modules/k8s_rollback.py index f12d3da432c..b1bd4b3ee4e 100644 --- a/plugins/modules/k8s_rollback.py +++ b/plugins/modules/k8s_rollback.py @@ -30,6 +30,7 @@ extends_documentation_fragment: - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_name_options + - kubernetes.core.k8s_turbo_options requirements: - "python >= 3.6" - "kubernetes >= 12.0.0" @@ -80,7 +81,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, NAME_ARG_SPEC) + AUTH_ARG_SPEC, NAME_ARG_SPEC, TURBO_ARG_SPEC) def get_managed_resource(module): @@ -181,6 +182,7 @@ def perform_action(module, k8s_ansible_mixin, resource): def argspec(): args = copy.deepcopy(AUTH_ARG_SPEC) + args.update(TURBO_ARG_SPEC) args.update(NAME_ARG_SPEC) args.update( dict( diff --git a/plugins/modules/k8s_scale.py b/plugins/modules/k8s_scale.py index 58d178eebca..841b0c10916 100644 --- a/plugins/modules/k8s_scale.py +++ b/plugins/modules/k8s_scale.py @@ -30,6 +30,7 @@ - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_resource_options - kubernetes.core.k8s_scale_options + - kubernetes.core.k8s_turbo_options options: label_selectors: @@ -145,7 +146,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, RESOURCE_ARG_SPEC, NAME_ARG_SPEC) + AUTH_ARG_SPEC, RESOURCE_ARG_SPEC, NAME_ARG_SPEC, TURBO_ARG_SPEC) SCALE_ARG_SPEC = { @@ -274,6 +275,7 @@ def _continue_or_exit(warn): def argspec(): args = copy.deepcopy(SCALE_ARG_SPEC) + args.update(TURBO_ARG_SPEC) args.update(RESOURCE_ARG_SPEC) args.update(NAME_ARG_SPEC) args.update(AUTH_ARG_SPEC) diff --git a/plugins/modules/k8s_service.py b/plugins/modules/k8s_service.py index d94ba5c0175..4a7cd4f92cf 100644 --- a/plugins/modules/k8s_service.py +++ b/plugins/modules/k8s_service.py @@ -24,6 +24,7 @@ - kubernetes.core.k8s_auth_options - kubernetes.core.k8s_resource_options - kubernetes.core.k8s_state_options + - kubernetes.core.k8s_turbo_options options: merge_type: @@ -148,7 +149,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, COMMON_ARG_SPEC, RESOURCE_ARG_SPEC) + AUTH_ARG_SPEC, COMMON_ARG_SPEC, RESOURCE_ARG_SPEC, TURBO_ARG_SPEC) SERVICE_ARG_SPEC = { 'apply': { @@ -188,6 +189,7 @@ def argspec(): argument_spec.update(COMMON_ARG_SPEC) argument_spec.update(RESOURCE_ARG_SPEC) argument_spec.update(SERVICE_ARG_SPEC) + argument_spec.update(TURBO_ARG_SPEC) return argument_spec