summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/class/oc_adm_registry.py
blob: a9911966bae7f5624fefaf757b84eb06c1119366 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# pylint: skip-file
# flake8: noqa

class RegistryException(Exception):
    ''' Registry Exception Class '''
    pass


class RegistryConfig(OpenShiftCLIConfig):
    ''' RegistryConfig is a DTO for the registry.  '''
    def __init__(self, rname, namespace, kubeconfig, registry_options):
        super(RegistryConfig, self).__init__(rname, namespace, kubeconfig, registry_options)


class Registry(OpenShiftCLI):
    ''' Class to wrap the oc command line tools '''

    volume_mount_path = 'spec.template.spec.containers[0].volumeMounts'
    volume_path = 'spec.template.spec.volumes'
    env_path = 'spec.template.spec.containers[0].env'

    def __init__(self,
                 registry_config,
                 verbose=False):
        ''' Constructor for Registry

           a registry consists of 3 or more parts
           - dc/docker-registry
           - svc/docker-registry

           Parameters:
           :registry_config:
           :verbose:
        '''
        super(Registry, self).__init__(registry_config.namespace, registry_config.kubeconfig, verbose)
        self.version = OCVersion(registry_config.kubeconfig, verbose)
        self.svc_ip = None
        self.portal_ip = None
        self.config = registry_config
        self.verbose = verbose
        self.registry_parts = [{'kind': 'dc', 'name': self.config.name},
                               {'kind': 'svc', 'name': self.config.name},
                              ]

        self.__prepared_registry = None
        self.volume_mounts = []
        self.volumes = []
        if self.config.config_options['volume_mounts']['value']:
            for volume in self.config.config_options['volume_mounts']['value']:
                volume_info = {'secret_name': volume.get('secret_name', None),
                               'name':        volume.get('name', None),
                               'type':        volume.get('type', None),
                               'path':        volume.get('path', None),
                               'claimName':   volume.get('claim_name', None),
                               'claimSize':   volume.get('claim_size', None),
                              }

                vol, vol_mount = Volume.create_volume_structure(volume_info)
                self.volumes.append(vol)
                self.volume_mounts.append(vol_mount)

        self.dconfig = None
        self.svc = None

    @property
    def deploymentconfig(self):
        ''' deploymentconfig property '''
        return self.dconfig

    @deploymentconfig.setter
    def deploymentconfig(self, config):
        ''' setter for deploymentconfig property '''
        self.dconfig = config

    @property
    def service(self):
        ''' service property '''
        return self.svc

    @service.setter
    def service(self, config):
        ''' setter for service property '''
        self.svc = config

    @property
    def prepared_registry(self):
        ''' prepared_registry property '''
        if not self.__prepared_registry:
            results = self.prepare_registry()
            if not results:
                raise RegistryException('Could not perform registry preparation.')
            self.__prepared_registry = results

        return self.__prepared_registry

    @prepared_registry.setter
    def prepared_registry(self, data):
        ''' setter method for prepared_registry attribute '''
        self.__prepared_registry = data

    def get(self):
        ''' return the self.registry_parts '''
        self.deploymentconfig = None
        self.service = None

        rval = 0
        for part in self.registry_parts:
            result = self._get(part['kind'], rname=part['name'])
            if result['returncode'] == 0 and part['kind'] == 'dc':
                self.deploymentconfig = DeploymentConfig(result['results'][0])
            elif result['returncode'] == 0 and part['kind'] == 'svc':
                self.service = Service(result['results'][0])

            if result['returncode'] != 0:
                rval = result['returncode']


        return {'returncode': rval, 'deploymentconfig': self.deploymentconfig, 'service': self.service}

    def exists(self):
        '''does the object exist?'''
        if self.deploymentconfig and self.service:
            return True

        return False

    def delete(self, complete=True):
        '''return all pods '''
        parts = []
        for part in self.registry_parts:
            if not complete and part['kind'] == 'svc':
                continue
            parts.append(self._delete(part['kind'], part['name']))

        # Clean up returned results
        rval = 0
        for part in parts:
            # pylint: disable=invalid-sequence-index
            if 'returncode' in part and part['returncode'] != 0:
                rval = part['returncode']

        return {'returncode': rval, 'results': parts}

    def prepare_registry(self):
        ''' prepare a registry for instantiation '''
        options = self.config.to_option_list()

        cmd = ['registry', '-n', self.config.namespace]
        cmd.extend(options)
        cmd.extend(['--dry-run=True', '-o', 'json'])

        results = self.openshift_cmd(cmd, oadm=True, output=True, output_type='json')
        # probably need to parse this
        # pylint thinks results is a string
        # pylint: disable=no-member
        if results['returncode'] != 0 and 'items' in results['results']:
            return results

        service = None
        deploymentconfig = None
        # pylint: disable=invalid-sequence-index
        for res in results['results']['items']:
            if res['kind'] == 'DeploymentConfig':
                deploymentconfig = DeploymentConfig(res)
            elif res['kind'] == 'Service':
                service = Service(res)

        # Verify we got a service and a deploymentconfig
        if not service or not deploymentconfig:
            return results

        # results will need to get parsed here and modifications added
        deploymentconfig = DeploymentConfig(self.add_modifications(deploymentconfig))

        # modify service ip
        if self.svc_ip:
            service.put('spec.clusterIP', self.svc_ip)
        if self.portal_ip:
            service.put('spec.portalIP', self.portal_ip)

        # the dry-run doesn't apply the selector correctly
        if self.service:
            service.put('spec.selector', self.service.get_selector())

        # need to create the service and the deploymentconfig
        service_file = Utils.create_tmp_file_from_contents('service', service.yaml_dict)
        deployment_file = Utils.create_tmp_file_from_contents('deploymentconfig', deploymentconfig.yaml_dict)

        return {"service": service,
                "service_file": service_file,
                "service_update": False,
                "deployment": deploymentconfig,
                "deployment_file": deployment_file,
                "deployment_update": False}

    def create(self):
        '''Create a registry'''
        results = []
        self.needs_update()
        # if the object is none, then we need to create it
        # if the object needs an update, then we should call replace
        # Handle the deploymentconfig
        if self.deploymentconfig is None:
            results.append(self._create(self.prepared_registry['deployment_file']))
        elif self.prepared_registry['deployment_update']:
            results.append(self._replace(self.prepared_registry['deployment_file']))

        # Handle the service
        if self.service is None:
            results.append(self._create(self.prepared_registry['service_file']))
        elif self.prepared_registry['service_update']:
            results.append(self._replace(self.prepared_registry['service_file']))

        # Clean up returned results
        rval = 0
        for result in results:
            # pylint: disable=invalid-sequence-index
            if 'returncode' in result and result['returncode'] != 0:
                rval = result['returncode']

        return {'returncode': rval, 'results': results}

    def update(self):
        '''run update for the registry.  This performs a replace if required'''
        # Store the current service IP
        if self.service:
            svcip = self.service.get('spec.clusterIP')
            if svcip:
                self.svc_ip = svcip
            portip = self.service.get('spec.portalIP')
            if portip:
                self.portal_ip = portip

        results = []
        if self.prepared_registry['deployment_update']:
            results.append(self._replace(self.prepared_registry['deployment_file']))
        if self.prepared_registry['service_update']:
            results.append(self._replace(self.prepared_registry['service_file']))

        # Clean up returned results
        rval = 0
        for result in results:
            if result['returncode'] != 0:
                rval = result['returncode']

        return {'returncode': rval, 'results': results}

    def add_modifications(self, deploymentconfig):
        ''' update a deployment config with changes '''
        # The environment variable for REGISTRY_HTTP_SECRET is autogenerated
        # We should set the generated deploymentconfig to the in memory version
        # the following modifications will overwrite if needed
        if self.deploymentconfig:
            result = self.deploymentconfig.get_env_var('REGISTRY_HTTP_SECRET')
            if result:
                deploymentconfig.update_env_var('REGISTRY_HTTP_SECRET', result['value'])

        # Currently we know that our deployment of a registry requires a few extra modifications
        # Modification 1
        # we need specific environment variables to be set
        for key, value in self.config.config_options['env_vars'].get('value', {}).items():
            if not deploymentconfig.exists_env_key(key):
                deploymentconfig.add_env_value(key, value)
            else:
                deploymentconfig.update_env_var(key, value)

        # Modification 2
        # we need specific volume variables to be set
        for volume in self.volumes:
            deploymentconfig.update_volume(volume)

        for vol_mount in self.volume_mounts:
            deploymentconfig.update_volume_mount(vol_mount)

        # Modification 3
        # Edits
        edit_results = []
        for edit in self.config.config_options['edits'].get('value', []):
            if edit['action'] == 'put':
                edit_results.append(deploymentconfig.put(edit['key'],
                                                         edit['value']))
            if edit['action'] == 'update':
                edit_results.append(deploymentconfig.update(edit['key'],
                                                            edit['value'],
                                                            edit.get('index', None),
                                                            edit.get('curr_value', None)))
            if edit['action'] == 'append':
                edit_results.append(deploymentconfig.append(edit['key'],
                                                            edit['value']))

        if edit_results and not any([res[0] for res in edit_results]):
            return None

        return deploymentconfig.yaml_dict

    def needs_update(self):
        ''' check to see if we need to update '''
        exclude_list = ['clusterIP', 'portalIP', 'type', 'protocol']
        if self.service is None or \
                not Utils.check_def_equal(self.prepared_registry['service'].yaml_dict,
                                          self.service.yaml_dict,
                                          exclude_list,
                                          debug=self.verbose):
            self.prepared_registry['service_update'] = True

        exclude_list = ['dnsPolicy',
                        'terminationGracePeriodSeconds',
                        'restartPolicy', 'timeoutSeconds',
                        'livenessProbe', 'readinessProbe',
                        'terminationMessagePath',
                        'securityContext',
                        'imagePullPolicy',
                        'protocol', # ports.portocol: TCP
                        'type', # strategy: {'type': 'rolling'}
                        'defaultMode', # added on secrets
                        'activeDeadlineSeconds', # added in 1.5 for timeouts
                       ]

        if self.deploymentconfig is None or \
                not Utils.check_def_equal(self.prepared_registry['deployment'].yaml_dict,
                                          self.deploymentconfig.yaml_dict,
                                          exclude_list,
                                          debug=self.verbose):
            self.prepared_registry['deployment_update'] = True

        return self.prepared_registry['deployment_update'] or self.prepared_registry['service_update'] or False

    # In the future, we would like to break out each ansible state into a function.
    # pylint: disable=too-many-branches,too-many-return-statements
    @staticmethod
    def run_ansible(params, check_mode):
        '''run idempotent ansible code'''

        rconfig = RegistryConfig(params['name'],
                                 params['namespace'],
                                 params['kubeconfig'],
                                 {'images': {'value': params['images'], 'include': True},
                                  'latest_images': {'value': params['latest_images'], 'include': True},
                                  'labels': {'value': params['labels'], 'include': True},
                                  'ports': {'value': ','.join(params['ports']), 'include': True},
                                  'replicas': {'value': params['replicas'], 'include': True},
                                  'selector': {'value': params['selector'], 'include': True},
                                  'service_account': {'value': params['service_account'], 'include': True},
                                  'mount_host': {'value': params['mount_host'], 'include': True},
                                  'env_vars': {'value': params['env_vars'], 'include': False},
                                  'volume_mounts': {'value': params['volume_mounts'], 'include': False},
                                  'edits': {'value': params['edits'], 'include': False},
                                  'enforce_quota': {'value': params['enforce_quota'], 'include': True},
                                  'daemonset': {'value': params['daemonset'], 'include': True},
                                  'tls_key': {'value': params['tls_key'], 'include': True},
                                  'tls_certificate': {'value': params['tls_certificate'], 'include': True},
                                 })


        ocregistry = Registry(rconfig, params['debug'])

        api_rval = ocregistry.get()

        state = params['state']
        ########
        # get
        ########
        if state == 'list':

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': False, 'results': api_rval, 'state': state}

        ########
        # Delete
        ########
        if state == 'absent':
            if not ocregistry.exists():
                return {'changed': False, 'state': state}

            if check_mode:
                return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete.'}

            # Unsure as to why this is angry with the return type.
            # pylint: disable=redefined-variable-type
            api_rval = ocregistry.delete()

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': True, 'results': api_rval, 'state': state}

        if state == 'present':
            ########
            # Create
            ########
            if not ocregistry.exists():

                if check_mode:
                    return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'}

                api_rval = ocregistry.create()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {'changed': True, 'results': api_rval, 'state': state}

            ########
            # Update
            ########
            if not params['force'] and not ocregistry.needs_update():
                return {'changed': False, 'state': state}

            if check_mode:
                return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'}

            api_rval = ocregistry.update()

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': True, 'results': api_rval, 'state': state}

        return {'failed': True, 'msg': 'Unknown state passed. %s' % state}