blob: a95e982a283dbc49e57ff55a71457e29a93c12eb [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
garciadeblas29d041a2024-10-22 12:54:22 +020020import traceback
garciadeblas40811852024-10-22 11:35:17 +020021from math import ceil
garciadeblas96b94f52024-07-08 16:18:21 +020022
23
24async def check_workflow_status(self, workflow_name):
garciadeblas40811852024-10-22 11:35:17 +020025 self.logger.info(f"check_workflow_status Enter: {workflow_name}")
garciadeblasb33813b2024-10-22 11:56:37 +020026 try:
27 return await self.readiness_loop(
28 item="workflow",
garciadeblas96b94f52024-07-08 16:18:21 +020029 name=workflow_name,
garciadeblasb33813b2024-10-22 11:56:37 +020030 namespace="osm-workflows",
31 flag="Completed",
32 timeout=300,
garciadeblas96b94f52024-07-08 16:18:21 +020033 )
garciadeblasb33813b2024-10-22 11:56:37 +020034 except Exception as e:
35 return False, f"Unexpected exception: {e}"
garciadeblas40811852024-10-22 11:35:17 +020036
37
38async def readiness_loop(self, item, name, namespace, flag, timeout):
39 self.logger.info("readiness_loop Enter")
40 self.logger.info(
41 f"{item} {name}. Namespace: {namespace}. Flag: {flag}. Timeout: {timeout}"
42 )
43 item_api_map = {
garciadeblasb33813b2024-10-22 11:56:37 +020044 "workflow": {
45 "api_group": "argoproj.io",
46 "api_plural": "workflows",
47 "api_version": "v1alpha1",
48 },
garciadeblas40811852024-10-22 11:35:17 +020049 "kustomization": {
50 "api_group": "kustomize.toolkit.fluxcd.io",
51 "api_plural": "kustomizations",
52 "api_version": "v1",
53 },
54 "cluster_aws": {
55 "api_group": "eks.aws.upbound.io",
56 "api_plural": "clusters",
57 "api_version": "v1beta1",
58 },
59 "cluster_azure": {
60 "api_group": "containerservice.azure.upbound.io",
61 "api_plural": "kubernetesclusters",
62 "api_version": "v1beta1",
63 },
64 "cluster_gcp": {
65 "api_group": "container.gcp.upbound.io",
66 "api_plural": "clusters",
67 "api_version": "v1beta2",
68 },
69 }
70 counter = 1
71 retry_time = self._odu_checkloop_retry_time
72 max_iterations = ceil(timeout / retry_time)
garciadeblasb33813b2024-10-22 11:56:37 +020073 api_group = item_api_map[item]["api_group"]
74 api_plural = item_api_map[item]["api_plural"]
75 api_version = item_api_map[item]["api_version"]
garciadeblas40811852024-10-22 11:35:17 +020076
77 while counter <= max_iterations:
78 generic_object = await self._kubectl.get_generic_object(
79 api_group=api_group,
80 api_plural=api_plural,
81 api_version=api_version,
82 namespace=namespace,
83 name=name,
84 )
85 if generic_object:
86 # self.logger.debug(f"{yaml.safe_dump(generic_object)}")
87 conditions = generic_object.get("status", {}).get("conditions", [])
88 else:
89 conditions = []
garciadeblas9ed46762024-10-22 12:47:05 +020090 self.logger.info(
91 f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
92 )
garciadeblas40811852024-10-22 11:35:17 +020093 result = next((item for item in conditions if item["type"] == flag), {})
94 if result.get("status", "False") == "True":
95 self.logger.info(
96 f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
97 )
98 return True, "COMPLETED"
99 await asyncio.sleep(retry_time)
100 counter += 1
101 return (
102 False,
103 "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)",
104 )
garciadeblas29d041a2024-10-22 12:54:22 +0200105
106
107async def common_check_list(self, checkings_list):
108 try:
109 for checking in checkings_list:
110 if checking["enable"]:
111 status, message = await self.readiness_loop(
112 item=checking["item"],
113 name=checking["name"],
114 namespace=checking["namespace"],
115 flag=checking["flag"],
116 timeout=checking["timeout"],
117 )
118 if not status:
119 return status, message
120 except Exception as e:
121 self.logger.debug(traceback.format_exc())
122 self.logger.debug(f"Exception: {e}", exc_info=True)
123 return False, f"Unexpected exception: {e}"
124 return True, "OK"