From 16059a8111e233c3fc257980cc5f25859f52ce86 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 26 Aug 2020 19:48:07 +0530 Subject: [PATCH] k8s_*: Group argument_spec accroding to usage (#199) Partially fix #36 Signed-off-by: Abhijeet Kasurde --- plugins/doc_fragments/k8s_resource_options.py | 4 +- plugins/module_utils/common.py | 6 ++ plugins/module_utils/raw.py | 4 +- plugins/module_utils/scale.py | 9 ++- plugins/modules/k8s_log.py | 41 ++++---------- plugins/modules/k8s_service.py | 56 ++----------------- tests/sanity/ignore-2.10.txt | 1 + tests/sanity/ignore-2.11.txt | 1 + 8 files changed, 34 insertions(+), 88 deletions(-) diff --git a/plugins/doc_fragments/k8s_resource_options.py b/plugins/doc_fragments/k8s_resource_options.py index 0742eed4..b5721453 100644 --- a/plugins/doc_fragments/k8s_resource_options.py +++ b/plugins/doc_fragments/k8s_resource_options.py @@ -15,8 +15,8 @@ class ModuleDocFragment(object): options: resource_definition: description: - - "Provide a valid YAML definition (either as a string, list, or dict) for an object when creating or updating. NOTE: I(kind), I(api_version), I(name), - and I(namespace) will be overwritten by corresponding values found in the provided I(resource_definition)." + - Provide a valid YAML definition (either as a string, list, or dict) for an object when creating or updating. + - "NOTE: I(kind), I(api_version), I(name), and I(namespace) will be overwritten by corresponding values found in the provided I(resource_definition)." aliases: - definition - inline diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index a94664f6..71e3e28a 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -83,6 +83,9 @@ COMMON_ARG_SPEC = { 'type': 'bool', 'default': False, }, +} + +RESOURCE_ARG_SPEC = { 'resource_definition': { 'type': list_dict_str, 'aliases': ['definition', 'inline'] @@ -90,6 +93,9 @@ COMMON_ARG_SPEC = { 'src': { 'type': 'path', }, +} + +NAME_ARG_SPEC = { 'kind': {}, 'name': {}, 'namespace': {}, diff --git a/plugins/module_utils/raw.py b/plugins/module_utils/raw.py index bae8f6ea..b694235a 100644 --- a/plugins/module_utils/raw.py +++ b/plugins/module_utils/raw.py @@ -25,7 +25,7 @@ import sys import traceback from ansible.module_utils.basic import missing_required_lib -from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC +from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC, RESOURCE_ARG_SPEC, NAME_ARG_SPEC from ansible.module_utils.six import string_types from ansible.module_utils._text import to_native from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule @@ -78,6 +78,8 @@ class KubernetesRawModule(KubernetesAnsibleModule): @property def argspec(self): argument_spec = copy.deepcopy(COMMON_ARG_SPEC) + argument_spec.update(copy.deepcopy(NAME_ARG_SPEC)) + argument_spec.update(copy.deepcopy(RESOURCE_ARG_SPEC)) argument_spec.update(copy.deepcopy(AUTH_ARG_SPEC)) argument_spec['merge_type'] = dict(type='list', elements='str', choices=['json', 'merge', 'strategic-merge']) argument_spec['wait'] = dict(type='bool', default=False) diff --git a/plugins/module_utils/scale.py b/plugins/module_utils/scale.py index 27f1abe0..27e1f067 100644 --- a/plugins/module_utils/scale.py +++ b/plugins/module_utils/scale.py @@ -21,7 +21,7 @@ __metaclass__ = type import copy -from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC +from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, RESOURCE_ARG_SPEC, NAME_ARG_SPEC from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule from ansible.module_utils.six import string_types @@ -142,11 +142,10 @@ class KubernetesAnsibleScaleModule(KubernetesAnsibleModule): @property def argspec(self): - args = copy.deepcopy(COMMON_ARG_SPEC) - args.pop('state') - args.pop('force') + args = copy.deepcopy(SCALE_ARG_SPEC) + args.update(RESOURCE_ARG_SPEC) + args.update(NAME_ARG_SPEC) args.update(AUTH_ARG_SPEC) - args.update(SCALE_ARG_SPEC) return args def scale(self, resource, existing_object, replicas, wait, wait_time): diff --git a/plugins/modules/k8s_log.py b/plugins/modules/k8s_log.py index bdf2037a..a30348b2 100644 --- a/plugins/modules/k8s_log.py +++ b/plugins/modules/k8s_log.py @@ -26,40 +26,25 @@ description: - Analogous to `kubectl logs` or `oc logs` extends_documentation_fragment: - community.kubernetes.k8s_auth_options + - community.kubernetes.k8s_name_options options: - api_version: - description: - - Use to specify the API version. in conjunction with I(kind), I(name), and I(namespace) to identify a - specific object. - - If using I(label_selector), cannot be overridden - default: v1 - aliases: - - api - - version - type: str kind: description: - - Use to specify an object model. Use in conjunction with I(api_version), I(name), and I(namespace) to identify a - specific object. - - If using I(label_selector), cannot be overridden - required: no + - Use to specify an object model. + - Use in conjunction with I(api_version), I(name), and I(namespace) to identify a specific object. + - If using I(label_selector), cannot be overridden. + type: str default: Pod - type: str - namespace: - description: - - Use to specify an object namespace. Use in conjunction with I(api_version), I(kind), and I(name) - to identify a specific object. - type: str name: description: - - Use to specify an object name. Use in conjunction with I(api_version), I(kind) and I(namespace) to identify a - specific object. - - Only one of I(name) or I(label_selector) may be provided + - Use to specify an object name. + - Use in conjunction with I(api_version), I(kind) and I(namespace) to identify a specific object. + - Only one of I(name) or I(label_selector) may be provided. type: str label_selectors: description: - List of label selectors to use to filter results - - Only one of I(name) or I(label_selector) may be provided + - Only one of I(name) or I(label_selector) may be provided. type: list elements: str container: @@ -129,7 +114,7 @@ import copy from ansible.module_utils.six import PY2 from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule -from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC +from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, NAME_ARG_SPEC class KubernetesLogModule(KubernetesAnsibleModule): @@ -142,12 +127,10 @@ class KubernetesLogModule(KubernetesAnsibleModule): @property def argspec(self): args = copy.deepcopy(AUTH_ARG_SPEC) + args.update(NAME_ARG_SPEC) args.update( dict( - kind=dict(default='Pod'), - api_version=dict(default='v1', aliases=['api', 'version']), - name=dict(), - namespace=dict(), + kind=dict(type='str', default='Pod'), container=dict(), label_selectors=dict(type='list', elements='str', default=[]), ) diff --git a/plugins/modules/k8s_service.py b/plugins/modules/k8s_service.py index 6df0ab5e..932825f7 100644 --- a/plugins/modules/k8s_service.py +++ b/plugins/modules/k8s_service.py @@ -22,43 +22,10 @@ description: extends_documentation_fragment: - community.kubernetes.k8s_auth_options + - community.kubernetes.k8s_resource_options + - community.kubernetes.k8s_state_options options: - resource_definition: - description: - - A partial YAML definition of the Service object being created/updated. Here you can define Kubernetes - Service Resource parameters not covered by this module's parameters. - - "NOTE: I(resource_definition) has lower priority than module parameters. If you try to define e.g. - I(metadata.namespace) here, that value will be ignored and I(metadata) used instead." - aliases: - - definition - - inline - type: dict - src: - description: - - "Provide a path to a file containing a valid YAML definition of an object dated. Mutually - exclusive with I(resource_definition). NOTE: I(kind), I(api_version), I(resource_name), and I(namespace) - will be overwritten by corresponding values found in the configuration read in from the I(src) file." - - Reads from the local file system. To read from the Ansible controller's file system, use the file lookup - plugin or template lookup plugin, combined with the from_yaml filter, and pass the result to - I(resource_definition). See Examples below. - type: path - state: - description: - - Determines if an object should be created, patched, or deleted. When set to C(present), an object will be - created, if it does not already exist. If set to C(absent), an existing object will be deleted. If set to - C(present), an existing object will be patched, if its attributes differ from those specified using - module options and I(resource_definition). - default: present - choices: - - present - - absent - type: str - force: - description: - - If set to C(True), and I(state) is C(present), an existing object will be replaced. - default: false - type: bool merge_type: description: - Whether to override the default patch merge approach with a specific type. By default, the strategic @@ -181,7 +148,7 @@ import traceback from collections import defaultdict -from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC +from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC, RESOURCE_ARG_SPEC from ansible_collections.community.kubernetes.plugins.module_utils.raw import KubernetesRawModule @@ -190,25 +157,10 @@ SERVICE_ARG_SPEC = { 'type': 'bool', 'default': False, }, - 'state': { - 'default': 'present', - 'choices': ['present', 'absent'], - }, - 'force': { - 'type': 'bool', - 'default': False, - }, - 'resource_definition': { - 'type': 'dict', - 'aliases': ['definition', 'inline'] - }, 'name': {'required': True}, 'namespace': {'required': True}, 'merge_type': {'type': 'list', 'elements': 'str', 'choices': ['json', 'merge', 'strategic-merge']}, 'selector': {'type': 'dict'}, - 'src': { - 'type': 'path', - }, 'type': { 'type': 'str', 'choices': [ @@ -240,6 +192,8 @@ class KubernetesService(KubernetesRawModule): def argspec(self): """ argspec property builder """ argument_spec = copy.deepcopy(AUTH_ARG_SPEC) + argument_spec.update(COMMON_ARG_SPEC) + argument_spec.update(RESOURCE_ARG_SPEC) argument_spec.update(SERVICE_ARG_SPEC) return argument_spec diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt index 0fa8c1c3..326af051 100644 --- a/tests/sanity/ignore-2.10.txt +++ b/tests/sanity/ignore-2.10.txt @@ -3,3 +3,4 @@ plugins/modules/k8s.py validate-modules:return-syntax-error plugins/modules/k8s_scale.py validate-modules:parameter-type-not-in-doc plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error +plugins/modules/k8s_service.py validate-modules:parameter-type-not-in-doc diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt index 0fa8c1c3..326af051 100644 --- a/tests/sanity/ignore-2.11.txt +++ b/tests/sanity/ignore-2.11.txt @@ -3,3 +3,4 @@ plugins/modules/k8s.py validate-modules:return-syntax-error plugins/modules/k8s_scale.py validate-modules:parameter-type-not-in-doc plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error +plugins/modules/k8s_service.py validate-modules:parameter-type-not-in-doc