blob: f3b929554c8634fad8c017dcf48f0b111419363d [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
garciadeblas8424d1b2024-10-24 12:52:11 +020020import yaml
garciadeblasda7251f2024-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):
garciadeblasda7251f2024-10-22 11:35:17 +020025 self.logger.info(f"check_workflow_status Enter: {workflow_name}")
garciadeblas365aad52024-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,
garciadeblas365aad52024-10-22 11:56:37 +020030 namespace="osm-workflows",
31 flag="Completed",
32 timeout=300,
garciadeblas96b94f52024-07-08 16:18:21 +020033 )
garciadeblas365aad52024-10-22 11:56:37 +020034 except Exception as e:
35 return False, f"Unexpected exception: {e}"
garciadeblasda7251f2024-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(
garciadeblas8424d1b2024-10-24 12:52:11 +020041 f"{item} {name}. Namespace: '{namespace}'. Flag: {flag}. Timeout: {timeout}"
garciadeblasda7251f2024-10-22 11:35:17 +020042 )
43 item_api_map = {
garciadeblas365aad52024-10-22 11:56:37 +020044 "workflow": {
45 "api_group": "argoproj.io",
46 "api_plural": "workflows",
47 "api_version": "v1alpha1",
48 },
garciadeblasda7251f2024-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 },
garciadeblas8424d1b2024-10-24 12:52:11 +020069 "nodepool_aws": {
70 "api_group": "eks.aws.upbound.io",
71 "api_plural": "nodegroups",
72 "api_version": "v1beta1",
73 },
74 "nodepool_gcp": {
75 "api_group": "container.gcp.upbound.io",
76 "api_plural": "nodepools",
77 "api_version": "v1beta2",
78 },
garciadeblasda7251f2024-10-22 11:35:17 +020079 }
80 counter = 1
81 retry_time = self._odu_checkloop_retry_time
82 max_iterations = ceil(timeout / retry_time)
garciadeblas365aad52024-10-22 11:56:37 +020083 api_group = item_api_map[item]["api_group"]
84 api_plural = item_api_map[item]["api_plural"]
85 api_version = item_api_map[item]["api_version"]
garciadeblasda7251f2024-10-22 11:35:17 +020086
87 while counter <= max_iterations:
88 generic_object = await self._kubectl.get_generic_object(
89 api_group=api_group,
90 api_plural=api_plural,
91 api_version=api_version,
92 namespace=namespace,
93 name=name,
94 )
95 if generic_object:
garciadeblas8424d1b2024-10-24 12:52:11 +020096 self.logger.debug(f"{yaml.safe_dump(generic_object)}")
garciadeblasda7251f2024-10-22 11:35:17 +020097 conditions = generic_object.get("status", {}).get("conditions", [])
98 else:
garciadeblas8424d1b2024-10-24 12:52:11 +020099 self.logger.debug(
100 f"Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
101 )
garciadeblasda7251f2024-10-22 11:35:17 +0200102 conditions = []
garciadeblas46a79a22024-10-22 12:47:05 +0200103 self.logger.info(
104 f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
105 )
garciadeblasda7251f2024-10-22 11:35:17 +0200106 result = next((item for item in conditions if item["type"] == flag), {})
107 if result.get("status", "False") == "True":
108 self.logger.info(
109 f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
110 )
111 return True, "COMPLETED"
112 await asyncio.sleep(retry_time)
113 counter += 1
114 return (
115 False,
116 "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)",
117 )