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