| 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 | 4623e98 | 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 | 5b09864 | 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 | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 39 | self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig) |
| garciadeblas | 5b09864 | 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 | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 43 | self._workflow_debug = "true" |
| 44 | self._workflow_dry_run = "false" |
| 45 | self._workflows = { |
| 46 | "create_cluster": { |
| 47 | "workflow_function": self.create_cluster, |
| 48 | "check_resource_function": self.check_create_cluster, |
| 49 | }, |
| 50 | "update_cluster": { |
| 51 | "workflow_function": self.update_cluster, |
| 52 | "check_resource_function": self.check_update_cluster, |
| 53 | }, |
| 54 | "delete_cluster": { |
| 55 | "workflow_function": self.delete_cluster, |
| 56 | "check_resource_function": self.check_delete_cluster, |
| 57 | }, |
| 58 | "register_cluster": { |
| 59 | "workflow_function": self.register_cluster, |
| 60 | "check_resource_function": self.check_register_cluster, |
| 61 | }, |
| 62 | "deregister_cluster": { |
| 63 | "workflow_function": self.deregister_cluster, |
| 64 | "check_resource_function": self.check_deregister_cluster, |
| 65 | }, |
| 66 | "create_profile": { |
| 67 | "workflow_function": self.create_profile, |
| 68 | "check_resource_function": self.check_create_profile, |
| 69 | }, |
| 70 | "delete_profile": { |
| 71 | "workflow_function": self.delete_profile, |
| 72 | "check_resource_function": self.check_delete_profile, |
| 73 | }, |
| 74 | "attach_profile_to_cluster": { |
| 75 | "workflow_function": self.attach_profile_to_cluster, |
| 76 | "check_resource_function": self.check_attach_profile_to_cluster, |
| 77 | }, |
| 78 | "detach_profile_from_cluster": { |
| 79 | "workflow_function": self.detach_profile_from_cluster, |
| 80 | "check_resource_function": self.check_detach_profile_from_cluster, |
| 81 | }, |
| 82 | "create_oka": { |
| 83 | "workflow_function": self.create_oka, |
| 84 | "check_resource_function": self.check_create_oka, |
| 85 | }, |
| 86 | "update_oka": { |
| 87 | "workflow_function": self.update_oka, |
| 88 | "check_resource_function": self.check_update_oka, |
| 89 | }, |
| 90 | "delete_oka": { |
| 91 | "workflow_function": self.delete_oka, |
| 92 | "check_resource_function": self.check_delete_oka, |
| 93 | }, |
| 94 | "create_ksus": { |
| 95 | "workflow_function": self.create_ksus, |
| 96 | "check_resource_function": self.check_create_ksus, |
| 97 | }, |
| 98 | "update_ksus": { |
| 99 | "workflow_function": self.update_ksus, |
| 100 | "check_resource_function": self.check_update_ksus, |
| 101 | }, |
| 102 | "delete_ksus": { |
| 103 | "workflow_function": self.delete_ksus, |
| 104 | "check_resource_function": self.check_delete_ksus, |
| 105 | }, |
| 106 | "clone_ksu": { |
| 107 | "workflow_function": self.clone_ksu, |
| 108 | "check_resource_function": self.check_clone_ksu, |
| 109 | }, |
| 110 | "move_ksu": { |
| 111 | "workflow_function": self.move_ksu, |
| 112 | "check_resource_function": self.check_move_ksu, |
| 113 | }, |
| 114 | "create_cloud_credentials": { |
| 115 | "workflow_function": self.create_cloud_credentials, |
| 116 | "check_resource_function": self.check_create_cloud_credentials, |
| 117 | }, |
| 118 | "update_cloud_credentials": { |
| 119 | "workflow_function": self.update_cloud_credentials, |
| 120 | "check_resource_function": self.check_update_cloud_credentials, |
| 121 | }, |
| 122 | "delete_cloud_credentials": { |
| 123 | "workflow_function": self.delete_cloud_credentials, |
| 124 | "check_resource_function": self.check_delete_cloud_credentials, |
| 125 | }, |
| 126 | "dummy_operation": { |
| 127 | "workflow_function": self.dummy_operation, |
| 128 | "check_resource_function": self.check_dummy_operation, |
| 129 | }, |
| 130 | } |
| 131 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 132 | super().__init__(msg, self.logger) |
| 133 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 134 | @property |
| 135 | def kubeconfig(self): |
| 136 | return self._kubeconfig |
| 137 | |
| 138 | # Imported methods |
| 139 | from osm_lcm.odu_libs.vim_mgmt import ( |
| 140 | create_cloud_credentials, |
| 141 | update_cloud_credentials, |
| 142 | delete_cloud_credentials, |
| 143 | check_create_cloud_credentials, |
| 144 | check_update_cloud_credentials, |
| 145 | check_delete_cloud_credentials, |
| 146 | ) |
| 147 | from osm_lcm.odu_libs.cluster_mgmt import ( |
| 148 | create_cluster, |
| 149 | update_cluster, |
| 150 | delete_cluster, |
| 151 | register_cluster, |
| 152 | deregister_cluster, |
| 153 | check_create_cluster, |
| 154 | check_update_cluster, |
| 155 | check_delete_cluster, |
| 156 | check_register_cluster, |
| 157 | check_deregister_cluster, |
| 158 | get_cluster_credentials, |
| 159 | ) |
| 160 | from osm_lcm.odu_libs.ksu import ( |
| 161 | create_ksus, |
| 162 | update_ksus, |
| 163 | delete_ksus, |
| 164 | clone_ksu, |
| 165 | move_ksu, |
| 166 | check_create_ksus, |
| 167 | check_update_ksus, |
| 168 | check_delete_ksus, |
| 169 | check_clone_ksu, |
| 170 | check_move_ksu, |
| 171 | ) |
| 172 | from osm_lcm.odu_libs.oka import ( |
| 173 | create_oka, |
| 174 | update_oka, |
| 175 | delete_oka, |
| 176 | check_create_oka, |
| 177 | check_update_oka, |
| 178 | check_delete_oka, |
| 179 | ) |
| 180 | from osm_lcm.odu_libs.profiles import ( |
| 181 | create_profile, |
| 182 | delete_profile, |
| 183 | attach_profile_to_cluster, |
| 184 | detach_profile_from_cluster, |
| 185 | check_create_profile, |
| 186 | check_delete_profile, |
| 187 | check_attach_profile_to_cluster, |
| 188 | check_detach_profile_from_cluster, |
| 189 | ) |
| 190 | from osm_lcm.odu_libs.workflows import ( |
| 191 | check_workflow_status, |
| 192 | ) |
| 193 | from osm_lcm.odu_libs.render import ( |
| 194 | render_jinja_template, |
| 195 | render_yaml_template, |
| 196 | ) |
| 197 | from osm_lcm.odu_libs.common import create_secret |
| 198 | |
| 199 | async def launch_workflow(self, key, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 200 | self.logger.info( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 201 | 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] | 202 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 203 | workflow_function = self._workflows[key]["workflow_function"] |
| 204 | self.logger.info("workflow function : {}".format(workflow_function)) |
| 205 | return await workflow_function(op_id, op_params, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 206 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 207 | async def dummy_operation(self, op_id, op_params, content): |
| 208 | self.logger.info("Empty operation status Enter") |
| 209 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 210 | return content["workflow_name"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 211 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 212 | async def check_resource_status(self, key, op_id, op_params, content): |
| 213 | self.logger.info( |
| 214 | f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 215 | ) |
| 216 | check_resource_function = self._workflows[key]["check_resource_function"] |
| 217 | self.logger.info("check_resource function : {}".format(check_resource_function)) |
| 218 | return await check_resource_function(op_id, op_params, content) |
| 219 | |
| 220 | async def check_dummy_operation(self, op_id, op_params, content): |
| 221 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 222 | return True, "OK" |