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