blob: 7f35c9d7a07ad0d6033e4542fae3adea7b6405b0 [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18
garciadeblas96b94f52024-07-08 16:18:21 +020019import yaml
20import base64
rshrif8911b92025-06-11 18:19:07 +000021import json
garciadeblas96b94f52024-07-08 16:18:21 +020022
23
24def gather_age_key(cluster):
25 pubkey = cluster.get("age_pubkey")
26 privkey = cluster.get("age_privkey")
27 # return both public and private key
28 return pubkey, privkey
29
30
garciadeblasdde3a312024-09-17 13:25:06 +020031async def create_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +020032 self.logger.info(f"create_cluster Enter. Operation {op_id}. Params: {op_params}")
33 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +020034
35 db_cluster = content["cluster"]
36 db_vim_account = content["vim_account"]
37
garciadeblas96b94f52024-07-08 16:18:21 +020038 workflow_template = "launcher-create-crossplane-cluster-and-bootstrap.j2"
39 workflow_name = f"create-cluster-{db_cluster['_id']}"
garciadeblas96b94f52024-07-08 16:18:21 +020040 cluster_name = db_cluster["git_name"].lower()
41
garciadeblas96b94f52024-07-08 16:18:21 +020042 # Get age key
43 public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster)
garciadeblas41859ce2025-02-04 16:08:51 +010044 # self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}")
45 # self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}")
garciadeblas96b94f52024-07-08 16:18:21 +020046
47 # Test kubectl connection
garciadeblas6d8acf32025-02-06 13:34:37 +010048 self.logger.debug(f"Testing kubectl: {self._kubectl}")
49 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
50 self.logger.debug(
51 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
52 )
garciadeblas96b94f52024-07-08 16:18:21 +020053 self.logger.debug(self._kubectl._get_kubectl_version())
54
garciadeblasdde3a312024-09-17 13:25:06 +020055 # Create temporal secret with agekey
garciadeblas96b94f52024-07-08 16:18:21 +020056 secret_name = f"secret-age-{cluster_name}"
57 secret_namespace = "osm-workflows"
58 secret_key = "agekey"
59 secret_value = private_key_new_cluster
garciadeblasadb81e82024-11-08 01:11:46 +010060 try:
garciadeblas6d8acf32025-02-06 13:34:37 +010061 self.logger.debug(f"Testing kubectl: {self._kubectl}")
62 self.logger.debug(
63 f"Testing kubectl configuration: {self._kubectl.configuration}"
64 )
65 self.logger.debug(
66 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
67 )
garciadeblasadb81e82024-11-08 01:11:46 +010068 await self.create_secret(
69 secret_name,
70 secret_namespace,
71 secret_key,
72 secret_value,
73 )
74 except Exception as e:
75 self.logger.info(f"Cannot create secret {secret_name}: {e}")
garciadeblas41859ce2025-02-04 16:08:51 +010076 return False, f"Cannot create secret {secret_name}: {e}"
garciadeblas96b94f52024-07-08 16:18:21 +020077
78 # Additional params for the workflow
79 cluster_kustomization_name = cluster_name
80 osm_project_name = "osm_admin" # TODO: get project name from content
garciadeblasdde3a312024-09-17 13:25:06 +020081 vim_account_id = db_cluster["vim_account"]
82 providerconfig_name = f"{vim_account_id}-config"
83 vim_type = db_vim_account["vim_type"]
garciadeblas753b1e32024-11-06 12:56:33 +010084 if db_cluster.get("bootstrap", True):
85 skip_bootstrap = "false"
86 else:
87 skip_bootstrap = "true"
garciadeblasdde3a312024-09-17 13:25:06 +020088 if vim_type == "azure":
89 cluster_type = "aks"
90 elif vim_type == "aws":
91 cluster_type = "eks"
92 elif vim_type == "gcp":
93 cluster_type = "gke"
garciadeblas96b94f52024-07-08 16:18:21 +020094 else:
garciadeblasdde3a312024-09-17 13:25:06 +020095 raise Exception("Not suitable VIM account to register cluster")
garciadeblas96b94f52024-07-08 16:18:21 +020096
rshrif8911b92025-06-11 18:19:07 +000097 # Create configmap for subnet
98 configmap_name = None
99 data = {}
100 private_subnets = op_params.get("private_subnet")
101 public_subnets = op_params.get("public_subnet")
102 if private_subnets or public_subnets:
103 configmap_name = f"{cluster_name}-parameters"
104 configmap_namespace = "managed-resources"
105 data["private_subnets"] = f"{json.dumps(private_subnets)}"
106 data["public_subnets"] = f"{json.dumps(public_subnets)}"
107 try:
108 self.logger.debug(f"Testing kubectl: {self._kubectl}")
109 self.logger.debug(
110 f"Testing kubectl configuration: {self._kubectl.configuration}"
111 )
112 self.logger.debug(
113 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
114 )
115 await self.create_configmap(
116 configmap_name,
117 configmap_namespace,
118 data,
119 )
120 except Exception as e:
121 self.logger.info(f"Cannot create configmap {configmap_name}: {e}")
122 return False, f"Cannot create configmap {configmap_name}: {e}"
123
garciadeblas96b94f52024-07-08 16:18:21 +0200124 # Render workflow
125 # workflow_kwargs = {
garciadeblas56c3aa82025-05-26 15:29:46 +0200126 # "git_fleet_url": self._repo_fleet_url,
127 # "git_sw_catalogs_url": self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200128 # }
129 # manifest = self.render_jinja_template(
130 # workflow_template,
131 # output_file=None,
132 # **workflow_kwargs
133 # )
134 manifest = self.render_jinja_template(
135 workflow_template,
136 output_file=None,
137 workflow_name=workflow_name,
garciadeblas56c3aa82025-05-26 15:29:46 +0200138 git_fleet_url=self._repo_fleet_url,
139 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200140 cluster_name=cluster_name,
141 cluster_type=cluster_type,
142 cluster_kustomization_name=cluster_kustomization_name,
143 providerconfig_name=providerconfig_name,
144 public_key_mgmt=self._pubkey,
145 public_key_new_cluster=public_key_new_cluster,
146 secret_name_private_key_new_cluster=secret_name,
rshrif8911b92025-06-11 18:19:07 +0000147 vm_size=db_cluster.get("node_size", "default"),
148 node_count=db_cluster.get("node_count", "default"),
garciadeblas96b94f52024-07-08 16:18:21 +0200149 k8s_version=db_cluster["k8s_version"],
150 cluster_location=db_cluster["region_name"],
rshrif8911b92025-06-11 18:19:07 +0000151 configmap_name=configmap_name if configmap_name else "default",
152 cluster_iam_role=db_cluster.get("iam_role", "default"),
153 cluster_private_subnets_id=db_cluster.get("private_subnet", "default"),
154 cluster_public_subnets_id=db_cluster.get("public_subnet", "default"),
garciadeblas96b94f52024-07-08 16:18:21 +0200155 osm_project_name=osm_project_name,
garciadeblasd84808e2024-11-18 17:10:00 +0100156 rg_name=db_cluster.get("resource_group", "''"),
157 preemptible_nodes=db_cluster.get("preemptible_nodes", "false"),
garciadeblas753b1e32024-11-06 12:56:33 +0100158 skip_bootstrap=skip_bootstrap,
garciadeblas96b94f52024-07-08 16:18:21 +0200159 workflow_debug=self._workflow_debug,
160 workflow_dry_run=self._workflow_dry_run,
161 )
162 self.logger.debug(f"Workflow manifest: {manifest}")
163
164 # Submit workflow
garciadeblas6d8acf32025-02-06 13:34:37 +0100165 self.logger.debug(f"Testing kubectl: {self._kubectl}")
166 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
167 self.logger.debug(
168 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
169 )
garciadeblas96b94f52024-07-08 16:18:21 +0200170 self._kubectl.create_generic_object(
171 namespace="osm-workflows",
172 manifest_dict=yaml.safe_load(manifest),
173 api_group="argoproj.io",
174 api_plural="workflows",
175 api_version="v1alpha1",
176 )
garciadeblasadb81e82024-11-08 01:11:46 +0100177 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200178
garciadeblas96b94f52024-07-08 16:18:21 +0200179
180async def update_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200181 self.logger.info(f"update_cluster Enter. Operation {op_id}. Params: {op_params}")
182 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200183
184 db_cluster = content["cluster"]
185 db_vim_account = content["vim_account"]
garciadeblas73cd5a22024-11-06 10:45:22 +0100186 cluster_name = db_cluster["git_name"].lower()
garciadeblas96b94f52024-07-08 16:18:21 +0200187
188 workflow_template = "launcher-update-crossplane-cluster.j2"
garciadeblasc8c75d42024-11-13 12:36:13 +0100189 workflow_name = f"update-cluster-{op_id}"
garciadeblas96b94f52024-07-08 16:18:21 +0200190 # cluster_name = db_cluster["name"].lower()
garciadeblas96b94f52024-07-08 16:18:21 +0200191
192 # Get age key
193 public_key_cluster, private_key_cluster = gather_age_key(db_cluster)
194 self.logger.debug(f"public_key_new_cluster={public_key_cluster}")
195 self.logger.debug(f"private_key_new_cluster={private_key_cluster}")
196
197 # Create secret with agekey
198 secret_name = f"secret-age-{cluster_name}"
199 secret_namespace = "osm-workflows"
200 secret_key = "agekey"
201 secret_value = private_key_cluster
garciadeblasadb81e82024-11-08 01:11:46 +0100202 try:
garciadeblas6d8acf32025-02-06 13:34:37 +0100203 self.logger.debug(f"Testing kubectl: {self._kubectl}")
204 self.logger.debug(
205 f"Testing kubectl configuration: {self._kubectl.configuration}"
206 )
207 self.logger.debug(
208 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
209 )
garciadeblasadb81e82024-11-08 01:11:46 +0100210 await self.create_secret(
211 secret_name,
212 secret_namespace,
213 secret_key,
214 secret_value,
215 )
216 except Exception as e:
217 self.logger.info(f"Cannot create secret {secret_name}: {e}")
garciadeblas41859ce2025-02-04 16:08:51 +0100218 return False, f"Cannot create secret {secret_name}: {e}"
garciadeblas96b94f52024-07-08 16:18:21 +0200219
220 # Additional params for the workflow
221 cluster_kustomization_name = cluster_name
222 osm_project_name = "osm_admin" # TODO: get project name from db_cluster
223 vim_account_id = db_cluster["vim_account"]
224 providerconfig_name = f"{vim_account_id}-config"
225 vim_type = db_vim_account["vim_type"]
garciadeblas73cd5a22024-11-06 10:45:22 +0100226 vm_size = op_params.get("node_size", db_cluster["node_size"])
227 node_count = op_params.get("node_count", db_cluster["node_count"])
228 k8s_version = op_params.get("k8s_version", db_cluster["k8s_version"])
garciadeblas96b94f52024-07-08 16:18:21 +0200229 if vim_type == "azure":
230 cluster_type = "aks"
231 elif vim_type == "aws":
232 cluster_type = "eks"
233 elif vim_type == "gcp":
234 cluster_type = "gke"
235 else:
236 raise Exception("Not suitable VIM account to update cluster")
237
238 # Render workflow
239 manifest = self.render_jinja_template(
240 workflow_template,
241 output_file=None,
242 workflow_name=workflow_name,
garciadeblas56c3aa82025-05-26 15:29:46 +0200243 git_fleet_url=self._repo_fleet_url,
244 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200245 cluster_name=cluster_name,
246 cluster_type=cluster_type,
247 cluster_kustomization_name=cluster_kustomization_name,
248 providerconfig_name=providerconfig_name,
249 public_key_mgmt=self._pubkey,
250 public_key_new_cluster=public_key_cluster,
251 secret_name_private_key_new_cluster=secret_name,
garciadeblas73cd5a22024-11-06 10:45:22 +0100252 vm_size=vm_size,
253 node_count=node_count,
254 k8s_version=k8s_version,
garciadeblas96b94f52024-07-08 16:18:21 +0200255 cluster_location=db_cluster["region_name"],
256 osm_project_name=osm_project_name,
garciadeblasd84808e2024-11-18 17:10:00 +0100257 rg_name=db_cluster.get("resource_group", "''"),
258 preemptible_nodes=db_cluster.get("preemptible_nodes", "false"),
garciadeblas96b94f52024-07-08 16:18:21 +0200259 workflow_debug=self._workflow_debug,
260 workflow_dry_run=self._workflow_dry_run,
261 )
262 self.logger.info(manifest)
263
264 # Submit workflow
garciadeblas6d8acf32025-02-06 13:34:37 +0100265 self.logger.debug(f"Testing kubectl: {self._kubectl}")
266 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
267 self.logger.debug(
268 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
269 )
garciadeblas96b94f52024-07-08 16:18:21 +0200270 self._kubectl.create_generic_object(
271 namespace="osm-workflows",
272 manifest_dict=yaml.safe_load(manifest),
273 api_group="argoproj.io",
274 api_plural="workflows",
275 api_version="v1alpha1",
276 )
garciadeblasadb81e82024-11-08 01:11:46 +0100277 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200278
279
280async def delete_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200281 self.logger.info(f"delete_cluster Enter. Operation {op_id}. Params: {op_params}")
282 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200283
284 db_cluster = content["cluster"]
285
286 workflow_template = "launcher-delete-cluster.j2"
287 workflow_name = f"delete-cluster-{db_cluster['_id']}"
288 # cluster_name = db_cluster["name"].lower()
289 cluster_name = db_cluster["git_name"].lower()
290
291 # Additional params for the workflow
292 cluster_kustomization_name = cluster_name
293 osm_project_name = "osm_admin" # TODO: get project name from DB
294
295 # Render workflow
296 manifest = self.render_jinja_template(
297 workflow_template,
298 output_file=None,
299 workflow_name=workflow_name,
garciadeblas56c3aa82025-05-26 15:29:46 +0200300 git_fleet_url=self._repo_fleet_url,
301 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200302 cluster_name=cluster_name,
303 cluster_kustomization_name=cluster_kustomization_name,
304 osm_project_name=osm_project_name,
305 workflow_debug=self._workflow_debug,
306 workflow_dry_run=self._workflow_dry_run,
307 )
308 self.logger.info(manifest)
309
310 # Submit workflow
garciadeblas6d8acf32025-02-06 13:34:37 +0100311 self.logger.debug(f"Testing kubectl: {self._kubectl}")
312 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
313 self.logger.debug(
314 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
315 )
garciadeblas96b94f52024-07-08 16:18:21 +0200316 self._kubectl.create_generic_object(
317 namespace="osm-workflows",
318 manifest_dict=yaml.safe_load(manifest),
319 api_group="argoproj.io",
320 api_plural="workflows",
321 api_version="v1alpha1",
322 )
garciadeblasadb81e82024-11-08 01:11:46 +0100323 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200324
325
326async def register_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200327 self.logger.info(f"register_cluster Enter. Operation {op_id}. Params: {op_params}")
328 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200329
330 db_cluster = content["cluster"]
garciadeblas96b94f52024-07-08 16:18:21 +0200331 cluster_name = db_cluster["git_name"].lower()
332
garciadeblasdde3a312024-09-17 13:25:06 +0200333 workflow_template = "launcher-bootstrap-cluster.j2"
334 workflow_name = f"register-cluster-{db_cluster['_id']}"
335
336 # Get age key
337 public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster)
338 self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}")
339 self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}")
340
341 # Create temporal secret with agekey
342 secret_name = f"secret-age-{cluster_name}"
343 secret_namespace = "osm-workflows"
344 secret_key = "agekey"
345 secret_value = private_key_new_cluster
garciadeblasadb81e82024-11-08 01:11:46 +0100346 try:
garciadeblas6d8acf32025-02-06 13:34:37 +0100347 self.logger.debug(f"Testing kubectl: {self._kubectl}")
348 self.logger.debug(
349 f"Testing kubectl configuration: {self._kubectl.configuration}"
350 )
351 self.logger.debug(
352 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
353 )
garciadeblasadb81e82024-11-08 01:11:46 +0100354 await self.create_secret(
355 secret_name,
356 secret_namespace,
357 secret_key,
358 secret_value,
359 )
360 except Exception as e:
361 self.logger.info(
362 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}"
363 )
garciadeblas41859ce2025-02-04 16:08:51 +0100364 return (
365 False,
366 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}",
367 )
garciadeblas96b94f52024-07-08 16:18:21 +0200368
garciadeblasdde3a312024-09-17 13:25:06 +0200369 # Create secret with kubeconfig
370 secret_name2 = f"kubeconfig-{cluster_name}"
371 secret_namespace2 = "managed-resources"
372 secret_key2 = "kubeconfig"
373 secret_value2 = yaml.safe_dump(
garciadeblasa82300f2024-11-18 10:24:26 +0100374 db_cluster["credentials"], indent=2, default_flow_style=False, sort_keys=False
garciadeblasdde3a312024-09-17 13:25:06 +0200375 )
garciadeblas91bb2c42024-11-12 11:17:12 +0100376 try:
garciadeblas6d8acf32025-02-06 13:34:37 +0100377 self.logger.debug(f"Testing kubectl: {self._kubectl}")
378 self.logger.debug(
379 f"Testing kubectl configuration: {self._kubectl.configuration}"
380 )
381 self.logger.debug(
382 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
383 )
garciadeblas91bb2c42024-11-12 11:17:12 +0100384 await self.create_secret(
385 secret_name2,
386 secret_namespace2,
387 secret_key2,
388 secret_value2,
389 )
390 except Exception as e:
391 self.logger.info(
392 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}"
393 )
garciadeblas41859ce2025-02-04 16:08:51 +0100394 return (
395 False,
396 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}",
397 )
garciadeblasdde3a312024-09-17 13:25:06 +0200398
399 # Additional params for the workflow
400 cluster_kustomization_name = cluster_name
401 osm_project_name = "osm_admin" # TODO: get project name from content
garciadeblas65047492025-09-17 23:33:13 +0200402 if db_cluster.get("openshift", True):
403 templates_dir = "/sw-catalogs/sw-catalogs-osm/cloud-resources/flux-remote-bootstrap/cluster-base-openshift/templates"
404 self.logger.info(
405 "Rendering OpenShift bootstrap templates from %s", templates_dir
406 )
407 else:
408 templates_dir = "/sw-catalogs/sw-catalogs-osm/cloud-resources/flux-remote-bootstrap/cluster-base/templates"
409 self.logger.info(
410 "Rendering Standard bootstrap templates from %s", templates_dir
411 )
garciadeblasdde3a312024-09-17 13:25:06 +0200412
413 manifest = self.render_jinja_template(
414 workflow_template,
415 output_file=None,
416 workflow_name=workflow_name,
garciadeblas56c3aa82025-05-26 15:29:46 +0200417 git_fleet_url=self._repo_fleet_url,
418 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblasdde3a312024-09-17 13:25:06 +0200419 cluster_name=cluster_name,
420 cluster_kustomization_name=cluster_kustomization_name,
garciadeblasdde3a312024-09-17 13:25:06 +0200421 public_key_mgmt=self._pubkey,
422 public_key_new_cluster=public_key_new_cluster,
423 secret_name_private_key_new_cluster=secret_name,
424 osm_project_name=osm_project_name,
garciadeblas65047492025-09-17 23:33:13 +0200425 templates_dir=templates_dir,
garciadeblasdde3a312024-09-17 13:25:06 +0200426 workflow_debug=self._workflow_debug,
427 workflow_dry_run=self._workflow_dry_run,
428 )
429 self.logger.debug(f"Workflow manifest: {manifest}")
430
431 # Submit workflow
garciadeblas6d8acf32025-02-06 13:34:37 +0100432 self.logger.debug(f"Testing kubectl: {self._kubectl}")
433 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
434 self.logger.debug(
435 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
436 )
garciadeblasdde3a312024-09-17 13:25:06 +0200437 self._kubectl.create_generic_object(
438 namespace="osm-workflows",
439 manifest_dict=yaml.safe_load(manifest),
440 api_group="argoproj.io",
441 api_plural="workflows",
442 api_version="v1alpha1",
443 )
garciadeblasadb81e82024-11-08 01:11:46 +0100444 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200445
446
447async def deregister_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200448 self.logger.info(
449 f"deregister_cluster Enter. Operation {op_id}. Params: {op_params}"
450 )
451 # self.logger.debug(f"Content: {content}")
garciadeblasdde3a312024-09-17 13:25:06 +0200452
453 db_cluster = content["cluster"]
454 cluster_name = db_cluster["git_name"].lower()
455
456 workflow_template = "launcher-disconnect-flux-remote-cluster.j2"
457 workflow_name = f"deregister-cluster-{db_cluster['_id']}"
458
459 # Additional params for the workflow
460 cluster_kustomization_name = cluster_name
461 osm_project_name = "osm_admin" # TODO: get project name from DB
462
463 # Render workflow
464 manifest = self.render_jinja_template(
465 workflow_template,
466 output_file=None,
467 workflow_name=workflow_name,
garciadeblas56c3aa82025-05-26 15:29:46 +0200468 git_fleet_url=self._repo_fleet_url,
garciadeblasdde3a312024-09-17 13:25:06 +0200469 cluster_kustomization_name=cluster_kustomization_name,
470 osm_project_name=osm_project_name,
471 workflow_debug=self._workflow_debug,
472 workflow_dry_run=self._workflow_dry_run,
473 )
474 self.logger.info(manifest)
475
476 # Submit workflow
garciadeblas6d8acf32025-02-06 13:34:37 +0100477 self.logger.debug(f"Testing kubectl: {self._kubectl}")
478 self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}")
479 self.logger.debug(
480 f"Testing kubectl configuration Host: {self._kubectl.configuration.host}"
481 )
garciadeblasdde3a312024-09-17 13:25:06 +0200482 self._kubectl.create_generic_object(
483 namespace="osm-workflows",
484 manifest_dict=yaml.safe_load(manifest),
485 api_group="argoproj.io",
486 api_plural="workflows",
487 api_version="v1alpha1",
488 )
garciadeblasadb81e82024-11-08 01:11:46 +0100489 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200490
491
492async def get_cluster_credentials(self, db_cluster):
493 """
494 returns the kubeconfig file of a K8s cluster in a dictionary
495 """
garciadeblas9e532812024-10-22 14:04:36 +0200496 self.logger.info("get_cluster_credentials Enter")
497 # self.logger.debug(f"Content: {db_cluster}")
garciadeblas96b94f52024-07-08 16:18:21 +0200498
499 secret_name = f"kubeconfig-{db_cluster['git_name'].lower()}"
500 secret_namespace = "managed-resources"
501 secret_key = "kubeconfig"
502
503 self.logger.info(f"Checking content of secret {secret_name} ...")
504 try:
505 returned_secret_data = await self._kubectl.get_secret_content(
506 name=secret_name,
507 namespace=secret_namespace,
508 )
509 returned_secret_value = base64.b64decode(
510 returned_secret_data[secret_key]
511 ).decode("utf-8")
512 return True, yaml.safe_load(returned_secret_value)
513 except Exception as e:
514 message = f"Not possible to get the credentials of the cluster. Exception: {e}"
515 self.logger.critical(message)
516 return False, message
517
518
garciadeblas28bff0f2024-09-16 12:53:07 +0200519async def clean_items_cluster_create(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200520 self.logger.info(
521 f"clean_items_cluster_create Enter. Operation {op_id}. Params: {op_params}"
522 )
523 self.logger.debug(f"Content: {content}")
garciadeblas28bff0f2024-09-16 12:53:07 +0200524 items = {
525 "secrets": [
526 {
527 "name": f"secret-age-{content['cluster']['git_name'].lower()}",
528 "namespace": "osm-workflows",
529 }
rshrif8911b92025-06-11 18:19:07 +0000530 ],
yshah4c0d1bc2025-09-23 09:53:26 +0000531 # "configmaps": [
532 # {
533 # "name": f"{content['cluster']['name']}-parameters",
534 # "namespace": "managed-resources",
535 # }
536 # ],
garciadeblas28bff0f2024-09-16 12:53:07 +0200537 }
538 try:
539 await self.clean_items(items)
540 return True, "OK"
541 except Exception as e:
542 return False, f"Error while cleaning items: {e}"
543
544
545async def clean_items_cluster_update(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200546 self.logger.info(
547 f"clean_items_cluster_update Enter. Operation {op_id}. Params: {op_params}"
548 )
549 # self.logger.debug(f"Content: {content}")
garciadeblas28bff0f2024-09-16 12:53:07 +0200550 return await self.clean_items_cluster_create(op_id, op_params, content)
551
552
garciadeblasdde3a312024-09-17 13:25:06 +0200553async def clean_items_cluster_register(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200554 self.logger.info(
555 f"clean_items_cluster_register Enter. Operation {op_id}. Params: {op_params}"
556 )
557 # self.logger.debug(f"Content: {content}")
garciadeblasdde3a312024-09-17 13:25:06 +0200558 # Clean secrets
559 cluster_name = content["cluster"]["git_name"].lower()
560 items = {
561 "secrets": [
562 {
563 "name": f"secret-age-{cluster_name}",
564 "namespace": "osm-workflows",
565 },
566 ]
567 }
568
569 try:
570 await self.clean_items(items)
garciadeblas28d6e692024-10-15 13:14:39 +0200571 return True, "OK"
garciadeblasdde3a312024-09-17 13:25:06 +0200572 except Exception as e:
573 return False, f"Error while cleaning items: {e}"
garciadeblas91bb2c42024-11-12 11:17:12 +0100574
575
576async def clean_items_cluster_deregister(self, op_id, op_params, content):
577 self.logger.info(
578 f"clean_items_cluster_deregister Enter. Operation {op_id}. Params: {op_params}"
579 )
580 # self.logger.debug(f"Content: {content}")
581 # Clean secrets
582 self.logger.info("Cleaning kubeconfig")
583 cluster_name = content["cluster"]["git_name"].lower()
584 items = {
585 "secrets": [
586 {
587 "name": f"kubeconfig-{cluster_name}",
588 "namespace": "managed-resources",
589 },
590 ]
591 }
592
593 try:
594 await self.clean_items(items)
595 return True, "OK"
596 except Exception as e:
597 return False, f"Error while cleaning items: {e}"