blob: 9c8e6e04b45eebda5621ddbe6cc8549852fedfe5 [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
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
rshri932105f2024-07-05 15:11:55 +00005#
garciadeblas96b94f52024-07-08 16:18:21 +02006# http://www.apache.org/licenses/LICENSE-2.0
rshri932105f2024-07-05 15:11:55 +00007#
8# Unless required by applicable law or agreed to in writing, software
garciadeblas96b94f52024-07-08 16:18:21 +02009# 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#######################################################################################
rshri932105f2024-07-05 15:11:55 +000015
16
17import logging
18from osm_lcm.lcm_utils import LcmBase
almagiacdd20ae2024-12-13 09:45:45 +010019from osm_lcm.n2vc import kubectl
garciadeblas96b94f52024-07-08 16:18:21 +020020
rshri932105f2024-07-05 15:11:55 +000021
22class 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
garciadeblas40539872024-09-11 14:28:38 +020030 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +000031 self.lcm_tasks = lcm_tasks
32 self.logger.info("Msg: {} lcm_tasks: {} ".format(msg, lcm_tasks))
33
garciadeblas96b94f52024-07-08 16:18:21 +020034 # self._kubeconfig = kubeconfig # TODO: get it from config
garciadeblas3364c472024-09-11 14:30:26 +020035 self.gitops_config = config["gitops"]
36 self.logger.debug(f"Config: {self.gitops_config}")
garciadeblasb8976952024-10-18 11:36:15 +020037 self._odu_checkloop_retry_time = 15
garciadeblas72412282024-11-07 12:41:54 +010038 self._kubeconfig = self.gitops_config["mgmtcluster_kubeconfig"]
garciadeblas96b94f52024-07-08 16:18:21 +020039 self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig)
garciadeblas3364c472024-09-11 14:30:26 +020040 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"]
garciadeblas0283bb22025-01-27 11:16:27 +010043 self._workflow_debug = str(self.gitops_config["workflow_debug"]).lower()
44 self._workflow_dry_run = str(self.gitops_config["workflow_dry_run"]).lower()
garciadeblas96b94f52024-07-08 16:18:21 +020045 self._workflows = {
46 "create_cluster": {
47 "workflow_function": self.create_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +020048 "clean_function": self.clean_items_cluster_create,
garciadeblas96b94f52024-07-08 16:18:21 +020049 },
50 "update_cluster": {
51 "workflow_function": self.update_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +020052 "clean_function": self.clean_items_cluster_update,
garciadeblas96b94f52024-07-08 16:18:21 +020053 },
54 "delete_cluster": {
55 "workflow_function": self.delete_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020056 },
57 "register_cluster": {
58 "workflow_function": self.register_cluster,
garciadeblasdde3a312024-09-17 13:25:06 +020059 "clean_function": self.clean_items_cluster_register,
garciadeblas96b94f52024-07-08 16:18:21 +020060 },
61 "deregister_cluster": {
62 "workflow_function": self.deregister_cluster,
garciadeblas91bb2c42024-11-12 11:17:12 +010063 "clean_function": self.clean_items_cluster_deregister,
garciadeblas96b94f52024-07-08 16:18:21 +020064 },
65 "create_profile": {
66 "workflow_function": self.create_profile,
garciadeblas96b94f52024-07-08 16:18:21 +020067 },
68 "delete_profile": {
69 "workflow_function": self.delete_profile,
garciadeblas96b94f52024-07-08 16:18:21 +020070 },
71 "attach_profile_to_cluster": {
72 "workflow_function": self.attach_profile_to_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020073 },
74 "detach_profile_from_cluster": {
75 "workflow_function": self.detach_profile_from_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020076 },
77 "create_oka": {
78 "workflow_function": self.create_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020079 },
80 "update_oka": {
81 "workflow_function": self.update_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020082 },
83 "delete_oka": {
84 "workflow_function": self.delete_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020085 },
86 "create_ksus": {
87 "workflow_function": self.create_ksus,
garciadeblasd8429852024-10-17 15:30:30 +020088 "clean_function": self.clean_items_ksu_create,
garciadeblas96b94f52024-07-08 16:18:21 +020089 },
90 "update_ksus": {
91 "workflow_function": self.update_ksus,
garciadeblasd8429852024-10-17 15:30:30 +020092 "clean_function": self.clean_items_ksu_update,
garciadeblas96b94f52024-07-08 16:18:21 +020093 },
94 "delete_ksus": {
95 "workflow_function": self.delete_ksus,
garciadeblas96b94f52024-07-08 16:18:21 +020096 },
97 "clone_ksu": {
98 "workflow_function": self.clone_ksu,
garciadeblas96b94f52024-07-08 16:18:21 +020099 },
100 "move_ksu": {
101 "workflow_function": self.move_ksu,
garciadeblas96b94f52024-07-08 16:18:21 +0200102 },
103 "create_cloud_credentials": {
104 "workflow_function": self.create_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200105 "clean_function": self.clean_items_cloud_credentials_create,
garciadeblas96b94f52024-07-08 16:18:21 +0200106 },
107 "update_cloud_credentials": {
108 "workflow_function": self.update_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200109 "clean_function": self.clean_items_cloud_credentials_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200110 },
111 "delete_cloud_credentials": {
112 "workflow_function": self.delete_cloud_credentials,
garciadeblas96b94f52024-07-08 16:18:21 +0200113 },
114 "dummy_operation": {
115 "workflow_function": self.dummy_operation,
garciadeblas96b94f52024-07-08 16:18:21 +0200116 },
117 }
118
rshri932105f2024-07-05 15:11:55 +0000119 super().__init__(msg, self.logger)
120
garciadeblas96b94f52024-07-08 16:18:21 +0200121 @property
122 def kubeconfig(self):
123 return self._kubeconfig
124
125 # Imported methods
126 from osm_lcm.odu_libs.vim_mgmt import (
127 create_cloud_credentials,
128 update_cloud_credentials,
129 delete_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200130 clean_items_cloud_credentials_create,
131 clean_items_cloud_credentials_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200132 )
133 from osm_lcm.odu_libs.cluster_mgmt import (
134 create_cluster,
135 update_cluster,
136 delete_cluster,
137 register_cluster,
138 deregister_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +0200139 clean_items_cluster_create,
140 clean_items_cluster_update,
garciadeblasdde3a312024-09-17 13:25:06 +0200141 clean_items_cluster_register,
garciadeblas91bb2c42024-11-12 11:17:12 +0100142 clean_items_cluster_deregister,
garciadeblas96b94f52024-07-08 16:18:21 +0200143 get_cluster_credentials,
144 )
145 from osm_lcm.odu_libs.ksu import (
146 create_ksus,
147 update_ksus,
148 delete_ksus,
149 clone_ksu,
150 move_ksu,
garciadeblasd8429852024-10-17 15:30:30 +0200151 clean_items_ksu_create,
152 clean_items_ksu_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200153 )
154 from osm_lcm.odu_libs.oka import (
155 create_oka,
156 update_oka,
157 delete_oka,
garciadeblas96b94f52024-07-08 16:18:21 +0200158 )
159 from osm_lcm.odu_libs.profiles import (
160 create_profile,
161 delete_profile,
162 attach_profile_to_cluster,
163 detach_profile_from_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +0200164 )
165 from osm_lcm.odu_libs.workflows import (
166 check_workflow_status,
garciadeblas40811852024-10-22 11:35:17 +0200167 readiness_loop,
garciadeblas96b94f52024-07-08 16:18:21 +0200168 )
169 from osm_lcm.odu_libs.render import (
170 render_jinja_template,
171 render_yaml_template,
172 )
garciadeblas28bff0f2024-09-16 12:53:07 +0200173 from osm_lcm.odu_libs.common import (
174 create_secret,
175 delete_secret,
176 )
garciadeblas96b94f52024-07-08 16:18:21 +0200177
178 async def launch_workflow(self, key, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000179 self.logger.info(
garciadeblas96b94f52024-07-08 16:18:21 +0200180 f"Workflow is getting into launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
rshri932105f2024-07-05 15:11:55 +0000181 )
garciadeblas96b94f52024-07-08 16:18:21 +0200182 workflow_function = self._workflows[key]["workflow_function"]
183 self.logger.info("workflow function : {}".format(workflow_function))
184 return await workflow_function(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000185
garciadeblas5359e992024-12-13 11:09:04 +0100186 async def dummy_clean_items(self, op_id, op_params, content):
garciadeblas98f9a3d2024-12-10 13:42:47 +0100187 self.logger.info(
garciadeblas5359e992024-12-13 11:09:04 +0100188 f"dummy_clean_items Enter. Operation {op_id}. Params: {op_params}"
garciadeblas98f9a3d2024-12-10 13:42:47 +0100189 )
garciadeblas5359e992024-12-13 11:09:04 +0100190 self.logger.debug(f"Content: {content}")
garciadeblas98f9a3d2024-12-10 13:42:47 +0100191 return True, "OK"
192
garciadeblas28bff0f2024-09-16 12:53:07 +0200193 async def clean_items_workflow(self, key, op_id, op_params, content):
194 self.logger.info(
195 f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
196 )
garciadeblas98f9a3d2024-12-10 13:42:47 +0100197 clean_items_function = self._workflows[key].get(
198 "clean_function", self.dummy_clean_items
199 )
garciadeblas28bff0f2024-09-16 12:53:07 +0200200 self.logger.info("clean items function : {}".format(clean_items_function))
201 return await clean_items_function(op_id, op_params, content)
202
garciadeblas96b94f52024-07-08 16:18:21 +0200203 async def dummy_operation(self, op_id, op_params, content):
204 self.logger.info("Empty operation status Enter")
205 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
206 return content["workflow_name"]
rshri932105f2024-07-05 15:11:55 +0000207
garciadeblas28bff0f2024-09-16 12:53:07 +0200208 async def clean_items(self, items):
209 # Delete secrets
210 for secret in items.get("secrets", []):
211 name = secret["name"]
212 namespace = secret["namespace"]
213 self.logger.info(f"Deleting secret {name} in namespace {namespace}")
214 self.delete_secret(name, namespace)
215 # Delete pvcs
216 for pvc in items.get("pvcs", []):
217 name = pvc["name"]
218 namespace = pvc["namespace"]
219 self.logger.info(f"Deleting pvc {name} in namespace {namespace}")
220 await self._kubectl.delete_pvc(name, namespace)