blob: 86cf74375910eb58ef7354d09b603bb5359a60d8 [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
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
19import asyncio
garciadeblas40811852024-10-22 11:35:17 +020020from math import ceil
garciadeblas96b94f52024-07-08 16:18:21 +020021
22
23async def check_workflow_status(self, workflow_name):
garciadeblas40811852024-10-22 11:35:17 +020024 self.logger.info(f"check_workflow_status Enter: {workflow_name}")
garciadeblasb33813b2024-10-22 11:56:37 +020025 try:
26 return await self.readiness_loop(
27 item="workflow",
garciadeblas96b94f52024-07-08 16:18:21 +020028 name=workflow_name,
garciadeblasb33813b2024-10-22 11:56:37 +020029 namespace="osm-workflows",
30 flag="Completed",
31 timeout=300,
garciadeblas96b94f52024-07-08 16:18:21 +020032 )
garciadeblasb33813b2024-10-22 11:56:37 +020033 except Exception as e:
34 return False, f"Unexpected exception: {e}"
garciadeblas40811852024-10-22 11:35:17 +020035
36
37async 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 = {
garciadeblasb33813b2024-10-22 11:56:37 +020043 "workflow": {
44 "api_group": "argoproj.io",
45 "api_plural": "workflows",
46 "api_version": "v1alpha1",
47 },
garciadeblas40811852024-10-22 11:35:17 +020048 "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)
garciadeblasb33813b2024-10-22 11:56:37 +020072 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"]
garciadeblas40811852024-10-22 11:35:17 +020075
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 = []
garciadeblas9ed46762024-10-22 12:47:05 +020089 self.logger.info(
90 f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
91 )
garciadeblas40811852024-10-22 11:35:17 +020092 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 )