blob: dac29d2c2f0ccb7f5b7219d513f05adc161ec847 [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
19
garciadeblas98a7a342024-08-22 10:05:47 +020020from n2vc import kubectl
garciadeblas96b94f52024-07-08 16:18:21 +020021
rshri932105f2024-07-05 15:11:55 +000022
23class 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
garciadeblas40539872024-09-11 14:28:38 +020031 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +000032 self.lcm_tasks = lcm_tasks
33 self.logger.info("Msg: {} lcm_tasks: {} ".format(msg, lcm_tasks))
34
garciadeblas96b94f52024-07-08 16:18:21 +020035 # self._kubeconfig = kubeconfig # TODO: get it from config
garciadeblas3364c472024-09-11 14:30:26 +020036 self.gitops_config = config["gitops"]
37 self.logger.debug(f"Config: {self.gitops_config}")
garciadeblasb8976952024-10-18 11:36:15 +020038 self._odu_checkloop_retry_time = 15
garciadeblas72412282024-11-07 12:41:54 +010039 self._kubeconfig = self.gitops_config["mgmtcluster_kubeconfig"]
garciadeblas96b94f52024-07-08 16:18:21 +020040 self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig)
garciadeblas3364c472024-09-11 14:30:26 +020041 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"]
garciadeblas96b94f52024-07-08 16:18:21 +020044 self._workflow_debug = "true"
45 self._workflow_dry_run = "false"
46 self._workflows = {
47 "create_cluster": {
48 "workflow_function": self.create_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +020049 "clean_function": self.clean_items_cluster_create,
garciadeblas96b94f52024-07-08 16:18:21 +020050 },
51 "update_cluster": {
52 "workflow_function": self.update_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +020053 "clean_function": self.clean_items_cluster_update,
garciadeblas96b94f52024-07-08 16:18:21 +020054 },
55 "delete_cluster": {
56 "workflow_function": self.delete_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020057 },
58 "register_cluster": {
59 "workflow_function": self.register_cluster,
garciadeblasdde3a312024-09-17 13:25:06 +020060 "clean_function": self.clean_items_cluster_register,
garciadeblas96b94f52024-07-08 16:18:21 +020061 },
62 "deregister_cluster": {
63 "workflow_function": self.deregister_cluster,
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,
garciadeblas96b94f52024-07-08 16:18:21 +0200142 get_cluster_credentials,
143 )
144 from osm_lcm.odu_libs.ksu import (
145 create_ksus,
146 update_ksus,
147 delete_ksus,
148 clone_ksu,
149 move_ksu,
garciadeblasd8429852024-10-17 15:30:30 +0200150 clean_items_ksu_create,
151 clean_items_ksu_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200152 )
153 from osm_lcm.odu_libs.oka import (
154 create_oka,
155 update_oka,
156 delete_oka,
garciadeblas96b94f52024-07-08 16:18:21 +0200157 )
158 from osm_lcm.odu_libs.profiles import (
159 create_profile,
160 delete_profile,
161 attach_profile_to_cluster,
162 detach_profile_from_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +0200163 )
164 from osm_lcm.odu_libs.workflows import (
165 check_workflow_status,
garciadeblas40811852024-10-22 11:35:17 +0200166 readiness_loop,
garciadeblas96b94f52024-07-08 16:18:21 +0200167 )
168 from osm_lcm.odu_libs.render import (
169 render_jinja_template,
170 render_yaml_template,
171 )
garciadeblas28bff0f2024-09-16 12:53:07 +0200172 from osm_lcm.odu_libs.common import (
173 create_secret,
174 delete_secret,
175 )
garciadeblas96b94f52024-07-08 16:18:21 +0200176
177 async def launch_workflow(self, key, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000178 self.logger.info(
garciadeblas96b94f52024-07-08 16:18:21 +0200179 f"Workflow is getting into launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
rshri932105f2024-07-05 15:11:55 +0000180 )
garciadeblas96b94f52024-07-08 16:18:21 +0200181 workflow_function = self._workflows[key]["workflow_function"]
182 self.logger.info("workflow function : {}".format(workflow_function))
183 return await workflow_function(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000184
garciadeblas28bff0f2024-09-16 12:53:07 +0200185 async def clean_items_workflow(self, key, op_id, op_params, content):
186 self.logger.info(
187 f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
188 )
189 clean_items_function = self._workflows[key]["clean_function"]
190 self.logger.info("clean items function : {}".format(clean_items_function))
191 return await clean_items_function(op_id, op_params, content)
192
garciadeblas96b94f52024-07-08 16:18:21 +0200193 async def dummy_operation(self, op_id, op_params, content):
194 self.logger.info("Empty operation status Enter")
195 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
196 return content["workflow_name"]
rshri932105f2024-07-05 15:11:55 +0000197
garciadeblas28bff0f2024-09-16 12:53:07 +0200198 async def clean_items(self, items):
199 # Delete secrets
200 for secret in items.get("secrets", []):
201 name = secret["name"]
202 namespace = secret["namespace"]
203 self.logger.info(f"Deleting secret {name} in namespace {namespace}")
204 self.delete_secret(name, namespace)
205 # Delete pvcs
206 for pvc in items.get("pvcs", []):
207 name = pvc["name"]
208 namespace = pvc["namespace"]
209 self.logger.info(f"Deleting pvc {name} in namespace {namespace}")
210 await self._kubectl.delete_pvc(name, namespace)