| 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 | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 21 | |
| 22 | |
| 23 | async def check_workflow_status(self, workflow_name): |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 24 | self.logger.info(f"check_workflow_status Enter: {workflow_name}") |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 25 | try: |
| 26 | return await self.readiness_loop( |
| 27 | item="workflow", |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 28 | name=workflow_name, |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 29 | namespace="osm-workflows", |
| 30 | flag="Completed", |
| 31 | timeout=300, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 32 | ) |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 33 | except Exception as e: |
| 34 | return False, f"Unexpected exception: {e}" |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 35 | |
| 36 | |
| 37 | async def readiness_loop(self, item, name, namespace, flag, timeout): |
| 38 | self.logger.info("readiness_loop Enter") |
| 39 | self.logger.info( |
| 40 | f"{item} {name}. Namespace: {namespace}. Flag: {flag}. Timeout: {timeout}" |
| 41 | ) |
| 42 | item_api_map = { |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 43 | "workflow": { |
| 44 | "api_group": "argoproj.io", |
| 45 | "api_plural": "workflows", |
| 46 | "api_version": "v1alpha1", |
| 47 | }, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 48 | "kustomization": { |
| 49 | "api_group": "kustomize.toolkit.fluxcd.io", |
| 50 | "api_plural": "kustomizations", |
| 51 | "api_version": "v1", |
| 52 | }, |
| 53 | "cluster_aws": { |
| 54 | "api_group": "eks.aws.upbound.io", |
| 55 | "api_plural": "clusters", |
| 56 | "api_version": "v1beta1", |
| 57 | }, |
| 58 | "cluster_azure": { |
| 59 | "api_group": "containerservice.azure.upbound.io", |
| 60 | "api_plural": "kubernetesclusters", |
| 61 | "api_version": "v1beta1", |
| 62 | }, |
| 63 | "cluster_gcp": { |
| 64 | "api_group": "container.gcp.upbound.io", |
| 65 | "api_plural": "clusters", |
| 66 | "api_version": "v1beta2", |
| 67 | }, |
| 68 | } |
| 69 | counter = 1 |
| 70 | retry_time = self._odu_checkloop_retry_time |
| 71 | max_iterations = ceil(timeout / retry_time) |
| garciadeblas | b33813b | 2024-10-22 11:56:37 +0200 | [diff] [blame] | 72 | api_group = item_api_map[item]["api_group"] |
| 73 | api_plural = item_api_map[item]["api_plural"] |
| 74 | api_version = item_api_map[item]["api_version"] |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 75 | |
| 76 | while counter <= max_iterations: |
| 77 | generic_object = await self._kubectl.get_generic_object( |
| 78 | api_group=api_group, |
| 79 | api_plural=api_plural, |
| 80 | api_version=api_version, |
| 81 | namespace=namespace, |
| 82 | name=name, |
| 83 | ) |
| 84 | if generic_object: |
| 85 | # self.logger.debug(f"{yaml.safe_dump(generic_object)}") |
| 86 | conditions = generic_object.get("status", {}).get("conditions", []) |
| 87 | else: |
| 88 | conditions = [] |
| garciadeblas | 9ed4676 | 2024-10-22 12:47:05 +0200 | [diff] [blame] | 89 | self.logger.info( |
| 90 | f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}" |
| 91 | ) |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 92 | result = next((item for item in conditions if item["type"] == flag), {}) |
| 93 | if result.get("status", "False") == "True": |
| 94 | self.logger.info( |
| 95 | f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)" |
| 96 | ) |
| 97 | return True, "COMPLETED" |
| 98 | await asyncio.sleep(retry_time) |
| 99 | counter += 1 |
| 100 | return ( |
| 101 | False, |
| 102 | "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)", |
| 103 | ) |