Fix check_worfklow_status to add additional condition regarding status.phase 04/14904/5
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 3 Feb 2025 15:12:43 +0000 (16:12 +0100)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 4 Feb 2025 17:32:08 +0000 (18:32 +0100)
Change-Id: I539ca5506cd44dad791844c1f5611ac530a5fbbf
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_lcm/k8s.py
osm_lcm/odu_libs/workflows.py

index 87b7160..952760e 100644 (file)
@@ -352,12 +352,12 @@ class ClusterLcm(GitOpsLcm):
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -600,12 +600,12 @@ class ClusterLcm(GitOpsLcm):
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -786,12 +786,12 @@ class ClusterLcm(GitOpsLcm):
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -874,12 +874,12 @@ class ClusterLcm(GitOpsLcm):
             self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -962,12 +962,12 @@ class ClusterLcm(GitOpsLcm):
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -1094,12 +1094,12 @@ class ClusterLcm(GitOpsLcm):
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -1202,7 +1202,7 @@ class ClusterLcm(GitOpsLcm):
                 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
             )
             return
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
@@ -1499,7 +1499,7 @@ class K8sAppLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1528,7 +1528,7 @@ class K8sAppLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1575,7 +1575,7 @@ class K8sResourceLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1606,7 +1606,7 @@ class K8sResourceLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1655,7 +1655,7 @@ class K8sInfraControllerLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1686,7 +1686,7 @@ class K8sInfraControllerLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1735,7 +1735,7 @@ class K8sInfraConfigLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1766,7 +1766,7 @@ class K8sInfraConfigLcm(GitOpsLcm):
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is{}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
index 1b5d5cc..84a2b9e 100644 (file)
@@ -26,7 +26,8 @@ async def check_workflow_status(self, workflow_name):
     if not workflow_name:
         return False, "Workflow was not launched"
     try:
-        return await self.readiness_loop(
+        # First check if the workflow ends successfully
+        completed, message = await self.readiness_loop(
             item="workflow",
             name=workflow_name,
             namespace="osm-workflows",
@@ -37,8 +38,23 @@ async def check_workflow_status(self, workflow_name):
             deleted=False,
             timeout=300,
         )
+        if completed:
+            # Then check if the workflow has a failed task
+            return await self.readiness_loop(
+                item="workflow",
+                name=workflow_name,
+                namespace="osm-workflows",
+                condition={
+                    "jsonpath_filter": "status.phase",
+                    "value": "Succeeded",
+                },
+                deleted=False,
+                timeout=0,
+            )
+        else:
+            return False, f"Workflow was not completed: {message}"
     except Exception as e:
-        return False, f"Unexpected exception: {e}"
+        return False, f"Workflow could not be completed. Unexpected exception: {e}"
 
 
 async def readiness_loop(
@@ -90,6 +106,8 @@ async def readiness_loop(
     counter = 1
     retry_time = self._odu_checkloop_retry_time
     max_iterations = ceil(timeout / retry_time)
+    if max_iterations < 1:
+        max_iterations = 1
     api_group = item_api_map[item]["api_group"]
     api_plural = item_api_map[item]["api_plural"]
     api_version = item_api_map[item]["api_version"]
@@ -150,7 +168,8 @@ async def readiness_loop(
                     )
         except Exception as e:
             self.logger.error(f"Exception: {e}")
-        await asyncio.sleep(retry_time)
+        if counter < max_iterations:
+            await asyncio.sleep(retry_time)
         counter += 1
     return (
         False,