blob: 094aa9e0f14f3bbc81e5b09ee58bf475128eee75 [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
garciadeblasceaa19d2024-10-24 12:52:11 +020020import yaml
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}")
garciadeblasadb81e82024-11-08 01:11:46 +010026 if not workflow_name:
27 return False, "Workflow was not launched"
garciadeblasb33813b2024-10-22 11:56:37 +020028 try:
29 return await self.readiness_loop(
30 item="workflow",
garciadeblas96b94f52024-07-08 16:18:21 +020031 name=workflow_name,
garciadeblasb33813b2024-10-22 11:56:37 +020032 namespace="osm-workflows",
33 flag="Completed",
34 timeout=300,
garciadeblas96b94f52024-07-08 16:18:21 +020035 )
garciadeblasb33813b2024-10-22 11:56:37 +020036 except Exception as e:
37 return False, f"Unexpected exception: {e}"
garciadeblas40811852024-10-22 11:35:17 +020038
39
40async def readiness_loop(self, item, name, namespace, flag, timeout):
41 self.logger.info("readiness_loop Enter")
42 self.logger.info(
garciadeblasceaa19d2024-10-24 12:52:11 +020043 f"{item} {name}. Namespace: '{namespace}'. Flag: {flag}. Timeout: {timeout}"
garciadeblas40811852024-10-22 11:35:17 +020044 )
45 item_api_map = {
garciadeblasb33813b2024-10-22 11:56:37 +020046 "workflow": {
47 "api_group": "argoproj.io",
48 "api_plural": "workflows",
49 "api_version": "v1alpha1",
50 },
garciadeblas40811852024-10-22 11:35:17 +020051 "kustomization": {
52 "api_group": "kustomize.toolkit.fluxcd.io",
53 "api_plural": "kustomizations",
54 "api_version": "v1",
55 },
56 "cluster_aws": {
57 "api_group": "eks.aws.upbound.io",
58 "api_plural": "clusters",
59 "api_version": "v1beta1",
60 },
61 "cluster_azure": {
62 "api_group": "containerservice.azure.upbound.io",
63 "api_plural": "kubernetesclusters",
64 "api_version": "v1beta1",
65 },
66 "cluster_gcp": {
67 "api_group": "container.gcp.upbound.io",
68 "api_plural": "clusters",
69 "api_version": "v1beta2",
70 },
garciadeblasceaa19d2024-10-24 12:52:11 +020071 "nodepool_aws": {
72 "api_group": "eks.aws.upbound.io",
73 "api_plural": "nodegroups",
74 "api_version": "v1beta1",
75 },
76 "nodepool_gcp": {
77 "api_group": "container.gcp.upbound.io",
78 "api_plural": "nodepools",
79 "api_version": "v1beta2",
80 },
garciadeblas40811852024-10-22 11:35:17 +020081 }
82 counter = 1
83 retry_time = self._odu_checkloop_retry_time
84 max_iterations = ceil(timeout / retry_time)
garciadeblasb33813b2024-10-22 11:56:37 +020085 api_group = item_api_map[item]["api_group"]
86 api_plural = item_api_map[item]["api_plural"]
87 api_version = item_api_map[item]["api_version"]
garciadeblas40811852024-10-22 11:35:17 +020088
89 while counter <= max_iterations:
90 generic_object = await self._kubectl.get_generic_object(
91 api_group=api_group,
92 api_plural=api_plural,
93 api_version=api_version,
94 namespace=namespace,
95 name=name,
96 )
97 if generic_object:
garciadeblasceaa19d2024-10-24 12:52:11 +020098 self.logger.debug(f"{yaml.safe_dump(generic_object)}")
garciadeblas40811852024-10-22 11:35:17 +020099 conditions = generic_object.get("status", {}).get("conditions", [])
100 else:
garciadeblasceaa19d2024-10-24 12:52:11 +0200101 self.logger.debug(
102 f"Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
103 )
garciadeblas40811852024-10-22 11:35:17 +0200104 conditions = []
garciadeblas9ed46762024-10-22 12:47:05 +0200105 self.logger.info(
106 f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
107 )
garciadeblas40811852024-10-22 11:35:17 +0200108 result = next((item for item in conditions if item["type"] == flag), {})
109 if result.get("status", "False") == "True":
110 self.logger.info(
111 f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
112 )
113 return True, "COMPLETED"
114 await asyncio.sleep(retry_time)
115 counter += 1
116 return (
117 False,
118 "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)",
119 )