Refactor check_workflow_status to use readiness_loop 64/14664/3
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 22 Oct 2024 09:56:37 +0000 (11:56 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 22 Oct 2024 10:19:24 +0000 (12:19 +0200)
Change-Id: Iffa340aa6dcff27870b2a213e06878c02b9392ac
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_lcm/odu_libs/workflows.py

index bc66bc6..f9e556c 100644 (file)
@@ -22,34 +22,16 @@ from math import ceil
 
 async def check_workflow_status(self, workflow_name):
     self.logger.info(f"check_workflow_status Enter: {workflow_name}")
-    timeout = 300
-    retry_time = 15
-    counter = 1
-    max_iterations = ceil(timeout / retry_time)
-    while counter <= max_iterations:
-        workflow = await self._kubectl.get_generic_object(
-            api_group="argoproj.io",
-            api_plural="workflows",
-            api_version="v1alpha1",
-            namespace="osm-workflows",
+    try:
+        return await self.readiness_loop(
+            item="workflow",
             name=workflow_name,
+            namespace="osm-workflows",
+            flag="Completed",
+            timeout=300,
         )
-        # self.logger.info(f"Workflow: {workflow}")
-        # self.logger.info(f"Workflow status: {workflow.get('status')}")
-        conditions = workflow.get("status", {}).get("conditions", [])
-        self.logger.info(f"Workflow status conditions: {conditions}")
-        result = next((item for item in conditions if item["type"] == "Completed"), {})
-        if result.get("status", "False") == "True":
-            self.logger.info(
-                f"Workflow {workflow_name} completed in {counter} iterations (aprox {counter*retry_time} seconds)"
-            )
-            return True, "COMPLETED"
-        await asyncio.sleep(retry_time)
-        counter += 1
-    return (
-        False,
-        "Workflow {workflow_name} did not complete in {max_iterations} iterations (aprox {timeout} seconds)",
-    )
+    except Exception as e:
+        return False, f"Unexpected exception: {e}"
 
 
 async def readiness_loop(self, item, name, namespace, flag, timeout):
@@ -58,6 +40,11 @@ async def readiness_loop(self, item, name, namespace, flag, timeout):
         f"{item} {name}. Namespace: {namespace}. Flag: {flag}. Timeout: {timeout}"
     )
     item_api_map = {
+        "workflow": {
+            "api_group": "argoproj.io",
+            "api_plural": "workflows",
+            "api_version": "v1alpha1",
+        },
         "kustomization": {
             "api_group": "kustomize.toolkit.fluxcd.io",
             "api_plural": "kustomizations",
@@ -82,9 +69,9 @@ async def readiness_loop(self, item, name, namespace, flag, timeout):
     counter = 1
     retry_time = self._odu_checkloop_retry_time
     max_iterations = ceil(timeout / retry_time)
-    api_group = item_api_map["api_group"]
-    api_plural = item_api_map["api_plural"]
-    api_version = item_api_map["api_version"]
+    api_group = item_api_map[item]["api_group"]
+    api_plural = item_api_map[item]["api_plural"]
+    api_version = item_api_map[item]["api_version"]
 
     while counter <= max_iterations:
         generic_object = await self._kubectl.get_generic_object(