From: garciadeblas Date: Fri, 8 Nov 2024 09:41:38 +0000 (+0100) Subject: Update progress in DB for resources checked in cluster creation X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F16%2F14716%2F4;p=osm%2FLCM.git Update progress in DB for resources checked in cluster creation Change-Id: Ia9731b7b65013164137a9a8eca58341491f2c3f3 Signed-off-by: garciadeblas --- diff --git a/osm_lcm/k8s.py b/osm_lcm/k8s.py index 445c3a2..888f7b3 100644 --- a/osm_lcm/k8s.py +++ b/osm_lcm/k8s.py @@ -41,7 +41,47 @@ class GitOpsLcm(LcmBase): self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") return True, "OK" - async def common_check_list(self, checkings_list): + def update_operation_history( + self, content, workflow_status=None, resource_status=None + ): + self.logger.info( + f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}" + ) + self.logger.debug(f"Content: {content}") + + op_id = content["current_operation"] + self.logger.debug("OP_id: {}".format(op_id)) + op_num = 0 + for operation in content["operationHistory"]: + self.logger.debug("Operations: {}".format(operation)) + if operation["op_id"] == op_id: + self.logger.debug("Found operation number: {}".format(op_num)) + now = time() + if workflow_status: + content["operationHistory"][op_num]["workflowState"] = "COMPLETED" + content["operationHistory"][op_num]["result"] = True + else: + content["operationHistory"][op_num]["workflowState"] = "ERROR" + content["operationHistory"][op_num]["operationState"] = "FAILED" + content["operationHistory"][op_num]["result"] = False + + if resource_status: + content["operationHistory"][op_num]["resourceState"] = "READY" + content["operationHistory"][op_num]["operationState"] = "COMPLETED" + content["operationHistory"][op_num]["result"] = True + else: + content["operationHistory"][op_num]["resourceState"] = "NOT_READY" + content["operationHistory"][op_num]["operationState"] = "FAILED" + content["operationHistory"][op_num]["result"] = False + + content["operationHistory"][op_num]["endDate"] = now + break + op_num += 1 + self.logger.debug("content: {}".format(content)) + + return content + + async def common_check_list(self, checkings_list, db_collection, db_item): try: for checking in checkings_list: if checking["enable"]: @@ -54,6 +94,12 @@ class GitOpsLcm(LcmBase): ) if not status: return status, message + else: + db_item["resourceState"] = checking["resourceState"] + db_item = self.update_operation_history( + db_item, "COMPLETED", checking["resourceState"] + ) + self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item) except Exception as e: self.logger.debug(traceback.format_exc()) self.logger.debug(f"Exception: {e}", exc_info=True) @@ -152,7 +198,10 @@ class ClusterLcm(GitOpsLcm): return async def check_create_cluster(self, op_id, op_params, content): - self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") + self.logger.info( + f"check_create_cluster Operation {op_id}. Params: {op_params}." + ) + # self.logger.debug(f"Content: {content}") db_cluster = content["cluster"] cluster_name = db_cluster["git_name"].lower() cluster_kustomization_name = cluster_name @@ -174,6 +223,7 @@ class ClusterLcm(GitOpsLcm): "flag": "Ready", "timeout": self._checkloop_kustomization_timeout, "enable": True, + "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", }, { "item": f"cluster_{cloud_type}", @@ -182,6 +232,7 @@ class ClusterLcm(GitOpsLcm): "flag": "Synced", "timeout": self._checkloop_resource_timeout, "enable": True, + "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER", }, { "item": f"cluster_{cloud_type}", @@ -190,6 +241,7 @@ class ClusterLcm(GitOpsLcm): "flag": "Ready", "timeout": self._checkloop_resource_timeout, "enable": True, + "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER", }, { "item": "kustomization", @@ -198,6 +250,7 @@ class ClusterLcm(GitOpsLcm): "flag": "Ready", "timeout": self._checkloop_kustomization_timeout, "enable": bootstrap, + "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", }, ] else: @@ -210,9 +263,10 @@ class ClusterLcm(GitOpsLcm): "flag": "Ready", "timeout": self._checkloop_resource_timeout, "enable": True, + "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL", } checkings_list.insert(3, nodepool_check) - return await self.common_check_list(checkings_list) + return await self.common_check_list(checkings_list, "clusters", db_cluster) def update_profile_state(self, db_cluster, workflow_status, resource_status): profiles = [ @@ -587,8 +641,10 @@ class ClusterLcm(GitOpsLcm): return async def check_deregister_cluster(self, op_id, op_params, content): - self.logger.info("check_deregister_cluster Enter") - self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {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() diff --git a/osm_lcm/lcm_utils.py b/osm_lcm/lcm_utils.py index 925fd2d..d31db59 100644 --- a/osm_lcm/lcm_utils.py +++ b/osm_lcm/lcm_utils.py @@ -227,48 +227,6 @@ class LcmBase: # except DbException as e: # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e)) - def update_operation_history( - self, content, workflow_status=None, resource_status=None - ): - self.logger.info("Update Operation History in LcmBase") - self.logger.info( - "Content: {} Workflow Status: {} Resource Status: {}".format( - content, workflow_status, resource_status - ) - ) - - op_id = content["current_operation"] - self.logger.debug("OP_id: {}".format(op_id)) - op_num = 0 - for operation in content["operationHistory"]: - self.logger.debug("Operations: {}".format(operation)) - if operation["op_id"] == op_id: - self.logger.debug("Found operation number: {}".format(op_num)) - now = time() - if workflow_status: - content["operationHistory"][op_num]["workflowState"] = "COMPLETED" - content["operationHistory"][op_num]["result"] = True - else: - content["operationHistory"][op_num]["workflowState"] = "ERROR" - content["operationHistory"][op_num]["operationState"] = "FAILED" - content["operationHistory"][op_num]["result"] = False - - if resource_status: - content["operationHistory"][op_num]["resourceState"] = "READY" - content["operationHistory"][op_num]["operationState"] = "COMPLETED" - content["operationHistory"][op_num]["result"] = True - else: - content["operationHistory"][op_num]["resourceState"] = "NOT_READY" - content["operationHistory"][op_num]["operationState"] = "FAILED" - content["operationHistory"][op_num]["result"] = False - - content["operationHistory"][op_num]["endDate"] = now - break - op_num += 1 - self.logger.debug("content: {}".format(content)) - - return content - @staticmethod def calculate_charm_hash(zipped_file): """Calculate the hash of charm files which ends with .charm