| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 5 | # |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 11 | # implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | ####################################################################################### |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | import logging |
| 18 | from osm_lcm.lcm_utils import LcmBase |
| 19 | |
| garciadeblas | 98a7a34 | 2024-08-22 10:05:47 +0200 | [diff] [blame] | 20 | from n2vc import kubectl |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 21 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 22 | |
| 23 | class OduWorkflow(LcmBase): |
| 24 | def __init__(self, msg, lcm_tasks, config): |
| 25 | """ |
| 26 | Init, Connect to database, filesystem storage, and messaging |
| 27 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 28 | :return: None |
| 29 | """ |
| 30 | |
| garciadeblas | 4053987 | 2024-09-11 14:28:38 +0200 | [diff] [blame] | 31 | self.logger = logging.getLogger("lcm.gitops") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 32 | self.lcm_tasks = lcm_tasks |
| 33 | self.logger.info("Msg: {} lcm_tasks: {} ".format(msg, lcm_tasks)) |
| 34 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 35 | # self._kubeconfig = kubeconfig # TODO: get it from config |
| garciadeblas | 3364c47 | 2024-09-11 14:30:26 +0200 | [diff] [blame] | 36 | self.gitops_config = config["gitops"] |
| 37 | self.logger.debug(f"Config: {self.gitops_config}") |
| garciadeblas | b897695 | 2024-10-18 11:36:15 +0200 | [diff] [blame] | 38 | self._odu_checkloop_retry_time = 15 |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 39 | self._kubeconfig = self.gitops_config["mgmtcluster_kubeconfig"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 40 | self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig) |
| garciadeblas | 3364c47 | 2024-09-11 14:30:26 +0200 | [diff] [blame] | 41 | self._repo_base_url = self.gitops_config["git_base_url"] |
| 42 | self._repo_user = self.gitops_config["user"] |
| 43 | self._pubkey = self.gitops_config["pubkey"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 44 | self._workflow_debug = "true" |
| 45 | self._workflow_dry_run = "false" |
| 46 | self._workflows = { |
| 47 | "create_cluster": { |
| 48 | "workflow_function": self.create_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 49 | "clean_function": self.clean_items_cluster_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 50 | }, |
| 51 | "update_cluster": { |
| 52 | "workflow_function": self.update_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 53 | "clean_function": self.clean_items_cluster_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 54 | }, |
| 55 | "delete_cluster": { |
| 56 | "workflow_function": self.delete_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 57 | }, |
| 58 | "register_cluster": { |
| 59 | "workflow_function": self.register_cluster, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 60 | "clean_function": self.clean_items_cluster_register, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 61 | }, |
| 62 | "deregister_cluster": { |
| 63 | "workflow_function": self.deregister_cluster, |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame^] | 64 | "clean_function": self.clean_items_cluster_deregister, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 65 | }, |
| 66 | "create_profile": { |
| 67 | "workflow_function": self.create_profile, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 68 | }, |
| 69 | "delete_profile": { |
| 70 | "workflow_function": self.delete_profile, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 71 | }, |
| 72 | "attach_profile_to_cluster": { |
| 73 | "workflow_function": self.attach_profile_to_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 74 | }, |
| 75 | "detach_profile_from_cluster": { |
| 76 | "workflow_function": self.detach_profile_from_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 77 | }, |
| 78 | "create_oka": { |
| 79 | "workflow_function": self.create_oka, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 80 | }, |
| 81 | "update_oka": { |
| 82 | "workflow_function": self.update_oka, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 83 | }, |
| 84 | "delete_oka": { |
| 85 | "workflow_function": self.delete_oka, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 86 | }, |
| 87 | "create_ksus": { |
| 88 | "workflow_function": self.create_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 89 | "clean_function": self.clean_items_ksu_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 90 | }, |
| 91 | "update_ksus": { |
| 92 | "workflow_function": self.update_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 93 | "clean_function": self.clean_items_ksu_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 94 | }, |
| 95 | "delete_ksus": { |
| 96 | "workflow_function": self.delete_ksus, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 97 | }, |
| 98 | "clone_ksu": { |
| 99 | "workflow_function": self.clone_ksu, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 100 | }, |
| 101 | "move_ksu": { |
| 102 | "workflow_function": self.move_ksu, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 103 | }, |
| 104 | "create_cloud_credentials": { |
| 105 | "workflow_function": self.create_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 106 | "clean_function": self.clean_items_cloud_credentials_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 107 | }, |
| 108 | "update_cloud_credentials": { |
| 109 | "workflow_function": self.update_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 110 | "clean_function": self.clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 111 | }, |
| 112 | "delete_cloud_credentials": { |
| 113 | "workflow_function": self.delete_cloud_credentials, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 114 | }, |
| 115 | "dummy_operation": { |
| 116 | "workflow_function": self.dummy_operation, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 117 | }, |
| 118 | } |
| 119 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 120 | super().__init__(msg, self.logger) |
| 121 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 122 | @property |
| 123 | def kubeconfig(self): |
| 124 | return self._kubeconfig |
| 125 | |
| 126 | # Imported methods |
| 127 | from osm_lcm.odu_libs.vim_mgmt import ( |
| 128 | create_cloud_credentials, |
| 129 | update_cloud_credentials, |
| 130 | delete_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 131 | clean_items_cloud_credentials_create, |
| 132 | clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 133 | ) |
| 134 | from osm_lcm.odu_libs.cluster_mgmt import ( |
| 135 | create_cluster, |
| 136 | update_cluster, |
| 137 | delete_cluster, |
| 138 | register_cluster, |
| 139 | deregister_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 140 | clean_items_cluster_create, |
| 141 | clean_items_cluster_update, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 142 | clean_items_cluster_register, |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame^] | 143 | clean_items_cluster_deregister, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 144 | get_cluster_credentials, |
| 145 | ) |
| 146 | from osm_lcm.odu_libs.ksu import ( |
| 147 | create_ksus, |
| 148 | update_ksus, |
| 149 | delete_ksus, |
| 150 | clone_ksu, |
| 151 | move_ksu, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 152 | clean_items_ksu_create, |
| 153 | clean_items_ksu_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 154 | ) |
| 155 | from osm_lcm.odu_libs.oka import ( |
| 156 | create_oka, |
| 157 | update_oka, |
| 158 | delete_oka, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 159 | ) |
| 160 | from osm_lcm.odu_libs.profiles import ( |
| 161 | create_profile, |
| 162 | delete_profile, |
| 163 | attach_profile_to_cluster, |
| 164 | detach_profile_from_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 165 | ) |
| 166 | from osm_lcm.odu_libs.workflows import ( |
| 167 | check_workflow_status, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 168 | readiness_loop, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 169 | ) |
| 170 | from osm_lcm.odu_libs.render import ( |
| 171 | render_jinja_template, |
| 172 | render_yaml_template, |
| 173 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 174 | from osm_lcm.odu_libs.common import ( |
| 175 | create_secret, |
| 176 | delete_secret, |
| 177 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 178 | |
| 179 | async def launch_workflow(self, key, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 180 | self.logger.info( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 181 | f"Workflow is getting into launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 182 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 183 | workflow_function = self._workflows[key]["workflow_function"] |
| 184 | self.logger.info("workflow function : {}".format(workflow_function)) |
| 185 | return await workflow_function(op_id, op_params, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 186 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 187 | async def clean_items_workflow(self, key, op_id, op_params, content): |
| 188 | self.logger.info( |
| 189 | f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 190 | ) |
| 191 | clean_items_function = self._workflows[key]["clean_function"] |
| 192 | self.logger.info("clean items function : {}".format(clean_items_function)) |
| 193 | return await clean_items_function(op_id, op_params, content) |
| 194 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 195 | async def dummy_operation(self, op_id, op_params, content): |
| 196 | self.logger.info("Empty operation status Enter") |
| 197 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 198 | return content["workflow_name"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 199 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 200 | async def clean_items(self, items): |
| 201 | # Delete secrets |
| 202 | for secret in items.get("secrets", []): |
| 203 | name = secret["name"] |
| 204 | namespace = secret["namespace"] |
| 205 | self.logger.info(f"Deleting secret {name} in namespace {namespace}") |
| 206 | self.delete_secret(name, namespace) |
| 207 | # Delete pvcs |
| 208 | for pvc in items.get("pvcs", []): |
| 209 | name = pvc["name"] |
| 210 | namespace = pvc["namespace"] |
| 211 | self.logger.info(f"Deleting pvc {name} in namespace {namespace}") |
| 212 | await self._kubectl.delete_pvc(name, namespace) |