Debian Codegraph @

magnum_capi_helm/driver_utils.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 # Collection of static functions that are shared within the driver.
14
15 import re
16
17 from magnum_capi_helm import conf
18
19 CONF = conf.CONF
20
21
22 def cluster_namespace(cluster):
23 # We create clusters in a project-specific namespace
24 # To generate the namespace, first sanitize the project id
25 project_id = re.sub("[^a-z0-9]", "", cluster.project_id.lower())
26 prefix = CONF.capi_helm.namespace_prefix
27 return f"{prefix}-{project_id}"
28
29
30 def sanitized_name(name, suffix=None):
31 if not name:
32 return None
33 return re.sub(
34 "[^a-z0-9]+",
35 "-",
36 (f"{name}-{suffix}" if suffix else name).lower(),
37 ).strip("-")
38
39
40 def chart_release_name(cluster):
41 return cluster.stack_id
42
43
44 def get_k8s_resource_name(cluster, name):
45 return sanitized_name(chart_release_name(cluster), name)