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