| 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 |
| almagia | cdd20ae | 2024-12-13 09:45:45 +0100 | [diff] [blame] | 19 | from osm_lcm.n2vc import kubectl |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 20 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 21 | |
| 22 | class OduWorkflow(LcmBase): |
| 23 | def __init__(self, msg, lcm_tasks, config): |
| 24 | """ |
| 25 | Init, Connect to database, filesystem storage, and messaging |
| 26 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 27 | :return: None |
| 28 | """ |
| 29 | |
| garciadeblas | 4053987 | 2024-09-11 14:28:38 +0200 | [diff] [blame] | 30 | self.logger = logging.getLogger("lcm.gitops") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 31 | self.lcm_tasks = lcm_tasks |
| 32 | self.logger.info("Msg: {} lcm_tasks: {} ".format(msg, lcm_tasks)) |
| 33 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 34 | # self._kubeconfig = kubeconfig # TODO: get it from config |
| garciadeblas | 3364c47 | 2024-09-11 14:30:26 +0200 | [diff] [blame] | 35 | self.gitops_config = config["gitops"] |
| 36 | self.logger.debug(f"Config: {self.gitops_config}") |
| garciadeblas | b897695 | 2024-10-18 11:36:15 +0200 | [diff] [blame] | 37 | self._odu_checkloop_retry_time = 15 |
| garciadeblas | 7241228 | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 38 | self._kubeconfig = self.gitops_config["mgmtcluster_kubeconfig"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 39 | self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig) |
| garciadeblas | 3364c47 | 2024-09-11 14:30:26 +0200 | [diff] [blame] | 40 | self._repo_base_url = self.gitops_config["git_base_url"] |
| 41 | self._repo_user = self.gitops_config["user"] |
| 42 | self._pubkey = self.gitops_config["pubkey"] |
| garciadeblas | 326144e | 2025-01-27 11:16:27 +0100 | [diff] [blame] | 43 | self._workflow_debug = str(self.gitops_config["workflow_debug"]).lower() |
| 44 | self._workflow_dry_run = str(self.gitops_config["workflow_dry_run"]).lower() |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 45 | self._workflows = { |
| 46 | "create_cluster": { |
| 47 | "workflow_function": self.create_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 48 | "clean_function": self.clean_items_cluster_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 49 | }, |
| 50 | "update_cluster": { |
| 51 | "workflow_function": self.update_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 52 | "clean_function": self.clean_items_cluster_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 53 | }, |
| 54 | "delete_cluster": { |
| 55 | "workflow_function": self.delete_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 56 | }, |
| 57 | "register_cluster": { |
| 58 | "workflow_function": self.register_cluster, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 59 | "clean_function": self.clean_items_cluster_register, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 60 | }, |
| 61 | "deregister_cluster": { |
| 62 | "workflow_function": self.deregister_cluster, |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 63 | "clean_function": self.clean_items_cluster_deregister, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 64 | }, |
| 65 | "create_profile": { |
| 66 | "workflow_function": self.create_profile, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 67 | }, |
| 68 | "delete_profile": { |
| 69 | "workflow_function": self.delete_profile, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 70 | }, |
| 71 | "attach_profile_to_cluster": { |
| 72 | "workflow_function": self.attach_profile_to_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 73 | }, |
| 74 | "detach_profile_from_cluster": { |
| 75 | "workflow_function": self.detach_profile_from_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 76 | }, |
| 77 | "create_oka": { |
| 78 | "workflow_function": self.create_oka, |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 79 | "clean_function": self.clean_items_oka_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 80 | }, |
| 81 | "update_oka": { |
| 82 | "workflow_function": self.update_oka, |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 83 | "clean_function": self.clean_items_oka_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 84 | }, |
| 85 | "delete_oka": { |
| 86 | "workflow_function": self.delete_oka, |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 87 | "clean_function": self.clean_items_oka_delete, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 88 | }, |
| 89 | "create_ksus": { |
| 90 | "workflow_function": self.create_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 91 | "clean_function": self.clean_items_ksu_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 92 | }, |
| 93 | "update_ksus": { |
| 94 | "workflow_function": self.update_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 95 | "clean_function": self.clean_items_ksu_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 96 | }, |
| 97 | "delete_ksus": { |
| 98 | "workflow_function": self.delete_ksus, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 99 | }, |
| 100 | "clone_ksu": { |
| 101 | "workflow_function": self.clone_ksu, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 102 | }, |
| 103 | "move_ksu": { |
| 104 | "workflow_function": self.move_ksu, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 105 | }, |
| 106 | "create_cloud_credentials": { |
| 107 | "workflow_function": self.create_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 108 | "clean_function": self.clean_items_cloud_credentials_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 109 | }, |
| 110 | "update_cloud_credentials": { |
| 111 | "workflow_function": self.update_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 112 | "clean_function": self.clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 113 | }, |
| 114 | "delete_cloud_credentials": { |
| 115 | "workflow_function": self.delete_cloud_credentials, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 116 | }, |
| 117 | "dummy_operation": { |
| 118 | "workflow_function": self.dummy_operation, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 119 | }, |
| 120 | } |
| 121 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 122 | super().__init__(msg, self.logger) |
| 123 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 124 | @property |
| 125 | def kubeconfig(self): |
| 126 | return self._kubeconfig |
| 127 | |
| 128 | # Imported methods |
| 129 | from osm_lcm.odu_libs.vim_mgmt import ( |
| 130 | create_cloud_credentials, |
| 131 | update_cloud_credentials, |
| 132 | delete_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 133 | clean_items_cloud_credentials_create, |
| 134 | clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 135 | ) |
| 136 | from osm_lcm.odu_libs.cluster_mgmt import ( |
| 137 | create_cluster, |
| 138 | update_cluster, |
| 139 | delete_cluster, |
| 140 | register_cluster, |
| 141 | deregister_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 142 | clean_items_cluster_create, |
| 143 | clean_items_cluster_update, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 144 | clean_items_cluster_register, |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 145 | clean_items_cluster_deregister, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 146 | get_cluster_credentials, |
| 147 | ) |
| 148 | from osm_lcm.odu_libs.ksu import ( |
| 149 | create_ksus, |
| 150 | update_ksus, |
| 151 | delete_ksus, |
| 152 | clone_ksu, |
| 153 | move_ksu, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 154 | clean_items_ksu_create, |
| 155 | clean_items_ksu_update, |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 156 | clean_items_ksu_delete, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 157 | ) |
| 158 | from osm_lcm.odu_libs.oka import ( |
| 159 | create_oka, |
| 160 | update_oka, |
| 161 | delete_oka, |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 162 | clean_items_oka_create, |
| 163 | clean_items_oka_update, |
| 164 | clean_items_oka_delete, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 165 | ) |
| 166 | from osm_lcm.odu_libs.profiles import ( |
| 167 | create_profile, |
| 168 | delete_profile, |
| 169 | attach_profile_to_cluster, |
| 170 | detach_profile_from_cluster, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 171 | ) |
| 172 | from osm_lcm.odu_libs.workflows import ( |
| 173 | check_workflow_status, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 174 | readiness_loop, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 175 | ) |
| 176 | from osm_lcm.odu_libs.render import ( |
| 177 | render_jinja_template, |
| 178 | render_yaml_template, |
| 179 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 180 | from osm_lcm.odu_libs.common import ( |
| 181 | create_secret, |
| 182 | delete_secret, |
| 183 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 184 | |
| 185 | async def launch_workflow(self, key, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 186 | self.logger.info( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 187 | 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] | 188 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 189 | workflow_function = self._workflows[key]["workflow_function"] |
| 190 | self.logger.info("workflow function : {}".format(workflow_function)) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 191 | try: |
| 192 | result, workflow_name = await workflow_function(op_id, op_params, content) |
| 193 | return result, workflow_name |
| 194 | except Exception as e: |
| 195 | self.logger.error(f"Error launching workflow: {e}") |
| 196 | return False, str(e) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 197 | |
| garciadeblas | 5359e99 | 2024-12-13 11:09:04 +0100 | [diff] [blame] | 198 | async def dummy_clean_items(self, op_id, op_params, content): |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 199 | self.logger.info( |
| garciadeblas | 5359e99 | 2024-12-13 11:09:04 +0100 | [diff] [blame] | 200 | f"dummy_clean_items Enter. Operation {op_id}. Params: {op_params}" |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 201 | ) |
| garciadeblas | 5359e99 | 2024-12-13 11:09:04 +0100 | [diff] [blame] | 202 | self.logger.debug(f"Content: {content}") |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 203 | return True, "OK" |
| 204 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 205 | async def clean_items_workflow(self, key, op_id, op_params, content): |
| 206 | self.logger.info( |
| 207 | f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 208 | ) |
| garciadeblas | 98f9a3d | 2024-12-10 13:42:47 +0100 | [diff] [blame] | 209 | clean_items_function = self._workflows[key].get( |
| 210 | "clean_function", self.dummy_clean_items |
| 211 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 212 | self.logger.info("clean items function : {}".format(clean_items_function)) |
| 213 | return await clean_items_function(op_id, op_params, content) |
| 214 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 215 | async def dummy_operation(self, op_id, op_params, content): |
| 216 | self.logger.info("Empty operation status Enter") |
| 217 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 218 | return content["workflow_name"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 219 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 220 | async def clean_items(self, items): |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame^] | 221 | # Delete pods |
| 222 | for pod in items.get("pods", []): |
| 223 | name = pod["name"] |
| 224 | namespace = pod["namespace"] |
| 225 | self.logger.info(f"Deleting pod {name} in namespace {namespace}") |
| 226 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 227 | self.logger.debug( |
| 228 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 229 | ) |
| 230 | self.logger.debug( |
| 231 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 232 | ) |
| 233 | await self._kubectl.delete_pod(name, namespace) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 234 | # Delete secrets |
| 235 | for secret in items.get("secrets", []): |
| 236 | name = secret["name"] |
| 237 | namespace = secret["namespace"] |
| 238 | self.logger.info(f"Deleting secret {name} in namespace {namespace}") |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 239 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 240 | self.logger.debug( |
| 241 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 242 | ) |
| 243 | self.logger.debug( |
| 244 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 245 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 246 | self.delete_secret(name, namespace) |
| 247 | # Delete pvcs |
| 248 | for pvc in items.get("pvcs", []): |
| 249 | name = pvc["name"] |
| 250 | namespace = pvc["namespace"] |
| 251 | self.logger.info(f"Deleting pvc {name} in namespace {namespace}") |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 252 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 253 | self.logger.debug( |
| 254 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 255 | ) |
| 256 | self.logger.debug( |
| 257 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 258 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 259 | await self._kubectl.delete_pvc(name, namespace) |