-
Notifications
You must be signed in to change notification settings - Fork 21
/
driver.py
641 lines (530 loc) · 23.5 KB
/
driver.py
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# Copyright (c) 2023 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import keystoneauth1
from magnum import objects as magnum_objects
from magnum.conductor import scale_manager
from magnum.drivers.common import driver
from magnum.objects import fields
from magnum_cluster_api import (
clients,
exceptions,
monitor,
objects,
resources,
sync,
utils,
)
def cluster_lock_wrapper(func):
def wrapper(*args, **kwargs):
cluster = args[2] # Assuming cluster is the second argument
with sync.ClusterLock(cluster.uuid):
return func(*args, **kwargs)
return wrapper
class BaseDriver(driver.Driver):
def __init__(self):
self.k8s_api = clients.get_pykube_api()
def create_cluster(
self, context, cluster: magnum_objects.Cluster, cluster_create_timeout: int
):
"""
Create cluster.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
# NOTE(mnaser): We want to set the `stack_id` as early as possible to
# make sure we can use it in the cluster creation.
cluster.stack_id = utils.generate_cluster_api_name(self.k8s_api)
cluster.save()
utils.validate_cluster(context, cluster)
resources.Namespace(self.k8s_api).apply()
return self._create_cluster(context, cluster)
@cluster_lock_wrapper
def _create_cluster(self, context, cluster: magnum_objects.Cluster):
osc = clients.get_openstack_api(context)
credential = osc.keystone().client.application_credentials.create(
user=cluster.user_id,
name=cluster.uuid,
description=f"Magnum cluster ({cluster.uuid})",
)
resources.CloudConfigSecret(
context,
self.k8s_api,
cluster,
osc.cinder_region_name(),
credential,
).apply()
resources.ApiCertificateAuthoritySecret(context, self.k8s_api, cluster).apply()
resources.EtcdCertificateAuthoritySecret(context, self.k8s_api, cluster).apply()
resources.FrontProxyCertificateAuthoritySecret(
context, self.k8s_api, cluster
).apply()
resources.ServiceAccountCertificateAuthoritySecret(
context, self.k8s_api, cluster
).apply()
resources.apply_cluster_from_magnum_cluster(
context, self.k8s_api, cluster, skip_auto_scaling_release=True
)
def _get_cluster_status_reason(self, capi_cluster):
capi_cluster_status_reason = ""
capi_ops_cluster_status_reason = ""
# Get the latest event message of the CAPI Cluster
capi_cluster_events = capi_cluster.events
if capi_cluster_events:
capi_cluster_status_reason += utils.format_event_message(
list(capi_cluster_events)[-1]
)
# Get the latest event message of the CAPI OpenstackCluster
capi_ops_cluster_events = []
capi_ops_cluster = capi_cluster.openstack_cluster
if capi_ops_cluster:
capi_ops_cluster_events = capi_ops_cluster.events
if capi_ops_cluster_events:
capi_ops_cluster_status_reason += utils.format_event_message(
list(capi_ops_cluster_events)[-1]
)
return "CAPI Cluster status: %s. CAPI OpenstackCluster status reason: %s" % (
capi_cluster_status_reason,
capi_ops_cluster_status_reason,
)
def update_cluster_control_plane_status(
self,
context,
cluster: magnum_objects.Cluster,
):
nodegroup = cluster.default_ng_master
action = nodegroup.status.split("_")[0]
kcp = resources.get_kubeadm_control_plane(self.k8s_api, cluster)
if kcp is None:
return nodegroup
generation = kcp.obj.get("status", {}).get("observedGeneration", 1)
if generation > 1:
action = "UPDATE"
ready = kcp.obj.get("status", {}).get("ready", False)
failure_message = kcp.obj.get("status", {}).get("failureMessage")
updated_replicas = kcp.obj.get("status", {}).get("updatedReplicas")
replicas = kcp.obj.get("status", {}).get("replicas")
if updated_replicas != replicas:
nodegroup.status = f"{action}_IN_PROGRESS"
elif updated_replicas == replicas and ready:
nodegroup.status = f"{action}_COMPLETE"
nodegroup.status_reason = failure_message
nodegroup.save()
return nodegroup
@cluster_lock_wrapper
def update_cluster_status(
self, context, cluster: magnum_objects.Cluster, use_admin_ctx: bool = False
):
# NOTE(mnaser): We may be called with a stale cluster object, so we
# need to refresh it to make sure we have the latest data.
cluster.refresh()
# TODO: watch for topology change instead
node_groups = [
self.update_cluster_control_plane_status(context, cluster)
] + self.update_nodegroups_status(context, cluster)
osc = clients.get_openstack_api(context)
capi_cluster = resources.Cluster(context, self.k8s_api, cluster).get_or_none()
if cluster.status in (
fields.ClusterStatus.CREATE_IN_PROGRESS,
fields.ClusterStatus.UPDATE_IN_PROGRESS,
):
# NOTE(mnaser): It's possible we run a cluster status update before
# the cluster is created. In that case, we don't want
# to update the cluster status.
if capi_cluster is None:
return
capi_cluster.reload()
status_map = {
c["type"]: c["status"] for c in capi_cluster.obj["status"]["conditions"]
}
for condition in ("ControlPlaneReady", "InfrastructureReady", "Ready"):
if status_map.get(condition) != "True":
cluster.status_reason = self._get_cluster_status_reason(
capi_cluster
)
cluster.save()
return
api_endpoint = capi_cluster.obj["spec"]["controlPlaneEndpoint"]
cluster.api_address = (
f"https://{api_endpoint['host']}:{api_endpoint['port']}"
)
cluster.coe_version = capi_cluster.obj["spec"]["topology"]["version"]
# NOTE(oleks): To avoid autoscaler crashes, we deploy it after the
# cluster api endpoint is reachable.
if (
cluster.status == fields.ClusterStatus.CREATE_IN_PROGRESS
and utils.get_auto_scaling_enabled(cluster)
):
resources.ClusterAutoscalerHelmRelease(self.k8s_api, cluster).apply()
for ng in node_groups:
if not ng.status.endswith("_COMPLETE"):
return
if ng.status == fields.ClusterStatus.DELETE_COMPLETE:
ng.destroy()
if cluster.status == fields.ClusterStatus.CREATE_IN_PROGRESS:
cluster.status_reason = None
cluster.status = fields.ClusterStatus.CREATE_COMPLETE
if cluster.status == fields.ClusterStatus.UPDATE_IN_PROGRESS:
cluster.status_reason = None
cluster.status = fields.ClusterStatus.UPDATE_COMPLETE
cluster.save()
if cluster.status == fields.ClusterStatus.DELETE_IN_PROGRESS:
if capi_cluster and capi_cluster.exists():
cluster.status_reason = self._get_cluster_status_reason(capi_cluster)
cluster.save()
return
# NOTE(mnaser): We delete the application credentials at this stage
# to make sure CAPI doesn't lose access to OpenStack.
try:
osc.keystone().client.application_credentials.find(
name=cluster.uuid,
user=cluster.user_id,
).delete()
except keystoneauth1.exceptions.http.NotFound:
pass
resources.CloudConfigSecret(context, self.k8s_api, cluster).delete()
resources.ApiCertificateAuthoritySecret(
context, self.k8s_api, cluster
).delete()
resources.EtcdCertificateAuthoritySecret(
context, self.k8s_api, cluster
).delete()
resources.FrontProxyCertificateAuthoritySecret(
context, self.k8s_api, cluster
).delete()
resources.ServiceAccountCertificateAuthoritySecret(
context, self.k8s_api, cluster
).delete()
cluster.status_reason = None
cluster.status = fields.ClusterStatus.DELETE_COMPLETE
cluster.save()
@cluster_lock_wrapper
def update_cluster(
self,
context,
cluster: magnum_objects.Cluster,
scale_manager=None,
rollback=False,
):
"""
Update cluster.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
raise NotImplementedError()
@cluster_lock_wrapper
def resize_cluster(
self,
context,
cluster: magnum_objects.Cluster,
resize_manager: scale_manager.ScaleManager,
node_count: int,
nodes_to_remove: list[str],
nodegroup: magnum_objects.NodeGroup = None,
):
"""
Resize cluster (primarily add or remove nodes).
The cluster object passed to this method is already not in `UPDATE_IN_PROGRESS`
state and the node group object passed to this method is in `UPDATE_IN_PROGRESS`
state and saved.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
utils.validate_cluster(context, cluster)
if nodes_to_remove:
machines = objects.Machine.objects(self.k8s_api).filter(
namespace="magnum-system",
selector={
"cluster.x-k8s.io/cluster-name": cluster.stack_id,
"topology.cluster.x-k8s.io/deployment-name": nodegroup.name,
},
)
for machine in machines:
instance_uuid = machine.obj["spec"]["providerID"].split("/")[-1]
if instance_uuid in nodes_to_remove:
machine.obj["metadata"].setdefault("annotations", {})
machine.obj["metadata"]["annotations"][
"cluster.x-k8s.io/delete-machine"
] = "yes"
utils.kube_apply_patch(machine)
self._update_nodegroup(context, cluster, nodegroup)
@cluster_lock_wrapper
def upgrade_cluster(
self,
context,
cluster: magnum_objects.Cluster,
cluster_template: magnum_objects.ClusterTemplate,
max_batch_size,
nodegroup: magnum_objects.NodeGroup,
scale_manager=None,
rollback=False,
):
"""
Upgrade a cluster to a new version of Kubernetes.
The only label that we change during the upgrade is the `kube_tag` label.
Historically, the upgrade cluster has been a "hammer" that was used to sync the
Kubernetes Cluster API objects with the Magnum objects. However, by doing this,
we're losing the ability to maintain the existing labels of the cluster.
For now, upgrade cluster simply modifies the labels that are necessary for the
upgrade, nothing else. For the future, we can perhaps use the `update_cluster`
API.
This method is called synchonously by the Magnum API, therefore it will be blocking
the Magnum API, so it should be as fast as possible.
"""
# XXX(mnaser): The Magnum API historically only did upgrade one node group at a
# time. This is a limitation of the Magnum API and not the Magnum
# Cluster API since doing multiple rolling upgrades was not very
# well supported in the past.
#
# The Magnum Cluster API does not have this limitation in this case
# we ignore the `nodegroup` parameter and upgrade the entire cluster
# at once.
cluster.cluster_template_id = cluster_template.uuid
cluster.labels["kube_tag"] = cluster_template.labels["kube_tag"]
for ng in cluster.nodegroups:
ng.status = fields.ClusterStatus.UPDATE_IN_PROGRESS
ng.image_id = cluster_template.image_id
ng.labels["kube_tag"] = cluster_template.labels["kube_tag"]
ng.save()
# NOTE(mnaser): We run a full apply on the cluster regardless of the changes, since
# the expectation is that running an upgrade operation will change
# the cluster in some way.
resources.apply_cluster_from_magnum_cluster(context, self.k8s_api, cluster)
# NOTE(mnaser): We do not save the cluster object here because the Magnum driver
# will save the object that it passed to us here.
@cluster_lock_wrapper
def delete_cluster(self, context, cluster: magnum_objects.Cluster):
"""
Delete cluster.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
if cluster.stack_id is None:
return
# NOTE(mnaser): This should be removed when this is fixed:
#
# https://github.com/kubernetes-sigs/cluster-api-provider-openstack/issues/842
# https://github.com/kubernetes-sigs/cluster-api-provider-openstack/pull/990
utils.delete_loadbalancers(context, cluster)
resources.ClusterResourceSet(self.k8s_api, cluster).delete()
resources.ClusterResourcesConfigMap(context, self.k8s_api, cluster).delete()
resources.Cluster(context, self.k8s_api, cluster).delete()
resources.ClusterAutoscalerHelmRelease(self.k8s_api, cluster).delete()
@cluster_lock_wrapper
def create_nodegroup(
self,
context,
cluster: magnum_objects.Cluster,
nodegroup: magnum_objects.NodeGroup,
):
"""
Create node group.
The cluster object passed to this method is already in `UPDATE_IN_PROGRESS` state
and the node group object passed to this method is in `CREATE_IN_PROGRESS` state.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
utils.validate_nodegroup(nodegroup, context)
cluster_resource = objects.Cluster.for_magnum_cluster(self.k8s_api, cluster)
cluster_resource.obj["spec"]["topology"]["workers"][
"machineDeployments"
].append(resources.mutate_machine_deployment(context, cluster, nodegroup))
utils.kube_apply_patch(cluster_resource)
def update_nodegroups_status(
self, context, cluster: magnum_objects.Cluster
) -> list[magnum_objects.NodeGroup]:
cluster_resource = objects.Cluster.for_magnum_cluster_or_none(
self.k8s_api, cluster
)
if cluster_resource is None:
return cluster.nodegroups
node_groups = []
for node_group in cluster.nodegroups:
# NOTE(mnaser): Nothing to do if the node group is in `DELETE_COMPLETE`
# state and skip work if it's a master node group.
if (
node_group.role == "master"
or node_group.status == fields.ClusterStatus.DELETE_COMPLETE
):
continue
node_groups.append(node_group)
md = objects.MachineDeployment.for_node_group_or_none(
self.k8s_api, cluster, node_group
)
# NOTE(mnaser): If the cluster is in `DELETE_IN_PROGRESS` state, we need to
# wait for the `MachineDeployment` to be deleted before we can
# mark the node group as `DELETE_COMPLETE`.
if (
node_group.status == fields.ClusterStatus.DELETE_IN_PROGRESS
and md is None
):
node_group.status = fields.ClusterStatus.DELETE_COMPLETE
node_group.save()
continue
md_is_running = (
md is not None and md.obj.get("status", {}).get("phase") == "Running"
)
# NOTE(mnaser): If the node group is in `CREATE_IN_PROGRESS` state, we need to
# wait for the `MachineDeployment` to be hit the `Running` phase
# before we can mark the node group as `CREATE_COMPLETE`.
if (
node_group.status == fields.ClusterStatus.CREATE_IN_PROGRESS
and md_is_running
):
node_group.status = fields.ClusterStatus.CREATE_COMPLETE
node_group.save()
continue
# Get list of all of the OpenStackMachine objects for this node group
machines = objects.OpenStackMachine.objects(
self.k8s_api, namespace="magnum-system"
).filter(
selector={
"cluster.x-k8s.io/cluster-name": cluster.stack_id,
"topology.cluster.x-k8s.io/deployment-name": node_group.name,
},
)
# Ensure that the image ID from the spec matches all of the OpenStackMachine objects
# for this node group
md_spec = cluster_resource.get_machine_deployment_spec(node_group.name)
md_variables = {
i["name"]: i["value"] for i in md_spec["variables"]["overrides"]
}
image_id_match = all(
[
machine.obj["spec"]["imageUUID"] == md_variables["imageUUID"]
for machine in machines
]
)
# NOTE(mnaser): If the cluster is in `UPDATE_IN_PROGRESS` state, we need to
# wait for the `MachineDeployment` to match the desired state
# from the `Cluster` resource and that it is in the `Running`
# phase before we can mark the node group as `UPDATE_COMPLETE`.
if (
node_group.status == fields.ClusterStatus.UPDATE_IN_PROGRESS
and md_is_running
and md.equals_spec(
cluster_resource.get_machine_deployment_spec(node_group.name)
)
and image_id_match
):
node_group.status = fields.ClusterStatus.UPDATE_COMPLETE
node_group.save()
continue
return node_groups
@cluster_lock_wrapper
def update_nodegroup(
self,
context,
cluster: magnum_objects.Cluster,
nodegroup: magnum_objects.NodeGroup,
):
"""
Update node group (primarily resize it)
This cluster object passed to this method is already in `UPDATE_IN_PROGRESS` state
and the node group object passed to this method is in `UPDATE_IN_PROGRESS` state
but it's not saved.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
self._update_nodegroup(context, cluster, nodegroup)
def _update_nodegroup(
self,
context,
cluster: magnum_objects.Cluster,
nodegroup: magnum_objects.NodeGroup,
):
utils.validate_nodegroup(nodegroup, context)
cluster_resource = objects.Cluster.for_magnum_cluster(self.k8s_api, cluster)
current_md_spec = cluster_resource.get_machine_deployment_spec(nodegroup.name)
target_md_spec = resources.mutate_machine_deployment(
context,
cluster,
nodegroup,
cluster_resource.get_machine_deployment_spec(nodegroup.name),
)
if current_md_spec == target_md_spec:
return
cluster_resource.set_machine_deployment_spec(nodegroup.name, target_md_spec)
utils.kube_apply_patch(cluster_resource)
nodegroup.status = fields.ClusterStatus.UPDATE_IN_PROGRESS
nodegroup.save()
cluster.status = fields.ClusterStatus.UPDATE_IN_PROGRESS
cluster.save()
@cluster_lock_wrapper
def delete_nodegroup(
self,
context,
cluster: magnum_objects.Cluster,
nodegroup: magnum_objects.NodeGroup,
):
"""
Delete node group.
The cluster resource that is passed to this method is already in `UPDATE_IN_PROGRESS`
however the node group object passed to this method is in `DELETE_IN_PROGRESS` state
but it's not saved.
This method is called asynchonously by the Magnum API, therefore it will not be
blocking the Magnum API.
"""
# NOTE(mnaser): We want to switch the node group to `DELETE_IN_PROGRESS` state
# as soon as possible to make sure that the Magnum API knows that
# the node group is being deleted.
nodegroup.status = fields.ClusterStatus.DELETE_IN_PROGRESS
nodegroup.save()
cluster_resource = objects.Cluster.for_magnum_cluster(self.k8s_api, cluster)
try:
md_index = cluster_resource.get_machine_deployment_index(nodegroup.name)
except exceptions.MachineDeploymentNotFound:
nodegroup.status = fields.ClusterStatus.DELETE_COMPLETE
nodegroup.save()
return
del cluster_resource.obj["spec"]["topology"]["workers"]["machineDeployments"][
md_index
]
utils.kube_apply_patch(cluster_resource)
@cluster_lock_wrapper
def get_monitor(self, context, cluster: magnum_objects.Cluster):
return monitor.Monitor(context, cluster)
# def rotate_ca_certificate(self, context, cluster):
# raise exception.NotSupported(
# "'rotate_ca_certificate' is not supported by this driver.")
def create_federation(self, context, federation):
raise NotImplementedError()
def update_federation(self, context, federation):
raise NotImplementedError()
def delete_federation(self, context, federation):
raise NotImplementedError()
class UbuntuDriver(BaseDriver):
@property
def provides(self):
return [
{"server_type": "vm", "os": "ubuntu", "coe": "kubernetes"},
]
class UbuntuFocalDriver(UbuntuDriver):
@property
def provides(self):
return [
{"server_type": "vm", "os": "ubuntu-focal", "coe": "kubernetes"},
]
class FlatcarDriver(BaseDriver):
@property
def provides(self):
return [
{"server_type": "vm", "os": "flatcar", "coe": "kubernetes"},
]
class RockyLinuxDriver(BaseDriver):
@property
def provides(self):
return [
{"server_type": "vm", "os": "rockylinux", "coe": "kubernetes"},
]