| 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}") |
| 38 | self._kubeconfig = self.gitops_config["mgmtcluster_kubeconfig"] |
| garciadeblas | b897695 | 2024-10-18 11:36:15 +0200 | [diff] [blame^] | 39 | self._odu_checkloop_kustomization_timeout = 900 |
| 40 | self._odu_checkloop_resource_timeout = 900 |
| 41 | self._odu_checkloop_retry_time = 15 |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 42 | self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig) |
| garciadeblas | 3364c47 | 2024-09-11 14:30:26 +0200 | [diff] [blame] | 43 | self._repo_base_url = self.gitops_config["git_base_url"] |
| 44 | self._repo_user = self.gitops_config["user"] |
| 45 | self._pubkey = self.gitops_config["pubkey"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 46 | self._workflow_debug = "true" |
| 47 | self._workflow_dry_run = "false" |
| 48 | self._workflows = { |
| 49 | "create_cluster": { |
| 50 | "workflow_function": self.create_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 51 | "clean_function": self.clean_items_cluster_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 52 | "check_resource_function": self.check_create_cluster, |
| 53 | }, |
| 54 | "update_cluster": { |
| 55 | "workflow_function": self.update_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 56 | "clean_function": self.clean_items_cluster_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 57 | "check_resource_function": self.check_update_cluster, |
| 58 | }, |
| 59 | "delete_cluster": { |
| 60 | "workflow_function": self.delete_cluster, |
| 61 | "check_resource_function": self.check_delete_cluster, |
| 62 | }, |
| 63 | "register_cluster": { |
| 64 | "workflow_function": self.register_cluster, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 65 | "clean_function": self.clean_items_cluster_register, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 66 | "check_resource_function": self.check_register_cluster, |
| 67 | }, |
| 68 | "deregister_cluster": { |
| 69 | "workflow_function": self.deregister_cluster, |
| 70 | "check_resource_function": self.check_deregister_cluster, |
| 71 | }, |
| 72 | "create_profile": { |
| 73 | "workflow_function": self.create_profile, |
| 74 | "check_resource_function": self.check_create_profile, |
| 75 | }, |
| 76 | "delete_profile": { |
| 77 | "workflow_function": self.delete_profile, |
| 78 | "check_resource_function": self.check_delete_profile, |
| 79 | }, |
| 80 | "attach_profile_to_cluster": { |
| 81 | "workflow_function": self.attach_profile_to_cluster, |
| 82 | "check_resource_function": self.check_attach_profile_to_cluster, |
| 83 | }, |
| 84 | "detach_profile_from_cluster": { |
| 85 | "workflow_function": self.detach_profile_from_cluster, |
| 86 | "check_resource_function": self.check_detach_profile_from_cluster, |
| 87 | }, |
| 88 | "create_oka": { |
| 89 | "workflow_function": self.create_oka, |
| 90 | "check_resource_function": self.check_create_oka, |
| 91 | }, |
| 92 | "update_oka": { |
| 93 | "workflow_function": self.update_oka, |
| 94 | "check_resource_function": self.check_update_oka, |
| 95 | }, |
| 96 | "delete_oka": { |
| 97 | "workflow_function": self.delete_oka, |
| 98 | "check_resource_function": self.check_delete_oka, |
| 99 | }, |
| 100 | "create_ksus": { |
| 101 | "workflow_function": self.create_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 102 | "clean_function": self.clean_items_ksu_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 103 | "check_resource_function": self.check_create_ksus, |
| 104 | }, |
| 105 | "update_ksus": { |
| 106 | "workflow_function": self.update_ksus, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 107 | "clean_function": self.clean_items_ksu_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 108 | "check_resource_function": self.check_update_ksus, |
| 109 | }, |
| 110 | "delete_ksus": { |
| 111 | "workflow_function": self.delete_ksus, |
| 112 | "check_resource_function": self.check_delete_ksus, |
| 113 | }, |
| 114 | "clone_ksu": { |
| 115 | "workflow_function": self.clone_ksu, |
| 116 | "check_resource_function": self.check_clone_ksu, |
| 117 | }, |
| 118 | "move_ksu": { |
| 119 | "workflow_function": self.move_ksu, |
| 120 | "check_resource_function": self.check_move_ksu, |
| 121 | }, |
| 122 | "create_cloud_credentials": { |
| 123 | "workflow_function": self.create_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 124 | "clean_function": self.clean_items_cloud_credentials_create, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 125 | "check_resource_function": self.check_create_cloud_credentials, |
| 126 | }, |
| 127 | "update_cloud_credentials": { |
| 128 | "workflow_function": self.update_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 129 | "clean_function": self.clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 130 | "check_resource_function": self.check_update_cloud_credentials, |
| 131 | }, |
| 132 | "delete_cloud_credentials": { |
| 133 | "workflow_function": self.delete_cloud_credentials, |
| 134 | "check_resource_function": self.check_delete_cloud_credentials, |
| 135 | }, |
| 136 | "dummy_operation": { |
| 137 | "workflow_function": self.dummy_operation, |
| 138 | "check_resource_function": self.check_dummy_operation, |
| 139 | }, |
| 140 | } |
| 141 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 142 | super().__init__(msg, self.logger) |
| 143 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 144 | @property |
| 145 | def kubeconfig(self): |
| 146 | return self._kubeconfig |
| 147 | |
| 148 | # Imported methods |
| 149 | from osm_lcm.odu_libs.vim_mgmt import ( |
| 150 | create_cloud_credentials, |
| 151 | update_cloud_credentials, |
| 152 | delete_cloud_credentials, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 153 | clean_items_cloud_credentials_create, |
| 154 | clean_items_cloud_credentials_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 155 | check_create_cloud_credentials, |
| 156 | check_update_cloud_credentials, |
| 157 | check_delete_cloud_credentials, |
| 158 | ) |
| 159 | from osm_lcm.odu_libs.cluster_mgmt import ( |
| 160 | create_cluster, |
| 161 | update_cluster, |
| 162 | delete_cluster, |
| 163 | register_cluster, |
| 164 | deregister_cluster, |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 165 | clean_items_cluster_create, |
| 166 | clean_items_cluster_update, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 167 | clean_items_cluster_register, |
| garciadeblas | b897695 | 2024-10-18 11:36:15 +0200 | [diff] [blame^] | 168 | check_kustomization_ready, |
| 169 | check_cluster_flag, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 170 | check_create_cluster, |
| 171 | check_update_cluster, |
| 172 | check_delete_cluster, |
| 173 | check_register_cluster, |
| 174 | check_deregister_cluster, |
| 175 | get_cluster_credentials, |
| 176 | ) |
| 177 | from osm_lcm.odu_libs.ksu import ( |
| 178 | create_ksus, |
| 179 | update_ksus, |
| 180 | delete_ksus, |
| 181 | clone_ksu, |
| 182 | move_ksu, |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 183 | clean_items_ksu_create, |
| 184 | clean_items_ksu_update, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 185 | check_create_ksus, |
| 186 | check_update_ksus, |
| 187 | check_delete_ksus, |
| 188 | check_clone_ksu, |
| 189 | check_move_ksu, |
| 190 | ) |
| 191 | from osm_lcm.odu_libs.oka import ( |
| 192 | create_oka, |
| 193 | update_oka, |
| 194 | delete_oka, |
| 195 | check_create_oka, |
| 196 | check_update_oka, |
| 197 | check_delete_oka, |
| 198 | ) |
| 199 | from osm_lcm.odu_libs.profiles import ( |
| 200 | create_profile, |
| 201 | delete_profile, |
| 202 | attach_profile_to_cluster, |
| 203 | detach_profile_from_cluster, |
| 204 | check_create_profile, |
| 205 | check_delete_profile, |
| 206 | check_attach_profile_to_cluster, |
| 207 | check_detach_profile_from_cluster, |
| 208 | ) |
| 209 | from osm_lcm.odu_libs.workflows import ( |
| 210 | check_workflow_status, |
| garciadeblas | 4081185 | 2024-10-22 11:35:17 +0200 | [diff] [blame] | 211 | readiness_loop, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 212 | ) |
| 213 | from osm_lcm.odu_libs.render import ( |
| 214 | render_jinja_template, |
| 215 | render_yaml_template, |
| 216 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 217 | from osm_lcm.odu_libs.common import ( |
| 218 | create_secret, |
| 219 | delete_secret, |
| 220 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 221 | |
| 222 | async def launch_workflow(self, key, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 223 | self.logger.info( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 224 | 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] | 225 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 226 | workflow_function = self._workflows[key]["workflow_function"] |
| 227 | self.logger.info("workflow function : {}".format(workflow_function)) |
| 228 | return await workflow_function(op_id, op_params, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 229 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 230 | async def clean_items_workflow(self, key, op_id, op_params, content): |
| 231 | self.logger.info( |
| 232 | f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 233 | ) |
| 234 | clean_items_function = self._workflows[key]["clean_function"] |
| 235 | self.logger.info("clean items function : {}".format(clean_items_function)) |
| 236 | return await clean_items_function(op_id, op_params, content) |
| 237 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 238 | async def dummy_operation(self, op_id, op_params, content): |
| 239 | self.logger.info("Empty operation status Enter") |
| 240 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 241 | return content["workflow_name"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 242 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 243 | async def check_resource_status(self, key, op_id, op_params, content): |
| 244 | self.logger.info( |
| 245 | f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 246 | ) |
| 247 | check_resource_function = self._workflows[key]["check_resource_function"] |
| 248 | self.logger.info("check_resource function : {}".format(check_resource_function)) |
| 249 | return await check_resource_function(op_id, op_params, content) |
| 250 | |
| 251 | async def check_dummy_operation(self, op_id, op_params, content): |
| 252 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 253 | return True, "OK" |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 254 | |
| 255 | async def clean_items(self, items): |
| 256 | # Delete secrets |
| 257 | for secret in items.get("secrets", []): |
| 258 | name = secret["name"] |
| 259 | namespace = secret["namespace"] |
| 260 | self.logger.info(f"Deleting secret {name} in namespace {namespace}") |
| 261 | self.delete_secret(name, namespace) |
| 262 | # Delete pvcs |
| 263 | for pvc in items.get("pvcs", []): |
| 264 | name = pvc["name"] |
| 265 | namespace = pvc["namespace"] |
| 266 | self.logger.info(f"Deleting pvc {name} in namespace {namespace}") |
| 267 | await self._kubectl.delete_pvc(name, namespace) |