summaryrefslogtreecommitdiffstats
path: root/ansible/library/rancher_k8s_environment.py
blob: d3d8ac02dcbd8545bf23f2f40e5da176f21e78cf (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
#!/usr/bin/python

DOCUMENTATION='''
---
module: rancher_k8s_environment
description:
  - This module will create or delete Kubernetes environment.
  - It will also delete other environments when variables are set accordingly.
notes:
  - It identifies environment only by name. Expect problems with same named environments.
  - All hosts running Kubernetes cluster should have same OS otherwise there
    is possibility of misbehavement.
options:
  server:
    required: true
    description:
      - Url of rancher server i.e. "http://10.0.0.1:8080".
  name:
    required: true
    descritpion:
      - Name of the environment to create/remove.
  descr:
    description:
      - Description of environment to create.
  state:
    description:
      - If "present" environment will be created or setup depending if it exists.
        With multiple environments with same name expect error.
        If "absent" environment will be removed. If multiple environments have same
        name all will be deleted.
    default: present
    choices: [present, absent]
  delete_not_k8s:
    description:
      - Indicates if environments with different orchestration than Kubernetes should
        be deleted.
    type: bool
    default: yes
  delete_other_k8s:
    description:
      - Indicates if environments with different name than specified should
        be deleted.
    type: bool
    default: no
  force:
    description:
      - Indicates if environment should be deleted and recreated.
    type: bool
    default: yes
  host_os:
    required: true
    description:
      - OS (family from ansible_os_family variable) of the hosts running cluster. If
        "RedHat" then datavolume fix will be applied.
        Fix described here:
          https://github.com/rancher/rancher/issues/10015
'''

import json
import time

import requests
from ansible.module_utils.basic import AnsibleModule



def get_existing_environments(rancher_address):
    req = requests.get('{}/v2-beta/projects'.format(rancher_address))
    envs = req.json()['data']
    return envs


def not_k8s_ids(environments):
    envs = filter(lambda x: x['orchestration'] != 'kubernetes', environments)
    return [env['id'] for env in envs]


def other_k8s_ids(environments, name):
    envs = filter(lambda x: x['orchestration'] == 'kubernetes' and x['name'] != name,
                  environments)
    return [env['id'] for env in envs]


def env_ids_by_name(environments, name):
    envs = filter(lambda x: x['name'] == name, environments)
    return [env['id'] for env in envs]


def env_info_by_id(environments, env_id):
    env = filter(lambda x: x['id'] == env_id, environments)
    return [{'id': x['id'], 'name': x['name']} for x in env][0]


def delete_multiple_environments(rancher_address, env_ids):
    deleted = []
    for env_id in env_ids:
        deleted.append(delete_environment(rancher_address, env_id))
    return deleted


def delete_environment(rancher_address, env_id):
    req = requests.delete('{}/v2-beta/projects/{}'.format(rancher_address, env_id))
    deleted = req.json()['data'][0]
    return {'id': deleted['id'],
            'name': deleted['name'],
            'orchestration': deleted['orchestration']}


def create_k8s_environment(rancher_address, name, descr):
    k8s_template_id = None
    for _ in range(10):
        k8s_template = requests.get(
            '{}/v2-beta/projecttemplates?name=Kubernetes'.format(rancher_address)).json()
        if k8s_template['data']:
            k8s_template_id = k8s_template['data'][0]['id']
            break
        time.sleep(3)
    if k8s_template_id is None:
        raise ValueError('Template for kubernetes not found.')
    body = {
        'name': name,
        'description': descr,
        'projectTemplateId': k8s_template_id,
        'allowSystemRole': False,
        'members': [],
        'virtualMachine': False,
        'servicesPortRange': None,
        'projectLinks': []
    }

    body_json = json.dumps(body)
    req = requests.post('{}/v2-beta/projects'.format(rancher_address), data=body_json)
    created = req.json()
    return {'id': created['id'], 'name': created['name']}


def get_kubelet_service(rancher_address, env_id):
    for _ in range(10):
        response = requests.get(
            '{}/v2-beta/projects/{}/services/?name=kubelet'.format(rancher_address,
                                                                   env_id))

        if response.status_code >= 400:
            # too early or too late for obtaining data
            # small delay will improve our chances to collect it
            time.sleep(1)
            continue

        content = response.json()

        if content['data']:
            return content['data'][0]

        # this is unfortunate, response from service api received but data
        # not available, lets try again
        time.sleep(5)

    return None


def fix_datavolume_rhel(rancher_address, env_id):
    kubelet_svc = get_kubelet_service(rancher_address, env_id)
    if kubelet_svc:
        try:
            data_volume_index = kubelet_svc['launchConfig']['dataVolumes'].index(
                '/sys:/sys:ro,rprivate')
        except ValueError:
            return 'Already changed'
        kubelet_svc['launchConfig']['dataVolumes'][
            data_volume_index] = '/sys/fs/cgroup:/sys/fs/cgroup:ro,rprivate'
        body = {
            'inServiceStrategy': {
                'batchSize': 1,
                'intervalMillis': 2000,
                'startFirst': False,
                'launchConfig': kubelet_svc['launchConfig'],
                'secondaryLaunchConfigs': []
            }
        }
        body_json = json.dumps(body)
        requests.post(
            '{}/v2-beta/projects/{}/services/{}?action=upgrade'.format(rancher_address,
                                                                       env_id,
                                                                       kubelet_svc[
                                                                           'id']),
            data=body_json)
        for _ in range(10):
            req_svc = requests.get(
                '{}/v2-beta/projects/{}/services/{}'.format(rancher_address, env_id,
                                                            kubelet_svc['id']))
            req_svc_content = req_svc.json()
            if 'finishupgrade' in req_svc_content['actions']:
                req_finish = requests.post(
                    req_svc_content['actions']['finishupgrade'])
                return {
                    'dataVolumes': req_finish.json()['upgrade']['inServiceStrategy'][
                        'launchConfig']['dataVolumes']}
            time.sleep(5)
    else:
        raise ValueError('Could not get kubelet service')


def create_registration_tokens(rancher_address, env_id):
    body = {'name': str(env_id)}
    body_json = json.dumps(body)
    response = requests.post(
        '{}/v2-beta/projects/{}/registrationtokens'.format(rancher_address, env_id,
                                                           data=body_json))
    for _ in range(10):
        tokens = requests.get(response.json()['links']['self'])
        tokens_content = tokens.json()
        if tokens_content['image'] is not None and tokens_content[
                'registrationUrl'] is not None:
            return {'image': tokens_content['image'],
                    'reg_url': tokens_content['registrationUrl']}
        time.sleep(3)
    return None


def get_registration_tokens(rancher_address, env_id):
    reg_tokens = requests.get(
        '{}/v2-beta/projects/{}/registrationtokens'.format(rancher_address, env_id))
    reg_tokens_content = reg_tokens.json()
    tokens = reg_tokens_content['data']
    if not tokens:
        return None
    return {'image': tokens[0]['image'], 'reg_url': tokens[0]['registrationUrl']}


def create_apikey(rancher_address, env_id):
    body = {
        'name': 'kubectl_env_{}'.format(env_id),
        'description': "Provides access to kubectl"
    }
    body_json = json.dumps(body)
    apikey_req = requests.post(
        '{}/v2-beta/apikey'.format(rancher_address, env_id, data=body_json))
    apikey_content = apikey_req.json()
    return {'public': apikey_content['publicValue'],
            'private': apikey_content['secretValue']}


def run_module():
    module = AnsibleModule(
        argument_spec=dict(
            server=dict(type='str', required=True),
            name=dict(type='str', required=True),
            descr=dict(type='str'),
            state=dict(type='str', choices=['present', 'absent'], default='present'),
            delete_other_k8s=dict(type='bool', default=False),
            delete_not_k8s=dict(type='bool', default=True),
            force=dict(type='bool', default=True),
            host_os=dict(type='str', required=True)
        )
    )

    params = module.params
    rancher_address = params['server']
    name = params['name']
    descr = params['descr']
    delete_not_k8s = params['delete_not_k8s']
    delete_other_k8s = params['delete_other_k8s']
    force = params['force']
    host_os = params['host_os']
    state = params['state']

    existing_envs = get_existing_environments(rancher_address)
    same_name_ids = env_ids_by_name(existing_envs, name)

    to_delete_ids = []
    changes = {}

    if delete_other_k8s:
        to_delete_ids += other_k8s_ids(existing_envs, name)

    if delete_not_k8s:
        to_delete_ids += not_k8s_ids(existing_envs)
    if force or state == 'absent':
        to_delete_ids += same_name_ids

    deleted = delete_multiple_environments(rancher_address, to_delete_ids)

    if deleted:
        changes['deleted'] = deleted
        if state == 'absent':
            module.exit_json(changed=True, deleted=changes['deleted'])
    else:
        if state == 'absent':
            module.exit_json(changed=False)

    if len(same_name_ids) > 1 and not force:
        module.fail_json(msg='Multiple environments with same name. '
                             'Use "force: yes" to delete '
                             'all environments with same name.')

    if same_name_ids and not force:
        changes['environment'] = env_info_by_id(existing_envs, same_name_ids[0])
        if host_os == 'RedHat':
            try:
                rhel_fix = fix_datavolume_rhel(rancher_address, same_name_ids[0])
                changes['rhel_fix'] = rhel_fix
            except ValueError as err:
                module.fail_json(
                    msg='Error: {} Try to recreate k8s environment.'.format(err))

        reg_tokens = get_registration_tokens(rancher_address, same_name_ids[0])
        if not reg_tokens:
            reg_tokens = create_registration_tokens(rancher_address, same_name_ids[0])
        changes['registration_tokens'] = reg_tokens

        apikey = create_apikey(rancher_address, same_name_ids[0])
        changes['apikey'] = apikey
        module.exit_json(changed=True, data=changes,
                         msg='New environment was not created. Only set up was done')
    try:
        new_env = create_k8s_environment(rancher_address, name, descr)
    except ValueError as err:
        module.fail_json(msg='Error: {} Try to recreate k8s environment.'.format(err))

    if host_os == 'RedHat':
        try:
            rhel_fix = fix_datavolume_rhel(rancher_address, new_env['id'])
            changes['rhel_fix'] = rhel_fix
        except ValueError as err:
            module.fail_json(msg='Error: {} Try to recreate k8s environment.'.format(
                err))

    reg_tokens = create_registration_tokens(rancher_address, new_env['id'])

    apikey = create_apikey(rancher_address, new_env['id'])

    changes['environment'] = new_env
    changes['registration_tokens'] = reg_tokens
    changes['apikey'] = apikey

    module.exit_json(changed=True, data=changes)


if __name__ == '__main__':
    run_module()