summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/k8s_common.py
blob: 7800898fff7a32be387c5cc1ac464ba41d4cfe8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#
#  Copyright 2017 Red Hat | Ansible
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

import copy
import json
import os

from ansible.module_utils.basic import AnsibleModule

try:
    from openshift.helper.ansible import KubernetesAnsibleModuleHelper, ARG_ATTRIBUTES_BLACKLIST
    from openshift.helper.exceptions import KubernetesException
    HAS_K8S_MODULE_HELPER = True
except ImportError as exc:
    HAS_K8S_MODULE_HELPER = False

try:
    import yaml
    HAS_YAML = True
except ImportError:
    HAS_YAML = False


class KubernetesAnsibleException(Exception):
    pass


class KubernetesAnsibleModule(AnsibleModule):
    @staticmethod
    def get_helper(api_version, kind):
        return KubernetesAnsibleModuleHelper(api_version, kind)

    def __init__(self, kind, api_version):
        self.api_version = api_version
        self.kind = kind
        self.argspec_cache = None

        if not HAS_K8S_MODULE_HELPER:
            raise KubernetesAnsibleException(
                "This module requires the OpenShift Python client. Try `pip install openshift`"
            )

        if not HAS_YAML:
            raise KubernetesAnsibleException(
                "This module requires PyYAML. Try `pip install PyYAML`"
            )

        try:
            self.helper = self.get_helper(api_version, kind)
        except Exception as exc:
            raise KubernetesAnsibleException(
                "Error initializing AnsibleModuleHelper: {}".format(exc)
            )

        mutually_exclusive = (
            ('resource_definition', 'src'),
        )

        AnsibleModule.__init__(self,
                               argument_spec=self.argspec,
                               supports_check_mode=True,
                               mutually_exclusive=mutually_exclusive)

    @property
    def argspec(self):
        """
        Build the module argument spec from the helper.argspec, removing any extra attributes not needed by
        Ansible.

        :return: dict: a valid Ansible argument spec
        """
        if not self.argspec_cache:
            spec = {
                'dry_run': {
                    'type': 'bool',
                    'default': False,
                    'description': [
                        "If set to C(True) the module will exit without executing any action."
                        "Useful to only generate YAML file definitions for the resources in the tasks."
                    ]
                }
            }

            for arg_name, arg_properties in self.helper.argspec.items():
                spec[arg_name] = {}
                for option, option_value in arg_properties.items():
                    if option not in ARG_ATTRIBUTES_BLACKLIST:
                        if option == 'choices':
                            if isinstance(option_value, dict):
                                spec[arg_name]['choices'] = [value for key, value in option_value.items()]
                            else:
                                spec[arg_name]['choices'] = option_value
                        else:
                            spec[arg_name][option] = option_value

            self.argspec_cache = spec
        return self.argspec_cache

    def execute_module(self):
        """
        Performs basic CRUD operations on the model object. Ends by calling
        AnsibleModule.fail_json(), if an error is encountered, otherwise
        AnsibleModule.exit_json() with a dict containing:
          changed: boolean
          api_version: the API version
          <kind>: a dict representing the object's state
        :return: None
        """

        if self.params.get('debug'):
            self.helper.enable_debug(reset_logfile=False)
            self.helper.log_argspec()

        resource_definition = self.params.get('resource_definition')
        if self.params.get('src'):
            resource_definition = self.load_resource_definition(self.params['src'])
        if resource_definition:
            resource_params = self.resource_to_parameters(resource_definition)
            self.params.update(resource_params)

        state = self.params.get('state', None)
        force = self.params.get('force', False)
        dry_run = self.params.pop('dry_run', False)
        name = self.params.get('name')
        namespace = self.params.get('namespace', None)
        existing = None

        return_attributes = dict(changed=False,
                                 api_version=self.api_version,
                                 request=self.helper.request_body_from_params(self.params))
        return_attributes[self.helper.base_model_name_snake] = {}

        if dry_run:
            self.exit_json(**return_attributes)

        try:
            auth_options = {}
            for key, value in self.helper.argspec.items():
                if value.get('auth_option') and self.params.get(key) is not None:
                    auth_options[key] = self.params[key]
            self.helper.set_client_config(**auth_options)
        except KubernetesException as e:
            self.fail_json(msg='Error loading config', error=str(e))

        if state is None:
            # This is a list, rollback or ? module with no 'state' param
            if self.helper.base_model_name_snake.endswith('list'):
                # For list modules, execute a GET, and exit
                k8s_obj = self._read(name, namespace)
                return_attributes[self.kind] = k8s_obj.to_dict()
                self.exit_json(**return_attributes)
            elif self.helper.has_method('create'):
                # For a rollback, execute a POST, and exit
                k8s_obj = self._create(namespace)
                return_attributes[self.kind] = k8s_obj.to_dict()
                return_attributes['changed'] = True
                self.exit_json(**return_attributes)
            else:
                self.fail_json(msg="Missing state parameter. Expected one of: present, absent")

        # CRUD modules
        try:
            existing = self.helper.get_object(name, namespace)
        except KubernetesException as exc:
            self.fail_json(msg='Failed to retrieve requested object: {}'.format(exc.message),
                           error=exc.value.get('status'))

        if state == 'absent':
            if not existing:
                # The object already does not exist
                self.exit_json(**return_attributes)
            else:
                # Delete the object
                if not self.check_mode:
                    try:
                        self.helper.delete_object(name, namespace)
                    except KubernetesException as exc:
                        self.fail_json(msg="Failed to delete object: {}".format(exc.message),
                                       error=exc.value.get('status'))
                return_attributes['changed'] = True
                self.exit_json(**return_attributes)
        else:
            if not existing:
                k8s_obj = self._create(namespace)
                return_attributes[self.kind] = k8s_obj.to_dict()
                return_attributes['changed'] = True
                self.exit_json(**return_attributes)

            if existing and force:
                k8s_obj = None
                request_body = self.helper.request_body_from_params(self.params)
                if not self.check_mode:
                    try:
                        k8s_obj = self.helper.replace_object(name, namespace, body=request_body)
                    except KubernetesException as exc:
                        self.fail_json(msg="Failed to replace object: {}".format(exc.message),
                                       error=exc.value.get('status'))
                return_attributes[self.kind] = k8s_obj.to_dict()
                return_attributes['changed'] = True
                self.exit_json(**return_attributes)

            # Check if existing object should be patched
            k8s_obj = copy.deepcopy(existing)
            try:
                self.helper.object_from_params(self.params, obj=k8s_obj)
            except KubernetesException as exc:
                self.fail_json(msg="Failed to patch object: {}".format(exc.message))
            match, diff = self.helper.objects_match(existing, k8s_obj)
            if match:
                return_attributes[self.kind] = existing.to_dict()
                self.exit_json(**return_attributes)
            else:
                self.helper.log('Existing:')
                self.helper.log(json.dumps(existing.to_dict(), indent=4))
                self.helper.log('\nDifferences:')
                self.helper.log(json.dumps(diff, indent=4))
            # Differences exist between the existing obj and requested params
            if not self.check_mode:
                try:
                    k8s_obj = self.helper.patch_object(name, namespace, k8s_obj)
                except KubernetesException as exc:
                    self.fail_json(msg="Failed to patch object: {}".format(exc.message))
            return_attributes[self.kind] = k8s_obj.to_dict()
            return_attributes['changed'] = True
            self.exit_json(**return_attributes)

    def _create(self, namespace):
        request_body = None
        k8s_obj = None
        try:
            request_body = self.helper.request_body_from_params(self.params)
        except KubernetesException as exc:
            self.fail_json(msg="Failed to create object: {}".format(exc.message))
        if not self.check_mode:
            try:
                k8s_obj = self.helper.create_object(namespace, body=request_body)
            except KubernetesException as exc:
                self.fail_json(msg="Failed to create object: {}".format(exc.message),
                               error=exc.value.get('status'))
        return k8s_obj

    def _read(self, name, namespace):
        k8s_obj = None
        try:
            k8s_obj = self.helper.get_object(name, namespace)
        except KubernetesException as exc:
            self.fail_json(msg='Failed to retrieve requested object',
                           error=exc.value.get('status'))
        return k8s_obj

    def load_resource_definition(self, src):
        """ Load the requested src path """
        result = None
        path = os.path.normpath(src)
        self.helper.log("Reading definition from {}".format(path))
        if not os.path.exists(path):
            self.fail_json(msg="Error accessing {}. Does the file exist?".format(path))
        try:
            result = yaml.safe_load(open(path, 'r'))
        except (IOError, yaml.YAMLError) as exc:
            self.fail_json(msg="Error loading resource_definition: {}".format(exc))
        return result

    def resource_to_parameters(self, resource):
        """ Converts a resource definition to module parameters """
        parameters = {}
        for key, value in resource.items():
            if key in ('apiVersion', 'kind', 'status'):
                continue
            elif key == 'metadata' and isinstance(value, dict):
                for meta_key, meta_value in value.items():
                    if meta_key in ('name', 'namespace', 'labels', 'annotations'):
                        parameters[meta_key] = meta_value
            elif key in self.helper.argspec and value is not None:
                    parameters[key] = value
            elif isinstance(value, dict):
                self._add_parameter(value, [key], parameters)
        self.helper.log("Request to parameters: {}".format(json.dumps(parameters)))
        return parameters

    def _add_parameter(self, request, path, parameters):
        for key, value in request.items():
            if path:
                param_name = '_'.join(path + [self.helper.attribute_to_snake(key)])
            else:
                param_name = self.helper.attribute_to_snake(key)
            if param_name in self.helper.argspec and value is not None:
                parameters[param_name] = value
            elif isinstance(value, dict):
                continue_path = copy.copy(path) if path else []
                continue_path.append(self.helper.attribute_to_snake(key))
                self._add_parameter(value, continue_path, parameters)
            else:
                self.fail_json(
                    msg=("Error parsing resource definition. Encountered {}, which does not map to a module "
                         "parameter. If this looks like a problem with the module, please open an issue at "
                         "github.com/openshift/openshift-restclient-python/issues").format(param_name)
                )