/
utils.py
480 lines (384 loc) · 14.9 KB
/
utils.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
# 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 json
import re
import string
import textwrap
import pykube
import shortuuid
import yaml
from magnum import objects as magnum_objects
from magnum.api import attr_validator
from magnum.common import context, exception, neutron, octavia
from magnum.common import utils as magnum_utils
from oslo_config import cfg
from oslo_serialization import base64
from oslo_utils import strutils, uuidutils
from tenacity import retry, retry_if_exception_type
from magnum_cluster_api import clients
from magnum_cluster_api import exceptions as mcapi_exceptions
from magnum_cluster_api import image_utils, images, objects
AVAILABLE_OPERATING_SYSTEMS = ["ubuntu", "flatcar", "rockylinux"]
CONF = cfg.CONF
def get_cluster_api_cloud_config_secret_name(cluster: magnum_objects.Cluster) -> str:
return f"{cluster.stack_id}-cloud-config"
def get_or_generate_cluster_api_cloud_config_secret_name(
api: pykube.HTTPClient, cluster: magnum_objects.Cluster
) -> str:
return f"{get_or_generate_cluster_api_name(api, cluster)}-cloud-config"
def get_or_generate_cluster_api_name(
api: pykube.HTTPClient, cluster: magnum_objects.Cluster
) -> str:
if cluster.stack_id is None:
cluster.stack_id = generate_cluster_api_name(api)
cluster.save()
return cluster.stack_id
@retry(retry=retry_if_exception_type(exception.Conflict))
def generate_cluster_api_name(
api: pykube.HTTPClient,
) -> str:
alphabet = string.ascii_lowercase + string.digits
su = shortuuid.ShortUUID(alphabet=alphabet)
name = "kube-%s" % (su.random(length=5))
if cluster_exists(api, name):
raise exception.Conflict("Generated name already exists")
return name
def cluster_exists(api: pykube.HTTPClient, name: str) -> bool:
try:
objects.Cluster.objects(api, namespace="magnum-system").get(name=name)
return True
except pykube.exceptions.ObjectDoesNotExist:
return False
def get_cloud_ca_cert() -> str:
"""
Get cloud ca certificate.
"""
return magnum_utils.get_openstack_ca()
def get_capi_client_ca_cert() -> str:
ca_file = CONF.capi_client.ca_file
if ca_file:
with open(ca_file) as fd:
return fd.read()
else:
return ""
def generate_cloud_controller_manager_config(
ctx: context.RequestContext,
api: pykube.HTTPClient,
cluster: magnum_objects.Cluster,
) -> str:
"""
Generate coniguration for openstack-cloud-controller-manager if it does
already exist.
"""
osc = clients.get_openstack_api(ctx)
data = pykube.Secret.objects(api, namespace="magnum-system").get_by_name(
get_cluster_api_cloud_config_secret_name(cluster)
)
clouds_yaml = base64.decode_as_text(data.obj["data"]["clouds.yaml"])
cloud_config = yaml.safe_load(clouds_yaml)
octavia_provider = cluster.labels.get("octavia_provider", "amphora")
octavia_lb_algorithm = cluster.labels.get("octavia_lb_algorithm")
if octavia_provider == "amphora" and octavia_lb_algorithm is None:
octavia_lb_algorithm = "ROUND_ROBIN"
elif octavia_provider == "ovn" and octavia_lb_algorithm is None:
octavia_lb_algorithm = "SOURCE_IP_PORT"
elif octavia_provider == "ovn" and octavia_lb_algorithm != "SOURCE_IP_PORT":
raise mcapi_exceptions.InvalidOctaviaLoadBalancerAlgorithm(
octavia_lb_algorithm=octavia_lb_algorithm
)
return textwrap.dedent(
f"""\
[Global]
auth-url={osc.url_for(service_type="identity", interface="public")}
region={cloud_config["clouds"]["default"]["region_name"]}
application-credential-id={cloud_config["clouds"]["default"]["auth"]["application_credential_id"]}
application-credential-secret={cloud_config["clouds"]["default"]["auth"]["application_credential_secret"]}
tls-insecure={"false" if CONF.drivers.verify_ca else "true"}
{"ca-file=/etc/config/ca.crt" if get_cloud_ca_cert() else ""}
[LoadBalancer]
lb-provider={octavia_provider}
lb-method={octavia_lb_algorithm}
"""
)
def generate_manila_csi_cloud_config(
ctx: context.RequestContext,
api: pykube.HTTPClient,
cluster: magnum_objects.Cluster,
) -> dict[str, str]:
"""
Generate coniguration of Openstack authentication for manila csi
"""
osc = clients.get_openstack_api(ctx)
data = pykube.Secret.objects(api, namespace="magnum-system").get_by_name(
get_cluster_api_cloud_config_secret_name(cluster)
)
clouds_yaml = base64.decode_as_text(data.obj["data"]["clouds.yaml"])
cloud_config = yaml.safe_load(clouds_yaml)
config = {
"os-authURL": osc.url_for(service_type="identity", interface="public"),
"os-region": cloud_config["clouds"]["default"]["region_name"],
"os-applicationCredentialID": cloud_config["clouds"]["default"]["auth"][
"application_credential_id"
],
"os-applicationCredentialSecret": cloud_config["clouds"]["default"]["auth"][
"application_credential_secret"
],
"os-TLSInsecure": (
("false" if CONF.drivers.verify_ca else "true")
if cloud_config["clouds"]["default"]["verify"]
else "true"
),
}
if get_cloud_ca_cert():
config["os-certAuthorityPath"] = "/etc/config/ca.crt"
return config
def get_kube_tag(cluster: magnum_objects.Cluster) -> str:
return cluster.labels.get("kube_tag", "v1.25.3")
def get_auto_scaling_enabled(cluster: magnum_objects.Cluster) -> bool:
return get_cluster_label_as_bool(cluster, "auto_scaling_enabled", False)
def get_auto_healing_enabled(cluster: magnum_objects.Cluster) -> bool:
return get_cluster_label_as_bool(cluster, "auto_healing_enabled", True)
def get_cluster_container_infra_prefix(cluster: magnum_objects.Cluster) -> str:
return cluster.labels.get("container_infra_prefix", "")
def get_cluster_floating_ip_disabled(cluster: magnum_objects.Cluster) -> bool:
return not get_cluster_label_as_bool(cluster, "master_lb_floating_ip_enabled", True)
def generate_containerd_config(
cluster: magnum_objects.Cluster,
):
image_repository = get_cluster_container_infra_prefix(cluster)
sandbox_image = image_utils.get_image(images.PAUSE, image_repository)
return textwrap.dedent(
"""\
# Use config version 2 to enable new configuration fields.
# Config file is parsed as version 1 by default.
version = 2
imports = ["/etc/containerd/conf.d/*.toml"]
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "{sandbox_image}"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
"""
).format(sandbox_image=sandbox_image)
def generate_systemd_proxy_config(cluster: magnum_objects.Cluster):
if (
cluster.cluster_template.http_proxy is not None
or cluster.cluster_template.https_proxy is not None
):
return textwrap.dedent(
"""\
[Service]
Environment="http_proxy={http_proxy}"
Environment="HTTP_PROXY={http_proxy}"
Environment="https_proxy={https_proxy}"
Environment="HTTPS_PROXY={https_proxy}"
Environment="no_proxy={no_proxy}"
Environment="NO_PROXY={no_proxy}"
"""
).format(
http_proxy=cluster.cluster_template.http_proxy,
https_proxy=cluster.cluster_template.https_proxy,
no_proxy=cluster.cluster_template.no_proxy,
)
else:
return ""
def generate_apt_proxy_config(cluster: magnum_objects.Cluster):
if (
cluster.cluster_template.http_proxy is not None
or cluster.cluster_template.https_proxy is not None
):
return textwrap.dedent(
"""\
Acquire::http::Proxy "{http_proxy}";
Acquire::https::Proxy "{https_proxy}";
"""
).format(
http_proxy=cluster.cluster_template.http_proxy,
https_proxy=cluster.cluster_template.https_proxy,
)
else:
return ""
def get_node_group_min_node_count(
node_group: magnum_objects.NodeGroup,
default=1,
) -> int:
if node_group.min_node_count == 0:
return default
return node_group.min_node_count
def get_node_group_max_node_count(
node_group: magnum_objects.NodeGroup,
) -> int:
if node_group.max_node_count is None:
return get_node_group_label_as_int(
node_group,
"max_node_count",
get_node_group_min_node_count(node_group) + 1,
)
return node_group.max_node_count
def get_node_group_label_as_int(
node_group: magnum_objects.NodeGroup,
key: str,
default: int,
) -> int:
value = node_group.labels.get(key, str(default))
return strutils.validate_integer(value, key)
def get_cluster_label_as_int(
cluster: magnum_objects.Cluster, key: str, default: int
) -> int:
value = cluster.labels.get(key, default)
return strutils.validate_integer(value, key)
def get_cluster_label_as_bool(
cluster: magnum_objects.Cluster, key: str, default: bool
) -> bool:
value = cluster.labels.get(key, default)
return strutils.bool_from_string(value, strict=True)
def delete_loadbalancers(ctx, cluster):
# NOTE(mnaser): This code is duplicated from magnum.common.octavia
# since the original code is very Heat-specific.
pattern = r"Kubernetes .+ from cluster %s" % cluster.uuid
admin_ctx = context.get_admin_context()
admin_clients = clients.get_openstack_api(admin_ctx)
user_clients = clients.get_openstack_api(ctx)
candidates = set()
try:
octavia_admin_client = admin_clients.octavia()
octavia_client = user_clients.octavia()
# Get load balancers created for service/ingress
lbs = octavia_client.load_balancer_list().get("loadbalancers", [])
lbs = [lb for lb in lbs if re.match(pattern, lb["description"])]
deleted = octavia._delete_loadbalancers(
ctx, lbs, cluster, octavia_admin_client, remove_fip=True
)
candidates.update(deleted)
if not candidates:
return
octavia.wait_for_lb_deleted(octavia_client, candidates)
except Exception as e:
raise exception.PreDeletionFailed(cluster_uuid=cluster.uuid, msg=str(e))
def format_event_message(event: pykube.Event):
return "%s: %s" % (
event.obj["reason"],
event.obj["message"],
)
def validate_flavor_name(cli: clients.OpenStackClients, flavor: str):
"""Check if a flavor with this specified name exists"""
if flavor is None:
return
flavor_list = cli.nova().flavors.list()
for f in flavor_list:
if f.name == flavor:
return
if f.id == flavor:
raise mcapi_exceptions.OpenstackFlavorInvalidName(flavor=flavor)
raise exception.FlavorNotFound(flavor=flavor)
def validate_cluster(ctx: context.RequestContext, cluster: magnum_objects.Cluster):
# Check master count
if (cluster.master_count % 2) == 0:
raise mcapi_exceptions.ClusterMasterCountEven
# Validate flavors
osc = clients.get_openstack_api(ctx)
validate_flavor_name(osc, cluster.master_flavor_id)
validate_flavor_name(osc, cluster.flavor_id)
# Check if fixed_network exists
if cluster.fixed_network:
if uuidutils.is_uuid_like(cluster.fixed_network):
neutron.get_network(
ctx,
cluster.fixed_network,
source="id",
target="name",
external=False,
)
else:
neutron.get_network(
ctx,
cluster.fixed_network,
source="name",
target="id",
external=False,
)
# Check if fixed_subnet exists
if cluster.fixed_subnet:
if uuidutils.is_uuid_like(cluster.fixed_subnet):
neutron.get_subnet(ctx, cluster.fixed_subnet, source="id", target="name")
else:
neutron.get_subnet(ctx, cluster.fixed_subnet, source="name", target="id")
def validate_nodegroup(
nodegroup: magnum_objects.NodeGroup, ctx: context.RequestContext
):
# Validate flavors
osc = clients.get_openstack_api(ctx)
validate_flavor_name(osc, nodegroup.flavor_id)
def get_operating_system(cluster: magnum_objects.Cluster):
cluster_distro = cluster.cluster_template.cluster_distro
for ops in AVAILABLE_OPERATING_SYSTEMS:
if cluster_distro.startswith(ops):
return ops
return None
def get_image_uuid(image_ref: str, ctx: context.RequestContext):
"""Get image uuid from image ref
:param image_ref: Image id or name
"""
osc = clients.get_openstack_api(ctx)
image_obj = attr_validator.validate_image(osc, image_ref)
return image_obj.get("id")
def convert_to_rfc1123(input: str) -> str:
"""
Convert a given string to RFC1123 format.
:param input: The string to be converted.
:type input: str
:return: The converted string in RFC1123 format.
:rtype: str
"""
return re.sub(r"[^a-zA-Z0-9]+", "-", input).lower()
def get_keystone_auth_default_policy(cluster: magnum_objects.Cluster):
default_policy = [
{
"resource": {
"verbs": ["list"],
"resources": ["pods", "services", "deployments", "pvc"],
"version": "*",
"namespace": "default",
},
"match": [
{"type": "role", "values": ["member"]},
{"type": "project", "values": [cluster.project_id]},
],
}
]
try:
with open(CONF.kubernetes.keystone_auth_default_policy) as f:
return json.loads(f.read())
except Exception:
return default_policy
def kube_apply_patch(resource):
if "metadata" in resource.obj:
resource.obj["metadata"]["managedFields"] = None
resp = resource.api.patch(
**resource.api_kwargs(
headers={
"Content-Type": "application/apply-patch+yaml",
},
params={
"fieldManager": "atmosphere-operator",
"force": True,
},
data=json.dumps(resource.obj),
)
)
resource.api.raise_for_status(resp)
resource.set_obj(resp.json())