blob: 80527edab39273be0aa41031d59e18262229bed2 [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}")
garciadeblasadb81e82024-11-08 01:11:46 +010025 if not workflow_name:
26 return False, "Workflow was not launched"
garciadeblasb33813b2024-10-22 11:56:37 +020027 try:
28 return await self.readiness_loop(
29 item="workflow",
garciadeblas96b94f52024-07-08 16:18:21 +020030 name=workflow_name,
garciadeblasb33813b2024-10-22 11:56:37 +020031 namespace="osm-workflows",
32 flag="Completed",
33 timeout=300,
garciadeblas96b94f52024-07-08 16:18:21 +020034 )
garciadeblasb33813b2024-10-22 11:56:37 +020035 except Exception as e:
36 return False, f"Unexpected exception: {e}"
garciadeblas40811852024-10-22 11:35:17 +020037
38
39async def readiness_loop(self, item, name, namespace, flag, timeout):
40 self.logger.info("readiness_loop Enter")
41 self.logger.info(
garciadeblasceaa19d2024-10-24 12:52:11 +020042 f"{item} {name}. Namespace: '{namespace}'. Flag: {flag}. Timeout: {timeout}"
garciadeblas40811852024-10-22 11:35:17 +020043 )
44 item_api_map = {
garciadeblasb33813b2024-10-22 11:56:37 +020045 "workflow": {
46 "api_group": "argoproj.io",
47 "api_plural": "workflows",
48 "api_version": "v1alpha1",
49 },
garciadeblas40811852024-10-22 11:35:17 +020050 "kustomization": {
51 "api_group": "kustomize.toolkit.fluxcd.io",
52 "api_plural": "kustomizations",
53 "api_version": "v1",
54 },
55 "cluster_aws": {
56 "api_group": "eks.aws.upbound.io",
57 "api_plural": "clusters",
58 "api_version": "v1beta1",
59 },
60 "cluster_azure": {
61 "api_group": "containerservice.azure.upbound.io",
62 "api_plural": "kubernetesclusters",
63 "api_version": "v1beta1",
64 },
65 "cluster_gcp": {
66 "api_group": "container.gcp.upbound.io",
67 "api_plural": "clusters",
68 "api_version": "v1beta2",
69 },
garciadeblasceaa19d2024-10-24 12:52:11 +020070 "nodepool_aws": {
71 "api_group": "eks.aws.upbound.io",
72 "api_plural": "nodegroups",
73 "api_version": "v1beta1",
74 },
75 "nodepool_gcp": {
76 "api_group": "container.gcp.upbound.io",
77 "api_plural": "nodepools",
78 "api_version": "v1beta2",
79 },
garciadeblas40811852024-10-22 11:35:17 +020080 }
81 counter = 1
82 retry_time = self._odu_checkloop_retry_time
83 max_iterations = ceil(timeout / retry_time)
garciadeblasb33813b2024-10-22 11:56:37 +020084 api_group = item_api_map[item]["api_group"]
85 api_plural = item_api_map[item]["api_plural"]
86 api_version = item_api_map[item]["api_version"]
garciadeblas40811852024-10-22 11:35:17 +020087
88 while counter <= max_iterations:
89 generic_object = await self._kubectl.get_generic_object(
90 api_group=api_group,
91 api_plural=api_plural,
92 api_version=api_version,
93 namespace=namespace,
94 name=name,
95 )
96 if generic_object:
garciadeblasa31a9162024-11-08 14:22:49 +010097 # self.logger.debug(f"{yaml.safe_dump(generic_object)}")
garciadeblas40811852024-10-22 11:35:17 +020098 conditions = generic_object.get("status", {}).get("conditions", [])
99 else:
garciadeblasa31a9162024-11-08 14:22:49 +0100100 self.logger.info(
garciadeblasceaa19d2024-10-24 12:52:11 +0200101 f"Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
102 )
garciadeblas40811852024-10-22 11:35:17 +0200103 conditions = []
garciadeblas9ed46762024-10-22 12:47:05 +0200104 self.logger.info(
105 f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
106 )
garciadeblas40811852024-10-22 11:35:17 +0200107 result = next((item for item in conditions if item["type"] == flag), {})
108 if result.get("status", "False") == "True":
109 self.logger.info(
110 f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
111 )
112 return True, "COMPLETED"
113 await asyncio.sleep(retry_time)
114 counter += 1
115 return (
116 False,
117 "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)",
118 )