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