From: garciadeblas Date: Tue, 12 Nov 2024 10:17:12 +0000 (+0100) Subject: Refactor cluster-deregister workflow to mimic cluster-create and catch exceptions... X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F23%2F14723%2F2;p=osm%2FLCM.git Refactor cluster-deregister workflow to mimic cluster-create and catch exceptions in secret creation Change-Id: I83ab2ebf032df665aa0935dbab36e7234188c984 Signed-off-by: garciadeblas --- diff --git a/osm_lcm/k8s.py b/osm_lcm/k8s.py index ce2a457..845a396 100644 --- a/osm_lcm/k8s.py +++ b/osm_lcm/k8s.py @@ -152,9 +152,6 @@ class ClusterLcm(GitOpsLcm): "create_cluster": { "check_resource_function": self.check_create_cluster, }, - "deregister_cluster": { - "check_resource_function": self.check_deregister_cluster, - }, } self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config) @@ -642,6 +639,14 @@ class ClusterLcm(GitOpsLcm): db_cluster = self.update_operation_history(db_cluster, workflow_status, None) self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) + # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded + clean_status, clean_msg = await self.odu.clean_items_workflow( + "deregister_cluster", op_id, op_params, content + ) + self.logger.info( + f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" + ) + if workflow_status: resource_status, resource_msg = await self.check_resource_status( "deregister_cluster", op_id, op_params, content @@ -667,29 +672,6 @@ class ClusterLcm(GitOpsLcm): self.db.del_one("clusters", {"_id": db_cluster["_id"]}) return - async def check_deregister_cluster(self, op_id, op_params, content): - self.logger.info( - f"check_deregister_cluster Operation {op_id}. Params: {op_params}." - ) - # self.logger.debug(f"Content: {content}") - # Clean secrets - self.logger.info("Cleaning kubeconfig") - cluster_name = content["cluster"]["git_name"].lower() - items = { - "secrets": [ - { - "name": f"kubeconfig-{cluster_name}", - "namespace": "managed-resources", - }, - ] - } - - try: - await self.odu.clean_items(items) - except Exception as e: - return False, f"Error while cleaning items: {e}" - return True, "OK" - async def get_creds(self, op_id, db_cluster): self.logger.info("Cluster get creds Enter") result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) diff --git a/osm_lcm/odu_libs/cluster_mgmt.py b/osm_lcm/odu_libs/cluster_mgmt.py index 0a19de1..6540b71 100644 --- a/osm_lcm/odu_libs/cluster_mgmt.py +++ b/osm_lcm/odu_libs/cluster_mgmt.py @@ -305,12 +305,18 @@ async def register_cluster(self, op_id, op_params, content): secret_value2 = yaml.safe_dump( db_cluster["credentials"], indent=4, default_flow_style=False, sort_keys=False ) - await self.create_secret( - secret_name2, - secret_namespace2, - secret_key2, - secret_value2, - ) + try: + await self.create_secret( + secret_name2, + secret_namespace2, + secret_key2, + secret_value2, + ) + except Exception as e: + self.logger.info( + f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}" + ) + return False, "" # Additional params for the workflow cluster_kustomization_name = cluster_name @@ -460,3 +466,27 @@ async def clean_items_cluster_register(self, op_id, op_params, content): return True, "OK" except Exception as e: return False, f"Error while cleaning items: {e}" + + +async def clean_items_cluster_deregister(self, op_id, op_params, content): + self.logger.info( + f"clean_items_cluster_deregister Enter. Operation {op_id}. Params: {op_params}" + ) + # self.logger.debug(f"Content: {content}") + # Clean secrets + self.logger.info("Cleaning kubeconfig") + cluster_name = content["cluster"]["git_name"].lower() + items = { + "secrets": [ + { + "name": f"kubeconfig-{cluster_name}", + "namespace": "managed-resources", + }, + ] + } + + try: + await self.clean_items(items) + return True, "OK" + except Exception as e: + return False, f"Error while cleaning items: {e}" diff --git a/osm_lcm/odu_workflows.py b/osm_lcm/odu_workflows.py index dac29d2..7e3f163 100644 --- a/osm_lcm/odu_workflows.py +++ b/osm_lcm/odu_workflows.py @@ -61,6 +61,7 @@ class OduWorkflow(LcmBase): }, "deregister_cluster": { "workflow_function": self.deregister_cluster, + "clean_function": self.clean_items_cluster_deregister, }, "create_profile": { "workflow_function": self.create_profile, @@ -139,6 +140,7 @@ class OduWorkflow(LcmBase): clean_items_cluster_create, clean_items_cluster_update, clean_items_cluster_register, + clean_items_cluster_deregister, get_cluster_credentials, ) from osm_lcm.odu_libs.ksu import (