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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Catalyst IT Ltd.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = r'''
---
module: coe_cluster
short_description: Manage COE cluster in OpenStack Cloud
author: OpenStack Ansible SIG
description:
- Add or remove a COE (Container Orchestration Engine) cluster
via OpenStack's Magnum aka Container Infrastructure Management API.
options:
cluster_template_id:
description:
- The template ID of cluster template.
- Required if I(state) is C(present).
type: str
discovery_url:
description:
- URL used for cluster node discovery.
type: str
flavor_id:
description:
- The flavor of the minion node for this cluster template.
type: str
is_floating_ip_enabled:
description:
- Indicates whether created cluster should have a floating ip.
- Whether enable or not using the floating IP of cloud provider. Some
cloud providers used floating IP, some used public IP, thus Magnum
provide this option for specifying the choice of using floating IP.
- If not set, the value of I(is_floating_ip_enabled) of the cluster template
specified with I(cluster_template_id) will be used.
- When I(is_floating_ip_enabled) is set to C(true), then
I(external_network_id) in cluster template must be defined.
type: bool
aliases: ['floating_ip_enabled']
keypair:
description:
- Name of the keypair to use.
type: str
labels:
description:
- One or more key/value pairs.
type: raw
master_count:
description:
- The number of master nodes for this cluster.
- Magnum's default value for I(master_count) is 1.
type: int
master_flavor_id:
description:
- The flavor of the master node for this cluster template.
type: str
name:
description:
- Name that has to be given to the cluster template.
required: true
type: str
node_count:
description:
- The number of nodes for this cluster.
- Magnum's default value for I(node_count) is 1.
type: int
state:
description:
- Indicate desired state of the resource.
choices: [present, absent]
default: present
type: str
extends_documentation_fragment:
- openstack.cloud.openstack
'''
RETURN = r'''
cluster:
description: Dictionary describing the cluster.
returned: On success when I(state) is C(present).
type: dict
contains:
api_address:
description: The endpoint URL of COE API exposed to end-users.
type: str
sample: https://172.24.4.30:6443
cluster_template_id:
description: The UUID of the cluster template.
type: str
sample: '7b1418c8-cea8-48fc-995d-52b66af9a9aa'
coe_version:
description: Version info of chosen COE in bay/cluster for helping
client in picking the right version of client.
type: str
sample: v1.11.1
create_timeout:
description: Timeout for creating the cluster in minutes.
Default to 60 if not set.
type: int
sample: 60
created_at:
description: The date and time in UTC at which the cluster is created.
type: str
sample: "2018-08-16T10:29:45+00:00"
discovery_url:
description: The custom discovery url for node discovery. This is used
by the COE to discover the servers that have been created
to host the containers. The actual discovery mechanism
varies with the COE. In some cases, the service fills in
the server info in the discovery service. In other cases,
if the discovery_url is not specified, the service will
use the public discovery service at
U(https://discovery.etcd.io). In this case, the service
will generate a unique url here for each bay and store the
info for the servers.
type: str
sample: https://discovery.etcd.io/a42ee38e7113f31f4d6324f24367aae5
fixed_network:
description: The name or ID of the network to provide connectivity to the
internal network for the bay/cluster.
type: str
fixed_subnet:
description: The fixed subnet to use when allocating network addresses
for nodes in bay/cluster.
type: str
flavor_id:
description: The flavor name or ID to use when booting the node servers.
Defaults to m1.small.
type: str
id:
description: Unique UUID for this cluster.
type: str
sample: '86246a4d-a16c-4a58-9e96ad7719fe0f9d'
is_floating_ip_enabled:
description: Indicates whether created clusters should have a
floating ip or not.
type: bool
sample: true
is_master_lb_enabled:
description: Indicates whether created clusters should have a load
balancer for master nodes or not.
type: bool
sample: true
keypair:
description: Name of the keypair to use.
type: str
sample: mykey
labels:
description: One or more key/value pairs.
type: dict
sample: {'key1': 'value1', 'key2': 'value2'}
master_addresses:
description: A list of floating IPs of all master nodes.
type: list
sample: ['172.24.4.5']
master_count:
description: The number of servers that will serve as master for the
bay/cluster. Set to more than 1 master to enable High
Availability. If the option master-lb-enabled is specified
in the baymodel/cluster template, the master servers will
be placed in a load balancer pool. Defaults to 1.
type: int
sample: 1
master_flavor_id:
description: The flavor of the master node for this baymodel/cluster
template.
type: str
sample: c1.c1r1
name:
description: Name that has to be given to the cluster.
type: str
sample: k8scluster
node_addresses:
description: A list of floating IPs of all servers that serve as nodes.
type: list
sample: ['172.24.4.8']
node_count:
description: The number of master nodes for this cluster.
type: int
sample: 1
stack_id:
description: The reference UUID of orchestration stack from Heat
orchestration service.
type: str
sample: '07767ec6-85f5-44cb-bd63-242a8e7f0d9d'
status:
description: Status of the cluster from the heat stack.
type: str
sample: 'CREATE_COMLETE'
status_reason:
description: Status reason of the cluster from the heat stack
type: str
sample: 'Stack CREATE completed successfully'
updated_at:
description: The date and time in UTC at which the cluster was updated.
type: str
sample: '2018-08-16T10:39:25+00:00'
uuid:
description: Unique UUID for this cluster.
type: str
sample: '86246a4d-a16c-4a58-9e96ad7719fe0f9d'
'''
EXAMPLES = r'''
- name: Create a new Kubernetes cluster
openstack.cloud.coe_cluster:
cloud: devstack
cluster_template_id: k8s-ha
keypair: mykey
master_count: 3
name: k8s
node_count: 5
'''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
class COEClusterModule(OpenStackModule):
argument_spec = dict(
cluster_template_id=dict(),
discovery_url=dict(),
flavor_id=dict(),
is_floating_ip_enabled=dict(type='bool',
aliases=['floating_ip_enabled']),
keypair=dict(no_log=False), # := noqa no-log-needed
labels=dict(type='raw'),
master_count=dict(type='int'),
master_flavor_id=dict(),
name=dict(required=True),
node_count=dict(type='int'),
state=dict(default='present', choices=['absent', 'present']),
)
module_kwargs = dict(
required_if=[
('state', 'present', ('cluster_template_id',))
],
supports_check_mode=True,
)
def run(self):
state = self.params['state']
cluster = self._find()
if self.ansible.check_mode:
self.exit_json(changed=self._will_change(state, cluster))
if state == 'present' and not cluster:
# Create cluster
cluster = self._create()
self.exit_json(changed=True,
cluster=cluster.to_dict(computed=False))
elif state == 'present' and cluster:
# Update cluster
update = self._build_update(cluster)
if update:
cluster = self._update(cluster, update)
self.exit_json(changed=bool(update),
cluster=cluster.to_dict(computed=False))
elif state == 'absent' and cluster:
# Delete cluster
self._delete(cluster)
self.exit_json(changed=True)
elif state == 'absent' and not cluster:
# Do nothing
self.exit_json(changed=False)
def _build_update(self, cluster):
update = {}
# TODO: Implement support for updates.
non_updateable_keys = [k for k in ['cluster_template_id',
'discovery_url', 'flavor_id',
'is_floating_ip_enabled', 'keypair',
'master_count', 'master_flavor_id',
'name', 'node_count']
if self.params[k] is not None
and self.params[k] != cluster[k]]
labels = self.params['labels']
if labels is not None:
if isinstance(labels, str):
labels = dict([tuple(kv.split(":"))
for kv in labels.split(",")])
if labels != cluster['labels']:
non_updateable_keys.append('labels')
if non_updateable_keys:
self.fail_json(msg='Cannot update parameters {0}'
.format(non_updateable_keys))
attributes = dict((k, self.params[k])
for k in []
if self.params[k] is not None
and self.params[k] != cluster[k])
if attributes:
update['attributes'] = attributes
return update
def _create(self):
# TODO: Complement *_id parameters with find_* functions to allow
# specifying names in addition to IDs.
kwargs = dict((k, self.params[k])
for k in ['cluster_template_id', 'discovery_url',
'flavor_id', 'is_floating_ip_enabled',
'keypair', 'master_count', 'master_flavor_id',
'name', 'node_count']
if self.params[k] is not None)
labels = self.params['labels']
if labels is not None:
if isinstance(labels, str):
labels = dict([tuple(kv.split(":"))
for kv in labels.split(",")])
kwargs['labels'] = labels
kwargs['create_timeout'] = self.params['timeout']
cluster = self.conn.container_infrastructure_management.\
create_cluster(**kwargs)
if not self.params['wait']:
# openstacksdk's create_cluster() returns a cluster's id only
# but we cannot use self.conn.container_infrastructure_management.\
# get_cluster(cluster_id) because it might return None as long as
# the cluster is being set up.
return cluster
if self.params['wait']:
cluster = self.sdk.resource.wait_for_status(
self.conn.container_infrastructure_management, cluster,
status='active',
failures=['error'],
wait=self.params['timeout'])
return cluster
def _delete(self, cluster):
self.conn.container_infrastructure_management.\
delete_cluster(cluster['id'])
if self.params['wait']:
self.sdk.resource.wait_for_delete(
self.conn.container_infrastructure_management, cluster,
interval=None, wait=self.params['timeout'])
def _find(self):
name = self.params['name']
filters = {}
cluster_template_id = self.params['cluster_template_id']
if cluster_template_id is not None:
filters['cluster_template_id'] = cluster_template_id
return self.conn.get_coe_cluster(name_or_id=name, filters=filters)
def _update(self, cluster, update):
attributes = update.get('attributes')
if attributes:
# TODO: Implement support for updates.
# cluster = self.conn.container_infrastructure_management.\
# update_cluster(...)
pass
return cluster
def _will_change(self, state, cluster):
if state == 'present' and not cluster:
return True
elif state == 'present' and cluster:
return bool(self._build_update(cluster))
elif state == 'absent' and cluster:
return True
else:
# state == 'absent' and not cluster:
return False
def main():
module = COEClusterModule()
module()
if __name__ == "__main__":
main()
|