| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | __author__ = ( |
| 17 | "Shrinithi R <shrinithi.r@tataelxsi.co.in>", |
| 18 | "Shahithya Y <shahithya.y@tataelxsi.co.in>", |
| 19 | ) |
| 20 | |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 21 | import copy |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 22 | import logging |
| yshah | d940c65 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 23 | from time import time |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 24 | import traceback |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 25 | from osm_lcm.lcm_utils import LcmBase |
| 26 | from copy import deepcopy |
| 27 | from osm_lcm import odu_workflows |
| 28 | from osm_lcm import vim_sdn |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 29 | from osm_lcm.data_utils.list_utils import find_in_list |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 30 | from osm_lcm.n2vc.kubectl import Kubectl |
| 31 | import yaml |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 32 | |
| yshah | 2f39b8a | 2024-12-19 11:06:24 +0000 | [diff] [blame] | 33 | MAP_PROFILE = { |
| 34 | "infra_controller_profiles": "infra-controllers", |
| 35 | "infra_config_profiles": "infra-configs", |
| 36 | "resource_profiles": "managed_resources", |
| 37 | "app_profiles": "apps", |
| 38 | } |
| 39 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 40 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 41 | class GitOpsLcm(LcmBase): |
| garciadeblas | ea865ff | 2024-11-20 12:42:49 +0100 | [diff] [blame] | 42 | db_collection = "gitops" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 43 | workflow_status = None |
| 44 | resource_status = None |
| 45 | |
| 46 | profile_collection_mapping = { |
| 47 | "infra_controller_profiles": "k8sinfra_controller", |
| 48 | "infra_config_profiles": "k8sinfra_config", |
| 49 | "resource_profiles": "k8sresource", |
| 50 | "app_profiles": "k8sapp", |
| 51 | } |
| garciadeblas | ea865ff | 2024-11-20 12:42:49 +0100 | [diff] [blame] | 52 | |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 53 | profile_type_mapping = { |
| 54 | "infra-controllers": "infra_controller_profiles", |
| 55 | "infra-configs": "infra_config_profiles", |
| 56 | "managed-resources": "resource_profiles", |
| 57 | "applications": "app_profiles", |
| 58 | } |
| 59 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 60 | def __init__(self, msg, lcm_tasks, config): |
| 61 | self.logger = logging.getLogger("lcm.gitops") |
| 62 | self.lcm_tasks = lcm_tasks |
| 63 | self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config) |
| 64 | self._checkloop_kustomization_timeout = 900 |
| 65 | self._checkloop_resource_timeout = 900 |
| 66 | self._workflows = {} |
| 67 | super().__init__(msg, self.logger) |
| 68 | |
| 69 | async def check_dummy_operation(self, op_id, op_params, content): |
| 70 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 71 | return True, "OK" |
| 72 | |
| garciadeblas | ea865ff | 2024-11-20 12:42:49 +0100 | [diff] [blame] | 73 | def initialize_operation(self, item_id, op_id): |
| 74 | db_item = self.db.get_one(self.db_collection, {"_id": item_id}) |
| 75 | operation = next( |
| 76 | (op for op in db_item.get("operationHistory", []) if op["op_id"] == op_id), |
| 77 | None, |
| 78 | ) |
| 79 | operation["workflowState"] = "PROCESSING" |
| 80 | operation["resourceState"] = "NOT_READY" |
| 81 | operation["operationState"] = "IN_PROGRESS" |
| 82 | operation["gitOperationInfo"] = None |
| 83 | db_item["current_operation"] = operation["op_id"] |
| 84 | self.db.set_one(self.db_collection, {"_id": item_id}, db_item) |
| 85 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 86 | def get_operation_params(self, item, operation_id): |
| 87 | operation_history = item.get("operationHistory", []) |
| 88 | operation = find_in_list( |
| 89 | operation_history, lambda op: op["op_id"] == operation_id |
| 90 | ) |
| 91 | return operation.get("operationParams", {}) |
| 92 | |
| 93 | def get_operation_type(self, item, operation_id): |
| 94 | operation_history = item.get("operationHistory", []) |
| 95 | operation = find_in_list( |
| 96 | operation_history, lambda op: op["op_id"] == operation_id |
| 97 | ) |
| 98 | return operation.get("operationType", {}) |
| 99 | |
| garciadeblas | be89070 | 2024-12-20 11:39:13 +0100 | [diff] [blame] | 100 | def update_state_operation_history( |
| 101 | self, content, op_id, workflow_state=None, resource_state=None |
| 102 | ): |
| 103 | self.logger.info( |
| 104 | f"Update state of operation {op_id} in Operation History in DB" |
| 105 | ) |
| 106 | self.logger.info( |
| 107 | f"Workflow state: {workflow_state}. Resource state: {resource_state}" |
| 108 | ) |
| 109 | self.logger.debug(f"Content: {content}") |
| 110 | |
| 111 | op_num = 0 |
| 112 | for operation in content["operationHistory"]: |
| 113 | self.logger.debug("Operations: {}".format(operation)) |
| 114 | if operation["op_id"] == op_id: |
| 115 | self.logger.debug("Found operation number: {}".format(op_num)) |
| 116 | if workflow_state is not None: |
| 117 | operation["workflowState"] = workflow_state |
| 118 | |
| 119 | if resource_state is not None: |
| 120 | operation["resourceState"] = resource_state |
| 121 | break |
| 122 | op_num += 1 |
| 123 | self.logger.debug("content: {}".format(content)) |
| 124 | |
| 125 | return content |
| 126 | |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 127 | def update_operation_history( |
| garciadeblas | f909289 | 2024-12-12 11:07:08 +0100 | [diff] [blame] | 128 | self, content, op_id, workflow_status=None, resource_status=None, op_end=True |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 129 | ): |
| 130 | self.logger.info( |
| 131 | f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}" |
| 132 | ) |
| 133 | self.logger.debug(f"Content: {content}") |
| 134 | |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 135 | op_num = 0 |
| 136 | for operation in content["operationHistory"]: |
| 137 | self.logger.debug("Operations: {}".format(operation)) |
| 138 | if operation["op_id"] == op_id: |
| 139 | self.logger.debug("Found operation number: {}".format(op_num)) |
| garciadeblas | 8bde3f4 | 2024-12-20 10:37:12 +0100 | [diff] [blame] | 140 | if workflow_status is not None: |
| 141 | if workflow_status: |
| 142 | operation["workflowState"] = "COMPLETED" |
| 143 | operation["result"] = True |
| 144 | else: |
| 145 | operation["workflowState"] = "ERROR" |
| 146 | operation["operationState"] = "FAILED" |
| 147 | operation["result"] = False |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 148 | |
| garciadeblas | 8bde3f4 | 2024-12-20 10:37:12 +0100 | [diff] [blame] | 149 | if resource_status is not None: |
| 150 | if resource_status: |
| 151 | operation["resourceState"] = "READY" |
| 152 | operation["operationState"] = "COMPLETED" |
| 153 | operation["result"] = True |
| 154 | else: |
| 155 | operation["resourceState"] = "NOT_READY" |
| 156 | operation["operationState"] = "FAILED" |
| 157 | operation["result"] = False |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 158 | |
| garciadeblas | f909289 | 2024-12-12 11:07:08 +0100 | [diff] [blame] | 159 | if op_end: |
| 160 | now = time() |
| 161 | operation["endDate"] = now |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 162 | break |
| 163 | op_num += 1 |
| 164 | self.logger.debug("content: {}".format(content)) |
| 165 | |
| 166 | return content |
| 167 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 168 | async def check_workflow_and_update_db(self, op_id, workflow_name, db_content): |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 169 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 170 | op_id, workflow_name |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 171 | ) |
| 172 | self.logger.info( |
| 173 | "Workflow Status: {} Workflow Message: {}".format( |
| 174 | workflow_status, workflow_msg |
| 175 | ) |
| 176 | ) |
| 177 | operation_type = self.get_operation_type(db_content, op_id) |
| 178 | if operation_type == "create" and workflow_status: |
| 179 | db_content["state"] = "CREATED" |
| 180 | elif operation_type == "create" and not workflow_status: |
| 181 | db_content["state"] = "FAILED_CREATION" |
| 182 | elif operation_type == "delete" and workflow_status: |
| 183 | db_content["state"] = "DELETED" |
| 184 | elif operation_type == "delete" and not workflow_status: |
| 185 | db_content["state"] = "FAILED_DELETION" |
| 186 | |
| 187 | if workflow_status: |
| 188 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 189 | else: |
| 190 | db_content["resourceState"] = "ERROR" |
| 191 | |
| 192 | db_content = self.update_operation_history( |
| 193 | db_content, op_id, workflow_status, None |
| 194 | ) |
| 195 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| 196 | return workflow_status |
| 197 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 198 | async def check_resource_and_update_db( |
| 199 | self, resource_name, op_id, op_params, db_content |
| 200 | ): |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 201 | workflow_status = True |
| 202 | |
| 203 | resource_status, resource_msg = await self.check_resource_status( |
| 204 | resource_name, op_id, op_params, db_content |
| 205 | ) |
| 206 | self.logger.info( |
| 207 | "Resource Status: {} Resource Message: {}".format( |
| 208 | resource_status, resource_msg |
| 209 | ) |
| 210 | ) |
| 211 | |
| 212 | if resource_status: |
| 213 | db_content["resourceState"] = "READY" |
| 214 | else: |
| 215 | db_content["resourceState"] = "ERROR" |
| 216 | |
| 217 | db_content = self.update_operation_history( |
| 218 | db_content, op_id, workflow_status, resource_status |
| 219 | ) |
| 220 | db_content["operatingState"] = "IDLE" |
| 221 | db_content["current_operation"] = None |
| 222 | return resource_status, db_content |
| 223 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 224 | async def common_check_list( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 225 | self, op_id, checkings_list, db_collection, db_item, kubectl_obj=None |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 226 | ): |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 227 | try: |
| 228 | for checking in checkings_list: |
| 229 | if checking["enable"]: |
| 230 | status, message = await self.odu.readiness_loop( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 231 | op_id=op_id, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 232 | item=checking["item"], |
| 233 | name=checking["name"], |
| 234 | namespace=checking["namespace"], |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 235 | condition=checking.get("condition"), |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 236 | deleted=checking.get("deleted", False), |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 237 | timeout=checking["timeout"], |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 238 | kubectl_obj=kubectl_obj, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 239 | ) |
| 240 | if not status: |
| garciadeblas | 656818e | 2025-01-21 18:48:58 +0100 | [diff] [blame] | 241 | error_message = "Resources not ready: " |
| 242 | error_message += checking.get("error_message", "") |
| 243 | return status, f"{error_message}: {message}" |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 244 | else: |
| 245 | db_item["resourceState"] = checking["resourceState"] |
| garciadeblas | be89070 | 2024-12-20 11:39:13 +0100 | [diff] [blame] | 246 | db_item = self.update_state_operation_history( |
| 247 | db_item, op_id, None, checking["resourceState"] |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 248 | ) |
| 249 | self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item) |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 250 | except Exception as e: |
| 251 | self.logger.debug(traceback.format_exc()) |
| 252 | self.logger.debug(f"Exception: {e}", exc_info=True) |
| 253 | return False, f"Unexpected exception: {e}" |
| 254 | return True, "OK" |
| 255 | |
| 256 | async def check_resource_status(self, key, op_id, op_params, content): |
| 257 | self.logger.info( |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 258 | f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}." |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 259 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 260 | self.logger.debug(f"Check resource status. Content: {content}") |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 261 | check_resource_function = self._workflows.get(key, {}).get( |
| 262 | "check_resource_function" |
| 263 | ) |
| 264 | self.logger.info("check_resource function : {}".format(check_resource_function)) |
| 265 | if check_resource_function: |
| 266 | return await check_resource_function(op_id, op_params, content) |
| 267 | else: |
| 268 | return await self.check_dummy_operation(op_id, op_params, content) |
| 269 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 270 | def check_force_delete_and_delete_from_db( |
| 271 | self, _id, workflow_status, resource_status, force |
| 272 | ): |
| 273 | self.logger.info( |
| 274 | f" Force: {force} Workflow status: {workflow_status} Resource Status: {resource_status}" |
| 275 | ) |
| 276 | if force and (not workflow_status or not resource_status): |
| 277 | self.db.del_one(self.db_collection, {"_id": _id}) |
| 278 | return True |
| 279 | return False |
| 280 | |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 281 | def decrypt_age_keys(self, content, fields=["age_pubkey", "age_privkey"]): |
| 282 | self.db.encrypt_decrypt_fields( |
| 283 | content, |
| 284 | "decrypt", |
| 285 | fields, |
| 286 | schema_version="1.11", |
| 287 | salt=content["_id"], |
| 288 | ) |
| 289 | |
| 290 | def encrypt_age_keys(self, content, fields=["age_pubkey", "age_privkey"]): |
| 291 | self.db.encrypt_decrypt_fields( |
| 292 | content, |
| 293 | "encrypt", |
| 294 | fields, |
| 295 | schema_version="1.11", |
| 296 | salt=content["_id"], |
| 297 | ) |
| 298 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 299 | def decrypted_copy(self, content, fields=["age_pubkey", "age_privkey"]): |
| 300 | # This deep copy is intended to be passed to ODU workflows. |
| 301 | content_copy = copy.deepcopy(content) |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 302 | |
| 303 | # decrypting the key |
| 304 | self.db.encrypt_decrypt_fields( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 305 | content_copy, |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 306 | "decrypt", |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 307 | fields, |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 308 | schema_version="1.11", |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 309 | salt=content_copy["_id"], |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 310 | ) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 311 | return content_copy |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 312 | |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 313 | def delete_profile_ksu(self, _id, profile_type): |
| 314 | filter_q = {"profile": {"_id": _id, "profile_type": profile_type}} |
| 315 | ksu_list = self.db.get_list("ksus", filter_q) |
| 316 | if ksu_list: |
| 317 | self.db.del_list("ksus", filter_q) |
| 318 | return |
| 319 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 320 | def cluster_kubectl(self, db_cluster): |
| 321 | cluster_kubeconfig = db_cluster["credentials"] |
| 322 | kubeconfig_path = f"/tmp/{db_cluster['_id']}_kubeconfig.yaml" |
| 323 | with open(kubeconfig_path, "w") as kubeconfig_file: |
| 324 | yaml.safe_dump(cluster_kubeconfig, kubeconfig_file) |
| 325 | return Kubectl(config_file=kubeconfig_path) |
| 326 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 327 | |
| 328 | class ClusterLcm(GitOpsLcm): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 329 | db_collection = "clusters" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 330 | |
| 331 | def __init__(self, msg, lcm_tasks, config): |
| 332 | """ |
| 333 | Init, Connect to database, filesystem storage, and messaging |
| 334 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 335 | :return: None |
| 336 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 337 | super().__init__(msg, lcm_tasks, config) |
| 338 | self._workflows = { |
| 339 | "create_cluster": { |
| 340 | "check_resource_function": self.check_create_cluster, |
| 341 | }, |
| garciadeblas | b0a42c2 | 2024-11-13 16:00:10 +0100 | [diff] [blame] | 342 | "register_cluster": { |
| 343 | "check_resource_function": self.check_register_cluster, |
| 344 | }, |
| 345 | "update_cluster": { |
| 346 | "check_resource_function": self.check_update_cluster, |
| 347 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 348 | "delete_cluster": { |
| 349 | "check_resource_function": self.check_delete_cluster, |
| 350 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 351 | } |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 352 | self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config) |
| 353 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 354 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 355 | self.logger.info("cluster Create Enter") |
| 356 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 357 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 358 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 359 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 360 | |
| 361 | # To initialize the operation states |
| 362 | self.initialize_operation(cluster_id, op_id) |
| 363 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 364 | # To get the cluster |
| 365 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 366 | |
| 367 | # To get the operation params details |
| 368 | op_params = self.get_operation_params(db_cluster, op_id) |
| 369 | |
| 370 | # To copy the cluster content and decrypting fields to use in workflows |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 371 | db_cluster_copy = self.decrypted_copy(db_cluster) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 372 | workflow_content = { |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 373 | "cluster": db_cluster_copy, |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 374 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 375 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 376 | # To get the vim account details |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 377 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 378 | workflow_content["vim_account"] = db_vim |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 379 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 380 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 381 | "create_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 382 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 383 | if not workflow_res: |
| 384 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 385 | db_cluster["state"] = "FAILED_CREATION" |
| 386 | db_cluster["resourceState"] = "ERROR" |
| 387 | db_cluster = self.update_operation_history( |
| 388 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 389 | ) |
| 390 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 391 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 392 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 393 | "create_cluster", op_id, op_params, workflow_content |
| 394 | ) |
| 395 | self.logger.info( |
| 396 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 397 | ) |
| 398 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 399 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 400 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 401 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 402 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 403 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 404 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 405 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 406 | workflow_status, workflow_msg |
| 407 | ) |
| 408 | ) |
| 409 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 410 | db_cluster["state"] = "CREATED" |
| 411 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 412 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 413 | db_cluster["state"] = "FAILED_CREATION" |
| 414 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 415 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 416 | db_cluster = self.update_operation_history( |
| 417 | db_cluster, op_id, workflow_status, None |
| 418 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 419 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 420 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 421 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 422 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 423 | "create_cluster", op_id, op_params, workflow_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 424 | ) |
| 425 | self.logger.info( |
| 426 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 427 | ) |
| 428 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 429 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 430 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 431 | "create_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 432 | ) |
| 433 | self.logger.info( |
| 434 | "resource_status is :{} and resource_msg is :{}".format( |
| 435 | resource_status, resource_msg |
| 436 | ) |
| 437 | ) |
| 438 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 439 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 440 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 441 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 442 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 443 | db_cluster["operatingState"] = "IDLE" |
| 444 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 445 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 446 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 447 | db_cluster["current_operation"] = None |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 448 | |
| 449 | # Retrieve credentials |
| 450 | cluster_creds = None |
| 451 | if db_cluster["resourceState"] == "READY" and db_cluster["state"] == "CREATED": |
| 452 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 453 | # TODO: manage the case where the credentials are not available |
| 454 | if result: |
| 455 | db_cluster["credentials"] = cluster_creds |
| 456 | |
| 457 | # Update db_cluster |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 458 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 459 | self.update_default_profile_agekeys(db_cluster_copy) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 460 | self.update_profile_state(db_cluster, workflow_status, resource_status) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 461 | |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 462 | # Register the cluster in k8sclusters collection |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 463 | db_register = self.db.get_one("k8sclusters", {"name": db_cluster["name"]}) |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 464 | if cluster_creds: |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 465 | db_register["credentials"] = cluster_creds |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 466 | # To call the lcm.py for registering the cluster in k8scluster lcm. |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 467 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 468 | register = await self.regist.create(db_register, order_id) |
| 469 | self.logger.debug(f"Register is : {register}") |
| 470 | else: |
| 471 | db_register["_admin"]["operationalState"] = "ERROR" |
| 472 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 473 | # To call the lcm.py for registering the cluster in k8scluster lcm. |
| 474 | db_register["credentials"] = cluster_creds |
| 475 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 476 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 477 | return |
| 478 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 479 | async def check_create_cluster(self, op_id, op_params, content): |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 480 | self.logger.info( |
| 481 | f"check_create_cluster Operation {op_id}. Params: {op_params}." |
| 482 | ) |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 483 | db_cluster = content["cluster"] |
| 484 | cluster_name = db_cluster["git_name"].lower() |
| 485 | cluster_kustomization_name = cluster_name |
| 486 | db_vim_account = content["vim_account"] |
| 487 | cloud_type = db_vim_account["vim_type"] |
| 488 | nodepool_name = "" |
| 489 | if cloud_type == "aws": |
| 490 | nodepool_name = f"{cluster_name}-nodegroup" |
| 491 | cluster_name = f"{cluster_name}-cluster" |
| 492 | elif cloud_type == "gcp": |
| 493 | nodepool_name = f"nodepool-{cluster_name}" |
| 494 | bootstrap = op_params.get("bootstrap", True) |
| 495 | if cloud_type in ("azure", "gcp", "aws"): |
| 496 | checkings_list = [ |
| 497 | { |
| 498 | "item": "kustomization", |
| 499 | "name": cluster_kustomization_name, |
| 500 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 501 | "condition": { |
| 502 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 503 | "value": "True", |
| 504 | }, |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 505 | "timeout": 1500, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 506 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 507 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 508 | }, |
| 509 | { |
| 510 | "item": f"cluster_{cloud_type}", |
| 511 | "name": cluster_name, |
| 512 | "namespace": "", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 513 | "condition": { |
| 514 | "jsonpath_filter": "status.conditions[?(@.type=='Synced')].status", |
| 515 | "value": "True", |
| 516 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 517 | "timeout": self._checkloop_resource_timeout, |
| 518 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 519 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 520 | }, |
| 521 | { |
| 522 | "item": f"cluster_{cloud_type}", |
| 523 | "name": cluster_name, |
| 524 | "namespace": "", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 525 | "condition": { |
| 526 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 527 | "value": "True", |
| 528 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 529 | "timeout": self._checkloop_resource_timeout, |
| 530 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 531 | "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 532 | }, |
| 533 | { |
| 534 | "item": "kustomization", |
| 535 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 536 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 537 | "condition": { |
| 538 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 539 | "value": "True", |
| 540 | }, |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 541 | "timeout": self._checkloop_resource_timeout, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 542 | "enable": bootstrap, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 543 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 544 | }, |
| 545 | ] |
| 546 | else: |
| 547 | return False, "Not suitable VIM account to check cluster status" |
| 548 | if nodepool_name: |
| 549 | nodepool_check = { |
| 550 | "item": f"nodepool_{cloud_type}", |
| 551 | "name": nodepool_name, |
| 552 | "namespace": "", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 553 | "condition": { |
| 554 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 555 | "value": "True", |
| 556 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 557 | "timeout": self._checkloop_resource_timeout, |
| 558 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 559 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 560 | } |
| 561 | checkings_list.insert(3, nodepool_check) |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 562 | return await self.common_check_list( |
| 563 | op_id, checkings_list, "clusters", db_cluster |
| 564 | ) |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 565 | |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 566 | def update_default_profile_agekeys(self, db_cluster): |
| 567 | profiles = [ |
| 568 | "infra_controller_profiles", |
| 569 | "infra_config_profiles", |
| 570 | "app_profiles", |
| 571 | "resource_profiles", |
| 572 | ] |
| 573 | self.logger.debug("the db_cluster is :{}".format(db_cluster)) |
| 574 | for profile_type in profiles: |
| 575 | profile_id = db_cluster[profile_type] |
| 576 | db_collection = self.profile_collection_mapping[profile_type] |
| 577 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| 578 | db_profile["age_pubkey"] = db_cluster["age_pubkey"] |
| 579 | db_profile["age_privkey"] = db_cluster["age_privkey"] |
| 580 | self.encrypt_age_keys(db_profile) |
| 581 | self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile) |
| 582 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 583 | def update_profile_state(self, db_cluster, workflow_status, resource_status): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 584 | profiles = [ |
| 585 | "infra_controller_profiles", |
| 586 | "infra_config_profiles", |
| 587 | "app_profiles", |
| 588 | "resource_profiles", |
| 589 | ] |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 590 | self.logger.debug("the db_cluster is :{}".format(db_cluster)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 591 | for profile_type in profiles: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 592 | profile_id = db_cluster[profile_type] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 593 | db_collection = self.profile_collection_mapping[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 594 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 595 | op_id = db_profile["operationHistory"][-1].get("op_id") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 596 | db_profile["state"] = db_cluster["state"] |
| 597 | db_profile["resourceState"] = db_cluster["resourceState"] |
| 598 | db_profile["operatingState"] = db_cluster["operatingState"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 599 | db_profile = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 600 | db_profile, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 601 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 602 | self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile) |
| 603 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 604 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 605 | self.logger.info("cluster delete Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 606 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 607 | try: |
| 608 | # To get the cluster and op ids |
| 609 | cluster_id = params["cluster_id"] |
| 610 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 611 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 612 | # To initialize the operation states |
| 613 | self.initialize_operation(cluster_id, op_id) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 614 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 615 | # To get the cluster |
| 616 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 617 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 618 | # To get the operation params details |
| 619 | op_params = self.get_operation_params(db_cluster, op_id) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 620 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 621 | # To copy the cluster content and decrypting fields to use in workflows |
| 622 | workflow_content = { |
| 623 | "cluster": self.decrypted_copy(db_cluster), |
| 624 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 625 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 626 | # To get the vim account details |
| 627 | db_vim = self.db.get_one( |
| 628 | "vim_accounts", {"name": db_cluster["vim_account"]} |
| 629 | ) |
| 630 | workflow_content["vim_account"] = db_vim |
| 631 | except Exception as e: |
| 632 | self.logger.debug(traceback.format_exc()) |
| 633 | self.logger.debug(f"Exception: {e}", exc_info=True) |
| 634 | raise e |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 635 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 636 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 637 | "delete_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 638 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 639 | if not workflow_res: |
| 640 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 641 | db_cluster["state"] = "FAILED_DELETION" |
| 642 | db_cluster["resourceState"] = "ERROR" |
| 643 | db_cluster = self.update_operation_history( |
| 644 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 645 | ) |
| 646 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 647 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 648 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 649 | "delete_cluster", op_id, op_params, workflow_content |
| 650 | ) |
| 651 | self.logger.info( |
| 652 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 653 | ) |
| 654 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 655 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 656 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 657 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 658 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 659 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 660 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 661 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 662 | workflow_status, workflow_msg |
| 663 | ) |
| 664 | ) |
| 665 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 666 | db_cluster["state"] = "DELETED" |
| 667 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 668 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 669 | db_cluster["state"] = "FAILED_DELETION" |
| 670 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 671 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 672 | db_cluster = self.update_operation_history( |
| 673 | db_cluster, op_id, workflow_status, None |
| 674 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 675 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 676 | |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 677 | # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded |
| 678 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 679 | "delete_cluster", op_id, op_params, workflow_content |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 680 | ) |
| 681 | self.logger.info( |
| 682 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 683 | ) |
| 684 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 685 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 686 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 687 | "delete_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 688 | ) |
| 689 | self.logger.info( |
| 690 | "resource_status is :{} and resource_msg is :{}".format( |
| 691 | resource_status, resource_msg |
| 692 | ) |
| 693 | ) |
| 694 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 695 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 696 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 697 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 698 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 699 | db_cluster["operatingState"] = "IDLE" |
| 700 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 701 | db_cluster, op_id, workflow_status, resource_status |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 702 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 703 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 704 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 705 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 706 | force = params.get("force", False) |
| 707 | if force: |
| 708 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 709 | cluster_id, workflow_status, resource_status, force |
| 710 | ) |
| 711 | if force_delete_status: |
| 712 | return |
| 713 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 714 | # To delete it from DB |
| 715 | if db_cluster["state"] == "DELETED": |
| 716 | self.delete_cluster(db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 717 | |
| 718 | # To delete it from k8scluster collection |
| 719 | self.db.del_one("k8sclusters", {"name": db_cluster["name"]}) |
| 720 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 721 | return |
| 722 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 723 | async def check_delete_cluster(self, op_id, op_params, content): |
| 724 | self.logger.info( |
| 725 | f"check_delete_cluster Operation {op_id}. Params: {op_params}." |
| 726 | ) |
| 727 | self.logger.debug(f"Content: {content}") |
| 728 | db_cluster = content["cluster"] |
| 729 | cluster_name = db_cluster["git_name"].lower() |
| 730 | cluster_kustomization_name = cluster_name |
| 731 | db_vim_account = content["vim_account"] |
| 732 | cloud_type = db_vim_account["vim_type"] |
| 733 | if cloud_type == "aws": |
| 734 | cluster_name = f"{cluster_name}-cluster" |
| 735 | if cloud_type in ("azure", "gcp", "aws"): |
| 736 | checkings_list = [ |
| 737 | { |
| 738 | "item": "kustomization", |
| 739 | "name": cluster_kustomization_name, |
| 740 | "namespace": "managed-resources", |
| 741 | "deleted": True, |
| 742 | "timeout": self._checkloop_kustomization_timeout, |
| 743 | "enable": True, |
| 744 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_DELETED", |
| 745 | }, |
| 746 | { |
| 747 | "item": f"cluster_{cloud_type}", |
| 748 | "name": cluster_name, |
| 749 | "namespace": "", |
| 750 | "deleted": True, |
| 751 | "timeout": self._checkloop_resource_timeout, |
| 752 | "enable": True, |
| 753 | "resourceState": "IN_PROGRESS.RESOURCE_DELETED.CLUSTER", |
| 754 | }, |
| 755 | ] |
| 756 | else: |
| 757 | return False, "Not suitable VIM account to check cluster status" |
| 758 | return await self.common_check_list( |
| 759 | op_id, checkings_list, "clusters", db_cluster |
| 760 | ) |
| 761 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 762 | def delete_cluster(self, db_cluster): |
| 763 | # Actually, item_content is equal to db_cluster |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 764 | # detach profiles |
| 765 | update_dict = None |
| 766 | profiles_to_detach = [ |
| 767 | "infra_controller_profiles", |
| 768 | "infra_config_profiles", |
| 769 | "app_profiles", |
| 770 | "resource_profiles", |
| 771 | ] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 772 | """ |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 773 | profiles_collection = { |
| 774 | "infra_controller_profiles": "k8sinfra_controller", |
| 775 | "infra_config_profiles": "k8sinfra_config", |
| 776 | "app_profiles": "k8sapp", |
| 777 | "resource_profiles": "k8sresource", |
| 778 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 779 | """ |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 780 | for profile_type in profiles_to_detach: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 781 | if db_cluster.get(profile_type): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 782 | profile_ids = db_cluster[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 783 | profile_ids_copy = deepcopy(profile_ids) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 784 | for profile_id in profile_ids_copy: |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 785 | db_collection = self.profile_collection_mapping[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 786 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| garciadeblas | c255285 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 787 | self.logger.debug("the db_profile is :{}".format(db_profile)) |
| 788 | self.logger.debug( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 789 | "the item_content name is :{}".format(db_cluster["name"]) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 790 | ) |
| garciadeblas | c255285 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 791 | self.logger.debug( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 792 | "the db_profile name is :{}".format(db_profile["name"]) |
| 793 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 794 | if db_cluster["name"] == db_profile["name"]: |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 795 | self.delete_profile_ksu(profile_id, profile_type) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 796 | self.db.del_one(db_collection, {"_id": profile_id}) |
| 797 | else: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 798 | profile_ids.remove(profile_id) |
| 799 | update_dict = {profile_type: profile_ids} |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 800 | self.db.set_one( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 801 | "clusters", {"_id": db_cluster["_id"]}, update_dict |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 802 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 803 | self.db.del_one("clusters", {"_id": db_cluster["_id"]}) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 804 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 805 | async def attach_profile(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 806 | self.logger.info("profile attach Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 807 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 808 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 809 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 810 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 811 | |
| 812 | # To initialize the operation states |
| 813 | self.initialize_operation(cluster_id, op_id) |
| 814 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 815 | # To get the cluster |
| 816 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 817 | |
| 818 | # To get the operation params details |
| 819 | op_params = self.get_operation_params(db_cluster, op_id) |
| 820 | |
| 821 | # To copy the cluster content and decrypting fields to use in workflows |
| 822 | workflow_content = { |
| 823 | "cluster": self.decrypted_copy(db_cluster), |
| 824 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 825 | |
| 826 | # To get the profile details |
| 827 | profile_id = params["profile_id"] |
| 828 | profile_type = params["profile_type"] |
| 829 | profile_collection = self.profile_collection_mapping[profile_type] |
| 830 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 831 | db_profile["profile_type"] = profile_type |
| 832 | # content["profile"] = db_profile |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 833 | workflow_content["profile"] = db_profile |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 834 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 835 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 836 | "attach_profile_to_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 837 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 838 | if not workflow_res: |
| 839 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 840 | db_cluster["resourceState"] = "ERROR" |
| 841 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 842 | db_cluster = self.update_operation_history( |
| 843 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 844 | ) |
| 845 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 846 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 847 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 848 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 849 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 850 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 851 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 852 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 853 | workflow_status, workflow_msg |
| 854 | ) |
| 855 | ) |
| 856 | if workflow_status: |
| 857 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 858 | else: |
| 859 | db_cluster["resourceState"] = "ERROR" |
| 860 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 861 | db_cluster = self.update_operation_history( |
| 862 | db_cluster, op_id, workflow_status, None |
| 863 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 864 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 865 | |
| 866 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 867 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 868 | "attach_profile_to_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 869 | ) |
| 870 | self.logger.info( |
| 871 | "resource_status is :{} and resource_msg is :{}".format( |
| 872 | resource_status, resource_msg |
| 873 | ) |
| 874 | ) |
| 875 | if resource_status: |
| 876 | db_cluster["resourceState"] = "READY" |
| 877 | else: |
| 878 | db_cluster["resourceState"] = "ERROR" |
| 879 | |
| 880 | db_cluster["operatingState"] = "IDLE" |
| 881 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 882 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 883 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 884 | profile_list = db_cluster[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 885 | if resource_status: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 886 | profile_list.append(profile_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 887 | db_cluster[profile_type] = profile_list |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 888 | db_cluster["current_operation"] = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 889 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 890 | |
| 891 | return |
| 892 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 893 | async def detach_profile(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 894 | self.logger.info("profile dettach Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 895 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 896 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 897 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 898 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 899 | |
| 900 | # To initialize the operation states |
| 901 | self.initialize_operation(cluster_id, op_id) |
| 902 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 903 | # To get the cluster |
| 904 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 905 | |
| 906 | # To get the operation params details |
| 907 | op_params = self.get_operation_params(db_cluster, op_id) |
| 908 | |
| 909 | # To copy the cluster content and decrypting fields to use in workflows |
| 910 | workflow_content = { |
| 911 | "cluster": self.decrypted_copy(db_cluster), |
| 912 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 913 | |
| 914 | # To get the profile details |
| 915 | profile_id = params["profile_id"] |
| 916 | profile_type = params["profile_type"] |
| 917 | profile_collection = self.profile_collection_mapping[profile_type] |
| 918 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 919 | db_profile["profile_type"] = profile_type |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 920 | workflow_content["profile"] = db_profile |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 921 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 922 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 923 | "detach_profile_from_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 924 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 925 | if not workflow_res: |
| 926 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 927 | db_cluster["resourceState"] = "ERROR" |
| 928 | db_cluster = self.update_operation_history( |
| 929 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 930 | ) |
| 931 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 932 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 933 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 934 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 935 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 936 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 937 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 938 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 939 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 940 | workflow_status, workflow_msg |
| 941 | ) |
| 942 | ) |
| 943 | if workflow_status: |
| 944 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 945 | else: |
| 946 | db_cluster["resourceState"] = "ERROR" |
| 947 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 948 | db_cluster = self.update_operation_history( |
| 949 | db_cluster, op_id, workflow_status, None |
| 950 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 951 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 952 | |
| 953 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 954 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 955 | "detach_profile_from_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 956 | ) |
| 957 | self.logger.info( |
| 958 | "resource_status is :{} and resource_msg is :{}".format( |
| 959 | resource_status, resource_msg |
| 960 | ) |
| 961 | ) |
| 962 | if resource_status: |
| 963 | db_cluster["resourceState"] = "READY" |
| 964 | else: |
| 965 | db_cluster["resourceState"] = "ERROR" |
| 966 | |
| 967 | db_cluster["operatingState"] = "IDLE" |
| 968 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 969 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 970 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 971 | profile_list = db_cluster[profile_type] |
| 972 | self.logger.info("profile list is : {}".format(profile_list)) |
| 973 | if resource_status: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 974 | profile_list.remove(profile_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 975 | db_cluster[profile_type] = profile_list |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 976 | db_cluster["current_operation"] = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 977 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 978 | |
| 979 | return |
| 980 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 981 | async def register(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 982 | self.logger.info("cluster register enter") |
| 983 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 984 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 985 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 986 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 987 | |
| 988 | # To initialize the operation states |
| 989 | self.initialize_operation(cluster_id, op_id) |
| 990 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 991 | # To get the cluster |
| 992 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 993 | |
| 994 | # To get the operation params details |
| 995 | op_params = self.get_operation_params(db_cluster, op_id) |
| 996 | |
| 997 | # To copy the cluster content and decrypting fields to use in workflows |
| 998 | workflow_content = { |
| 999 | "cluster": self.decrypted_copy(db_cluster), |
| 1000 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1001 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1002 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1003 | "register_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1004 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1005 | if not workflow_res: |
| 1006 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1007 | db_cluster["state"] = "FAILED_CREATION" |
| 1008 | db_cluster["resourceState"] = "ERROR" |
| 1009 | db_cluster = self.update_operation_history( |
| 1010 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1011 | ) |
| 1012 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1013 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1014 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1015 | "register_cluster", op_id, op_params, workflow_content |
| 1016 | ) |
| 1017 | self.logger.info( |
| 1018 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1019 | ) |
| 1020 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1021 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1022 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1023 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1024 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1025 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1026 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1027 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1028 | workflow_status, workflow_msg |
| 1029 | ) |
| 1030 | ) |
| 1031 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1032 | db_cluster["state"] = "CREATED" |
| 1033 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1034 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1035 | db_cluster["state"] = "FAILED_CREATION" |
| 1036 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1037 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1038 | db_cluster = self.update_operation_history( |
| 1039 | db_cluster, op_id, workflow_status, None |
| 1040 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1041 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1042 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 1043 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1044 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1045 | "register_cluster", op_id, op_params, workflow_content |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 1046 | ) |
| 1047 | self.logger.info( |
| 1048 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1049 | ) |
| 1050 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1051 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1052 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1053 | "register_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1054 | ) |
| 1055 | self.logger.info( |
| 1056 | "resource_status is :{} and resource_msg is :{}".format( |
| 1057 | resource_status, resource_msg |
| 1058 | ) |
| 1059 | ) |
| 1060 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1061 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1062 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1063 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1064 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1065 | db_cluster["operatingState"] = "IDLE" |
| 1066 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1067 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1068 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1069 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1070 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1071 | |
| 1072 | db_register = self.db.get_one("k8sclusters", {"name": db_cluster["name"]}) |
| 1073 | db_register["credentials"] = db_cluster["credentials"] |
| 1074 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 1075 | |
| 1076 | if db_cluster["resourceState"] == "READY" and db_cluster["state"] == "CREATED": |
| 1077 | # To call the lcm.py for registering the cluster in k8scluster lcm. |
| 1078 | register = await self.regist.create(db_register, order_id) |
| 1079 | self.logger.debug(f"Register is : {register}") |
| 1080 | else: |
| 1081 | db_register["_admin"]["operationalState"] = "ERROR" |
| 1082 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 1083 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1084 | return |
| 1085 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1086 | async def check_register_cluster(self, op_id, op_params, content): |
| 1087 | self.logger.info( |
| 1088 | f"check_register_cluster Operation {op_id}. Params: {op_params}." |
| 1089 | ) |
| 1090 | # self.logger.debug(f"Content: {content}") |
| 1091 | db_cluster = content["cluster"] |
| 1092 | cluster_name = db_cluster["git_name"].lower() |
| 1093 | cluster_kustomization_name = cluster_name |
| 1094 | bootstrap = op_params.get("bootstrap", True) |
| 1095 | checkings_list = [ |
| 1096 | { |
| 1097 | "item": "kustomization", |
| 1098 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 1099 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 1100 | "condition": { |
| 1101 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 1102 | "value": "True", |
| 1103 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1104 | "timeout": self._checkloop_kustomization_timeout, |
| 1105 | "enable": bootstrap, |
| 1106 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| 1107 | }, |
| 1108 | ] |
| 1109 | return await self.common_check_list( |
| 1110 | op_id, checkings_list, "clusters", db_cluster |
| 1111 | ) |
| 1112 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1113 | async def deregister(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1114 | self.logger.info("cluster deregister enter") |
| 1115 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1116 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1117 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1118 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1119 | |
| 1120 | # To initialize the operation states |
| 1121 | self.initialize_operation(cluster_id, op_id) |
| 1122 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1123 | # To get the cluster |
| 1124 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1125 | |
| 1126 | # To get the operation params details |
| 1127 | op_params = self.get_operation_params(db_cluster, op_id) |
| 1128 | |
| 1129 | # To copy the cluster content and decrypting fields to use in workflows |
| 1130 | workflow_content = { |
| 1131 | "cluster": self.decrypted_copy(db_cluster), |
| 1132 | } |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1133 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1134 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1135 | "deregister_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1136 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1137 | if not workflow_res: |
| 1138 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1139 | db_cluster["state"] = "FAILED_DELETION" |
| 1140 | db_cluster["resourceState"] = "ERROR" |
| 1141 | db_cluster = self.update_operation_history( |
| 1142 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1143 | ) |
| 1144 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1145 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1146 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1147 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1148 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1149 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1150 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1151 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1152 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1153 | workflow_status, workflow_msg |
| 1154 | ) |
| 1155 | ) |
| 1156 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1157 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1158 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1159 | db_cluster["state"] = "FAILED_DELETION" |
| 1160 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1161 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1162 | db_cluster = self.update_operation_history( |
| 1163 | db_cluster, op_id, workflow_status, None |
| 1164 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1165 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1166 | |
| 1167 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1168 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1169 | "deregister_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1170 | ) |
| 1171 | self.logger.info( |
| 1172 | "resource_status is :{} and resource_msg is :{}".format( |
| 1173 | resource_status, resource_msg |
| 1174 | ) |
| 1175 | ) |
| 1176 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1177 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1178 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1179 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1180 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1181 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1182 | db_cluster, op_id, workflow_status, resource_status |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1183 | ) |
| 1184 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1185 | |
| garciadeblas | 9338045 | 2025-02-05 09:32:52 +0100 | [diff] [blame] | 1186 | await self.delete(params, order_id) |
| 1187 | # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded |
| 1188 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1189 | "deregister_cluster", op_id, op_params, workflow_content |
| 1190 | ) |
| 1191 | self.logger.info( |
| 1192 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1193 | ) |
| 1194 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1195 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1196 | async def get_creds(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1197 | self.logger.info("Cluster get creds Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1198 | cluster_id = params["cluster_id"] |
| 1199 | op_id = params["operation_id"] |
| 1200 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1201 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 1202 | if result: |
| 1203 | db_cluster["credentials"] = cluster_creds |
| yshah | d940c65 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 1204 | op_len = 0 |
| 1205 | for operations in db_cluster["operationHistory"]: |
| 1206 | if operations["op_id"] == op_id: |
| 1207 | db_cluster["operationHistory"][op_len]["result"] = result |
| 1208 | db_cluster["operationHistory"][op_len]["endDate"] = time() |
| 1209 | op_len += 1 |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1210 | db_cluster["current_operation"] = None |
| yshah | d940c65 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 1211 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1212 | self.logger.info("Cluster Get Creds Exit") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1213 | return |
| 1214 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1215 | async def update(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1216 | self.logger.info("Cluster update Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1217 | # To get the cluster details |
| 1218 | cluster_id = params["cluster_id"] |
| 1219 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1220 | |
| 1221 | # To get the operation params details |
| 1222 | op_id = params["operation_id"] |
| 1223 | op_params = self.get_operation_params(db_cluster, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1224 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1225 | # To copy the cluster content and decrypting fields to use in workflows |
| 1226 | workflow_content = { |
| 1227 | "cluster": self.decrypted_copy(db_cluster), |
| 1228 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | # vim account details |
| 1231 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1232 | workflow_content["vim_account"] = db_vim |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1233 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1234 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1235 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1236 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1237 | if not workflow_res: |
| 1238 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1239 | db_cluster["resourceState"] = "ERROR" |
| 1240 | db_cluster = self.update_operation_history( |
| 1241 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1242 | ) |
| 1243 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1244 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1245 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1246 | "update_cluster", op_id, op_params, workflow_content |
| 1247 | ) |
| 1248 | self.logger.info( |
| 1249 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1250 | ) |
| 1251 | return |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1252 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1253 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1254 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1255 | ) |
| 1256 | self.logger.info( |
| 1257 | "Workflow Status: {} Workflow Message: {}".format( |
| 1258 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1259 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1260 | ) |
| 1261 | |
| 1262 | if workflow_status: |
| 1263 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1264 | else: |
| 1265 | db_cluster["resourceState"] = "ERROR" |
| 1266 | |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1267 | db_cluster = self.update_operation_history( |
| 1268 | db_cluster, op_id, workflow_status, None |
| 1269 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1270 | # self.logger.info("Db content: {}".format(db_content)) |
| 1271 | # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster) |
| 1272 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1273 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1274 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1275 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1276 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1277 | ) |
| 1278 | self.logger.info( |
| 1279 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1280 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1281 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1282 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1283 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1284 | ) |
| 1285 | self.logger.info( |
| 1286 | "Resource Status: {} Resource Message: {}".format( |
| 1287 | resource_status, resource_msg |
| 1288 | ) |
| 1289 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1290 | |
| 1291 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1292 | db_cluster["resourceState"] = "READY" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1293 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1294 | db_cluster["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1295 | |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1296 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1297 | db_cluster, op_id, workflow_status, resource_status |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1298 | ) |
| 1299 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1300 | db_cluster["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1301 | # self.logger.info("db_cluster: {}".format(db_cluster)) |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 1302 | # TODO: verify condition |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1303 | # For the moment, if the workflow completed successfully, then we update the db accordingly. |
| 1304 | if workflow_status: |
| 1305 | if "k8s_version" in op_params: |
| 1306 | db_cluster["k8s_version"] = op_params["k8s_version"] |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1307 | if "node_count" in op_params: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1308 | db_cluster["node_count"] = op_params["node_count"] |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1309 | if "node_size" in op_params: |
| 1310 | db_cluster["node_count"] = op_params["node_size"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1311 | # self.db.set_one(self.db_collection, {"_id": _id}, db_content) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1312 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1313 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1314 | return |
| 1315 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1316 | async def check_update_cluster(self, op_id, op_params, content): |
| 1317 | self.logger.info( |
| 1318 | f"check_update_cluster Operation {op_id}. Params: {op_params}." |
| 1319 | ) |
| 1320 | self.logger.debug(f"Content: {content}") |
| garciadeblas | d7d8bde | 2025-01-27 18:31:06 +0100 | [diff] [blame] | 1321 | # return await self.check_dummy_operation(op_id, op_params, content) |
| 1322 | db_cluster = content["cluster"] |
| 1323 | cluster_name = db_cluster["git_name"].lower() |
| 1324 | cluster_kustomization_name = cluster_name |
| 1325 | db_vim_account = content["vim_account"] |
| 1326 | cloud_type = db_vim_account["vim_type"] |
| 1327 | if cloud_type == "aws": |
| 1328 | cluster_name = f"{cluster_name}-cluster" |
| 1329 | if cloud_type in ("azure", "gcp", "aws"): |
| 1330 | checkings_list = [ |
| 1331 | { |
| 1332 | "item": "kustomization", |
| 1333 | "name": cluster_kustomization_name, |
| 1334 | "namespace": "managed-resources", |
| 1335 | "condition": { |
| 1336 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 1337 | "value": "True", |
| 1338 | }, |
| 1339 | "timeout": self._checkloop_kustomization_timeout, |
| 1340 | "enable": True, |
| 1341 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 1342 | }, |
| 1343 | ] |
| 1344 | else: |
| 1345 | return False, "Not suitable VIM account to check cluster status" |
| 1346 | # Scale operation |
| 1347 | if "node_count" in op_params: |
| 1348 | checkings_list.append( |
| 1349 | { |
| 1350 | "item": f"cluster_{cloud_type}", |
| 1351 | "name": cluster_name, |
| 1352 | "namespace": "", |
| 1353 | "condition": { |
| 1354 | "jsonpath_filter": "status.atProvider.defaultNodePool[0].nodeCount", |
| 1355 | "value": f"{op_params['node_count']}", |
| 1356 | }, |
| garciadeblas | 65cd989 | 2025-02-08 10:42:08 +0100 | [diff] [blame] | 1357 | "timeout": self._checkloop_resource_timeout * 3, |
| garciadeblas | d7d8bde | 2025-01-27 18:31:06 +0100 | [diff] [blame] | 1358 | "enable": True, |
| 1359 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODE_COUNT.CLUSTER", |
| 1360 | } |
| 1361 | ) |
| 1362 | # Upgrade operation |
| 1363 | if "k8s_version" in op_params: |
| 1364 | checkings_list.append( |
| 1365 | { |
| 1366 | "item": f"cluster_{cloud_type}", |
| 1367 | "name": cluster_name, |
| 1368 | "namespace": "", |
| 1369 | "condition": { |
| 1370 | "jsonpath_filter": "status.atProvider.defaultNodePool[0].orchestratorVersion", |
| 1371 | "value": op_params["k8s_version"], |
| 1372 | }, |
| 1373 | "timeout": self._checkloop_resource_timeout * 2, |
| 1374 | "enable": True, |
| 1375 | "resourceState": "IN_PROGRESS.RESOURCE_READY.K8S_VERSION.CLUSTER", |
| 1376 | } |
| 1377 | ) |
| 1378 | return await self.common_check_list( |
| 1379 | op_id, checkings_list, "clusters", db_cluster |
| 1380 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1381 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1382 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1383 | class CloudCredentialsLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1384 | db_collection = "vim_accounts" |
| 1385 | |
| 1386 | def __init__(self, msg, lcm_tasks, config): |
| 1387 | """ |
| 1388 | Init, Connect to database, filesystem storage, and messaging |
| 1389 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1390 | :return: None |
| 1391 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1392 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1393 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1394 | async def add(self, params, order_id): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1395 | self.logger.info("Cloud Credentials create") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1396 | vim_id = params["_id"] |
| 1397 | op_id = vim_id |
| 1398 | op_params = params |
| 1399 | db_content = self.db.get_one(self.db_collection, {"_id": vim_id}) |
| 1400 | vim_config = db_content.get("config", {}) |
| 1401 | self.db.encrypt_decrypt_fields( |
| 1402 | vim_config.get("credentials"), |
| 1403 | "decrypt", |
| 1404 | ["password", "secret"], |
| 1405 | schema_version=db_content["schema_version"], |
| 1406 | salt=vim_id, |
| 1407 | ) |
| 1408 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1409 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1410 | "create_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1411 | ) |
| 1412 | |
| 1413 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1414 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1415 | ) |
| 1416 | |
| 1417 | self.logger.info( |
| 1418 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1419 | ) |
| 1420 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1421 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1422 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1423 | "create_cloud_credentials", op_id, op_params, db_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1424 | ) |
| 1425 | self.logger.info( |
| 1426 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1427 | ) |
| 1428 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1429 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1430 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1431 | "create_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1432 | ) |
| 1433 | self.logger.info( |
| 1434 | "Resource Status: {} Resource Message: {}".format( |
| 1435 | resource_status, resource_msg |
| 1436 | ) |
| 1437 | ) |
| garciadeblas | 15b8a30 | 2024-09-23 12:40:13 +0200 | [diff] [blame] | 1438 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1439 | db_content["_admin"]["operationalState"] = "ENABLED" |
| 1440 | for operation in db_content["_admin"]["operations"]: |
| garciadeblas | 15b8a30 | 2024-09-23 12:40:13 +0200 | [diff] [blame] | 1441 | if operation["lcmOperationType"] == "create": |
| 1442 | operation["operationState"] = "ENABLED" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1443 | self.logger.info("Content : {}".format(db_content)) |
| 1444 | self.db.set_one("vim_accounts", {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1445 | return |
| 1446 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1447 | async def edit(self, params, order_id): |
| 1448 | self.logger.info("Cloud Credentials Update") |
| 1449 | vim_id = params["_id"] |
| 1450 | op_id = vim_id |
| 1451 | op_params = params |
| 1452 | db_content = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| 1453 | vim_config = db_content.get("config", {}) |
| 1454 | self.db.encrypt_decrypt_fields( |
| 1455 | vim_config.get("credentials"), |
| 1456 | "decrypt", |
| 1457 | ["password", "secret"], |
| 1458 | schema_version=db_content["schema_version"], |
| 1459 | salt=vim_id, |
| 1460 | ) |
| 1461 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1462 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1463 | "update_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1464 | ) |
| 1465 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1466 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1467 | ) |
| 1468 | self.logger.info( |
| 1469 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1470 | ) |
| 1471 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1472 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1473 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1474 | "update_cloud_credentials", op_id, op_params, db_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1475 | ) |
| 1476 | self.logger.info( |
| 1477 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1478 | ) |
| 1479 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1480 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1481 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1482 | "update_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1483 | ) |
| 1484 | self.logger.info( |
| 1485 | "Resource Status: {} Resource Message: {}".format( |
| 1486 | resource_status, resource_msg |
| 1487 | ) |
| 1488 | ) |
| 1489 | return |
| 1490 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1491 | async def remove(self, params, order_id): |
| 1492 | self.logger.info("Cloud Credentials remove") |
| 1493 | vim_id = params["_id"] |
| 1494 | op_id = vim_id |
| 1495 | op_params = params |
| 1496 | db_content = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| 1497 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1498 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1499 | "delete_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1500 | ) |
| 1501 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1502 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1503 | ) |
| 1504 | self.logger.info( |
| 1505 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1506 | ) |
| 1507 | |
| 1508 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1509 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1510 | "delete_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1511 | ) |
| 1512 | self.logger.info( |
| 1513 | "Resource Status: {} Resource Message: {}".format( |
| 1514 | resource_status, resource_msg |
| 1515 | ) |
| 1516 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1517 | self.db.del_one(self.db_collection, {"_id": db_content["_id"]}) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1518 | return |
| 1519 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1520 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1521 | class K8sAppLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1522 | db_collection = "k8sapp" |
| 1523 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1524 | def __init__(self, msg, lcm_tasks, config): |
| 1525 | """ |
| 1526 | Init, Connect to database, filesystem storage, and messaging |
| 1527 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1528 | :return: None |
| 1529 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1530 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1531 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1532 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1533 | self.logger.info("App Create Enter") |
| 1534 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1535 | op_id = params["operation_id"] |
| 1536 | profile_id = params["profile_id"] |
| 1537 | |
| 1538 | # To initialize the operation states |
| 1539 | self.initialize_operation(profile_id, op_id) |
| 1540 | |
| 1541 | content = self.db.get_one("k8sapp", {"_id": profile_id}) |
| 1542 | content["profile_type"] = "applications" |
| 1543 | op_params = self.get_operation_params(content, op_id) |
| 1544 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| 1545 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1546 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1547 | "create_profile", op_id, op_params, content |
| 1548 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1549 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1550 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1551 | workflow_status = await self.check_workflow_and_update_db( |
| 1552 | op_id, workflow_name, content |
| 1553 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1554 | |
| 1555 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1556 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1557 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1558 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1559 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1560 | self.logger.info(f"App Create Exit with resource status: {resource_status}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1561 | return |
| 1562 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1563 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1564 | self.logger.info("App delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1565 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1566 | op_id = params["operation_id"] |
| 1567 | profile_id = params["profile_id"] |
| 1568 | |
| 1569 | # To initialize the operation states |
| 1570 | self.initialize_operation(profile_id, op_id) |
| 1571 | |
| 1572 | content = self.db.get_one("k8sapp", {"_id": profile_id}) |
| 1573 | op_params = self.get_operation_params(content, op_id) |
| 1574 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1575 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1576 | "delete_profile", op_id, op_params, content |
| 1577 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1578 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1579 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1580 | workflow_status = await self.check_workflow_and_update_db( |
| 1581 | op_id, workflow_name, content |
| 1582 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1583 | |
| 1584 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1585 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1586 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1587 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1588 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1589 | force = params.get("force", False) |
| 1590 | if force: |
| 1591 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1592 | profile_id, workflow_status, resource_status, force |
| 1593 | ) |
| 1594 | if force_delete_status: |
| 1595 | return |
| 1596 | |
| 1597 | self.logger.info(f"Resource status: {resource_status}") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1598 | if resource_status: |
| 1599 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1600 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 1601 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1602 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1603 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 1604 | self.logger.info(f"App Delete Exit with resource status: {resource_status}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1605 | return |
| 1606 | |
| 1607 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1608 | class K8sResourceLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1609 | db_collection = "k8sresource" |
| 1610 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1611 | def __init__(self, msg, lcm_tasks, config): |
| 1612 | """ |
| 1613 | Init, Connect to database, filesystem storage, and messaging |
| 1614 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1615 | :return: None |
| 1616 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1617 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1618 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1619 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1620 | self.logger.info("Resource Create Enter") |
| 1621 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1622 | op_id = params["operation_id"] |
| 1623 | profile_id = params["profile_id"] |
| 1624 | |
| 1625 | # To initialize the operation states |
| 1626 | self.initialize_operation(profile_id, op_id) |
| 1627 | |
| 1628 | content = self.db.get_one("k8sresource", {"_id": profile_id}) |
| 1629 | content["profile_type"] = "managed-resources" |
| 1630 | op_params = self.get_operation_params(content, op_id) |
| 1631 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| 1632 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1633 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1634 | "create_profile", op_id, op_params, content |
| 1635 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1636 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1637 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1638 | workflow_status = await self.check_workflow_and_update_db( |
| 1639 | op_id, workflow_name, content |
| 1640 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1641 | |
| 1642 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1643 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1644 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1645 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1646 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1647 | self.logger.info( |
| 1648 | f"Resource Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1649 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1650 | return |
| 1651 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1652 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1653 | self.logger.info("Resource delete Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1654 | |
| 1655 | op_id = params["operation_id"] |
| 1656 | profile_id = params["profile_id"] |
| 1657 | |
| 1658 | # To initialize the operation states |
| 1659 | self.initialize_operation(profile_id, op_id) |
| 1660 | |
| 1661 | content = self.db.get_one("k8sresource", {"_id": profile_id}) |
| 1662 | op_params = self.get_operation_params(content, op_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1663 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1664 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1665 | "delete_profile", op_id, op_params, content |
| 1666 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1667 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1668 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1669 | workflow_status = await self.check_workflow_and_update_db( |
| 1670 | op_id, workflow_name, content |
| 1671 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1672 | |
| 1673 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1674 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1675 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1676 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1677 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1678 | force = params.get("force", False) |
| 1679 | if force: |
| 1680 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1681 | profile_id, workflow_status, resource_status, force |
| 1682 | ) |
| 1683 | if force_delete_status: |
| 1684 | return |
| 1685 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1686 | if resource_status: |
| 1687 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1688 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 1689 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1690 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1691 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 1692 | self.logger.info( |
| 1693 | f"Resource Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1694 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1695 | return |
| 1696 | |
| 1697 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1698 | class K8sInfraControllerLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1699 | db_collection = "k8sinfra_controller" |
| 1700 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1701 | def __init__(self, msg, lcm_tasks, config): |
| 1702 | """ |
| 1703 | Init, Connect to database, filesystem storage, and messaging |
| 1704 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1705 | :return: None |
| 1706 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1707 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1708 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1709 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1710 | self.logger.info("Infra controller Create Enter") |
| 1711 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1712 | op_id = params["operation_id"] |
| 1713 | profile_id = params["profile_id"] |
| 1714 | |
| 1715 | # To initialize the operation states |
| 1716 | self.initialize_operation(profile_id, op_id) |
| 1717 | |
| 1718 | content = self.db.get_one("k8sinfra_controller", {"_id": profile_id}) |
| 1719 | content["profile_type"] = "infra-controllers" |
| 1720 | op_params = self.get_operation_params(content, op_id) |
| 1721 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| 1722 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1723 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1724 | "create_profile", op_id, op_params, content |
| 1725 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1726 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1727 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1728 | workflow_status = await self.check_workflow_and_update_db( |
| 1729 | op_id, workflow_name, content |
| 1730 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1731 | |
| 1732 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1733 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1734 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1735 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1736 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1737 | self.logger.info( |
| 1738 | f"Infra Controller Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1739 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1740 | return |
| 1741 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1742 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1743 | self.logger.info("Infra controller delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1744 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1745 | op_id = params["operation_id"] |
| 1746 | profile_id = params["profile_id"] |
| 1747 | |
| 1748 | # To initialize the operation states |
| 1749 | self.initialize_operation(profile_id, op_id) |
| 1750 | |
| 1751 | content = self.db.get_one("k8sinfra_controller", {"_id": profile_id}) |
| 1752 | op_params = self.get_operation_params(content, op_id) |
| 1753 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1754 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1755 | "delete_profile", op_id, op_params, content |
| 1756 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1757 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1758 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1759 | workflow_status = await self.check_workflow_and_update_db( |
| 1760 | op_id, workflow_name, content |
| 1761 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1762 | |
| 1763 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1764 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1765 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1766 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1767 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1768 | force = params.get("force", False) |
| 1769 | if force: |
| 1770 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1771 | profile_id, workflow_status, resource_status, force |
| 1772 | ) |
| 1773 | if force_delete_status: |
| 1774 | return |
| 1775 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1776 | if resource_status: |
| 1777 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1778 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 1779 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1780 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1781 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 1782 | self.logger.info( |
| 1783 | f"Infra Controller Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1784 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1785 | return |
| 1786 | |
| 1787 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1788 | class K8sInfraConfigLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1789 | db_collection = "k8sinfra_config" |
| 1790 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1791 | def __init__(self, msg, lcm_tasks, config): |
| 1792 | """ |
| 1793 | Init, Connect to database, filesystem storage, and messaging |
| 1794 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1795 | :return: None |
| 1796 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1797 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1798 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1799 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1800 | self.logger.info("Infra config Create Enter") |
| 1801 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1802 | op_id = params["operation_id"] |
| 1803 | profile_id = params["profile_id"] |
| 1804 | |
| 1805 | # To initialize the operation states |
| 1806 | self.initialize_operation(profile_id, op_id) |
| 1807 | |
| 1808 | content = self.db.get_one("k8sinfra_config", {"_id": profile_id}) |
| 1809 | content["profile_type"] = "infra-configs" |
| 1810 | op_params = self.get_operation_params(content, op_id) |
| 1811 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| 1812 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1813 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1814 | "create_profile", op_id, op_params, content |
| 1815 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1816 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1817 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1818 | workflow_status = await self.check_workflow_and_update_db( |
| 1819 | op_id, workflow_name, content |
| 1820 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1821 | |
| 1822 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1823 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1824 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1825 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1826 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1827 | self.logger.info( |
| 1828 | f"Infra Config Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1829 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1830 | return |
| 1831 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1832 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1833 | self.logger.info("Infra config delete Enter") |
| 1834 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1835 | op_id = params["operation_id"] |
| 1836 | profile_id = params["profile_id"] |
| 1837 | |
| 1838 | # To initialize the operation states |
| 1839 | self.initialize_operation(profile_id, op_id) |
| 1840 | |
| 1841 | content = self.db.get_one("k8sinfra_config", {"_id": profile_id}) |
| 1842 | op_params = self.get_operation_params(content, op_id) |
| 1843 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1844 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1845 | "delete_profile", op_id, op_params, content |
| 1846 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1847 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1848 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1849 | workflow_status = await self.check_workflow_and_update_db( |
| 1850 | op_id, workflow_name, content |
| 1851 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1852 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1853 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1854 | resource_status, content = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1855 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1856 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1857 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1858 | force = params.get("force", False) |
| 1859 | if force: |
| 1860 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1861 | profile_id, workflow_status, resource_status, force |
| 1862 | ) |
| 1863 | if force_delete_status: |
| 1864 | return |
| 1865 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1866 | if resource_status: |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1867 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1868 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 1869 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1870 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1871 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 1872 | self.logger.info( |
| 1873 | f"Infra Config Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1874 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1875 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1876 | return |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1877 | |
| 1878 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1879 | class OkaLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1880 | db_collection = "okas" |
| 1881 | |
| 1882 | def __init__(self, msg, lcm_tasks, config): |
| 1883 | """ |
| 1884 | Init, Connect to database, filesystem storage, and messaging |
| 1885 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1886 | :return: None |
| 1887 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1888 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1889 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1890 | async def create(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1891 | self.logger.info("OKA Create Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1892 | op_id = params["operation_id"] |
| 1893 | oka_id = params["oka_id"] |
| 1894 | self.initialize_operation(oka_id, op_id) |
| 1895 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 1896 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1897 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1898 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1899 | "create_oka", op_id, op_params, db_content |
| 1900 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1901 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1902 | workflow_status = await self.check_workflow_and_update_db( |
| 1903 | op_id, workflow_name, db_content |
| 1904 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1905 | |
| 1906 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1907 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1908 | "create_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1909 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1910 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 1911 | |
| 1912 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1913 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1914 | "create_oka", op_id, op_params, db_content |
| 1915 | ) |
| 1916 | self.logger.info( |
| 1917 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1918 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1919 | self.logger.info(f"OKA Create Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1920 | return |
| 1921 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1922 | async def edit(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1923 | self.logger.info("OKA Edit Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1924 | op_id = params["operation_id"] |
| 1925 | oka_id = params["oka_id"] |
| 1926 | self.initialize_operation(oka_id, op_id) |
| 1927 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 1928 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1929 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1930 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1931 | "update_oka", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1932 | ) |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1933 | workflow_status = await self.check_workflow_and_update_db( |
| 1934 | op_id, workflow_name, db_content |
| 1935 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1936 | |
| 1937 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1938 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1939 | "update_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1940 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1941 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 1942 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1943 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1944 | "update_oka", op_id, op_params, db_content |
| 1945 | ) |
| 1946 | self.logger.info( |
| 1947 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1948 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1949 | self.logger.info(f"OKA Update Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1950 | return |
| 1951 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1952 | async def delete(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1953 | self.logger.info("OKA delete Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1954 | op_id = params["operation_id"] |
| 1955 | oka_id = params["oka_id"] |
| 1956 | self.initialize_operation(oka_id, op_id) |
| 1957 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 1958 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1959 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1960 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1961 | "delete_oka", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1962 | ) |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1963 | workflow_status = await self.check_workflow_and_update_db( |
| 1964 | op_id, workflow_name, db_content |
| 1965 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1966 | |
| 1967 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1968 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1969 | "delete_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1970 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1971 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1972 | force = params.get("force", False) |
| 1973 | if force: |
| 1974 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1975 | oka_id, workflow_status, resource_status, force |
| 1976 | ) |
| 1977 | if force_delete_status: |
| 1978 | return |
| 1979 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1980 | if resource_status: |
| 1981 | db_content["state"] == "DELETED" |
| 1982 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1983 | self.db.del_one(self.db_collection, {"_id": db_content["_id"]}) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 1984 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1985 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1986 | "delete_oka", op_id, op_params, db_content |
| 1987 | ) |
| 1988 | self.logger.info( |
| 1989 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1990 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1991 | self.logger.info(f"OKA Delete Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1992 | return |
| 1993 | |
| 1994 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1995 | class KsuLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1996 | db_collection = "ksus" |
| 1997 | |
| 1998 | def __init__(self, msg, lcm_tasks, config): |
| 1999 | """ |
| 2000 | Init, Connect to database, filesystem storage, and messaging |
| 2001 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2002 | :return: None |
| 2003 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2004 | super().__init__(msg, lcm_tasks, config) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2005 | self._workflows = { |
| 2006 | "create_ksus": { |
| 2007 | "check_resource_function": self.check_create_ksus, |
| 2008 | }, |
| 2009 | "delete_ksus": { |
| 2010 | "check_resource_function": self.check_delete_ksus, |
| 2011 | }, |
| 2012 | } |
| 2013 | |
| 2014 | def get_dbclusters_from_profile(self, profile_id, profile_type): |
| 2015 | cluster_list = [] |
| 2016 | db_clusters = self.db.get_list("clusters") |
| 2017 | self.logger.info(f"Getting list of clusters for {profile_type} {profile_id}") |
| 2018 | for db_cluster in db_clusters: |
| 2019 | if profile_id in db_cluster.get(profile_type, []): |
| 2020 | self.logger.info( |
| 2021 | f"Profile {profile_id} found in cluster {db_cluster['name']}" |
| 2022 | ) |
| 2023 | cluster_list.append(db_cluster) |
| 2024 | return cluster_list |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2025 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2026 | async def create(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2027 | self.logger.info("ksu Create Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2028 | db_content = [] |
| 2029 | op_params = [] |
| 2030 | op_id = params["operation_id"] |
| 2031 | for ksu_id in params["ksus_list"]: |
| 2032 | self.logger.info("Ksu ID: {}".format(ksu_id)) |
| 2033 | self.initialize_operation(ksu_id, op_id) |
| 2034 | db_ksu = self.db.get_one(self.db_collection, {"_id": ksu_id}) |
| 2035 | self.logger.info("Db KSU: {}".format(db_ksu)) |
| 2036 | db_content.append(db_ksu) |
| 2037 | ksu_params = {} |
| 2038 | ksu_params = self.get_operation_params(db_ksu, op_id) |
| 2039 | self.logger.info("Operation Params: {}".format(ksu_params)) |
| 2040 | # Update ksu_params["profile"] with profile name and age-pubkey |
| 2041 | profile_type = ksu_params["profile"]["profile_type"] |
| 2042 | profile_id = ksu_params["profile"]["_id"] |
| 2043 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2044 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 2045 | # db_profile is decrypted inline |
| 2046 | # No need to use decrypted_copy because db_profile won't be updated. |
| 2047 | self.decrypt_age_keys(db_profile) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2048 | ksu_params["profile"]["name"] = db_profile["name"] |
| 2049 | ksu_params["profile"]["age_pubkey"] = db_profile.get("age_pubkey", "") |
| 2050 | # Update ksu_params["oka"] with sw_catalog_path (when missing) |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2051 | # TODO: remove this in favor of doing it in ODU workflow |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2052 | for oka in ksu_params["oka"]: |
| 2053 | if "sw_catalog_path" not in oka: |
| 2054 | oka_id = oka["_id"] |
| 2055 | db_oka = self.db.get_one("okas", {"_id": oka_id}) |
| yshah | 2f39b8a | 2024-12-19 11:06:24 +0000 | [diff] [blame] | 2056 | oka_type = MAP_PROFILE[ |
| 2057 | db_oka.get("profile_type", "infra_controller_profiles") |
| 2058 | ] |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2059 | oka[ |
| 2060 | "sw_catalog_path" |
| garciadeblas | 29f8bcf | 2025-01-24 14:24:41 +0100 | [diff] [blame] | 2061 | ] = f"{oka_type}/{db_oka['git_name'].lower()}/templates" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2062 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2063 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2064 | # A single workflow is launched for all KSUs |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2065 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2066 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2067 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2068 | # Update workflow status in all KSUs |
| 2069 | wf_status_list = [] |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2070 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2071 | workflow_status = await self.check_workflow_and_update_db( |
| 2072 | op_id, workflow_name, db_ksu |
| 2073 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2074 | wf_status_list.append(workflow_status) |
| 2075 | # Update resource status in all KSUs |
| 2076 | # TODO: Is an operation correct if n KSUs are right and 1 is not OK? |
| 2077 | res_status_list = [] |
| 2078 | for db_ksu, ksu_params, wf_status in zip(db_content, op_params, wf_status_list): |
| 2079 | if wf_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2080 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2081 | "create_ksus", op_id, ksu_params, db_ksu |
| 2082 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2083 | else: |
| 2084 | resource_status = False |
| 2085 | res_status_list.append(resource_status) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2086 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2087 | |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 2088 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2089 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2090 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 2091 | ) |
| 2092 | self.logger.info( |
| 2093 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 2094 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2095 | self.logger.info(f"KSU Create EXIT with Resource Status {res_status_list}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2096 | return |
| 2097 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2098 | async def edit(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2099 | self.logger.info("ksu edit Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2100 | db_content = [] |
| 2101 | op_params = [] |
| 2102 | op_id = params["operation_id"] |
| 2103 | for ksu_id in params["ksus_list"]: |
| 2104 | self.initialize_operation(ksu_id, op_id) |
| 2105 | db_ksu = self.db.get_one("ksus", {"_id": ksu_id}) |
| 2106 | db_content.append(db_ksu) |
| 2107 | ksu_params = {} |
| 2108 | ksu_params = self.get_operation_params(db_ksu, op_id) |
| 2109 | # Update ksu_params["profile"] with profile name and age-pubkey |
| 2110 | profile_type = ksu_params["profile"]["profile_type"] |
| 2111 | profile_id = ksu_params["profile"]["_id"] |
| 2112 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2113 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 2114 | # db_profile is decrypted inline |
| 2115 | # No need to use decrypted_copy because db_profile won't be updated. |
| 2116 | self.decrypt_age_keys(db_profile) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2117 | ksu_params["profile"]["name"] = db_profile["name"] |
| 2118 | ksu_params["profile"]["age_pubkey"] = db_profile.get("age_pubkey", "") |
| 2119 | # Update ksu_params["oka"] with sw_catalog_path (when missing) |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2120 | # TODO: remove this in favor of doing it in ODU workflow |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2121 | for oka in ksu_params["oka"]: |
| 2122 | if "sw_catalog_path" not in oka: |
| 2123 | oka_id = oka["_id"] |
| 2124 | db_oka = self.db.get_one("okas", {"_id": oka_id}) |
| yshah | 2f39b8a | 2024-12-19 11:06:24 +0000 | [diff] [blame] | 2125 | oka_type = MAP_PROFILE[ |
| 2126 | db_oka.get("profile_type", "infra_controller_profiles") |
| 2127 | ] |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2128 | oka[ |
| 2129 | "sw_catalog_path" |
| 2130 | ] = f"{oka_type}/{db_oka['git_name']}/templates" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2131 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2132 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2133 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2134 | "update_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2135 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2136 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2137 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2138 | workflow_status = await self.check_workflow_and_update_db( |
| 2139 | op_id, workflow_name, db_ksu |
| 2140 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2141 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2142 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2143 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2144 | "update_ksus", op_id, ksu_params, db_ksu |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2145 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2146 | db_ksu["name"] = ksu_params["name"] |
| 2147 | db_ksu["description"] = ksu_params["description"] |
| 2148 | db_ksu["profile"]["profile_type"] = ksu_params["profile"][ |
| 2149 | "profile_type" |
| 2150 | ] |
| 2151 | db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"] |
| 2152 | db_ksu["oka"] = ksu_params["oka"] |
| 2153 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2154 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2155 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2156 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 2157 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2158 | ) |
| 2159 | self.logger.info( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2160 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2161 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2162 | self.logger.info(f"KSU Update EXIT with Resource Status {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2163 | return |
| 2164 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2165 | async def delete(self, params, order_id): |
| 2166 | self.logger.info("ksu delete Enter") |
| 2167 | db_content = [] |
| 2168 | op_params = [] |
| 2169 | op_id = params["operation_id"] |
| 2170 | for ksu_id in params["ksus_list"]: |
| 2171 | self.initialize_operation(ksu_id, op_id) |
| 2172 | db_ksu = self.db.get_one("ksus", {"_id": ksu_id}) |
| 2173 | db_content.append(db_ksu) |
| 2174 | ksu_params = {} |
| 2175 | ksu_params["profile"] = {} |
| 2176 | ksu_params["profile"]["profile_type"] = db_ksu["profile"]["profile_type"] |
| 2177 | ksu_params["profile"]["_id"] = db_ksu["profile"]["_id"] |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame^] | 2178 | # Update ksu_params["profile"] with profile name |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2179 | profile_type = ksu_params["profile"]["profile_type"] |
| 2180 | profile_id = ksu_params["profile"]["_id"] |
| 2181 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2182 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 2183 | ksu_params["profile"]["name"] = db_profile["name"] |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2184 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2185 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2186 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2187 | "delete_ksus", op_id, op_params, db_content |
| 2188 | ) |
| 2189 | |
| 2190 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2191 | workflow_status = await self.check_workflow_and_update_db( |
| 2192 | op_id, workflow_name, db_ksu |
| 2193 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2194 | |
| 2195 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2196 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2197 | "delete_ksus", op_id, ksu_params, db_ksu |
| 2198 | ) |
| 2199 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2200 | force = params.get("force", False) |
| 2201 | if force: |
| 2202 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2203 | db_ksu["_id"], workflow_status, resource_status, force |
| 2204 | ) |
| 2205 | if force_delete_status: |
| 2206 | return |
| 2207 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2208 | if resource_status: |
| 2209 | db_ksu["state"] == "DELETED" |
| 2210 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2211 | self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]}) |
| 2212 | |
| 2213 | self.logger.info(f"KSU Delete Exit with resource status: {resource_status}") |
| 2214 | return |
| 2215 | |
| 2216 | async def clone(self, params, order_id): |
| 2217 | self.logger.info("ksu clone Enter") |
| 2218 | op_id = params["operation_id"] |
| 2219 | ksus_id = params["ksus_list"][0] |
| 2220 | self.initialize_operation(ksus_id, op_id) |
| 2221 | db_content = self.db.get_one(self.db_collection, {"_id": ksus_id}) |
| 2222 | op_params = self.get_operation_params(db_content, op_id) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2223 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2224 | "clone_ksus", op_id, op_params, db_content |
| 2225 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2226 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2227 | workflow_status = await self.check_workflow_and_update_db( |
| 2228 | op_id, workflow_name, db_content |
| 2229 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2230 | |
| 2231 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2232 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2233 | "clone_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2234 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2235 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2236 | |
| 2237 | self.logger.info(f"KSU Clone Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2238 | return |
| 2239 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2240 | async def move(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2241 | self.logger.info("ksu move Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2242 | op_id = params["operation_id"] |
| 2243 | ksus_id = params["ksus_list"][0] |
| 2244 | self.initialize_operation(ksus_id, op_id) |
| 2245 | db_content = self.db.get_one(self.db_collection, {"_id": ksus_id}) |
| 2246 | op_params = self.get_operation_params(db_content, op_id) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2247 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2248 | "move_ksus", op_id, op_params, db_content |
| 2249 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2250 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2251 | workflow_status = await self.check_workflow_and_update_db( |
| 2252 | op_id, workflow_name, db_content |
| 2253 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2254 | |
| 2255 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2256 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2257 | "move_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2258 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2259 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2260 | |
| 2261 | self.logger.info(f"KSU Move Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2262 | return |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2263 | |
| 2264 | async def check_create_ksus(self, op_id, op_params, content): |
| 2265 | self.logger.info(f"check_create_ksus Operation {op_id}. Params: {op_params}.") |
| 2266 | self.logger.debug(f"Content: {content}") |
| 2267 | db_ksu = content |
| 2268 | kustomization_name = db_ksu["git_name"].lower() |
| 2269 | oka_list = op_params["oka"] |
| 2270 | oka_item = oka_list[0] |
| 2271 | oka_params = oka_item.get("transformation", {}) |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2272 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2273 | profile_id = op_params.get("profile", {}).get("_id") |
| 2274 | profile_type = op_params.get("profile", {}).get("profile_type") |
| 2275 | self.logger.info( |
| 2276 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2277 | ) |
| 2278 | dbcluster_list = self.get_dbclusters_from_profile(profile_id, profile_type) |
| 2279 | if not dbcluster_list: |
| 2280 | self.logger.info(f"No clusters found for profile {profile_id}.") |
| 2281 | for db_cluster in dbcluster_list: |
| 2282 | try: |
| 2283 | self.logger.info( |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 2284 | f"Checking status of KSU {db_ksu['name']} in cluster {db_cluster['name']}." |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2285 | ) |
| 2286 | cluster_kubectl = self.cluster_kubectl(db_cluster) |
| 2287 | checkings_list = [ |
| 2288 | { |
| 2289 | "item": "kustomization", |
| 2290 | "name": kustomization_name, |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2291 | "namespace": kustomization_ns, |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 2292 | "condition": { |
| 2293 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 2294 | "value": "True", |
| 2295 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2296 | "timeout": self._checkloop_kustomization_timeout, |
| 2297 | "enable": True, |
| 2298 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 2299 | }, |
| 2300 | ] |
| 2301 | self.logger.info( |
| 2302 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2303 | ) |
| 2304 | result, message = await self.common_check_list( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 2305 | op_id, checkings_list, "ksus", db_ksu, kubectl_obj=cluster_kubectl |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2306 | ) |
| 2307 | if not result: |
| 2308 | return False, message |
| 2309 | except Exception as e: |
| 2310 | self.logger.error( |
| 2311 | f"Error checking KSU in cluster {db_cluster['name']}." |
| 2312 | ) |
| 2313 | self.logger.error(e) |
| 2314 | return False, f"Error checking KSU in cluster {db_cluster['name']}." |
| 2315 | return True, "OK" |
| 2316 | |
| 2317 | async def check_delete_ksus(self, op_id, op_params, content): |
| 2318 | self.logger.info(f"check_delete_ksus Operation {op_id}. Params: {op_params}.") |
| 2319 | self.logger.debug(f"Content: {content}") |
| 2320 | db_ksu = content |
| 2321 | kustomization_name = db_ksu["git_name"].lower() |
| 2322 | oka_list = db_ksu["oka"] |
| 2323 | oka_item = oka_list[0] |
| 2324 | oka_params = oka_item.get("transformation", {}) |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2325 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2326 | profile_id = op_params.get("profile", {}).get("_id") |
| 2327 | profile_type = op_params.get("profile", {}).get("profile_type") |
| 2328 | self.logger.info( |
| 2329 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2330 | ) |
| 2331 | dbcluster_list = self.get_dbclusters_from_profile(profile_id, profile_type) |
| 2332 | if not dbcluster_list: |
| 2333 | self.logger.info(f"No clusters found for profile {profile_id}.") |
| 2334 | for db_cluster in dbcluster_list: |
| 2335 | try: |
| 2336 | self.logger.info( |
| 2337 | f"Checking status of KSU in cluster {db_cluster['name']}." |
| 2338 | ) |
| 2339 | cluster_kubectl = self.cluster_kubectl(db_cluster) |
| 2340 | checkings_list = [ |
| 2341 | { |
| 2342 | "item": "kustomization", |
| 2343 | "name": kustomization_name, |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2344 | "namespace": kustomization_ns, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2345 | "deleted": True, |
| 2346 | "timeout": self._checkloop_kustomization_timeout, |
| 2347 | "enable": True, |
| 2348 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_DELETED", |
| 2349 | }, |
| 2350 | ] |
| 2351 | self.logger.info( |
| 2352 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2353 | ) |
| 2354 | result, message = await self.common_check_list( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 2355 | op_id, checkings_list, "ksus", db_ksu, kubectl_obj=cluster_kubectl |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2356 | ) |
| 2357 | if not result: |
| 2358 | return False, message |
| 2359 | except Exception as e: |
| 2360 | self.logger.error( |
| 2361 | f"Error checking KSU in cluster {db_cluster['name']}." |
| 2362 | ) |
| 2363 | self.logger.error(e) |
| 2364 | return False, f"Error checking KSU in cluster {db_cluster['name']}." |
| 2365 | return True, "OK" |