| 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 | 5e10915 | 2025-05-19 12:29:01 +0000 | [diff] [blame] | 313 | def delete_ksu_dependency(self, _id, data): |
| 314 | used_oka = [] |
| 315 | existing_oka = [] |
| 316 | |
| 317 | for oka_data in data["oka"]: |
| 318 | if oka_data.get("_id"): |
| 319 | used_oka.append(oka_data["_id"]) |
| 320 | |
| 321 | all_ksu_data = self.db.get_list("ksus", {}) |
| 322 | for ksu_data in all_ksu_data: |
| 323 | if ksu_data["_id"] != _id: |
| 324 | for oka_data in ksu_data["oka"]: |
| 325 | if oka_data.get("_id"): |
| 326 | if oka_data["_id"] not in existing_oka: |
| 327 | existing_oka.append(oka_data["_id"]) |
| 328 | |
| 329 | self.logger.info(f"Used OKA: {used_oka}") |
| 330 | self.logger.info(f"Existing OKA: {existing_oka}") |
| 331 | |
| 332 | for oka_id in used_oka: |
| 333 | if oka_id not in existing_oka: |
| 334 | self.db.set_one( |
| 335 | "okas", {"_id": oka_id}, {"_admin.usageState": "NOT_IN_USE"} |
| 336 | ) |
| 337 | |
| 338 | return |
| 339 | |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 340 | def delete_profile_ksu(self, _id, profile_type): |
| 341 | filter_q = {"profile": {"_id": _id, "profile_type": profile_type}} |
| 342 | ksu_list = self.db.get_list("ksus", filter_q) |
| yshah | 5e10915 | 2025-05-19 12:29:01 +0000 | [diff] [blame] | 343 | for ksu_data in ksu_list: |
| 344 | self.delete_ksu_dependency(ksu_data["_id"], ksu_data) |
| 345 | |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 346 | if ksu_list: |
| 347 | self.db.del_list("ksus", filter_q) |
| 348 | return |
| 349 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 350 | def cluster_kubectl(self, db_cluster): |
| 351 | cluster_kubeconfig = db_cluster["credentials"] |
| 352 | kubeconfig_path = f"/tmp/{db_cluster['_id']}_kubeconfig.yaml" |
| 353 | with open(kubeconfig_path, "w") as kubeconfig_file: |
| 354 | yaml.safe_dump(cluster_kubeconfig, kubeconfig_file) |
| 355 | return Kubectl(config_file=kubeconfig_path) |
| 356 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 357 | |
| yshah | 83a3057 | 2025-06-13 08:38:49 +0000 | [diff] [blame] | 358 | class NodeGroupLcm(GitOpsLcm): |
| 359 | db_collection = "nodegroups" |
| 360 | |
| 361 | def __init__(self, msg, lcm_tasks, config): |
| 362 | """ |
| 363 | Init, Connect to database, filesystem storage, and messaging |
| 364 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 365 | :return: None |
| 366 | """ |
| 367 | super().__init__(msg, lcm_tasks, config) |
| 368 | self._workflows = { |
| 369 | "add_nodegroup": { |
| 370 | "check_resource_function": self.check_add_nodegroup, |
| 371 | }, |
| 372 | "scale_nodegroup": { |
| 373 | "check_resource_function": self.check_scale_nodegroup, |
| 374 | }, |
| 375 | "delete_nodegroup": { |
| 376 | "check_resource_function": self.check_delete_nodegroup, |
| 377 | }, |
| 378 | } |
| 379 | |
| 380 | async def create(self, params, order_id): |
| 381 | self.logger.info("Add NodeGroup Enter") |
| 382 | |
| 383 | # To get the nodegroup and op ids |
| 384 | nodegroup_id = params["nodegroup_id"] |
| 385 | op_id = params["operation_id"] |
| 386 | |
| 387 | # To initialize the operation states |
| 388 | self.initialize_operation(nodegroup_id, op_id) |
| 389 | |
| 390 | # To get the nodegroup details and control plane from DB |
| 391 | db_nodegroup = self.db.get_one(self.db_collection, {"_id": nodegroup_id}) |
| 392 | db_cluster = self.db.get_one("clusters", {"_id": db_nodegroup["cluster_id"]}) |
| 393 | |
| 394 | # To get the operation params details |
| 395 | op_params = self.get_operation_params(db_nodegroup, op_id) |
| 396 | self.logger.info(f"Operations Params: {op_params}") |
| 397 | |
| 398 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| 399 | |
| 400 | # To copy the cluster content and decrypting fields to use in workflows |
| 401 | workflow_content = { |
| 402 | "nodegroup": db_nodegroup, |
| 403 | "cluster": db_cluster, |
| 404 | "vim_account": db_vim, |
| 405 | } |
| 406 | self.logger.info(f"Workflow content: {workflow_content}") |
| 407 | |
| 408 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| 409 | "add_nodegroup", op_id, op_params, workflow_content |
| 410 | ) |
| 411 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| 412 | |
| 413 | workflow_status = await self.check_workflow_and_update_db( |
| 414 | op_id, workflow_name, db_nodegroup |
| 415 | ) |
| 416 | |
| 417 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 418 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 419 | "add_nodegroup", op_id, op_params, workflow_content |
| 420 | ) |
| 421 | self.logger.info( |
| 422 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 423 | ) |
| 424 | if workflow_status: |
| 425 | resource_status, content = await self.check_resource_and_update_db( |
| 426 | "add_nodegroup", op_id, op_params, db_nodegroup |
| 427 | ) |
| 428 | self.db.set_one(self.db_collection, {"_id": db_nodegroup["_id"]}, db_nodegroup) |
| 429 | self.logger.info(f"Add NodeGroup Exit with resource status: {resource_status}") |
| 430 | return |
| 431 | |
| 432 | async def check_add_nodegroup(self, op_id, op_params, content): |
| 433 | self.logger.info(f"check_add_nodegroup Operation {op_id}. Params: {op_params}.") |
| 434 | self.logger.info(f"Content: {content}") |
| 435 | db_nodegroup = content |
| 436 | nodegroup_name = db_nodegroup["git_name"].lower() |
| 437 | nodegroup_kustomization_name = nodegroup_name |
| 438 | checkings_list = [ |
| 439 | { |
| 440 | "item": "kustomization", |
| 441 | "name": nodegroup_kustomization_name, |
| 442 | "namespace": "managed-resources", |
| 443 | "condition": { |
| 444 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 445 | "value": "True", |
| 446 | }, |
| 447 | "timeout": self._checkloop_kustomization_timeout, |
| 448 | "enable": True, |
| 449 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 450 | }, |
| 451 | { |
| yshah | 4a2abbe | 2025-09-23 09:58:27 +0000 | [diff] [blame^] | 452 | "item": "nodegroup_aws", |
| yshah | 83a3057 | 2025-06-13 08:38:49 +0000 | [diff] [blame] | 453 | "name": nodegroup_name, |
| 454 | "namespace": "", |
| 455 | "condition": { |
| 456 | "jsonpath_filter": "status.conditions[?(@.type=='Synced')].status", |
| 457 | "value": "True", |
| 458 | }, |
| 459 | "timeout": self._checkloop_resource_timeout, |
| 460 | "enable": True, |
| 461 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.NODEGROUP", |
| 462 | }, |
| 463 | { |
| yshah | 4a2abbe | 2025-09-23 09:58:27 +0000 | [diff] [blame^] | 464 | "item": "nodegroup_aws", |
| yshah | 83a3057 | 2025-06-13 08:38:49 +0000 | [diff] [blame] | 465 | "name": nodegroup_name, |
| 466 | "namespace": "", |
| 467 | "condition": { |
| 468 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 469 | "value": "True", |
| 470 | }, |
| 471 | "timeout": self._checkloop_resource_timeout, |
| 472 | "enable": True, |
| 473 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEGROUP", |
| 474 | }, |
| 475 | ] |
| 476 | self.logger.info(f"Checking list: {checkings_list}") |
| 477 | result, message = await self.common_check_list( |
| 478 | op_id, checkings_list, "nodegroups", db_nodegroup |
| 479 | ) |
| 480 | if not result: |
| 481 | return False, message |
| 482 | return True, "OK" |
| 483 | |
| 484 | async def scale(self, params, order_id): |
| 485 | self.logger.info("Scale nodegroup Enter") |
| 486 | |
| 487 | op_id = params["operation_id"] |
| 488 | nodegroup_id = params["nodegroup_id"] |
| 489 | |
| 490 | # To initialize the operation states |
| 491 | self.initialize_operation(nodegroup_id, op_id) |
| 492 | |
| 493 | db_nodegroup = self.db.get_one(self.db_collection, {"_id": nodegroup_id}) |
| 494 | db_cluster = self.db.get_one("clusters", {"_id": db_nodegroup["cluster_id"]}) |
| 495 | op_params = self.get_operation_params(db_nodegroup, op_id) |
| 496 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| 497 | |
| 498 | workflow_content = { |
| 499 | "nodegroup": db_nodegroup, |
| 500 | "cluster": db_cluster, |
| 501 | "vim_account": db_vim, |
| 502 | } |
| 503 | self.logger.info(f"Workflow content: {workflow_content}") |
| 504 | |
| 505 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| 506 | "scale_nodegroup", op_id, op_params, workflow_content |
| 507 | ) |
| 508 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| 509 | |
| 510 | workflow_status = await self.check_workflow_and_update_db( |
| 511 | op_id, workflow_name, db_nodegroup |
| 512 | ) |
| 513 | |
| 514 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 515 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 516 | "scale_nodegroup", op_id, op_params, workflow_content |
| 517 | ) |
| 518 | self.logger.info( |
| 519 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 520 | ) |
| 521 | |
| 522 | if workflow_status: |
| 523 | resource_status, content = await self.check_resource_and_update_db( |
| 524 | "scale_nodegroup", op_id, op_params, db_nodegroup |
| 525 | ) |
| 526 | |
| 527 | if resource_status: |
| 528 | db_nodegroup["state"] = "READY" |
| 529 | self.db.set_one( |
| 530 | self.db_collection, {"_id": db_nodegroup["_id"]}, db_nodegroup |
| 531 | ) |
| 532 | self.logger.info( |
| 533 | f"Nodegroup Scale Exit with resource status: {resource_status}" |
| 534 | ) |
| 535 | return |
| 536 | |
| 537 | async def check_scale_nodegroup(self, op_id, op_params, content): |
| 538 | self.logger.info( |
| 539 | f"check_scale_nodegroup Operation {op_id}. Params: {op_params}." |
| 540 | ) |
| 541 | self.logger.debug(f"Content: {content}") |
| 542 | db_nodegroup = content |
| 543 | nodegroup_name = db_nodegroup["git_name"].lower() |
| 544 | nodegroup_kustomization_name = nodegroup_name |
| 545 | checkings_list = [ |
| 546 | { |
| 547 | "item": "kustomization", |
| 548 | "name": nodegroup_kustomization_name, |
| 549 | "namespace": "managed-resources", |
| 550 | "condition": { |
| 551 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 552 | "value": "True", |
| 553 | }, |
| 554 | "timeout": self._checkloop_kustomization_timeout, |
| 555 | "enable": True, |
| 556 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 557 | }, |
| 558 | { |
| yshah | 4a2abbe | 2025-09-23 09:58:27 +0000 | [diff] [blame^] | 559 | "item": "nodegroup_aws", |
| yshah | 83a3057 | 2025-06-13 08:38:49 +0000 | [diff] [blame] | 560 | "name": nodegroup_name, |
| 561 | "namespace": "", |
| 562 | "condition": { |
| 563 | "jsonpath_filter": "status.atProvider.scalingConfig[0].desiredSize", |
| 564 | "value": f"{op_params['node_count']}", |
| 565 | }, |
| 566 | "timeout": self._checkloop_resource_timeout, |
| 567 | "enable": True, |
| 568 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.NODEGROUP", |
| 569 | }, |
| 570 | ] |
| 571 | self.logger.info(f"Checking list: {checkings_list}") |
| 572 | return await self.common_check_list( |
| 573 | op_id, checkings_list, "nodegroups", db_nodegroup |
| 574 | ) |
| 575 | |
| 576 | async def delete(self, params, order_id): |
| 577 | self.logger.info("Delete nodegroup Enter") |
| 578 | |
| 579 | op_id = params["operation_id"] |
| 580 | nodegroup_id = params["nodegroup_id"] |
| 581 | |
| 582 | # To initialize the operation states |
| 583 | self.initialize_operation(nodegroup_id, op_id) |
| 584 | |
| 585 | db_nodegroup = self.db.get_one(self.db_collection, {"_id": nodegroup_id}) |
| 586 | db_cluster = self.db.get_one("clusters", {"_id": db_nodegroup["cluster_id"]}) |
| 587 | op_params = self.get_operation_params(db_nodegroup, op_id) |
| 588 | |
| 589 | workflow_content = {"nodegroup": db_nodegroup, "cluster": db_cluster} |
| 590 | |
| 591 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| 592 | "delete_nodegroup", op_id, op_params, workflow_content |
| 593 | ) |
| 594 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| 595 | |
| 596 | workflow_status = await self.check_workflow_and_update_db( |
| 597 | op_id, workflow_name, db_nodegroup |
| 598 | ) |
| 599 | |
| 600 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 601 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 602 | "delete_nodegroup", op_id, op_params, workflow_content |
| 603 | ) |
| 604 | self.logger.info( |
| 605 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 606 | ) |
| 607 | |
| 608 | if workflow_status: |
| 609 | resource_status, content = await self.check_resource_and_update_db( |
| 610 | "delete_nodegroup", op_id, op_params, db_nodegroup |
| 611 | ) |
| 612 | |
| 613 | if resource_status: |
| 614 | node_count = db_cluster.get("node_count") |
| 615 | new_node_count = node_count - 1 |
| 616 | self.logger.info(f"New Node count: {new_node_count}") |
| 617 | db_cluster["node_count"] = new_node_count |
| 618 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 619 | db_nodegroup["state"] = "DELETED" |
| 620 | self.db.set_one( |
| 621 | self.db_collection, {"_id": db_nodegroup["_id"]}, db_nodegroup |
| 622 | ) |
| 623 | self.db.del_one(self.db_collection, {"_id": db_nodegroup["_id"]}) |
| 624 | self.logger.info( |
| 625 | f"Nodegroup Delete Exit with resource status: {resource_status}" |
| 626 | ) |
| 627 | return |
| 628 | |
| 629 | async def check_delete_nodegroup(self, op_id, op_params, content): |
| 630 | self.logger.info( |
| 631 | f"check_delete_nodegroup Operation {op_id}. Params: {op_params}." |
| 632 | ) |
| 633 | db_nodegroup = content |
| 634 | nodegroup_name = db_nodegroup["git_name"].lower() |
| 635 | nodegroup_kustomization_name = nodegroup_name |
| 636 | checkings_list = [ |
| 637 | { |
| 638 | "item": "kustomization", |
| 639 | "name": nodegroup_kustomization_name, |
| 640 | "namespace": "managed-resources", |
| 641 | "deleted": True, |
| 642 | "timeout": self._checkloop_kustomization_timeout, |
| 643 | "enable": True, |
| 644 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_DELETED", |
| 645 | }, |
| 646 | { |
| yshah | 4a2abbe | 2025-09-23 09:58:27 +0000 | [diff] [blame^] | 647 | "item": "nodegroup_aws", |
| yshah | 83a3057 | 2025-06-13 08:38:49 +0000 | [diff] [blame] | 648 | "name": nodegroup_name, |
| 649 | "namespace": "", |
| 650 | "deleted": True, |
| 651 | "timeout": self._checkloop_resource_timeout, |
| 652 | "enable": True, |
| 653 | "resourceState": "IN_PROGRESS.RESOURCE_DELETED.NODEGROUP", |
| 654 | }, |
| 655 | ] |
| 656 | self.logger.info(f"Checking list: {checkings_list}") |
| 657 | return await self.common_check_list( |
| 658 | op_id, checkings_list, "nodegroups", db_nodegroup |
| 659 | ) |
| 660 | |
| 661 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 662 | class ClusterLcm(GitOpsLcm): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 663 | db_collection = "clusters" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 664 | |
| 665 | def __init__(self, msg, lcm_tasks, config): |
| 666 | """ |
| 667 | Init, Connect to database, filesystem storage, and messaging |
| 668 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 669 | :return: None |
| 670 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 671 | super().__init__(msg, lcm_tasks, config) |
| 672 | self._workflows = { |
| 673 | "create_cluster": { |
| 674 | "check_resource_function": self.check_create_cluster, |
| 675 | }, |
| garciadeblas | b0a42c2 | 2024-11-13 16:00:10 +0100 | [diff] [blame] | 676 | "register_cluster": { |
| 677 | "check_resource_function": self.check_register_cluster, |
| 678 | }, |
| 679 | "update_cluster": { |
| 680 | "check_resource_function": self.check_update_cluster, |
| 681 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 682 | "delete_cluster": { |
| 683 | "check_resource_function": self.check_delete_cluster, |
| 684 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 685 | } |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 686 | self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config) |
| 687 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 688 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 689 | self.logger.info("cluster Create Enter") |
| garciadeblas | 8bdb3d4 | 2025-04-04 00:19:13 +0200 | [diff] [blame] | 690 | workflow_status = None |
| 691 | resource_status = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 692 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 693 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 694 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 695 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 696 | |
| 697 | # To initialize the operation states |
| 698 | self.initialize_operation(cluster_id, op_id) |
| 699 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 700 | # To get the cluster |
| 701 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 702 | |
| 703 | # To get the operation params details |
| 704 | op_params = self.get_operation_params(db_cluster, op_id) |
| 705 | |
| 706 | # To copy the cluster content and decrypting fields to use in workflows |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 707 | db_cluster_copy = self.decrypted_copy(db_cluster) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 708 | workflow_content = { |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 709 | "cluster": db_cluster_copy, |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 710 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 711 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 712 | # To get the vim account details |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 713 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 714 | workflow_content["vim_account"] = db_vim |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 715 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 716 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 717 | "create_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 718 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 719 | if not workflow_res: |
| 720 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 721 | db_cluster["state"] = "FAILED_CREATION" |
| 722 | db_cluster["resourceState"] = "ERROR" |
| 723 | db_cluster = self.update_operation_history( |
| 724 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 725 | ) |
| 726 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 727 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 728 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 729 | "create_cluster", op_id, op_params, workflow_content |
| 730 | ) |
| 731 | self.logger.info( |
| 732 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 733 | ) |
| 734 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 735 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 736 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 737 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 738 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 739 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 740 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 741 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 742 | workflow_status, workflow_msg |
| 743 | ) |
| 744 | ) |
| 745 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 746 | db_cluster["state"] = "CREATED" |
| 747 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 748 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 749 | db_cluster["state"] = "FAILED_CREATION" |
| 750 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 751 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 752 | db_cluster = self.update_operation_history( |
| 753 | db_cluster, op_id, workflow_status, None |
| 754 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 755 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 756 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 757 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 758 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 759 | "create_cluster", op_id, op_params, workflow_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 760 | ) |
| 761 | self.logger.info( |
| 762 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 763 | ) |
| 764 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 765 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 766 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 767 | "create_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 768 | ) |
| 769 | self.logger.info( |
| 770 | "resource_status is :{} and resource_msg is :{}".format( |
| 771 | resource_status, resource_msg |
| 772 | ) |
| 773 | ) |
| 774 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 775 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 776 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 777 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 778 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 779 | db_cluster["operatingState"] = "IDLE" |
| 780 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 781 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 782 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 783 | db_cluster["current_operation"] = None |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 784 | |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 785 | # Retrieve credentials and subnets and register the cluster in k8sclusters collection |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 786 | cluster_creds = None |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 787 | db_register = self.db.get_one("k8sclusters", {"name": db_cluster["name"]}) |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 788 | if db_cluster["resourceState"] == "READY" and db_cluster["state"] == "CREATED": |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 789 | # Retrieve credentials |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 790 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 791 | # TODO: manage the case where the credentials are not available |
| 792 | if result: |
| 793 | db_cluster["credentials"] = cluster_creds |
| 794 | |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 795 | # Retrieve subnets |
| yshah | 1f070ba | 2025-09-23 09:47:12 +0000 | [diff] [blame] | 796 | if op_params.get("private_subnet") and op_params.get("public_subnet"): |
| 797 | db_cluster["private_subnet"] = op_params["private_subnet"] |
| 798 | db_cluster["public_subnet"] = op_params["public_subnet"] |
| 799 | else: |
| 800 | if db_vim["vim_type"] == "aws": |
| 801 | generic_object = await self.odu.list_object( |
| 802 | api_group="ec2.aws.upbound.io", |
| 803 | api_plural="subnets", |
| 804 | api_version="v1beta1", |
| 805 | ) |
| 806 | private_subnet = [] |
| 807 | public_subnet = [] |
| 808 | for subnet in generic_object: |
| 809 | labels = subnet.get("metadata", {}).get("labels", {}) |
| 810 | status = subnet.get("status", {}).get("atProvider", {}) |
| 811 | # Extract relevant label values |
| 812 | cluster_label = labels.get("cluster") |
| 813 | access_label = labels.get("access") |
| 814 | subnet_id = status.get("id") |
| 815 | # Apply filtering |
| 816 | if cluster_label == db_cluster["name"] and subnet_id: |
| 817 | if access_label == "private": |
| 818 | private_subnet.append(subnet_id) |
| 819 | elif access_label == "public": |
| 820 | public_subnet.append(subnet_id) |
| 821 | # Update db_cluster |
| 822 | db_cluster["private_subnet"] = private_subnet |
| 823 | db_cluster["public_subnet"] = public_subnet |
| 824 | self.logger.info("DB cluster: {}".format(db_cluster)) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 825 | |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 826 | # Register the cluster in k8sclusters collection |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 827 | db_register["credentials"] = cluster_creds |
| garciadeblas | 41a600e | 2025-01-21 11:49:38 +0100 | [diff] [blame] | 828 | # To call the lcm.py for registering the cluster in k8scluster lcm. |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 829 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 830 | register = await self.regist.create(db_register, order_id) |
| 831 | self.logger.debug(f"Register is : {register}") |
| 832 | else: |
| 833 | db_register["_admin"]["operationalState"] = "ERROR" |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 834 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 835 | |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 836 | # Update db_cluster |
| 837 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 838 | self.update_default_profile_agekeys(db_cluster_copy) |
| 839 | self.update_profile_state(db_cluster, workflow_status, resource_status) |
| 840 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 841 | return |
| 842 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 843 | async def check_create_cluster(self, op_id, op_params, content): |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 844 | self.logger.info( |
| 845 | f"check_create_cluster Operation {op_id}. Params: {op_params}." |
| 846 | ) |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 847 | db_cluster = content["cluster"] |
| 848 | cluster_name = db_cluster["git_name"].lower() |
| 849 | cluster_kustomization_name = cluster_name |
| 850 | db_vim_account = content["vim_account"] |
| 851 | cloud_type = db_vim_account["vim_type"] |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 852 | nodegroup_name = "" |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 853 | if cloud_type == "aws": |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 854 | nodegroup_name = f"{cluster_name}-nodegroup" |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 855 | cluster_name = f"{cluster_name}-cluster" |
| 856 | elif cloud_type == "gcp": |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 857 | nodegroup_name = f"nodepool-{cluster_name}" |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 858 | bootstrap = op_params.get("bootstrap", True) |
| 859 | if cloud_type in ("azure", "gcp", "aws"): |
| 860 | checkings_list = [ |
| 861 | { |
| 862 | "item": "kustomization", |
| 863 | "name": cluster_kustomization_name, |
| 864 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 865 | "condition": { |
| 866 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 867 | "value": "True", |
| 868 | }, |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 869 | "timeout": 1500, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 870 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 871 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 872 | }, |
| 873 | { |
| 874 | "item": f"cluster_{cloud_type}", |
| 875 | "name": cluster_name, |
| 876 | "namespace": "", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 877 | "condition": { |
| 878 | "jsonpath_filter": "status.conditions[?(@.type=='Synced')].status", |
| 879 | "value": "True", |
| 880 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 881 | "timeout": self._checkloop_resource_timeout, |
| 882 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 883 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 884 | }, |
| 885 | { |
| 886 | "item": f"cluster_{cloud_type}", |
| 887 | "name": cluster_name, |
| 888 | "namespace": "", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 889 | "condition": { |
| 890 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 891 | "value": "True", |
| 892 | }, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 893 | "timeout": self._checkloop_resource_timeout, |
| 894 | "enable": True, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 895 | "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 896 | }, |
| 897 | { |
| 898 | "item": "kustomization", |
| 899 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 900 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 901 | "condition": { |
| 902 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 903 | "value": "True", |
| 904 | }, |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 905 | "timeout": self._checkloop_resource_timeout, |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 906 | "enable": bootstrap, |
| garciadeblas | 7eae6f4 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 907 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 908 | }, |
| 909 | ] |
| 910 | else: |
| 911 | return False, "Not suitable VIM account to check cluster status" |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 912 | if cloud_type != "aws": |
| 913 | if nodegroup_name: |
| 914 | nodegroup_check = { |
| 915 | "item": f"nodegroup_{cloud_type}", |
| 916 | "name": nodegroup_name, |
| 917 | "namespace": "", |
| 918 | "condition": { |
| 919 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 920 | "value": "True", |
| 921 | }, |
| 922 | "timeout": self._checkloop_resource_timeout, |
| 923 | "enable": True, |
| 924 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEGROUP", |
| 925 | } |
| 926 | checkings_list.insert(3, nodegroup_check) |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 927 | return await self.common_check_list( |
| 928 | op_id, checkings_list, "clusters", db_cluster |
| 929 | ) |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 930 | |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 931 | def update_default_profile_agekeys(self, db_cluster): |
| 932 | profiles = [ |
| 933 | "infra_controller_profiles", |
| 934 | "infra_config_profiles", |
| 935 | "app_profiles", |
| 936 | "resource_profiles", |
| 937 | ] |
| 938 | self.logger.debug("the db_cluster is :{}".format(db_cluster)) |
| 939 | for profile_type in profiles: |
| 940 | profile_id = db_cluster[profile_type] |
| 941 | db_collection = self.profile_collection_mapping[profile_type] |
| 942 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| 943 | db_profile["age_pubkey"] = db_cluster["age_pubkey"] |
| 944 | db_profile["age_privkey"] = db_cluster["age_privkey"] |
| 945 | self.encrypt_age_keys(db_profile) |
| 946 | self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile) |
| 947 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 948 | def update_profile_state(self, db_cluster, workflow_status, resource_status): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 949 | profiles = [ |
| 950 | "infra_controller_profiles", |
| 951 | "infra_config_profiles", |
| 952 | "app_profiles", |
| 953 | "resource_profiles", |
| 954 | ] |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 955 | self.logger.debug("the db_cluster is :{}".format(db_cluster)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 956 | for profile_type in profiles: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 957 | profile_id = db_cluster[profile_type] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 958 | db_collection = self.profile_collection_mapping[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 959 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 960 | op_id = db_profile["operationHistory"][-1].get("op_id") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 961 | db_profile["state"] = db_cluster["state"] |
| 962 | db_profile["resourceState"] = db_cluster["resourceState"] |
| 963 | db_profile["operatingState"] = db_cluster["operatingState"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 964 | db_profile = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 965 | db_profile, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 966 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 967 | self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile) |
| 968 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 969 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 970 | self.logger.info("cluster delete Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 971 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 972 | try: |
| 973 | # To get the cluster and op ids |
| 974 | cluster_id = params["cluster_id"] |
| 975 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 976 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 977 | # To initialize the operation states |
| 978 | self.initialize_operation(cluster_id, op_id) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 979 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 980 | # To get the cluster |
| 981 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 982 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 983 | # To get the operation params details |
| 984 | op_params = self.get_operation_params(db_cluster, op_id) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 985 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 986 | # To copy the cluster content and decrypting fields to use in workflows |
| 987 | workflow_content = { |
| 988 | "cluster": self.decrypted_copy(db_cluster), |
| 989 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 990 | |
| garciadeblas | 926ffac | 2025-02-12 16:45:40 +0100 | [diff] [blame] | 991 | # To get the vim account details |
| 992 | db_vim = self.db.get_one( |
| 993 | "vim_accounts", {"name": db_cluster["vim_account"]} |
| 994 | ) |
| 995 | workflow_content["vim_account"] = db_vim |
| 996 | except Exception as e: |
| 997 | self.logger.debug(traceback.format_exc()) |
| 998 | self.logger.debug(f"Exception: {e}", exc_info=True) |
| 999 | raise e |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1000 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1001 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1002 | "delete_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1003 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1004 | if not workflow_res: |
| 1005 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1006 | db_cluster["state"] = "FAILED_DELETION" |
| 1007 | db_cluster["resourceState"] = "ERROR" |
| 1008 | db_cluster = self.update_operation_history( |
| 1009 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1010 | ) |
| 1011 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1012 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1013 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1014 | "delete_cluster", op_id, op_params, workflow_content |
| 1015 | ) |
| 1016 | self.logger.info( |
| 1017 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1018 | ) |
| 1019 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1020 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1021 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1022 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1023 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1024 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1025 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1026 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1027 | workflow_status, workflow_msg |
| 1028 | ) |
| 1029 | ) |
| 1030 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1031 | db_cluster["state"] = "DELETED" |
| 1032 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1033 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1034 | db_cluster["state"] = "FAILED_DELETION" |
| 1035 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1036 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1037 | db_cluster = self.update_operation_history( |
| 1038 | db_cluster, op_id, workflow_status, None |
| 1039 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1040 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1041 | |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 1042 | # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded |
| 1043 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1044 | "delete_cluster", op_id, op_params, workflow_content |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 1045 | ) |
| 1046 | self.logger.info( |
| 1047 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1048 | ) |
| 1049 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1050 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1051 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1052 | "delete_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1053 | ) |
| 1054 | self.logger.info( |
| 1055 | "resource_status is :{} and resource_msg is :{}".format( |
| 1056 | resource_status, resource_msg |
| 1057 | ) |
| 1058 | ) |
| 1059 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1060 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1061 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1062 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1063 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1064 | db_cluster["operatingState"] = "IDLE" |
| 1065 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1066 | db_cluster, op_id, workflow_status, resource_status |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1067 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1068 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1069 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1070 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1071 | force = params.get("force", False) |
| 1072 | if force: |
| 1073 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1074 | cluster_id, workflow_status, resource_status, force |
| 1075 | ) |
| 1076 | if force_delete_status: |
| 1077 | return |
| 1078 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1079 | # To delete it from DB |
| 1080 | if db_cluster["state"] == "DELETED": |
| 1081 | self.delete_cluster(db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1082 | |
| 1083 | # To delete it from k8scluster collection |
| 1084 | self.db.del_one("k8sclusters", {"name": db_cluster["name"]}) |
| 1085 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1086 | return |
| 1087 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1088 | async def check_delete_cluster(self, op_id, op_params, content): |
| 1089 | self.logger.info( |
| 1090 | f"check_delete_cluster Operation {op_id}. Params: {op_params}." |
| 1091 | ) |
| 1092 | self.logger.debug(f"Content: {content}") |
| 1093 | db_cluster = content["cluster"] |
| 1094 | cluster_name = db_cluster["git_name"].lower() |
| 1095 | cluster_kustomization_name = cluster_name |
| 1096 | db_vim_account = content["vim_account"] |
| 1097 | cloud_type = db_vim_account["vim_type"] |
| 1098 | if cloud_type == "aws": |
| 1099 | cluster_name = f"{cluster_name}-cluster" |
| 1100 | if cloud_type in ("azure", "gcp", "aws"): |
| 1101 | checkings_list = [ |
| 1102 | { |
| 1103 | "item": "kustomization", |
| 1104 | "name": cluster_kustomization_name, |
| 1105 | "namespace": "managed-resources", |
| 1106 | "deleted": True, |
| 1107 | "timeout": self._checkloop_kustomization_timeout, |
| 1108 | "enable": True, |
| 1109 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_DELETED", |
| 1110 | }, |
| 1111 | { |
| 1112 | "item": f"cluster_{cloud_type}", |
| 1113 | "name": cluster_name, |
| 1114 | "namespace": "", |
| 1115 | "deleted": True, |
| 1116 | "timeout": self._checkloop_resource_timeout, |
| 1117 | "enable": True, |
| 1118 | "resourceState": "IN_PROGRESS.RESOURCE_DELETED.CLUSTER", |
| 1119 | }, |
| 1120 | ] |
| 1121 | else: |
| 1122 | return False, "Not suitable VIM account to check cluster status" |
| 1123 | return await self.common_check_list( |
| 1124 | op_id, checkings_list, "clusters", db_cluster |
| 1125 | ) |
| 1126 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1127 | def delete_cluster(self, db_cluster): |
| 1128 | # Actually, item_content is equal to db_cluster |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1129 | # detach profiles |
| 1130 | update_dict = None |
| 1131 | profiles_to_detach = [ |
| 1132 | "infra_controller_profiles", |
| 1133 | "infra_config_profiles", |
| 1134 | "app_profiles", |
| 1135 | "resource_profiles", |
| 1136 | ] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1137 | """ |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1138 | profiles_collection = { |
| 1139 | "infra_controller_profiles": "k8sinfra_controller", |
| 1140 | "infra_config_profiles": "k8sinfra_config", |
| 1141 | "app_profiles": "k8sapp", |
| 1142 | "resource_profiles": "k8sresource", |
| 1143 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1144 | """ |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1145 | for profile_type in profiles_to_detach: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1146 | if db_cluster.get(profile_type): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1147 | profile_ids = db_cluster[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1148 | profile_ids_copy = deepcopy(profile_ids) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1149 | for profile_id in profile_ids_copy: |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1150 | db_collection = self.profile_collection_mapping[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1151 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| garciadeblas | c255285 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 1152 | self.logger.debug("the db_profile is :{}".format(db_profile)) |
| 1153 | self.logger.debug( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1154 | "the item_content name is :{}".format(db_cluster["name"]) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1155 | ) |
| garciadeblas | c255285 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 1156 | self.logger.debug( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1157 | "the db_profile name is :{}".format(db_profile["name"]) |
| 1158 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1159 | if db_cluster["name"] == db_profile["name"]: |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1160 | self.delete_profile_ksu(profile_id, profile_type) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1161 | self.db.del_one(db_collection, {"_id": profile_id}) |
| 1162 | else: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1163 | profile_ids.remove(profile_id) |
| 1164 | update_dict = {profile_type: profile_ids} |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1165 | self.db.set_one( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1166 | "clusters", {"_id": db_cluster["_id"]}, update_dict |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1167 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1168 | self.db.del_one("clusters", {"_id": db_cluster["_id"]}) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1169 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1170 | async def attach_profile(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1171 | self.logger.info("profile attach Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1172 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1173 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1174 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1175 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1176 | |
| 1177 | # To initialize the operation states |
| 1178 | self.initialize_operation(cluster_id, op_id) |
| 1179 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1180 | # To get the cluster |
| 1181 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1182 | |
| 1183 | # To get the operation params details |
| 1184 | op_params = self.get_operation_params(db_cluster, op_id) |
| 1185 | |
| 1186 | # To copy the cluster content and decrypting fields to use in workflows |
| 1187 | workflow_content = { |
| 1188 | "cluster": self.decrypted_copy(db_cluster), |
| 1189 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1190 | |
| 1191 | # To get the profile details |
| 1192 | profile_id = params["profile_id"] |
| 1193 | profile_type = params["profile_type"] |
| 1194 | profile_collection = self.profile_collection_mapping[profile_type] |
| 1195 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 1196 | db_profile["profile_type"] = profile_type |
| 1197 | # content["profile"] = db_profile |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1198 | workflow_content["profile"] = db_profile |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1199 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1200 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1201 | "attach_profile_to_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1202 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1203 | if not workflow_res: |
| 1204 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1205 | db_cluster["resourceState"] = "ERROR" |
| 1206 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1207 | db_cluster = self.update_operation_history( |
| 1208 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1209 | ) |
| 1210 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1211 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1212 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1213 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1214 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1215 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1216 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1217 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1218 | workflow_status, workflow_msg |
| 1219 | ) |
| 1220 | ) |
| 1221 | if workflow_status: |
| 1222 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1223 | else: |
| 1224 | db_cluster["resourceState"] = "ERROR" |
| 1225 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1226 | db_cluster = self.update_operation_history( |
| 1227 | db_cluster, op_id, workflow_status, None |
| 1228 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1229 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1230 | |
| 1231 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1232 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1233 | "attach_profile_to_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1234 | ) |
| 1235 | self.logger.info( |
| 1236 | "resource_status is :{} and resource_msg is :{}".format( |
| 1237 | resource_status, resource_msg |
| 1238 | ) |
| 1239 | ) |
| 1240 | if resource_status: |
| 1241 | db_cluster["resourceState"] = "READY" |
| 1242 | else: |
| 1243 | db_cluster["resourceState"] = "ERROR" |
| 1244 | |
| 1245 | db_cluster["operatingState"] = "IDLE" |
| 1246 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1247 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1248 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1249 | profile_list = db_cluster[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1250 | if resource_status: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1251 | profile_list.append(profile_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1252 | db_cluster[profile_type] = profile_list |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1253 | db_cluster["current_operation"] = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1254 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1255 | |
| 1256 | return |
| 1257 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1258 | async def detach_profile(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1259 | self.logger.info("profile dettach Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1260 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1261 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1262 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1263 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1264 | |
| 1265 | # To initialize the operation states |
| 1266 | self.initialize_operation(cluster_id, op_id) |
| 1267 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1268 | # To get the cluster |
| 1269 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1270 | |
| 1271 | # To get the operation params details |
| 1272 | op_params = self.get_operation_params(db_cluster, op_id) |
| 1273 | |
| 1274 | # To copy the cluster content and decrypting fields to use in workflows |
| 1275 | workflow_content = { |
| 1276 | "cluster": self.decrypted_copy(db_cluster), |
| 1277 | } |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1278 | |
| 1279 | # To get the profile details |
| 1280 | profile_id = params["profile_id"] |
| 1281 | profile_type = params["profile_type"] |
| 1282 | profile_collection = self.profile_collection_mapping[profile_type] |
| 1283 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 1284 | db_profile["profile_type"] = profile_type |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1285 | workflow_content["profile"] = db_profile |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1286 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1287 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1288 | "detach_profile_from_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1289 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1290 | if not workflow_res: |
| 1291 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1292 | db_cluster["resourceState"] = "ERROR" |
| 1293 | db_cluster = self.update_operation_history( |
| 1294 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1295 | ) |
| 1296 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1297 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1298 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1299 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1300 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1301 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1302 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1303 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1304 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1305 | workflow_status, workflow_msg |
| 1306 | ) |
| 1307 | ) |
| 1308 | if workflow_status: |
| 1309 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1310 | else: |
| 1311 | db_cluster["resourceState"] = "ERROR" |
| 1312 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1313 | db_cluster = self.update_operation_history( |
| 1314 | db_cluster, op_id, workflow_status, None |
| 1315 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1316 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1317 | |
| 1318 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1319 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1320 | "detach_profile_from_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1321 | ) |
| 1322 | self.logger.info( |
| 1323 | "resource_status is :{} and resource_msg is :{}".format( |
| 1324 | resource_status, resource_msg |
| 1325 | ) |
| 1326 | ) |
| 1327 | if resource_status: |
| 1328 | db_cluster["resourceState"] = "READY" |
| 1329 | else: |
| 1330 | db_cluster["resourceState"] = "ERROR" |
| 1331 | |
| 1332 | db_cluster["operatingState"] = "IDLE" |
| 1333 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1334 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1335 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1336 | profile_list = db_cluster[profile_type] |
| 1337 | self.logger.info("profile list is : {}".format(profile_list)) |
| 1338 | if resource_status: |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1339 | profile_list.remove(profile_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1340 | db_cluster[profile_type] = profile_list |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1341 | db_cluster["current_operation"] = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1342 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1343 | |
| 1344 | return |
| 1345 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1346 | async def register(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1347 | self.logger.info("cluster register enter") |
| garciadeblas | 8bdb3d4 | 2025-04-04 00:19:13 +0200 | [diff] [blame] | 1348 | workflow_status = None |
| 1349 | resource_status = None |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1350 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1351 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1352 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1353 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1354 | |
| 1355 | # To initialize the operation states |
| 1356 | self.initialize_operation(cluster_id, op_id) |
| 1357 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1358 | # To get the cluster |
| 1359 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1360 | |
| 1361 | # To get the operation params details |
| 1362 | op_params = self.get_operation_params(db_cluster, op_id) |
| 1363 | |
| 1364 | # To copy the cluster content and decrypting fields to use in workflows |
| garciadeblas | 8bdb3d4 | 2025-04-04 00:19:13 +0200 | [diff] [blame] | 1365 | db_cluster_copy = self.decrypted_copy(db_cluster) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1366 | workflow_content = { |
| garciadeblas | 8bdb3d4 | 2025-04-04 00:19:13 +0200 | [diff] [blame] | 1367 | "cluster": db_cluster_copy, |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1368 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1369 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1370 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1371 | "register_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1372 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1373 | if not workflow_res: |
| 1374 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1375 | db_cluster["state"] = "FAILED_CREATION" |
| 1376 | db_cluster["resourceState"] = "ERROR" |
| 1377 | db_cluster = self.update_operation_history( |
| 1378 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1379 | ) |
| 1380 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1381 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1382 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1383 | "register_cluster", op_id, op_params, workflow_content |
| 1384 | ) |
| 1385 | self.logger.info( |
| 1386 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1387 | ) |
| 1388 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1389 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1390 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1391 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1392 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1393 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1394 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1395 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1396 | workflow_status, workflow_msg |
| 1397 | ) |
| 1398 | ) |
| 1399 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1400 | db_cluster["state"] = "CREATED" |
| 1401 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1402 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1403 | db_cluster["state"] = "FAILED_CREATION" |
| 1404 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1405 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1406 | db_cluster = self.update_operation_history( |
| 1407 | db_cluster, op_id, workflow_status, None |
| 1408 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1409 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1410 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 1411 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1412 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1413 | "register_cluster", op_id, op_params, workflow_content |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 1414 | ) |
| 1415 | self.logger.info( |
| 1416 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1417 | ) |
| 1418 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1419 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1420 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1421 | "register_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1422 | ) |
| 1423 | self.logger.info( |
| 1424 | "resource_status is :{} and resource_msg is :{}".format( |
| 1425 | resource_status, resource_msg |
| 1426 | ) |
| 1427 | ) |
| 1428 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1429 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1430 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1431 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1432 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1433 | db_cluster["operatingState"] = "IDLE" |
| 1434 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1435 | db_cluster, op_id, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1436 | ) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1437 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1438 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1439 | |
| garciadeblas | 8bdb3d4 | 2025-04-04 00:19:13 +0200 | [diff] [blame] | 1440 | # Update default profile agekeys and state |
| 1441 | self.update_default_profile_agekeys(db_cluster_copy) |
| 1442 | self.update_profile_state(db_cluster, workflow_status, resource_status) |
| 1443 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1444 | db_register = self.db.get_one("k8sclusters", {"name": db_cluster["name"]}) |
| 1445 | db_register["credentials"] = db_cluster["credentials"] |
| 1446 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 1447 | |
| 1448 | if db_cluster["resourceState"] == "READY" and db_cluster["state"] == "CREATED": |
| 1449 | # To call the lcm.py for registering the cluster in k8scluster lcm. |
| 1450 | register = await self.regist.create(db_register, order_id) |
| 1451 | self.logger.debug(f"Register is : {register}") |
| 1452 | else: |
| 1453 | db_register["_admin"]["operationalState"] = "ERROR" |
| 1454 | self.db.set_one("k8sclusters", {"_id": db_register["_id"]}, db_register) |
| 1455 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1456 | return |
| 1457 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1458 | async def check_register_cluster(self, op_id, op_params, content): |
| 1459 | self.logger.info( |
| 1460 | f"check_register_cluster Operation {op_id}. Params: {op_params}." |
| 1461 | ) |
| 1462 | # self.logger.debug(f"Content: {content}") |
| 1463 | db_cluster = content["cluster"] |
| 1464 | cluster_name = db_cluster["git_name"].lower() |
| 1465 | cluster_kustomization_name = cluster_name |
| 1466 | bootstrap = op_params.get("bootstrap", True) |
| 1467 | checkings_list = [ |
| 1468 | { |
| 1469 | "item": "kustomization", |
| 1470 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 1471 | "namespace": "managed-resources", |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 1472 | "condition": { |
| 1473 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 1474 | "value": "True", |
| 1475 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1476 | "timeout": self._checkloop_kustomization_timeout, |
| 1477 | "enable": bootstrap, |
| 1478 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| 1479 | }, |
| 1480 | ] |
| 1481 | return await self.common_check_list( |
| 1482 | op_id, checkings_list, "clusters", db_cluster |
| 1483 | ) |
| 1484 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1485 | async def deregister(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1486 | self.logger.info("cluster deregister enter") |
| 1487 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1488 | # To get the cluster and op ids |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1489 | cluster_id = params["cluster_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1490 | op_id = params["operation_id"] |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1491 | |
| 1492 | # To initialize the operation states |
| 1493 | self.initialize_operation(cluster_id, op_id) |
| 1494 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1495 | # To get the cluster |
| 1496 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1497 | |
| 1498 | # To get the operation params details |
| 1499 | op_params = self.get_operation_params(db_cluster, op_id) |
| 1500 | |
| 1501 | # To copy the cluster content and decrypting fields to use in workflows |
| 1502 | workflow_content = { |
| 1503 | "cluster": self.decrypted_copy(db_cluster), |
| 1504 | } |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1505 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1506 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1507 | "deregister_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1508 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1509 | if not workflow_res: |
| 1510 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1511 | db_cluster["state"] = "FAILED_DELETION" |
| 1512 | db_cluster["resourceState"] = "ERROR" |
| 1513 | db_cluster = self.update_operation_history( |
| 1514 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1515 | ) |
| 1516 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1517 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1518 | |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1519 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1520 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1521 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1522 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1523 | self.logger.info( |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1524 | "workflow_status is: {} and workflow_msg is: {}".format( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1525 | workflow_status, workflow_msg |
| 1526 | ) |
| 1527 | ) |
| 1528 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1529 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1530 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1531 | db_cluster["state"] = "FAILED_DELETION" |
| 1532 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1533 | # has to call update_operation_history return content |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1534 | db_cluster = self.update_operation_history( |
| 1535 | db_cluster, op_id, workflow_status, None |
| 1536 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1537 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1538 | |
| 1539 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1540 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1541 | "deregister_cluster", op_id, op_params, workflow_content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1542 | ) |
| 1543 | self.logger.info( |
| 1544 | "resource_status is :{} and resource_msg is :{}".format( |
| 1545 | resource_status, resource_msg |
| 1546 | ) |
| 1547 | ) |
| 1548 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1549 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1550 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1551 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1552 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1553 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1554 | db_cluster, op_id, workflow_status, resource_status |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1555 | ) |
| 1556 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1557 | |
| garciadeblas | 9338045 | 2025-02-05 09:32:52 +0100 | [diff] [blame] | 1558 | await self.delete(params, order_id) |
| 1559 | # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded |
| 1560 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1561 | "deregister_cluster", op_id, op_params, workflow_content |
| 1562 | ) |
| 1563 | self.logger.info( |
| 1564 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1565 | ) |
| 1566 | return |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1567 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1568 | async def get_creds(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1569 | self.logger.info("Cluster get creds Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1570 | cluster_id = params["cluster_id"] |
| 1571 | op_id = params["operation_id"] |
| 1572 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1573 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 1574 | if result: |
| 1575 | db_cluster["credentials"] = cluster_creds |
| yshah | d940c65 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 1576 | op_len = 0 |
| 1577 | for operations in db_cluster["operationHistory"]: |
| 1578 | if operations["op_id"] == op_id: |
| 1579 | db_cluster["operationHistory"][op_len]["result"] = result |
| 1580 | db_cluster["operationHistory"][op_len]["endDate"] = time() |
| 1581 | op_len += 1 |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1582 | db_cluster["current_operation"] = None |
| yshah | d940c65 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 1583 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1584 | self.logger.info("Cluster Get Creds Exit") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1585 | return |
| 1586 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1587 | async def update(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1588 | self.logger.info("Cluster update Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1589 | # To get the cluster details |
| 1590 | cluster_id = params["cluster_id"] |
| 1591 | db_cluster = self.db.get_one("clusters", {"_id": cluster_id}) |
| 1592 | |
| 1593 | # To get the operation params details |
| 1594 | op_id = params["operation_id"] |
| 1595 | op_params = self.get_operation_params(db_cluster, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1596 | |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1597 | # To copy the cluster content and decrypting fields to use in workflows |
| 1598 | workflow_content = { |
| 1599 | "cluster": self.decrypted_copy(db_cluster), |
| 1600 | } |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1601 | |
| 1602 | # vim account details |
| 1603 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1604 | workflow_content["vim_account"] = db_vim |
| rshri | c356494 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 1605 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1606 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1607 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1608 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1609 | if not workflow_res: |
| 1610 | self.logger.error(f"Failed to launch workflow: {workflow_name}") |
| 1611 | db_cluster["resourceState"] = "ERROR" |
| 1612 | db_cluster = self.update_operation_history( |
| 1613 | db_cluster, op_id, workflow_status=False, resource_status=None |
| 1614 | ) |
| 1615 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1616 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1617 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1618 | "update_cluster", op_id, op_params, workflow_content |
| 1619 | ) |
| 1620 | self.logger.info( |
| 1621 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1622 | ) |
| 1623 | return |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1624 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1625 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1626 | op_id, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1627 | ) |
| 1628 | self.logger.info( |
| 1629 | "Workflow Status: {} Workflow Message: {}".format( |
| 1630 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1631 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1632 | ) |
| 1633 | |
| 1634 | if workflow_status: |
| 1635 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1636 | else: |
| 1637 | db_cluster["resourceState"] = "ERROR" |
| 1638 | |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1639 | db_cluster = self.update_operation_history( |
| 1640 | db_cluster, op_id, workflow_status, None |
| 1641 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1642 | # self.logger.info("Db content: {}".format(db_content)) |
| 1643 | # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster) |
| 1644 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 1645 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1646 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1647 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1648 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1649 | ) |
| 1650 | self.logger.info( |
| 1651 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1652 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1653 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1654 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 995cbf3 | 2024-12-18 12:54:00 +0100 | [diff] [blame] | 1655 | "update_cluster", op_id, op_params, workflow_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1656 | ) |
| 1657 | self.logger.info( |
| 1658 | "Resource Status: {} Resource Message: {}".format( |
| 1659 | resource_status, resource_msg |
| 1660 | ) |
| 1661 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1662 | |
| 1663 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1664 | db_cluster["resourceState"] = "READY" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1665 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1666 | db_cluster["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1667 | |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1668 | db_cluster = self.update_operation_history( |
| yshah | cb9075f | 2024-11-22 12:08:57 +0000 | [diff] [blame] | 1669 | db_cluster, op_id, workflow_status, resource_status |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1670 | ) |
| 1671 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1672 | db_cluster["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1673 | # self.logger.info("db_cluster: {}".format(db_cluster)) |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 1674 | # TODO: verify condition |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1675 | # For the moment, if the workflow completed successfully, then we update the db accordingly. |
| 1676 | if workflow_status: |
| 1677 | if "k8s_version" in op_params: |
| 1678 | db_cluster["k8s_version"] = op_params["k8s_version"] |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1679 | if "node_count" in op_params: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1680 | db_cluster["node_count"] = op_params["node_count"] |
| yshah | 0defcd5 | 2024-11-18 07:41:35 +0000 | [diff] [blame] | 1681 | if "node_size" in op_params: |
| 1682 | db_cluster["node_count"] = op_params["node_size"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1683 | # self.db.set_one(self.db_collection, {"_id": _id}, db_content) |
| shahithya | 70a3fc9 | 2024-11-12 11:01:05 +0000 | [diff] [blame] | 1684 | db_cluster["current_operation"] = None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1685 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1686 | return |
| 1687 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1688 | async def check_update_cluster(self, op_id, op_params, content): |
| 1689 | self.logger.info( |
| 1690 | f"check_update_cluster Operation {op_id}. Params: {op_params}." |
| 1691 | ) |
| 1692 | self.logger.debug(f"Content: {content}") |
| garciadeblas | d7d8bde | 2025-01-27 18:31:06 +0100 | [diff] [blame] | 1693 | # return await self.check_dummy_operation(op_id, op_params, content) |
| 1694 | db_cluster = content["cluster"] |
| 1695 | cluster_name = db_cluster["git_name"].lower() |
| 1696 | cluster_kustomization_name = cluster_name |
| 1697 | db_vim_account = content["vim_account"] |
| 1698 | cloud_type = db_vim_account["vim_type"] |
| 1699 | if cloud_type == "aws": |
| 1700 | cluster_name = f"{cluster_name}-cluster" |
| 1701 | if cloud_type in ("azure", "gcp", "aws"): |
| 1702 | checkings_list = [ |
| 1703 | { |
| 1704 | "item": "kustomization", |
| 1705 | "name": cluster_kustomization_name, |
| 1706 | "namespace": "managed-resources", |
| 1707 | "condition": { |
| 1708 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 1709 | "value": "True", |
| 1710 | }, |
| 1711 | "timeout": self._checkloop_kustomization_timeout, |
| 1712 | "enable": True, |
| 1713 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 1714 | }, |
| 1715 | ] |
| 1716 | else: |
| 1717 | return False, "Not suitable VIM account to check cluster status" |
| 1718 | # Scale operation |
| 1719 | if "node_count" in op_params: |
| garciadeblas | 1ca0985 | 2025-05-30 11:19:06 +0200 | [diff] [blame] | 1720 | if cloud_type in ("azure", "gcp"): |
| 1721 | checkings_list.append( |
| 1722 | { |
| 1723 | "item": f"cluster_{cloud_type}", |
| 1724 | "name": cluster_name, |
| 1725 | "namespace": "", |
| 1726 | "condition": { |
| 1727 | "jsonpath_filter": "status.atProvider.defaultNodePool[0].nodeCount", |
| 1728 | "value": f"{op_params['node_count']}", |
| 1729 | }, |
| 1730 | "timeout": self._checkloop_resource_timeout * 3, |
| 1731 | "enable": True, |
| 1732 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODE_COUNT.CLUSTER", |
| 1733 | } |
| 1734 | ) |
| 1735 | elif cloud_type == "aws": |
| 1736 | checkings_list.append( |
| 1737 | { |
| 1738 | "item": f"nodegroup_{cloud_type}", |
| 1739 | "name": f"{cluster_name}-nodegroup", |
| 1740 | "namespace": "", |
| 1741 | "condition": { |
| 1742 | "jsonpath_filter": "status.atProvider.scalingConfig[0].desiredSize", |
| 1743 | "value": f"{op_params['node_count']}", |
| 1744 | }, |
| 1745 | "timeout": self._checkloop_resource_timeout * 3, |
| 1746 | "enable": True, |
| 1747 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODE_COUNT.CLUSTER", |
| 1748 | } |
| 1749 | ) |
| 1750 | |
| garciadeblas | d7d8bde | 2025-01-27 18:31:06 +0100 | [diff] [blame] | 1751 | # Upgrade operation |
| 1752 | if "k8s_version" in op_params: |
| 1753 | checkings_list.append( |
| 1754 | { |
| 1755 | "item": f"cluster_{cloud_type}", |
| 1756 | "name": cluster_name, |
| 1757 | "namespace": "", |
| 1758 | "condition": { |
| 1759 | "jsonpath_filter": "status.atProvider.defaultNodePool[0].orchestratorVersion", |
| 1760 | "value": op_params["k8s_version"], |
| 1761 | }, |
| 1762 | "timeout": self._checkloop_resource_timeout * 2, |
| 1763 | "enable": True, |
| 1764 | "resourceState": "IN_PROGRESS.RESOURCE_READY.K8S_VERSION.CLUSTER", |
| 1765 | } |
| 1766 | ) |
| 1767 | return await self.common_check_list( |
| 1768 | op_id, checkings_list, "clusters", db_cluster |
| 1769 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 1770 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1771 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1772 | class CloudCredentialsLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1773 | db_collection = "vim_accounts" |
| 1774 | |
| 1775 | def __init__(self, msg, lcm_tasks, config): |
| 1776 | """ |
| 1777 | Init, Connect to database, filesystem storage, and messaging |
| 1778 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1779 | :return: None |
| 1780 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1781 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1782 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1783 | async def add(self, params, order_id): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1784 | self.logger.info("Cloud Credentials create") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1785 | vim_id = params["_id"] |
| 1786 | op_id = vim_id |
| 1787 | op_params = params |
| 1788 | db_content = self.db.get_one(self.db_collection, {"_id": vim_id}) |
| 1789 | vim_config = db_content.get("config", {}) |
| 1790 | self.db.encrypt_decrypt_fields( |
| 1791 | vim_config.get("credentials"), |
| 1792 | "decrypt", |
| 1793 | ["password", "secret"], |
| 1794 | schema_version=db_content["schema_version"], |
| 1795 | salt=vim_id, |
| 1796 | ) |
| 1797 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1798 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1799 | "create_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1800 | ) |
| 1801 | |
| 1802 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1803 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1804 | ) |
| 1805 | |
| 1806 | self.logger.info( |
| 1807 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1808 | ) |
| 1809 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1810 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1811 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1812 | "create_cloud_credentials", op_id, op_params, db_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1813 | ) |
| 1814 | self.logger.info( |
| 1815 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1816 | ) |
| 1817 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1818 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1819 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1820 | "create_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1821 | ) |
| 1822 | self.logger.info( |
| 1823 | "Resource Status: {} Resource Message: {}".format( |
| 1824 | resource_status, resource_msg |
| 1825 | ) |
| 1826 | ) |
| garciadeblas | 15b8a30 | 2024-09-23 12:40:13 +0200 | [diff] [blame] | 1827 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1828 | db_content["_admin"]["operationalState"] = "ENABLED" |
| 1829 | for operation in db_content["_admin"]["operations"]: |
| garciadeblas | 15b8a30 | 2024-09-23 12:40:13 +0200 | [diff] [blame] | 1830 | if operation["lcmOperationType"] == "create": |
| 1831 | operation["operationState"] = "ENABLED" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1832 | self.logger.info("Content : {}".format(db_content)) |
| 1833 | self.db.set_one("vim_accounts", {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1834 | return |
| 1835 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1836 | async def edit(self, params, order_id): |
| 1837 | self.logger.info("Cloud Credentials Update") |
| 1838 | vim_id = params["_id"] |
| 1839 | op_id = vim_id |
| 1840 | op_params = params |
| 1841 | db_content = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| 1842 | vim_config = db_content.get("config", {}) |
| 1843 | self.db.encrypt_decrypt_fields( |
| 1844 | vim_config.get("credentials"), |
| 1845 | "decrypt", |
| 1846 | ["password", "secret"], |
| 1847 | schema_version=db_content["schema_version"], |
| 1848 | salt=vim_id, |
| 1849 | ) |
| 1850 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1851 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1852 | "update_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1853 | ) |
| 1854 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1855 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1856 | ) |
| 1857 | self.logger.info( |
| 1858 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1859 | ) |
| 1860 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1861 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1862 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1863 | "update_cloud_credentials", op_id, op_params, db_content |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 1864 | ) |
| 1865 | self.logger.info( |
| 1866 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1867 | ) |
| 1868 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1869 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1870 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1871 | "update_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1872 | ) |
| 1873 | self.logger.info( |
| 1874 | "Resource Status: {} Resource Message: {}".format( |
| 1875 | resource_status, resource_msg |
| 1876 | ) |
| 1877 | ) |
| 1878 | return |
| 1879 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1880 | async def remove(self, params, order_id): |
| 1881 | self.logger.info("Cloud Credentials remove") |
| 1882 | vim_id = params["_id"] |
| 1883 | op_id = vim_id |
| 1884 | op_params = params |
| 1885 | db_content = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| 1886 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1887 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1888 | "delete_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1889 | ) |
| 1890 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| garciadeblas | c89134b | 2025-02-05 16:36:17 +0100 | [diff] [blame] | 1891 | op_id, workflow_name |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1892 | ) |
| 1893 | self.logger.info( |
| 1894 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 1895 | ) |
| 1896 | |
| 1897 | if workflow_status: |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1898 | resource_status, resource_msg = await self.check_resource_status( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1899 | "delete_cloud_credentials", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1900 | ) |
| 1901 | self.logger.info( |
| 1902 | "Resource Status: {} Resource Message: {}".format( |
| 1903 | resource_status, resource_msg |
| 1904 | ) |
| 1905 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1906 | self.db.del_one(self.db_collection, {"_id": db_content["_id"]}) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1907 | return |
| 1908 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1909 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1910 | class K8sAppLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1911 | db_collection = "k8sapp" |
| 1912 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1913 | def __init__(self, msg, lcm_tasks, config): |
| 1914 | """ |
| 1915 | Init, Connect to database, filesystem storage, and messaging |
| 1916 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1917 | :return: None |
| 1918 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1919 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1920 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1921 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1922 | self.logger.info("App Create Enter") |
| 1923 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1924 | op_id = params["operation_id"] |
| 1925 | profile_id = params["profile_id"] |
| 1926 | |
| 1927 | # To initialize the operation states |
| 1928 | self.initialize_operation(profile_id, op_id) |
| 1929 | |
| 1930 | content = self.db.get_one("k8sapp", {"_id": profile_id}) |
| 1931 | content["profile_type"] = "applications" |
| 1932 | op_params = self.get_operation_params(content, op_id) |
| 1933 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| 1934 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1935 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1936 | "create_profile", op_id, op_params, content |
| 1937 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1938 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1939 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1940 | workflow_status = await self.check_workflow_and_update_db( |
| 1941 | op_id, workflow_name, content |
| 1942 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1943 | |
| 1944 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1945 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1946 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1947 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1948 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1949 | self.logger.info(f"App Create Exit with resource status: {resource_status}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1950 | return |
| 1951 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1952 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1953 | self.logger.info("App delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1954 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1955 | op_id = params["operation_id"] |
| 1956 | profile_id = params["profile_id"] |
| 1957 | |
| 1958 | # To initialize the operation states |
| 1959 | self.initialize_operation(profile_id, op_id) |
| 1960 | |
| 1961 | content = self.db.get_one("k8sapp", {"_id": profile_id}) |
| 1962 | op_params = self.get_operation_params(content, op_id) |
| 1963 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 1964 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1965 | "delete_profile", op_id, op_params, content |
| 1966 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 1967 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1968 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1969 | workflow_status = await self.check_workflow_and_update_db( |
| 1970 | op_id, workflow_name, content |
| 1971 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1972 | |
| 1973 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 1974 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1975 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1976 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1977 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 1978 | force = params.get("force", False) |
| 1979 | if force: |
| 1980 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 1981 | profile_id, workflow_status, resource_status, force |
| 1982 | ) |
| 1983 | if force_delete_status: |
| 1984 | return |
| 1985 | |
| 1986 | self.logger.info(f"Resource status: {resource_status}") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1987 | if resource_status: |
| 1988 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 1989 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 1990 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 1991 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 1992 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 1993 | self.logger.info(f"App Delete Exit with resource status: {resource_status}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1994 | return |
| 1995 | |
| 1996 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1997 | class K8sResourceLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 1998 | db_collection = "k8sresource" |
| 1999 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2000 | def __init__(self, msg, lcm_tasks, config): |
| 2001 | """ |
| 2002 | Init, Connect to database, filesystem storage, and messaging |
| 2003 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2004 | :return: None |
| 2005 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2006 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2007 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2008 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2009 | self.logger.info("Resource Create Enter") |
| 2010 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2011 | op_id = params["operation_id"] |
| 2012 | profile_id = params["profile_id"] |
| 2013 | |
| 2014 | # To initialize the operation states |
| 2015 | self.initialize_operation(profile_id, op_id) |
| 2016 | |
| 2017 | content = self.db.get_one("k8sresource", {"_id": profile_id}) |
| 2018 | content["profile_type"] = "managed-resources" |
| 2019 | op_params = self.get_operation_params(content, op_id) |
| 2020 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| 2021 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2022 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2023 | "create_profile", op_id, op_params, content |
| 2024 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2025 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2026 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2027 | workflow_status = await self.check_workflow_and_update_db( |
| 2028 | op_id, workflow_name, content |
| 2029 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2030 | |
| 2031 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2032 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2033 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2034 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2035 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2036 | self.logger.info( |
| 2037 | f"Resource Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2038 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2039 | return |
| 2040 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2041 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2042 | self.logger.info("Resource delete Enter") |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2043 | |
| 2044 | op_id = params["operation_id"] |
| 2045 | profile_id = params["profile_id"] |
| 2046 | |
| 2047 | # To initialize the operation states |
| 2048 | self.initialize_operation(profile_id, op_id) |
| 2049 | |
| 2050 | content = self.db.get_one("k8sresource", {"_id": profile_id}) |
| 2051 | op_params = self.get_operation_params(content, op_id) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2052 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2053 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2054 | "delete_profile", op_id, op_params, content |
| 2055 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2056 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2057 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2058 | workflow_status = await self.check_workflow_and_update_db( |
| 2059 | op_id, workflow_name, content |
| 2060 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2061 | |
| 2062 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2063 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2064 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2065 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2066 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2067 | force = params.get("force", False) |
| 2068 | if force: |
| 2069 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2070 | profile_id, workflow_status, resource_status, force |
| 2071 | ) |
| 2072 | if force_delete_status: |
| 2073 | return |
| 2074 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2075 | if resource_status: |
| 2076 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 2077 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 2078 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2079 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2080 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 2081 | self.logger.info( |
| 2082 | f"Resource Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2083 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2084 | return |
| 2085 | |
| 2086 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2087 | class K8sInfraControllerLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2088 | db_collection = "k8sinfra_controller" |
| 2089 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2090 | def __init__(self, msg, lcm_tasks, config): |
| 2091 | """ |
| 2092 | Init, Connect to database, filesystem storage, and messaging |
| 2093 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2094 | :return: None |
| 2095 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2096 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2097 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2098 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2099 | self.logger.info("Infra controller Create Enter") |
| 2100 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2101 | op_id = params["operation_id"] |
| 2102 | profile_id = params["profile_id"] |
| 2103 | |
| 2104 | # To initialize the operation states |
| 2105 | self.initialize_operation(profile_id, op_id) |
| 2106 | |
| 2107 | content = self.db.get_one("k8sinfra_controller", {"_id": profile_id}) |
| 2108 | content["profile_type"] = "infra-controllers" |
| 2109 | op_params = self.get_operation_params(content, op_id) |
| 2110 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| 2111 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2112 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2113 | "create_profile", op_id, op_params, content |
| 2114 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2115 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2116 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2117 | workflow_status = await self.check_workflow_and_update_db( |
| 2118 | op_id, workflow_name, content |
| 2119 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2120 | |
| 2121 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2122 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2123 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2124 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2125 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2126 | self.logger.info( |
| 2127 | f"Infra Controller Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2128 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2129 | return |
| 2130 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2131 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2132 | self.logger.info("Infra controller delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2133 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2134 | op_id = params["operation_id"] |
| 2135 | profile_id = params["profile_id"] |
| 2136 | |
| 2137 | # To initialize the operation states |
| 2138 | self.initialize_operation(profile_id, op_id) |
| 2139 | |
| 2140 | content = self.db.get_one("k8sinfra_controller", {"_id": profile_id}) |
| 2141 | op_params = self.get_operation_params(content, op_id) |
| 2142 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2143 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2144 | "delete_profile", op_id, op_params, content |
| 2145 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2146 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2147 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2148 | workflow_status = await self.check_workflow_and_update_db( |
| 2149 | op_id, workflow_name, content |
| 2150 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2151 | |
| 2152 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2153 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2154 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2155 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2156 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2157 | force = params.get("force", False) |
| 2158 | if force: |
| 2159 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2160 | profile_id, workflow_status, resource_status, force |
| 2161 | ) |
| 2162 | if force_delete_status: |
| 2163 | return |
| 2164 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2165 | if resource_status: |
| 2166 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 2167 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 2168 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2169 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2170 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 2171 | self.logger.info( |
| 2172 | f"Infra Controller Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2173 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2174 | return |
| 2175 | |
| 2176 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2177 | class K8sInfraConfigLcm(GitOpsLcm): |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2178 | db_collection = "k8sinfra_config" |
| 2179 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2180 | def __init__(self, msg, lcm_tasks, config): |
| 2181 | """ |
| 2182 | Init, Connect to database, filesystem storage, and messaging |
| 2183 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2184 | :return: None |
| 2185 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2186 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2187 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2188 | async def create(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2189 | self.logger.info("Infra config Create Enter") |
| 2190 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2191 | op_id = params["operation_id"] |
| 2192 | profile_id = params["profile_id"] |
| 2193 | |
| 2194 | # To initialize the operation states |
| 2195 | self.initialize_operation(profile_id, op_id) |
| 2196 | |
| 2197 | content = self.db.get_one("k8sinfra_config", {"_id": profile_id}) |
| 2198 | content["profile_type"] = "infra-configs" |
| 2199 | op_params = self.get_operation_params(content, op_id) |
| 2200 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| 2201 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2202 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2203 | "create_profile", op_id, op_params, content |
| 2204 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2205 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2206 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2207 | workflow_status = await self.check_workflow_and_update_db( |
| 2208 | op_id, workflow_name, content |
| 2209 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2210 | |
| 2211 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2212 | resource_status, content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2213 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2214 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2215 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2216 | self.logger.info( |
| 2217 | f"Infra Config Create Exit with resource status: {resource_status}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2218 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2219 | return |
| 2220 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2221 | async def delete(self, params, order_id): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2222 | self.logger.info("Infra config delete Enter") |
| 2223 | |
| rshri | 948f7de | 2024-12-02 03:42:35 +0000 | [diff] [blame] | 2224 | op_id = params["operation_id"] |
| 2225 | profile_id = params["profile_id"] |
| 2226 | |
| 2227 | # To initialize the operation states |
| 2228 | self.initialize_operation(profile_id, op_id) |
| 2229 | |
| 2230 | content = self.db.get_one("k8sinfra_config", {"_id": profile_id}) |
| 2231 | op_params = self.get_operation_params(content, op_id) |
| 2232 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2233 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2234 | "delete_profile", op_id, op_params, content |
| 2235 | ) |
| garciadeblas | 26d733c | 2025-02-03 16:12:43 +0100 | [diff] [blame] | 2236 | self.logger.info("workflow_name is: {}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2237 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2238 | workflow_status = await self.check_workflow_and_update_db( |
| 2239 | op_id, workflow_name, content |
| 2240 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2241 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2242 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2243 | resource_status, content = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2244 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2245 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2246 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2247 | force = params.get("force", False) |
| 2248 | if force: |
| 2249 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2250 | profile_id, workflow_status, resource_status, force |
| 2251 | ) |
| 2252 | if force_delete_status: |
| 2253 | return |
| 2254 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2255 | if resource_status: |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2256 | content["state"] = "DELETED" |
| yshah | 6bad889 | 2025-02-11 12:37:04 +0000 | [diff] [blame] | 2257 | profile_type = self.profile_type_mapping[content["profile_type"]] |
| 2258 | self.delete_profile_ksu(profile_id, profile_type) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2259 | self.db.set_one(self.db_collection, {"_id": content["_id"]}, content) |
| 2260 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 2261 | self.logger.info( |
| 2262 | f"Infra Config Delete Exit with resource status: {resource_status}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2263 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2264 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 2265 | return |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2266 | |
| 2267 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2268 | class OkaLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2269 | db_collection = "okas" |
| 2270 | |
| 2271 | def __init__(self, msg, lcm_tasks, config): |
| 2272 | """ |
| 2273 | Init, Connect to database, filesystem storage, and messaging |
| 2274 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2275 | :return: None |
| 2276 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2277 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2278 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2279 | async def create(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2280 | self.logger.info("OKA Create Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2281 | op_id = params["operation_id"] |
| 2282 | oka_id = params["oka_id"] |
| 2283 | self.initialize_operation(oka_id, op_id) |
| 2284 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 2285 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2286 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2287 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2288 | "create_oka", op_id, op_params, db_content |
| 2289 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2290 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2291 | workflow_status = await self.check_workflow_and_update_db( |
| 2292 | op_id, workflow_name, db_content |
| 2293 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2294 | |
| 2295 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2296 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2297 | "create_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2298 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2299 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 2300 | |
| 2301 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2302 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 2303 | "create_oka", op_id, op_params, db_content |
| 2304 | ) |
| 2305 | self.logger.info( |
| 2306 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 2307 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2308 | self.logger.info(f"OKA Create Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2309 | return |
| 2310 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2311 | async def edit(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2312 | self.logger.info("OKA Edit Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2313 | op_id = params["operation_id"] |
| 2314 | oka_id = params["oka_id"] |
| 2315 | self.initialize_operation(oka_id, op_id) |
| 2316 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 2317 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2318 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2319 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2320 | "update_oka", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2321 | ) |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2322 | workflow_status = await self.check_workflow_and_update_db( |
| 2323 | op_id, workflow_name, db_content |
| 2324 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2325 | |
| 2326 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2327 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2328 | "update_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2329 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2330 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 2331 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2332 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 2333 | "update_oka", op_id, op_params, db_content |
| 2334 | ) |
| 2335 | self.logger.info( |
| 2336 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 2337 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2338 | self.logger.info(f"OKA Update Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2339 | return |
| 2340 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2341 | async def delete(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2342 | self.logger.info("OKA delete Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2343 | op_id = params["operation_id"] |
| 2344 | oka_id = params["oka_id"] |
| 2345 | self.initialize_operation(oka_id, op_id) |
| 2346 | db_content = self.db.get_one(self.db_collection, {"_id": oka_id}) |
| 2347 | op_params = self.get_operation_params(db_content, op_id) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2348 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2349 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2350 | "delete_oka", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2351 | ) |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2352 | workflow_status = await self.check_workflow_and_update_db( |
| 2353 | op_id, workflow_name, db_content |
| 2354 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2355 | |
| 2356 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2357 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2358 | "delete_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2359 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2360 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2361 | force = params.get("force", False) |
| 2362 | if force: |
| 2363 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2364 | oka_id, workflow_status, resource_status, force |
| 2365 | ) |
| 2366 | if force_delete_status: |
| 2367 | return |
| 2368 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2369 | if resource_status: |
| 2370 | db_content["state"] == "DELETED" |
| 2371 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2372 | self.db.del_one(self.db_collection, {"_id": db_content["_id"]}) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 2373 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2374 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 2375 | "delete_oka", op_id, op_params, db_content |
| 2376 | ) |
| 2377 | self.logger.info( |
| 2378 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 2379 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2380 | self.logger.info(f"OKA Delete Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2381 | return |
| 2382 | |
| 2383 | |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2384 | class KsuLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2385 | db_collection = "ksus" |
| 2386 | |
| 2387 | def __init__(self, msg, lcm_tasks, config): |
| 2388 | """ |
| 2389 | Init, Connect to database, filesystem storage, and messaging |
| 2390 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 2391 | :return: None |
| 2392 | """ |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 2393 | super().__init__(msg, lcm_tasks, config) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2394 | self._workflows = { |
| 2395 | "create_ksus": { |
| 2396 | "check_resource_function": self.check_create_ksus, |
| 2397 | }, |
| 2398 | "delete_ksus": { |
| 2399 | "check_resource_function": self.check_delete_ksus, |
| 2400 | }, |
| 2401 | } |
| 2402 | |
| 2403 | def get_dbclusters_from_profile(self, profile_id, profile_type): |
| 2404 | cluster_list = [] |
| 2405 | db_clusters = self.db.get_list("clusters") |
| 2406 | self.logger.info(f"Getting list of clusters for {profile_type} {profile_id}") |
| 2407 | for db_cluster in db_clusters: |
| 2408 | if profile_id in db_cluster.get(profile_type, []): |
| 2409 | self.logger.info( |
| 2410 | f"Profile {profile_id} found in cluster {db_cluster['name']}" |
| 2411 | ) |
| 2412 | cluster_list.append(db_cluster) |
| 2413 | return cluster_list |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2414 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2415 | async def create(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2416 | self.logger.info("ksu Create Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2417 | db_content = [] |
| 2418 | op_params = [] |
| 2419 | op_id = params["operation_id"] |
| 2420 | for ksu_id in params["ksus_list"]: |
| 2421 | self.logger.info("Ksu ID: {}".format(ksu_id)) |
| 2422 | self.initialize_operation(ksu_id, op_id) |
| 2423 | db_ksu = self.db.get_one(self.db_collection, {"_id": ksu_id}) |
| 2424 | self.logger.info("Db KSU: {}".format(db_ksu)) |
| 2425 | db_content.append(db_ksu) |
| 2426 | ksu_params = {} |
| 2427 | ksu_params = self.get_operation_params(db_ksu, op_id) |
| 2428 | self.logger.info("Operation Params: {}".format(ksu_params)) |
| 2429 | # Update ksu_params["profile"] with profile name and age-pubkey |
| 2430 | profile_type = ksu_params["profile"]["profile_type"] |
| 2431 | profile_id = ksu_params["profile"]["_id"] |
| 2432 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2433 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 2434 | # db_profile is decrypted inline |
| 2435 | # No need to use decrypted_copy because db_profile won't be updated. |
| 2436 | self.decrypt_age_keys(db_profile) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2437 | ksu_params["profile"]["name"] = db_profile["name"] |
| 2438 | ksu_params["profile"]["age_pubkey"] = db_profile.get("age_pubkey", "") |
| 2439 | # Update ksu_params["oka"] with sw_catalog_path (when missing) |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2440 | # TODO: remove this in favor of doing it in ODU workflow |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2441 | for oka in ksu_params["oka"]: |
| 2442 | if "sw_catalog_path" not in oka: |
| 2443 | oka_id = oka["_id"] |
| 2444 | db_oka = self.db.get_one("okas", {"_id": oka_id}) |
| yshah | 2f39b8a | 2024-12-19 11:06:24 +0000 | [diff] [blame] | 2445 | oka_type = MAP_PROFILE[ |
| 2446 | db_oka.get("profile_type", "infra_controller_profiles") |
| 2447 | ] |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2448 | oka[ |
| 2449 | "sw_catalog_path" |
| garciadeblas | 29f8bcf | 2025-01-24 14:24:41 +0100 | [diff] [blame] | 2450 | ] = f"{oka_type}/{db_oka['git_name'].lower()}/templates" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2451 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2452 | |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2453 | # A single workflow is launched for all KSUs |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2454 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2455 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2456 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2457 | # Update workflow status in all KSUs |
| 2458 | wf_status_list = [] |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2459 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2460 | workflow_status = await self.check_workflow_and_update_db( |
| 2461 | op_id, workflow_name, db_ksu |
| 2462 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2463 | wf_status_list.append(workflow_status) |
| 2464 | # Update resource status in all KSUs |
| 2465 | # TODO: Is an operation correct if n KSUs are right and 1 is not OK? |
| 2466 | res_status_list = [] |
| 2467 | for db_ksu, ksu_params, wf_status in zip(db_content, op_params, wf_status_list): |
| 2468 | if wf_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2469 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2470 | "create_ksus", op_id, ksu_params, db_ksu |
| 2471 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2472 | else: |
| 2473 | resource_status = False |
| 2474 | res_status_list.append(resource_status) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2475 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2476 | |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 2477 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2478 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2479 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 2480 | ) |
| 2481 | self.logger.info( |
| 2482 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 2483 | ) |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2484 | self.logger.info(f"KSU Create EXIT with Resource Status {res_status_list}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2485 | return |
| 2486 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2487 | async def edit(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2488 | self.logger.info("ksu edit Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2489 | db_content = [] |
| 2490 | op_params = [] |
| 2491 | op_id = params["operation_id"] |
| 2492 | for ksu_id in params["ksus_list"]: |
| 2493 | self.initialize_operation(ksu_id, op_id) |
| 2494 | db_ksu = self.db.get_one("ksus", {"_id": ksu_id}) |
| 2495 | db_content.append(db_ksu) |
| 2496 | ksu_params = {} |
| 2497 | ksu_params = self.get_operation_params(db_ksu, op_id) |
| 2498 | # Update ksu_params["profile"] with profile name and age-pubkey |
| 2499 | profile_type = ksu_params["profile"]["profile_type"] |
| 2500 | profile_id = ksu_params["profile"]["_id"] |
| 2501 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2502 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 2503 | # db_profile is decrypted inline |
| 2504 | # No need to use decrypted_copy because db_profile won't be updated. |
| 2505 | self.decrypt_age_keys(db_profile) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2506 | ksu_params["profile"]["name"] = db_profile["name"] |
| 2507 | ksu_params["profile"]["age_pubkey"] = db_profile.get("age_pubkey", "") |
| 2508 | # Update ksu_params["oka"] with sw_catalog_path (when missing) |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2509 | # TODO: remove this in favor of doing it in ODU workflow |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2510 | for oka in ksu_params["oka"]: |
| 2511 | if "sw_catalog_path" not in oka: |
| 2512 | oka_id = oka["_id"] |
| 2513 | db_oka = self.db.get_one("okas", {"_id": oka_id}) |
| yshah | 2f39b8a | 2024-12-19 11:06:24 +0000 | [diff] [blame] | 2514 | oka_type = MAP_PROFILE[ |
| 2515 | db_oka.get("profile_type", "infra_controller_profiles") |
| 2516 | ] |
| garciadeblas | 9f7c9c5 | 2025-01-17 01:06:05 +0100 | [diff] [blame] | 2517 | oka[ |
| 2518 | "sw_catalog_path" |
| 2519 | ] = f"{oka_type}/{db_oka['git_name']}/templates" |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2520 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2521 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2522 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2523 | "update_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2524 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2525 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2526 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2527 | workflow_status = await self.check_workflow_and_update_db( |
| 2528 | op_id, workflow_name, db_ksu |
| 2529 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2530 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2531 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2532 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2533 | "update_ksus", op_id, ksu_params, db_ksu |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2534 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2535 | db_ksu["name"] = ksu_params["name"] |
| 2536 | db_ksu["description"] = ksu_params["description"] |
| 2537 | db_ksu["profile"]["profile_type"] = ksu_params["profile"][ |
| 2538 | "profile_type" |
| 2539 | ] |
| 2540 | db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"] |
| 2541 | db_ksu["oka"] = ksu_params["oka"] |
| 2542 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2543 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2544 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 2545 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 2546 | "create_ksus", op_id, op_params, db_content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2547 | ) |
| 2548 | self.logger.info( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2549 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2550 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2551 | self.logger.info(f"KSU Update EXIT with Resource Status {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2552 | return |
| 2553 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2554 | async def delete(self, params, order_id): |
| 2555 | self.logger.info("ksu delete Enter") |
| 2556 | db_content = [] |
| 2557 | op_params = [] |
| 2558 | op_id = params["operation_id"] |
| 2559 | for ksu_id in params["ksus_list"]: |
| 2560 | self.initialize_operation(ksu_id, op_id) |
| 2561 | db_ksu = self.db.get_one("ksus", {"_id": ksu_id}) |
| 2562 | db_content.append(db_ksu) |
| 2563 | ksu_params = {} |
| 2564 | ksu_params["profile"] = {} |
| 2565 | ksu_params["profile"]["profile_type"] = db_ksu["profile"]["profile_type"] |
| 2566 | ksu_params["profile"]["_id"] = db_ksu["profile"]["_id"] |
| garciadeblas | d41e929 | 2025-03-11 15:44:25 +0100 | [diff] [blame] | 2567 | # Update ksu_params["profile"] with profile name |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2568 | profile_type = ksu_params["profile"]["profile_type"] |
| 2569 | profile_id = ksu_params["profile"]["_id"] |
| 2570 | profile_collection = self.profile_collection_mapping[profile_type] |
| 2571 | db_profile = self.db.get_one(profile_collection, {"_id": profile_id}) |
| 2572 | ksu_params["profile"]["name"] = db_profile["name"] |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2573 | op_params.append(ksu_params) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2574 | |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2575 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2576 | "delete_ksus", op_id, op_params, db_content |
| 2577 | ) |
| 2578 | |
| 2579 | for db_ksu, ksu_params in zip(db_content, op_params): |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2580 | workflow_status = await self.check_workflow_and_update_db( |
| 2581 | op_id, workflow_name, db_ksu |
| 2582 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2583 | |
| 2584 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2585 | resource_status, db_ksu = await self.check_resource_and_update_db( |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2586 | "delete_ksus", op_id, ksu_params, db_ksu |
| 2587 | ) |
| 2588 | |
| yshah | b36649f | 2025-02-28 09:01:51 +0000 | [diff] [blame] | 2589 | force = params.get("force", False) |
| 2590 | if force: |
| 2591 | force_delete_status = self.check_force_delete_and_delete_from_db( |
| 2592 | db_ksu["_id"], workflow_status, resource_status, force |
| 2593 | ) |
| 2594 | if force_delete_status: |
| 2595 | return |
| 2596 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2597 | if resource_status: |
| 2598 | db_ksu["state"] == "DELETED" |
| yshah | 5e10915 | 2025-05-19 12:29:01 +0000 | [diff] [blame] | 2599 | self.delete_ksu_dependency(db_ksu["_id"], db_ksu) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2600 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 2601 | self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]}) |
| 2602 | |
| 2603 | self.logger.info(f"KSU Delete Exit with resource status: {resource_status}") |
| 2604 | return |
| 2605 | |
| 2606 | async def clone(self, params, order_id): |
| 2607 | self.logger.info("ksu clone Enter") |
| 2608 | op_id = params["operation_id"] |
| 2609 | ksus_id = params["ksus_list"][0] |
| 2610 | self.initialize_operation(ksus_id, op_id) |
| 2611 | db_content = self.db.get_one(self.db_collection, {"_id": ksus_id}) |
| 2612 | op_params = self.get_operation_params(db_content, op_id) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2613 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2614 | "clone_ksus", op_id, op_params, db_content |
| 2615 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2616 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2617 | workflow_status = await self.check_workflow_and_update_db( |
| 2618 | op_id, workflow_name, db_content |
| 2619 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2620 | |
| 2621 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2622 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2623 | "clone_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2624 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2625 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2626 | |
| 2627 | self.logger.info(f"KSU Clone Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2628 | return |
| 2629 | |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2630 | async def move(self, params, order_id): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2631 | self.logger.info("ksu move Enter") |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2632 | op_id = params["operation_id"] |
| 2633 | ksus_id = params["ksus_list"][0] |
| 2634 | self.initialize_operation(ksus_id, op_id) |
| 2635 | db_content = self.db.get_one(self.db_collection, {"_id": ksus_id}) |
| 2636 | op_params = self.get_operation_params(db_content, op_id) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 2637 | workflow_res, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2638 | "move_ksus", op_id, op_params, db_content |
| 2639 | ) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2640 | |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2641 | workflow_status = await self.check_workflow_and_update_db( |
| 2642 | op_id, workflow_name, db_content |
| 2643 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2644 | |
| 2645 | if workflow_status: |
| garciadeblas | 33b36e7 | 2025-01-17 12:49:19 +0100 | [diff] [blame] | 2646 | resource_status, db_content = await self.check_resource_and_update_db( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2647 | "move_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2648 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 2649 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 564ec9c | 2024-11-29 07:33:32 +0000 | [diff] [blame] | 2650 | |
| 2651 | self.logger.info(f"KSU Move Exit with resource status: {resource_status}") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 2652 | return |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2653 | |
| 2654 | async def check_create_ksus(self, op_id, op_params, content): |
| 2655 | self.logger.info(f"check_create_ksus Operation {op_id}. Params: {op_params}.") |
| 2656 | self.logger.debug(f"Content: {content}") |
| 2657 | db_ksu = content |
| 2658 | kustomization_name = db_ksu["git_name"].lower() |
| 2659 | oka_list = op_params["oka"] |
| 2660 | oka_item = oka_list[0] |
| 2661 | oka_params = oka_item.get("transformation", {}) |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2662 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2663 | profile_id = op_params.get("profile", {}).get("_id") |
| 2664 | profile_type = op_params.get("profile", {}).get("profile_type") |
| 2665 | self.logger.info( |
| 2666 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2667 | ) |
| 2668 | dbcluster_list = self.get_dbclusters_from_profile(profile_id, profile_type) |
| 2669 | if not dbcluster_list: |
| 2670 | self.logger.info(f"No clusters found for profile {profile_id}.") |
| 2671 | for db_cluster in dbcluster_list: |
| 2672 | try: |
| 2673 | self.logger.info( |
| garciadeblas | ae23848 | 2025-02-03 08:44:19 +0100 | [diff] [blame] | 2674 | f"Checking status of KSU {db_ksu['name']} in cluster {db_cluster['name']}." |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2675 | ) |
| 2676 | cluster_kubectl = self.cluster_kubectl(db_cluster) |
| 2677 | checkings_list = [ |
| 2678 | { |
| 2679 | "item": "kustomization", |
| 2680 | "name": kustomization_name, |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2681 | "namespace": kustomization_ns, |
| garciadeblas | 7cf480d | 2025-01-27 16:53:45 +0100 | [diff] [blame] | 2682 | "condition": { |
| 2683 | "jsonpath_filter": "status.conditions[?(@.type=='Ready')].status", |
| 2684 | "value": "True", |
| 2685 | }, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2686 | "timeout": self._checkloop_kustomization_timeout, |
| 2687 | "enable": True, |
| 2688 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 2689 | }, |
| 2690 | ] |
| 2691 | self.logger.info( |
| 2692 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2693 | ) |
| 2694 | result, message = await self.common_check_list( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 2695 | op_id, checkings_list, "ksus", db_ksu, kubectl_obj=cluster_kubectl |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2696 | ) |
| 2697 | if not result: |
| 2698 | return False, message |
| 2699 | except Exception as e: |
| 2700 | self.logger.error( |
| 2701 | f"Error checking KSU in cluster {db_cluster['name']}." |
| 2702 | ) |
| 2703 | self.logger.error(e) |
| 2704 | return False, f"Error checking KSU in cluster {db_cluster['name']}." |
| 2705 | return True, "OK" |
| 2706 | |
| 2707 | async def check_delete_ksus(self, op_id, op_params, content): |
| 2708 | self.logger.info(f"check_delete_ksus Operation {op_id}. Params: {op_params}.") |
| 2709 | self.logger.debug(f"Content: {content}") |
| 2710 | db_ksu = content |
| 2711 | kustomization_name = db_ksu["git_name"].lower() |
| 2712 | oka_list = db_ksu["oka"] |
| 2713 | oka_item = oka_list[0] |
| 2714 | oka_params = oka_item.get("transformation", {}) |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2715 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2716 | profile_id = op_params.get("profile", {}).get("_id") |
| 2717 | profile_type = op_params.get("profile", {}).get("profile_type") |
| 2718 | self.logger.info( |
| 2719 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2720 | ) |
| 2721 | dbcluster_list = self.get_dbclusters_from_profile(profile_id, profile_type) |
| 2722 | if not dbcluster_list: |
| 2723 | self.logger.info(f"No clusters found for profile {profile_id}.") |
| 2724 | for db_cluster in dbcluster_list: |
| 2725 | try: |
| 2726 | self.logger.info( |
| 2727 | f"Checking status of KSU in cluster {db_cluster['name']}." |
| 2728 | ) |
| 2729 | cluster_kubectl = self.cluster_kubectl(db_cluster) |
| 2730 | checkings_list = [ |
| 2731 | { |
| 2732 | "item": "kustomization", |
| 2733 | "name": kustomization_name, |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 2734 | "namespace": kustomization_ns, |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2735 | "deleted": True, |
| 2736 | "timeout": self._checkloop_kustomization_timeout, |
| 2737 | "enable": True, |
| 2738 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_DELETED", |
| 2739 | }, |
| 2740 | ] |
| 2741 | self.logger.info( |
| 2742 | f"Checking status of KSU {db_ksu['name']} for profile {profile_id}." |
| 2743 | ) |
| 2744 | result, message = await self.common_check_list( |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 2745 | op_id, checkings_list, "ksus", db_ksu, kubectl_obj=cluster_kubectl |
| garciadeblas | ad6d1ba | 2025-01-22 16:02:18 +0100 | [diff] [blame] | 2746 | ) |
| 2747 | if not result: |
| 2748 | return False, message |
| 2749 | except Exception as e: |
| 2750 | self.logger.error( |
| 2751 | f"Error checking KSU in cluster {db_cluster['name']}." |
| 2752 | ) |
| 2753 | self.logger.error(e) |
| 2754 | return False, f"Error checking KSU in cluster {db_cluster['name']}." |
| 2755 | return True, "OK" |