blob: 7e3f16325d3e9d9ee268d930817c5e3c026f4c2f [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,
garciadeblas91bb2c42024-11-12 11:17:12 +010064 "clean_function": self.clean_items_cluster_deregister,
garciadeblas96b94f52024-07-08 16:18:21 +020065 },
66 "create_profile": {
67 "workflow_function": self.create_profile,
garciadeblas96b94f52024-07-08 16:18:21 +020068 },
69 "delete_profile": {
70 "workflow_function": self.delete_profile,
garciadeblas96b94f52024-07-08 16:18:21 +020071 },
72 "attach_profile_to_cluster": {
73 "workflow_function": self.attach_profile_to_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020074 },
75 "detach_profile_from_cluster": {
76 "workflow_function": self.detach_profile_from_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +020077 },
78 "create_oka": {
79 "workflow_function": self.create_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020080 },
81 "update_oka": {
82 "workflow_function": self.update_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020083 },
84 "delete_oka": {
85 "workflow_function": self.delete_oka,
garciadeblas96b94f52024-07-08 16:18:21 +020086 },
87 "create_ksus": {
88 "workflow_function": self.create_ksus,
garciadeblasd8429852024-10-17 15:30:30 +020089 "clean_function": self.clean_items_ksu_create,
garciadeblas96b94f52024-07-08 16:18:21 +020090 },
91 "update_ksus": {
92 "workflow_function": self.update_ksus,
garciadeblasd8429852024-10-17 15:30:30 +020093 "clean_function": self.clean_items_ksu_update,
garciadeblas96b94f52024-07-08 16:18:21 +020094 },
95 "delete_ksus": {
96 "workflow_function": self.delete_ksus,
garciadeblas96b94f52024-07-08 16:18:21 +020097 },
98 "clone_ksu": {
99 "workflow_function": self.clone_ksu,
garciadeblas96b94f52024-07-08 16:18:21 +0200100 },
101 "move_ksu": {
102 "workflow_function": self.move_ksu,
garciadeblas96b94f52024-07-08 16:18:21 +0200103 },
104 "create_cloud_credentials": {
105 "workflow_function": self.create_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200106 "clean_function": self.clean_items_cloud_credentials_create,
garciadeblas96b94f52024-07-08 16:18:21 +0200107 },
108 "update_cloud_credentials": {
109 "workflow_function": self.update_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200110 "clean_function": self.clean_items_cloud_credentials_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200111 },
112 "delete_cloud_credentials": {
113 "workflow_function": self.delete_cloud_credentials,
garciadeblas96b94f52024-07-08 16:18:21 +0200114 },
115 "dummy_operation": {
116 "workflow_function": self.dummy_operation,
garciadeblas96b94f52024-07-08 16:18:21 +0200117 },
118 }
119
rshri932105f2024-07-05 15:11:55 +0000120 super().__init__(msg, self.logger)
121
garciadeblas96b94f52024-07-08 16:18:21 +0200122 @property
123 def kubeconfig(self):
124 return self._kubeconfig
125
126 # Imported methods
127 from osm_lcm.odu_libs.vim_mgmt import (
128 create_cloud_credentials,
129 update_cloud_credentials,
130 delete_cloud_credentials,
garciadeblas28bff0f2024-09-16 12:53:07 +0200131 clean_items_cloud_credentials_create,
132 clean_items_cloud_credentials_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200133 )
134 from osm_lcm.odu_libs.cluster_mgmt import (
135 create_cluster,
136 update_cluster,
137 delete_cluster,
138 register_cluster,
139 deregister_cluster,
garciadeblas28bff0f2024-09-16 12:53:07 +0200140 clean_items_cluster_create,
141 clean_items_cluster_update,
garciadeblasdde3a312024-09-17 13:25:06 +0200142 clean_items_cluster_register,
garciadeblas91bb2c42024-11-12 11:17:12 +0100143 clean_items_cluster_deregister,
garciadeblas96b94f52024-07-08 16:18:21 +0200144 get_cluster_credentials,
145 )
146 from osm_lcm.odu_libs.ksu import (
147 create_ksus,
148 update_ksus,
149 delete_ksus,
150 clone_ksu,
151 move_ksu,
garciadeblasd8429852024-10-17 15:30:30 +0200152 clean_items_ksu_create,
153 clean_items_ksu_update,
garciadeblas96b94f52024-07-08 16:18:21 +0200154 )
155 from osm_lcm.odu_libs.oka import (
156 create_oka,
157 update_oka,
158 delete_oka,
garciadeblas96b94f52024-07-08 16:18:21 +0200159 )
160 from osm_lcm.odu_libs.profiles import (
161 create_profile,
162 delete_profile,
163 attach_profile_to_cluster,
164 detach_profile_from_cluster,
garciadeblas96b94f52024-07-08 16:18:21 +0200165 )
166 from osm_lcm.odu_libs.workflows import (
167 check_workflow_status,
garciadeblas40811852024-10-22 11:35:17 +0200168 readiness_loop,
garciadeblas96b94f52024-07-08 16:18:21 +0200169 )
170 from osm_lcm.odu_libs.render import (
171 render_jinja_template,
172 render_yaml_template,
173 )
garciadeblas28bff0f2024-09-16 12:53:07 +0200174 from osm_lcm.odu_libs.common import (
175 create_secret,
176 delete_secret,
177 )
garciadeblas96b94f52024-07-08 16:18:21 +0200178
179 async def launch_workflow(self, key, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000180 self.logger.info(
garciadeblas96b94f52024-07-08 16:18:21 +0200181 f"Workflow is getting into launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
rshri932105f2024-07-05 15:11:55 +0000182 )
garciadeblas96b94f52024-07-08 16:18:21 +0200183 workflow_function = self._workflows[key]["workflow_function"]
184 self.logger.info("workflow function : {}".format(workflow_function))
185 return await workflow_function(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000186
garciadeblas28bff0f2024-09-16 12:53:07 +0200187 async def clean_items_workflow(self, key, op_id, op_params, content):
188 self.logger.info(
189 f"Cleaning items created during workflow launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
190 )
191 clean_items_function = self._workflows[key]["clean_function"]
192 self.logger.info("clean items function : {}".format(clean_items_function))
193 return await clean_items_function(op_id, op_params, content)
194
garciadeblas96b94f52024-07-08 16:18:21 +0200195 async def dummy_operation(self, op_id, op_params, content):
196 self.logger.info("Empty operation status Enter")
197 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
198 return content["workflow_name"]
rshri932105f2024-07-05 15:11:55 +0000199
garciadeblas28bff0f2024-09-16 12:53:07 +0200200 async def clean_items(self, items):
201 # Delete secrets
202 for secret in items.get("secrets", []):
203 name = secret["name"]
204 namespace = secret["namespace"]
205 self.logger.info(f"Deleting secret {name} in namespace {namespace}")
206 self.delete_secret(name, namespace)
207 # Delete pvcs
208 for pvc in items.get("pvcs", []):
209 name = pvc["name"]
210 namespace = pvc["namespace"]
211 self.logger.info(f"Deleting pvc {name} in namespace {namespace}")
212 await self._kubectl.delete_pvc(name, namespace)