| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 19 | import asyncio |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 20 | from math import ceil |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 21 | from jsonpath_ng.ext import parse |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 22 | |
| 23 | |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 24 | async def check_workflow_status(self, op_id, workflow_name): |
| 25 | self.logger.info(f"Op {op_id}, check_workflow_status Enter: {workflow_name}") |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 26 | if not workflow_name: |
| 27 | return False, "Workflow was not launched" |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 28 | try: |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 29 | # First check if the workflow ends successfully |
| 30 | completed, message = await self.readiness_loop( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 31 | op_id, |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 32 | item="workflow", |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 33 | name=workflow_name, |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 34 | namespace="osm-workflows", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 35 | condition={ |
| 36 | "jsonpath_filter": "status.conditions[?(@.type=='Completed')].status", |
| 37 | "value": "True", |
| 38 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 39 | deleted=False, |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 40 | timeout=300, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 41 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 42 | if completed: |
| 43 | # Then check if the workflow has a failed task |
| 44 | return await self.readiness_loop( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 45 | op_id, |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 46 | item="workflow", |
| 47 | name=workflow_name, |
| 48 | namespace="osm-workflows", |
| 49 | condition={ |
| 50 | "jsonpath_filter": "status.phase", |
| 51 | "value": "Succeeded", |
| 52 | }, |
| 53 | deleted=False, |
| 54 | timeout=0, |
| 55 | ) |
| 56 | else: |
| 57 | return False, f"Workflow was not completed: {message}" |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 58 | except Exception as e: |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 59 | return False, f"Workflow could not be completed. Unexpected exception: {e}" |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 60 | |
| 61 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 62 | async def readiness_loop( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 63 | self, op_id, item, name, namespace, condition, deleted, timeout, kubectl_obj=None |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 64 | ): |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 65 | if kubectl_obj is None: |
| 66 | kubectl_obj = self._kubectl |
| 67 | self.logger.info("readiness_loop Enter") |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 68 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 69 | f"Op {op_id}. {item} {name}. Namespace: '{namespace}'. Condition: {condition}. Deleted: {deleted}. Timeout: {timeout}" |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 70 | ) |
| 71 | item_api_map = { |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 72 | "workflow": { |
| 73 | "api_group": "argoproj.io", |
| 74 | "api_plural": "workflows", |
| 75 | "api_version": "v1alpha1", |
| 76 | }, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 77 | "kustomization": { |
| 78 | "api_group": "kustomize.toolkit.fluxcd.io", |
| 79 | "api_plural": "kustomizations", |
| 80 | "api_version": "v1", |
| 81 | }, |
| 82 | "cluster_aws": { |
| 83 | "api_group": "eks.aws.upbound.io", |
| 84 | "api_plural": "clusters", |
| 85 | "api_version": "v1beta1", |
| 86 | }, |
| 87 | "cluster_azure": { |
| 88 | "api_group": "containerservice.azure.upbound.io", |
| 89 | "api_plural": "kubernetesclusters", |
| 90 | "api_version": "v1beta1", |
| 91 | }, |
| 92 | "cluster_gcp": { |
| 93 | "api_group": "container.gcp.upbound.io", |
| 94 | "api_plural": "clusters", |
| 95 | "api_version": "v1beta2", |
| 96 | }, |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 97 | "nodegroup_aws": { |
| garciadeblas | ceaa19d | 2024-10-24 12:52:11 +0200 | [diff] [blame] | 98 | "api_group": "eks.aws.upbound.io", |
| 99 | "api_plural": "nodegroups", |
| 100 | "api_version": "v1beta1", |
| 101 | }, |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 102 | "nodegroup_gcp": { |
| garciadeblas | ceaa19d | 2024-10-24 12:52:11 +0200 | [diff] [blame] | 103 | "api_group": "container.gcp.upbound.io", |
| 104 | "api_plural": "nodepools", |
| 105 | "api_version": "v1beta2", |
| 106 | }, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 107 | } |
| 108 | counter = 1 |
| 109 | retry_time = self._odu_checkloop_retry_time |
| 110 | max_iterations = ceil(timeout / retry_time) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 111 | if max_iterations < 1: |
| 112 | max_iterations = 1 |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 113 | api_group = item_api_map[item]["api_group"] |
| 114 | api_plural = item_api_map[item]["api_plural"] |
| 115 | api_version = item_api_map[item]["api_version"] |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 116 | |
| 117 | while counter <= max_iterations: |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 118 | iteration_prefix = f"Op {op_id}. Iteration {counter}/{max_iterations}" |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 119 | try: |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 120 | self.logger.info(f"Op {op_id}. Iteration {counter}/{max_iterations}") |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 121 | generic_object = await kubectl_obj.get_generic_object( |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 122 | api_group=api_group, |
| 123 | api_plural=api_plural, |
| 124 | api_version=api_version, |
| 125 | namespace=namespace, |
| 126 | name=name, |
| garciadeblas | ceaa19d | 2024-10-24 12:52:11 +0200 | [diff] [blame] | 127 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 128 | if deleted: |
| 129 | if generic_object: |
| 130 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 131 | f"{iteration_prefix}. Found {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}" |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 132 | ) |
| 133 | else: |
| 134 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 135 | f"{iteration_prefix}. {item} {name} deleted after {counter} iterations (aprox {counter*retry_time} seconds)" |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 136 | ) |
| 137 | return True, "COMPLETED" |
| 138 | else: |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 139 | if not condition: |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 140 | return True, "Nothing to check" |
| 141 | if generic_object: |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 142 | # If there is object, conditions must be checked |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 143 | # self.logger.debug(f"{yaml.safe_dump(generic_object)}") |
| 144 | conditions = generic_object.get("status", {}).get("conditions", []) |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 145 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 146 | f"{iteration_prefix}. Object found: {item} status conditions: {conditions}" |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 147 | ) |
| 148 | jsonpath_expr = parse(condition["jsonpath_filter"]) |
| 149 | match = jsonpath_expr.find(generic_object) |
| 150 | if match: |
| garciadeblas | 65cd989 | 2025-02-08 10:42:08 +0100 | [diff] [blame] | 151 | value = str(match[0].value) |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 152 | condition_function = condition.get( |
| 153 | "function", lambda x, y: x == y |
| 154 | ) |
| 155 | if condition_function(condition["value"], value): |
| 156 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 157 | f"{iteration_prefix}. {item} {name} met the condition {condition} with {value} in {counter} iterations (aprox {counter*retry_time} seconds)" |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 158 | ) |
| 159 | return True, "COMPLETED" |
| 160 | else: |
| 161 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 162 | f"{iteration_prefix}. {item} {name} did not meet the condition {condition} with value {value}" |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 163 | ) |
| 164 | else: |
| 165 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 166 | f"{iteration_prefix}. No match for filter {condition.get('jsonpath_filter', '-')} in {item} {name}" |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 167 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 168 | else: |
| 169 | self.logger.info( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 170 | f"{iteration_prefix}. Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}" |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 171 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 172 | except Exception as e: |
| 173 | self.logger.error(f"Exception: {e}") |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 174 | if counter < max_iterations: |
| 175 | await asyncio.sleep(retry_time) |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 176 | counter += 1 |
| 177 | return ( |
| 178 | False, |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 179 | f"Op {op_id}. {item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)", |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 180 | ) |