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