blob: bc66bc6ad9dc30cd5ea20bebbb322500974df01d [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}")
garciadeblas96b94f52024-07-08 16:18:21 +020025 timeout = 300
26 retry_time = 15
garciadeblas40811852024-10-22 11:35:17 +020027 counter = 1
28 max_iterations = ceil(timeout / retry_time)
29 while counter <= max_iterations:
garciadeblas96b94f52024-07-08 16:18:21 +020030 workflow = await self._kubectl.get_generic_object(
31 api_group="argoproj.io",
32 api_plural="workflows",
33 api_version="v1alpha1",
34 namespace="osm-workflows",
35 name=workflow_name,
36 )
37 # self.logger.info(f"Workflow: {workflow}")
38 # self.logger.info(f"Workflow status: {workflow.get('status')}")
39 conditions = workflow.get("status", {}).get("conditions", [])
40 self.logger.info(f"Workflow status conditions: {conditions}")
41 result = next((item for item in conditions if item["type"] == "Completed"), {})
42 if result.get("status", "False") == "True":
43 self.logger.info(
garciadeblas40811852024-10-22 11:35:17 +020044 f"Workflow {workflow_name} completed in {counter} iterations (aprox {counter*retry_time} seconds)"
garciadeblas96b94f52024-07-08 16:18:21 +020045 )
46 return True, "COMPLETED"
47 await asyncio.sleep(retry_time)
garciadeblas40811852024-10-22 11:35:17 +020048 counter += 1
49 return (
50 False,
51 "Workflow {workflow_name} did not complete in {max_iterations} iterations (aprox {timeout} seconds)",
52 )
53
54
55async def readiness_loop(self, item, name, namespace, flag, timeout):
56 self.logger.info("readiness_loop Enter")
57 self.logger.info(
58 f"{item} {name}. Namespace: {namespace}. Flag: {flag}. Timeout: {timeout}"
59 )
60 item_api_map = {
61 "kustomization": {
62 "api_group": "kustomize.toolkit.fluxcd.io",
63 "api_plural": "kustomizations",
64 "api_version": "v1",
65 },
66 "cluster_aws": {
67 "api_group": "eks.aws.upbound.io",
68 "api_plural": "clusters",
69 "api_version": "v1beta1",
70 },
71 "cluster_azure": {
72 "api_group": "containerservice.azure.upbound.io",
73 "api_plural": "kubernetesclusters",
74 "api_version": "v1beta1",
75 },
76 "cluster_gcp": {
77 "api_group": "container.gcp.upbound.io",
78 "api_plural": "clusters",
79 "api_version": "v1beta2",
80 },
81 }
82 counter = 1
83 retry_time = self._odu_checkloop_retry_time
84 max_iterations = ceil(timeout / retry_time)
85 api_group = item_api_map["api_group"]
86 api_plural = item_api_map["api_plural"]
87 api_version = item_api_map["api_version"]
88
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:
98 # self.logger.debug(f"{yaml.safe_dump(generic_object)}")
99 conditions = generic_object.get("status", {}).get("conditions", [])
100 else:
101 conditions = []
102 self.logger.info(f"{item} status conditions: {conditions}")
103 result = next((item for item in conditions if item["type"] == flag), {})
104 if result.get("status", "False") == "True":
105 self.logger.info(
106 f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
107 )
108 return True, "COMPLETED"
109 await asyncio.sleep(retry_time)
110 counter += 1
111 return (
112 False,
113 "{item} {name} was not ready after {max_iterations} iterations (aprox {timeout} seconds)",
114 )