diff options
author | Will Thames <will@thames.id.au> | 2019-07-09 06:47:41 +1000 |
---|---|---|
committer | Jill R <4121322+jillr@users.noreply.github.com> | 2019-07-08 13:47:41 -0700 |
commit | 446919de6f5fbbcff8706d7eff147595140547fb (patch) | |
tree | f2165ae12d5301cc75ca8e836d28a533106c3531 /lib | |
parent | 105f60cf480572fb5547794cda1f9a05559ae636 (diff) | |
download | ansible-446919de6f5fbbcff8706d7eff147595140547fb.tar.gz |
Add apply to k8s module (#49053)
* Add apply to k8s module
Use apply method for updating k8s resources.
* Improve apply documentation
* k8s: Make apply and merge_type mutually exclusive
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/config/module_defaults.yml | 2 | ||||
-rw-r--r-- | lib/ansible/module_utils/k8s/raw.py | 58 | ||||
-rw-r--r-- | lib/ansible/modules/clustering/k8s/k8s.py | 10 |
3 files changed, 59 insertions, 11 deletions
diff --git a/lib/ansible/config/module_defaults.yml b/lib/ansible/config/module_defaults.yml index ded586fee6..98c1a5575f 100644 --- a/lib/ansible/config/module_defaults.yml +++ b/lib/ansible/config/module_defaults.yml @@ -610,6 +610,8 @@ groupings: - k8s k8s_facts: - k8s + k8s_info: + - k8s k8s_service: - k8s k8s_scale: diff --git a/lib/ansible/module_utils/k8s/raw.py b/lib/ansible/module_utils/k8s/raw.py index c986a3d22f..02a2ebf835 100644 --- a/lib/ansible/module_utils/k8s/raw.py +++ b/lib/ansible/module_utils/k8s/raw.py @@ -31,8 +31,6 @@ from ansible.module_utils.six import string_types from ansible.module_utils.k8s.common import KubernetesAnsibleModule from ansible.module_utils.common.dict_transformations import dict_merge -from distutils.version import LooseVersion - try: import yaml @@ -84,6 +82,7 @@ class KubernetesRawModule(KubernetesAnsibleModule): argument_spec['wait_condition'] = dict(type='dict', default=None, options=self.condition_spec) argument_spec['validate'] = dict(type='dict', default=None, options=self.validate_spec) argument_spec['append_hash'] = dict(type='bool', default=False) + argument_spec['apply'] = dict(type='bool') return argument_spec def __init__(self, k8s_kind=None, *args, **kwargs): @@ -92,6 +91,7 @@ class KubernetesRawModule(KubernetesAnsibleModule): mutually_exclusive = [ ('resource_definition', 'src'), + ('merge_type', 'apply'), ] KubernetesAnsibleModule.__init__(self, *args, @@ -115,6 +115,13 @@ class KubernetesRawModule(KubernetesAnsibleModule): if self.params['merge_type']: if LooseVersion(self.openshift_version) < LooseVersion("0.6.2"): self.fail_json(msg=missing_required_lib("openshift >= 0.6.2", reason="for merge_type")) + if self.params.get('apply') is not None: + if LooseVersion(self.openshift_version) < LooseVersion("0.9.0"): + self.fail_json(msg=missing_required_lib("openshift >= 0.9.0", reason="for apply")) + self.apply = self.params['apply'] + else: + self.apply = LooseVersion(self.openshift_version) >= LooseVersion("0.9.0") + if resource_definition: if isinstance(resource_definition, string_types): try: @@ -130,14 +137,14 @@ class KubernetesRawModule(KubernetesAnsibleModule): self.resource_definitions = self.load_resource_definitions(src) if not resource_definition and not src: - self.resource_definitions = [{ - 'kind': self.kind, - 'apiVersion': self.api_version, - 'metadata': { - 'name': self.name, - 'namespace': self.namespace - } - }] + implicit_definition = dict( + kind=self.kind, + apiVersion=self.api_version, + metadata=dict(name=self.name) + ) + if self.namespace: + implicit_definition['metadata']['namespace'] = self.namespace + self.resource_definitions = [implicit_definition] def flatten_list_kind(self, list_resource, definitions): flattened = [] @@ -231,7 +238,9 @@ class KubernetesRawModule(KubernetesAnsibleModule): if self.append_hash and definition['kind'] in ['ConfigMap', 'Secret']: name = '%s-%s' % (name, generate_hash(definition)) definition['metadata']['name'] = name - params = dict(name=name, namespace=namespace) + params = dict(name=name) + if namespace: + params['namespace'] = namespace existing = resource.get(**params) except NotFoundError: # Remove traceback so that it doesn't show up in later failures @@ -271,6 +280,33 @@ class KubernetesRawModule(KubernetesAnsibleModule): self.fail_json(msg="Resource deletion timed out", **result) return result else: + if self.apply: + if self.check_mode: + k8s_obj = definition + else: + try: + k8s_obj = resource.apply(definition, namespace=namespace).to_dict() + except DynamicApiError as exc: + msg = "Failed to apply object: {0}".format(exc.body) + if self.warnings: + msg += "\n" + "\n ".join(self.warnings) + self.fail_json(msg=msg, error=exc.status, status=exc.status, reason=exc.reason) + success = True + result['result'] = k8s_obj + if wait: + success, result['result'], result['duration'] = self.wait(resource, definition, wait_timeout) + if existing: + existing = existing.to_dict() + else: + existing = {} + match, diffs = self.diff_objects(existing, result['result']) + result['changed'] = not match + result['diff'] = diffs + result['method'] = 'apply' + if not success: + self.fail_json(msg="Resource apply timed out", **result) + return result + if not existing: if self.check_mode: k8s_obj = definition diff --git a/lib/ansible/modules/clustering/k8s/k8s.py b/lib/ansible/modules/clustering/k8s/k8s.py index d50b6f02c6..88a95ac96a 100644 --- a/lib/ansible/modules/clustering/k8s/k8s.py +++ b/lib/ansible/modules/clustering/k8s/k8s.py @@ -59,6 +59,7 @@ options: - If openshift >= 0.6.2, this defaults to C(['strategic-merge', 'merge']), which is ideal for using the same parameters on resource kinds that combine Custom Resources and built-in resources. For openshift < 0.6.2, the default is simply C(strategic-merge). + - mutually exclusive with C(apply) choices: - json - merge @@ -130,6 +131,15 @@ options: the generated hash and append_hash=no) type: bool version_added: "2.8" + apply: + description: + - C(apply) compares the desired resource definition with the previously supplied resource definition, + ignoring properties that are automatically generated + - C(apply) works better with Services than 'force=yes' + - C(apply) defaults to True if the openshift library is new enough to support it (0.9.0 or newer) + - mutually exclusive with C(merge_type) + type: bool + version_added: "2.9" requirements: - "python >= 2.7" |