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