blob: efca1e2ec53a16105fc6adf1dd503a7c76bef2b9 [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
19from pyrage import x25519
20import yaml
21import base64
22
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
31def generate_age_key():
32 ident = x25519.Identity.generate()
33 # gets the public key
34 pubkey = ident.to_public()
35 # gets the private key
36 privkey = str(ident)
37 # return both public and private key
38 return pubkey, privkey
39
40
garciadeblasdde3a312024-09-17 13:25:06 +020041async def create_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +020042 self.logger.info(f"create_cluster Enter. Operation {op_id}. Params: {op_params}")
43 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +020044
45 db_cluster = content["cluster"]
46 db_vim_account = content["vim_account"]
47
garciadeblas96b94f52024-07-08 16:18:21 +020048 workflow_template = "launcher-create-crossplane-cluster-and-bootstrap.j2"
49 workflow_name = f"create-cluster-{db_cluster['_id']}"
garciadeblas96b94f52024-07-08 16:18:21 +020050 cluster_name = db_cluster["git_name"].lower()
51
garciadeblas96b94f52024-07-08 16:18:21 +020052 # Get age key
53 public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster)
garciadeblasdc805482025-02-04 16:08:51 +010054 # self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}")
55 # self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}")
garciadeblas96b94f52024-07-08 16:18:21 +020056
57 # Test kubectl connection
58 self.logger.debug(self._kubectl._get_kubectl_version())
59
garciadeblasdde3a312024-09-17 13:25:06 +020060 # Create temporal secret with agekey
garciadeblas96b94f52024-07-08 16:18:21 +020061 secret_name = f"secret-age-{cluster_name}"
62 secret_namespace = "osm-workflows"
63 secret_key = "agekey"
64 secret_value = private_key_new_cluster
garciadeblasadb81e82024-11-08 01:11:46 +010065 try:
66 await self.create_secret(
67 secret_name,
68 secret_namespace,
69 secret_key,
70 secret_value,
71 )
72 except Exception as e:
73 self.logger.info(f"Cannot create secret {secret_name}: {e}")
garciadeblasdc805482025-02-04 16:08:51 +010074 return False, f"Cannot create secret {secret_name}: {e}"
garciadeblas96b94f52024-07-08 16:18:21 +020075
76 # Additional params for the workflow
77 cluster_kustomization_name = cluster_name
78 osm_project_name = "osm_admin" # TODO: get project name from content
garciadeblasdde3a312024-09-17 13:25:06 +020079 vim_account_id = db_cluster["vim_account"]
80 providerconfig_name = f"{vim_account_id}-config"
81 vim_type = db_vim_account["vim_type"]
garciadeblas753b1e32024-11-06 12:56:33 +010082 if db_cluster.get("bootstrap", True):
83 skip_bootstrap = "false"
84 else:
85 skip_bootstrap = "true"
garciadeblasdde3a312024-09-17 13:25:06 +020086 if vim_type == "azure":
87 cluster_type = "aks"
88 elif vim_type == "aws":
89 cluster_type = "eks"
90 elif vim_type == "gcp":
91 cluster_type = "gke"
garciadeblas96b94f52024-07-08 16:18:21 +020092 else:
garciadeblasdde3a312024-09-17 13:25:06 +020093 raise Exception("Not suitable VIM account to register cluster")
garciadeblas96b94f52024-07-08 16:18:21 +020094
95 # Render workflow
96 # workflow_kwargs = {
97 # "git_fleet_url": f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
98 # "git_sw_catalogs_url": f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
99 # }
100 # manifest = self.render_jinja_template(
101 # workflow_template,
102 # output_file=None,
103 # **workflow_kwargs
104 # )
105 manifest = self.render_jinja_template(
106 workflow_template,
107 output_file=None,
108 workflow_name=workflow_name,
109 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
110 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
111 cluster_name=cluster_name,
112 cluster_type=cluster_type,
113 cluster_kustomization_name=cluster_kustomization_name,
114 providerconfig_name=providerconfig_name,
115 public_key_mgmt=self._pubkey,
116 public_key_new_cluster=public_key_new_cluster,
117 secret_name_private_key_new_cluster=secret_name,
118 vm_size=db_cluster["node_size"],
119 node_count=db_cluster["node_count"],
120 k8s_version=db_cluster["k8s_version"],
121 cluster_location=db_cluster["region_name"],
122 osm_project_name=osm_project_name,
garciadeblasd84808e2024-11-18 17:10:00 +0100123 rg_name=db_cluster.get("resource_group", "''"),
124 preemptible_nodes=db_cluster.get("preemptible_nodes", "false"),
garciadeblas753b1e32024-11-06 12:56:33 +0100125 skip_bootstrap=skip_bootstrap,
garciadeblas96b94f52024-07-08 16:18:21 +0200126 workflow_debug=self._workflow_debug,
127 workflow_dry_run=self._workflow_dry_run,
128 )
129 self.logger.debug(f"Workflow manifest: {manifest}")
130
131 # Submit workflow
132 self._kubectl.create_generic_object(
133 namespace="osm-workflows",
134 manifest_dict=yaml.safe_load(manifest),
135 api_group="argoproj.io",
136 api_plural="workflows",
137 api_version="v1alpha1",
138 )
garciadeblasadb81e82024-11-08 01:11:46 +0100139 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200140
garciadeblas96b94f52024-07-08 16:18:21 +0200141
142async def update_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200143 self.logger.info(f"update_cluster Enter. Operation {op_id}. Params: {op_params}")
144 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200145
146 db_cluster = content["cluster"]
147 db_vim_account = content["vim_account"]
garciadeblas73cd5a22024-11-06 10:45:22 +0100148 cluster_name = db_cluster["git_name"].lower()
garciadeblas96b94f52024-07-08 16:18:21 +0200149
150 workflow_template = "launcher-update-crossplane-cluster.j2"
garciadeblasc8c75d42024-11-13 12:36:13 +0100151 workflow_name = f"update-cluster-{op_id}"
garciadeblas96b94f52024-07-08 16:18:21 +0200152 # cluster_name = db_cluster["name"].lower()
garciadeblas96b94f52024-07-08 16:18:21 +0200153
154 # Get age key
155 public_key_cluster, private_key_cluster = gather_age_key(db_cluster)
156 self.logger.debug(f"public_key_new_cluster={public_key_cluster}")
157 self.logger.debug(f"private_key_new_cluster={private_key_cluster}")
158
159 # Create secret with agekey
160 secret_name = f"secret-age-{cluster_name}"
161 secret_namespace = "osm-workflows"
162 secret_key = "agekey"
163 secret_value = private_key_cluster
garciadeblasadb81e82024-11-08 01:11:46 +0100164 try:
165 await self.create_secret(
166 secret_name,
167 secret_namespace,
168 secret_key,
169 secret_value,
170 )
171 except Exception as e:
172 self.logger.info(f"Cannot create secret {secret_name}: {e}")
garciadeblasdc805482025-02-04 16:08:51 +0100173 return False, f"Cannot create secret {secret_name}: {e}"
garciadeblas96b94f52024-07-08 16:18:21 +0200174
175 # Additional params for the workflow
176 cluster_kustomization_name = cluster_name
177 osm_project_name = "osm_admin" # TODO: get project name from db_cluster
178 vim_account_id = db_cluster["vim_account"]
179 providerconfig_name = f"{vim_account_id}-config"
180 vim_type = db_vim_account["vim_type"]
garciadeblas73cd5a22024-11-06 10:45:22 +0100181 vm_size = op_params.get("node_size", db_cluster["node_size"])
182 node_count = op_params.get("node_count", db_cluster["node_count"])
183 k8s_version = op_params.get("k8s_version", db_cluster["k8s_version"])
garciadeblas96b94f52024-07-08 16:18:21 +0200184 if vim_type == "azure":
185 cluster_type = "aks"
186 elif vim_type == "aws":
187 cluster_type = "eks"
188 elif vim_type == "gcp":
189 cluster_type = "gke"
190 else:
191 raise Exception("Not suitable VIM account to update cluster")
192
193 # Render workflow
194 manifest = self.render_jinja_template(
195 workflow_template,
196 output_file=None,
197 workflow_name=workflow_name,
198 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
199 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
200 cluster_name=cluster_name,
201 cluster_type=cluster_type,
202 cluster_kustomization_name=cluster_kustomization_name,
203 providerconfig_name=providerconfig_name,
204 public_key_mgmt=self._pubkey,
205 public_key_new_cluster=public_key_cluster,
206 secret_name_private_key_new_cluster=secret_name,
garciadeblas73cd5a22024-11-06 10:45:22 +0100207 vm_size=vm_size,
208 node_count=node_count,
209 k8s_version=k8s_version,
garciadeblas96b94f52024-07-08 16:18:21 +0200210 cluster_location=db_cluster["region_name"],
211 osm_project_name=osm_project_name,
garciadeblasd84808e2024-11-18 17:10:00 +0100212 rg_name=db_cluster.get("resource_group", "''"),
213 preemptible_nodes=db_cluster.get("preemptible_nodes", "false"),
garciadeblas96b94f52024-07-08 16:18:21 +0200214 workflow_debug=self._workflow_debug,
215 workflow_dry_run=self._workflow_dry_run,
216 )
217 self.logger.info(manifest)
218
219 # Submit workflow
220 self._kubectl.create_generic_object(
221 namespace="osm-workflows",
222 manifest_dict=yaml.safe_load(manifest),
223 api_group="argoproj.io",
224 api_plural="workflows",
225 api_version="v1alpha1",
226 )
garciadeblasadb81e82024-11-08 01:11:46 +0100227 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200228
229
230async def delete_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200231 self.logger.info(f"delete_cluster Enter. Operation {op_id}. Params: {op_params}")
232 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200233
234 db_cluster = content["cluster"]
235
236 workflow_template = "launcher-delete-cluster.j2"
237 workflow_name = f"delete-cluster-{db_cluster['_id']}"
238 # cluster_name = db_cluster["name"].lower()
239 cluster_name = db_cluster["git_name"].lower()
240
241 # Additional params for the workflow
242 cluster_kustomization_name = cluster_name
243 osm_project_name = "osm_admin" # TODO: get project name from DB
244
245 # Render workflow
246 manifest = self.render_jinja_template(
247 workflow_template,
248 output_file=None,
249 workflow_name=workflow_name,
250 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
251 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
252 cluster_name=cluster_name,
253 cluster_kustomization_name=cluster_kustomization_name,
254 osm_project_name=osm_project_name,
255 workflow_debug=self._workflow_debug,
256 workflow_dry_run=self._workflow_dry_run,
257 )
258 self.logger.info(manifest)
259
260 # Submit workflow
261 self._kubectl.create_generic_object(
262 namespace="osm-workflows",
263 manifest_dict=yaml.safe_load(manifest),
264 api_group="argoproj.io",
265 api_plural="workflows",
266 api_version="v1alpha1",
267 )
garciadeblasadb81e82024-11-08 01:11:46 +0100268 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200269
270
271async def register_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200272 self.logger.info(f"register_cluster Enter. Operation {op_id}. Params: {op_params}")
273 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200274
275 db_cluster = content["cluster"]
garciadeblas96b94f52024-07-08 16:18:21 +0200276 cluster_name = db_cluster["git_name"].lower()
277
garciadeblasdde3a312024-09-17 13:25:06 +0200278 workflow_template = "launcher-bootstrap-cluster.j2"
279 workflow_name = f"register-cluster-{db_cluster['_id']}"
280
281 # Get age key
282 public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster)
283 self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}")
284 self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}")
285
286 # Create temporal secret with agekey
287 secret_name = f"secret-age-{cluster_name}"
288 secret_namespace = "osm-workflows"
289 secret_key = "agekey"
290 secret_value = private_key_new_cluster
garciadeblasadb81e82024-11-08 01:11:46 +0100291 try:
292 await self.create_secret(
293 secret_name,
294 secret_namespace,
295 secret_key,
296 secret_value,
297 )
298 except Exception as e:
299 self.logger.info(
300 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}"
301 )
garciadeblasdc805482025-02-04 16:08:51 +0100302 return (
303 False,
304 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}",
305 )
garciadeblas96b94f52024-07-08 16:18:21 +0200306
garciadeblasdde3a312024-09-17 13:25:06 +0200307 # Create secret with kubeconfig
308 secret_name2 = f"kubeconfig-{cluster_name}"
309 secret_namespace2 = "managed-resources"
310 secret_key2 = "kubeconfig"
311 secret_value2 = yaml.safe_dump(
garciadeblasa82300f2024-11-18 10:24:26 +0100312 db_cluster["credentials"], indent=2, default_flow_style=False, sort_keys=False
garciadeblasdde3a312024-09-17 13:25:06 +0200313 )
garciadeblas91bb2c42024-11-12 11:17:12 +0100314 try:
315 await self.create_secret(
316 secret_name2,
317 secret_namespace2,
318 secret_key2,
319 secret_value2,
320 )
321 except Exception as e:
322 self.logger.info(
323 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}"
324 )
garciadeblasdc805482025-02-04 16:08:51 +0100325 return (
326 False,
327 f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}",
328 )
garciadeblasdde3a312024-09-17 13:25:06 +0200329
330 # Additional params for the workflow
331 cluster_kustomization_name = cluster_name
332 osm_project_name = "osm_admin" # TODO: get project name from content
garciadeblasdde3a312024-09-17 13:25:06 +0200333
334 manifest = self.render_jinja_template(
335 workflow_template,
336 output_file=None,
337 workflow_name=workflow_name,
338 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
339 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
340 cluster_name=cluster_name,
341 cluster_kustomization_name=cluster_kustomization_name,
garciadeblasdde3a312024-09-17 13:25:06 +0200342 public_key_mgmt=self._pubkey,
343 public_key_new_cluster=public_key_new_cluster,
344 secret_name_private_key_new_cluster=secret_name,
345 osm_project_name=osm_project_name,
346 workflow_debug=self._workflow_debug,
347 workflow_dry_run=self._workflow_dry_run,
348 )
349 self.logger.debug(f"Workflow manifest: {manifest}")
350
351 # Submit workflow
352 self._kubectl.create_generic_object(
353 namespace="osm-workflows",
354 manifest_dict=yaml.safe_load(manifest),
355 api_group="argoproj.io",
356 api_plural="workflows",
357 api_version="v1alpha1",
358 )
garciadeblasadb81e82024-11-08 01:11:46 +0100359 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200360
361
362async def deregister_cluster(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200363 self.logger.info(
364 f"deregister_cluster Enter. Operation {op_id}. Params: {op_params}"
365 )
366 # self.logger.debug(f"Content: {content}")
garciadeblasdde3a312024-09-17 13:25:06 +0200367
368 db_cluster = content["cluster"]
369 cluster_name = db_cluster["git_name"].lower()
370
371 workflow_template = "launcher-disconnect-flux-remote-cluster.j2"
372 workflow_name = f"deregister-cluster-{db_cluster['_id']}"
373
374 # Additional params for the workflow
375 cluster_kustomization_name = cluster_name
376 osm_project_name = "osm_admin" # TODO: get project name from DB
377
378 # Render workflow
379 manifest = self.render_jinja_template(
380 workflow_template,
381 output_file=None,
382 workflow_name=workflow_name,
383 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
384 cluster_kustomization_name=cluster_kustomization_name,
385 osm_project_name=osm_project_name,
386 workflow_debug=self._workflow_debug,
387 workflow_dry_run=self._workflow_dry_run,
388 )
389 self.logger.info(manifest)
390
391 # Submit workflow
392 self._kubectl.create_generic_object(
393 namespace="osm-workflows",
394 manifest_dict=yaml.safe_load(manifest),
395 api_group="argoproj.io",
396 api_plural="workflows",
397 api_version="v1alpha1",
398 )
garciadeblasadb81e82024-11-08 01:11:46 +0100399 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200400
401
402async def get_cluster_credentials(self, db_cluster):
403 """
404 returns the kubeconfig file of a K8s cluster in a dictionary
405 """
garciadeblas9e532812024-10-22 14:04:36 +0200406 self.logger.info("get_cluster_credentials Enter")
407 # self.logger.debug(f"Content: {db_cluster}")
garciadeblas96b94f52024-07-08 16:18:21 +0200408
409 secret_name = f"kubeconfig-{db_cluster['git_name'].lower()}"
410 secret_namespace = "managed-resources"
411 secret_key = "kubeconfig"
412
413 self.logger.info(f"Checking content of secret {secret_name} ...")
414 try:
415 returned_secret_data = await self._kubectl.get_secret_content(
416 name=secret_name,
417 namespace=secret_namespace,
418 )
419 returned_secret_value = base64.b64decode(
420 returned_secret_data[secret_key]
421 ).decode("utf-8")
422 return True, yaml.safe_load(returned_secret_value)
423 except Exception as e:
424 message = f"Not possible to get the credentials of the cluster. Exception: {e}"
425 self.logger.critical(message)
426 return False, message
427
428
garciadeblas28bff0f2024-09-16 12:53:07 +0200429async def clean_items_cluster_create(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200430 self.logger.info(
431 f"clean_items_cluster_create Enter. Operation {op_id}. Params: {op_params}"
432 )
433 self.logger.debug(f"Content: {content}")
garciadeblas28bff0f2024-09-16 12:53:07 +0200434 items = {
435 "secrets": [
436 {
437 "name": f"secret-age-{content['cluster']['git_name'].lower()}",
438 "namespace": "osm-workflows",
439 }
440 ]
441 }
442 try:
443 await self.clean_items(items)
444 return True, "OK"
445 except Exception as e:
446 return False, f"Error while cleaning items: {e}"
447
448
449async def clean_items_cluster_update(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200450 self.logger.info(
451 f"clean_items_cluster_update Enter. Operation {op_id}. Params: {op_params}"
452 )
453 # self.logger.debug(f"Content: {content}")
garciadeblas28bff0f2024-09-16 12:53:07 +0200454 return await self.clean_items_cluster_create(op_id, op_params, content)
455
456
garciadeblasdde3a312024-09-17 13:25:06 +0200457async def clean_items_cluster_register(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200458 self.logger.info(
459 f"clean_items_cluster_register Enter. Operation {op_id}. Params: {op_params}"
460 )
461 # self.logger.debug(f"Content: {content}")
garciadeblasdde3a312024-09-17 13:25:06 +0200462 # Clean secrets
463 cluster_name = content["cluster"]["git_name"].lower()
464 items = {
465 "secrets": [
466 {
467 "name": f"secret-age-{cluster_name}",
468 "namespace": "osm-workflows",
469 },
470 ]
471 }
472
473 try:
474 await self.clean_items(items)
garciadeblas28d6e692024-10-15 13:14:39 +0200475 return True, "OK"
garciadeblasdde3a312024-09-17 13:25:06 +0200476 except Exception as e:
477 return False, f"Error while cleaning items: {e}"
garciadeblas91bb2c42024-11-12 11:17:12 +0100478
479
480async def clean_items_cluster_deregister(self, op_id, op_params, content):
481 self.logger.info(
482 f"clean_items_cluster_deregister Enter. Operation {op_id}. Params: {op_params}"
483 )
484 # self.logger.debug(f"Content: {content}")
485 # Clean secrets
486 self.logger.info("Cleaning kubeconfig")
487 cluster_name = content["cluster"]["git_name"].lower()
488 items = {
489 "secrets": [
490 {
491 "name": f"kubeconfig-{cluster_name}",
492 "namespace": "managed-resources",
493 },
494 ]
495 }
496
497 try:
498 await self.clean_items(items)
499 return True, "OK"
500 except Exception as e:
501 return False, f"Error while cleaning items: {e}"