blob: 7f4d6991b2443cd5411629dc7d10cf3956889c63 [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18
19import yaml
20
21
garciadeblasf7dfdb72024-09-25 12:15:40 +020022MAP_PROFILE = {
23 "infra_controller_profiles": "infra-controllers",
24 "infra_config_profiles": "infra-controllers",
25 "resource_profiles": "managed_resources",
26 "app_profiles": "apps",
27}
28
29
garciadeblas96b94f52024-07-08 16:18:21 +020030async def create_ksus(self, op_id, op_params_list, content_list):
garciadeblas9e532812024-10-22 14:04:36 +020031 self.logger.info(f"create_ksus Enter. Operation {op_id}. Params: {op_params_list}")
32 # self.logger.debug(f"Content: {content_list}")
garciadeblas96b94f52024-07-08 16:18:21 +020033
34 if len(content_list) > 1:
35 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
36 db_ksu = content_list[0]
37 ksu_params = op_params_list[0]
38 oka_list = ksu_params["oka"]
39 if len(oka_list) > 1:
40 raise Exception(
41 "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
42 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020043 oka_item = oka_list[0]
garciadeblas1b2933c2024-10-16 11:43:36 +020044 oka_params = oka_item.get("transformation", {})
garciadeblasf7dfdb72024-09-25 12:15:40 +020045 if "sw_catalog_path" in oka_item:
46 oka_path = oka_item["sw_catalog_path"]
47 else:
48 oka_type = "infra-controllers"
49 oka_name = oka_item["git_name"]
50 oka_path = f"{oka_type}/{oka_name}/templates"
garciadeblas96b94f52024-07-08 16:18:21 +020051
52 workflow_template = "launcher-create-ksu-hr.j2"
53 workflow_name = f"create-ksus-{op_id}"
54 ksu_name = db_ksu["git_name"].lower()
55
56 # Additional params for the workflow
57 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
58 kustomization_name = ksu_name
59 helmrelease_name = ksu_name
garciadeblas96b94f52024-07-08 16:18:21 +020060 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +020061 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +020062 profile_name = ksu_params.get("profile", {}).get("name")
63 age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
garciadeblas1b2933c2024-10-16 11:43:36 +020064 target_ns = oka_params.get("namespace", "default")
65 substitute_environment = oka_params.get("substitute_environment", "false")
66 substitution_filter = oka_params.get("substitution_filter", "")
67 custom_env_vars = oka_params.get("custom_env_vars", "")
garciadeblas96b94f52024-07-08 16:18:21 +020068 if custom_env_vars:
69 custom_env_vars = "|\n" + "\n".join(
garciadeblasf7dfdb72024-09-25 12:15:40 +020070 [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
garciadeblas96b94f52024-07-08 16:18:21 +020071 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020072 else:
73 custom_env_vars = '""'
garciadeblas1b2933c2024-10-16 11:43:36 +020074 inline_values = oka_params.get("inline_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +020075 if inline_values:
76 yaml_string = yaml.safe_dump(
77 inline_values, sort_keys=False, default_flow_style=False
78 )
79 inline_values = "|\n" + "\n".join(
80 [" " * 8 + line for line in yaml_string.splitlines()]
81 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020082 else:
83 inline_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +020084 is_preexisting_cm = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +020085 cm_values = oka_params.get("configmap_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +020086 if cm_values:
87 yaml_string = yaml.safe_dump(
88 cm_values, sort_keys=False, default_flow_style=False
89 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020090 cm_values = "|\n" + "\n".join(
garciadeblas96b94f52024-07-08 16:18:21 +020091 [" " * 8 + line for line in yaml_string.splitlines()]
92 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020093 values_configmap_name = f"cm-{ksu_name}"
94 cm_key = "values.yaml"
95 else:
96 values_configmap_name = ""
97 cm_key = ""
98 cm_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +020099 is_preexisting_secret = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200100 secret_values = oka_params.get("secret_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200101 if secret_values:
102 values_secret_name = f"secret-{ksu_name}"
103 reference_secret_for_values = f"ref-secret-{ksu_name}"
104 reference_key_for_values = f"ref-key-{ksu_name}"
105 secret_values = yaml.safe_dump(
106 secret_values, sort_keys=False, default_flow_style=False
107 )
108 else:
109 values_secret_name = ""
garciadeblasf7dfdb72024-09-25 12:15:40 +0200110 reference_secret_for_values = "this-secret-does-not-exist"
111 reference_key_for_values = "this-key-does-not-exist"
garciadeblas96b94f52024-07-08 16:18:21 +0200112 sync = "true"
113
114 if secret_values:
115 secret_namespace = "osm-workflows"
116 # Create secret
117 await self.create_secret(
118 reference_secret_for_values,
119 secret_namespace,
120 reference_key_for_values,
121 secret_values,
122 )
123
124 # Render workflow
125 manifest = self.render_jinja_template(
126 workflow_template,
127 output_file=None,
128 workflow_name=workflow_name,
129 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
130 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
131 templates_path=oka_path,
132 substitute_environment=substitute_environment,
133 substitution_filter=substitution_filter,
134 custom_env_vars=custom_env_vars,
135 kustomization_name=kustomization_name,
136 helmrelease_name=helmrelease_name,
137 inline_values=inline_values,
138 is_preexisting_secret=is_preexisting_secret,
139 target_ns=target_ns,
140 age_public_key=age_public_key,
141 values_secret_name=values_secret_name,
142 reference_secret_for_values=reference_secret_for_values,
143 reference_key_for_values=reference_key_for_values,
144 is_preexisting_cm=is_preexisting_cm,
145 values_configmap_name=values_configmap_name,
garciadeblasf7dfdb72024-09-25 12:15:40 +0200146 cm_key=cm_key,
garciadeblas96b94f52024-07-08 16:18:21 +0200147 cm_values=cm_values,
148 ksu_name=ksu_name,
149 profile_name=profile_name,
150 profile_type=profile_type,
151 osm_project_name=osm_project_name,
152 sync=sync,
153 workflow_debug=self._workflow_debug,
154 workflow_dry_run=self._workflow_dry_run,
155 )
156 self.logger.debug(f"Workflow manifest: {manifest}")
157
158 # Submit workflow
159 self._kubectl.create_generic_object(
160 namespace="osm-workflows",
161 manifest_dict=yaml.safe_load(manifest),
162 api_group="argoproj.io",
163 api_plural="workflows",
164 api_version="v1alpha1",
165 )
garciadeblasadb81e82024-11-08 01:11:46 +0100166 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200167
168
169async def update_ksus(self, op_id, op_params_list, content_list):
garciadeblas9e532812024-10-22 14:04:36 +0200170 self.logger.info(f"update_ksus Enter. Operation {op_id}. Params: {op_params_list}")
171 # self.logger.debug(f"Content: {content_list}")
garciadeblas96b94f52024-07-08 16:18:21 +0200172
173 if len(content_list) > 1:
174 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
175 db_ksu = content_list[0]
176 ksu_params = op_params_list[0]
177 oka_list = ksu_params["oka"]
178 if len(oka_list) > 1:
179 raise Exception(
180 "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
181 )
garciadeblas1b2933c2024-10-16 11:43:36 +0200182 oka_item = oka_list[0]
183 oka_params = oka_item.get("transformation", {})
184 if "sw_catalog_path" in oka_item:
185 oka_path = oka_item["sw_catalog_path"]
186 else:
187 oka_type = "infra-controllers"
188 oka_name = oka_item["git_name"]
189 oka_path = f"{oka_type}/{oka_name}/templates"
garciadeblas96b94f52024-07-08 16:18:21 +0200190
191 workflow_template = "launcher-update-ksu-hr.j2"
192 workflow_name = f"update-ksus-{op_id}"
193 ksu_name = db_ksu["git_name"].lower()
194
195 # Additional params for the workflow
196 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
197 kustomization_name = ksu_name
198 helmrelease_name = ksu_name
garciadeblas96b94f52024-07-08 16:18:21 +0200199 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +0200200 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +0200201 profile_name = ksu_params.get("profile", {}).get("name")
202 age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
garciadeblas1b2933c2024-10-16 11:43:36 +0200203 target_ns = oka_params.get("namespace", "default")
204 substitute_environment = oka_params.get("substitute_environment", "false")
205 substitution_filter = oka_params.get("substitution_filter", "")
206 custom_env_vars = oka_params.get("custom_env_vars", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200207 if custom_env_vars:
208 custom_env_vars = "|\n" + "\n".join(
garciadeblasf7dfdb72024-09-25 12:15:40 +0200209 [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
garciadeblas96b94f52024-07-08 16:18:21 +0200210 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200211 else:
212 custom_env_vars = '""'
garciadeblas1b2933c2024-10-16 11:43:36 +0200213 inline_values = oka_params.get("inline_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200214 if inline_values:
215 yaml_string = yaml.safe_dump(
216 inline_values, sort_keys=False, default_flow_style=False
217 )
218 inline_values = "|\n" + "\n".join(
219 [" " * 8 + line for line in yaml_string.splitlines()]
220 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200221 else:
222 inline_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +0200223 is_preexisting_cm = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200224 cm_values = oka_params.get("configmap_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200225 if cm_values:
226 yaml_string = yaml.safe_dump(
227 cm_values, sort_keys=False, default_flow_style=False
228 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200229 cm_values = "|\n" + "\n".join(
garciadeblas96b94f52024-07-08 16:18:21 +0200230 [" " * 8 + line for line in yaml_string.splitlines()]
231 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200232 values_configmap_name = f"cm-{ksu_name}"
233 cm_key = "values.yaml"
234 else:
235 values_configmap_name = ""
236 cm_key = ""
237 cm_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +0200238 is_preexisting_secret = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200239 secret_values = oka_params.get("secret_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200240 if secret_values:
241 values_secret_name = f"secret-{ksu_name}"
242 reference_secret_for_values = f"ref-secret-{ksu_name}"
243 reference_key_for_values = f"ref-key-{ksu_name}"
244 secret_values = yaml.safe_dump(
245 secret_values, sort_keys=False, default_flow_style=False
246 )
247 else:
248 values_secret_name = ""
garciadeblasf7dfdb72024-09-25 12:15:40 +0200249 reference_secret_for_values = "this-secret-does-not-exist"
250 reference_key_for_values = "this-key-does-not-exist"
garciadeblas96b94f52024-07-08 16:18:21 +0200251
252 if secret_values:
253 secret_namespace = "osm-workflows"
254 # Create secret
255 await self.create_secret(
256 reference_secret_for_values,
257 secret_namespace,
258 reference_key_for_values,
259 secret_values,
260 )
261
262 # Render workflow
263 manifest = self.render_jinja_template(
264 workflow_template,
265 output_file=None,
266 workflow_name=workflow_name,
267 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
268 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
269 templates_path=oka_path,
270 substitute_environment=substitute_environment,
271 substitution_filter=substitution_filter,
272 custom_env_vars=custom_env_vars,
273 kustomization_name=kustomization_name,
274 helmrelease_name=helmrelease_name,
275 inline_values=inline_values,
276 is_preexisting_secret=is_preexisting_secret,
277 target_ns=target_ns,
278 age_public_key=age_public_key,
279 values_secret_name=values_secret_name,
280 reference_secret_for_values=reference_secret_for_values,
281 reference_key_for_values=reference_key_for_values,
282 is_preexisting_cm=is_preexisting_cm,
283 values_configmap_name=values_configmap_name,
garciadeblasf7dfdb72024-09-25 12:15:40 +0200284 cm_key=cm_key,
garciadeblas96b94f52024-07-08 16:18:21 +0200285 cm_values=cm_values,
286 ksu_name=ksu_name,
287 profile_name=profile_name,
288 profile_type=profile_type,
289 osm_project_name=osm_project_name,
290 workflow_debug=self._workflow_debug,
291 workflow_dry_run=self._workflow_dry_run,
292 )
293 self.logger.debug(f"Workflow manifest: {manifest}")
294
295 # Submit workflow
296 self._kubectl.create_generic_object(
297 namespace="osm-workflows",
298 manifest_dict=yaml.safe_load(manifest),
299 api_group="argoproj.io",
300 api_plural="workflows",
301 api_version="v1alpha1",
302 )
garciadeblasadb81e82024-11-08 01:11:46 +0100303 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200304
305
306async def delete_ksus(self, op_id, op_params_list, content_list):
garciadeblas9e532812024-10-22 14:04:36 +0200307 self.logger.info(f"delete_ksus Enter. Operation {op_id}. Params: {op_params_list}")
308 # self.logger.debug(f"Content: {content_list}")
garciadeblas96b94f52024-07-08 16:18:21 +0200309
310 if len(content_list) > 1:
311 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
312 db_ksu = content_list[0]
313 ksu_params = op_params_list[0]
314
315 workflow_template = "launcher-delete-ksu.j2"
316 workflow_name = f"delete-ksus-{op_id}"
317 ksu_name = db_ksu["git_name"].lower()
318
319 # Additional params for the workflow
320 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
321 profile_name = ksu_params.get("profile", {}).get("name")
322 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +0200323 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +0200324
325 # Render workflow
326 manifest = self.render_jinja_template(
327 workflow_template,
328 output_file=None,
329 workflow_name=workflow_name,
330 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
331 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
332 ksu_name=ksu_name,
333 profile_name=profile_name,
334 profile_type=profile_type,
335 osm_project_name=osm_project_name,
336 workflow_debug=self._workflow_debug,
337 workflow_dry_run=self._workflow_dry_run,
338 )
339 self.logger.debug(f"Workflow manifest: {manifest}")
340
341 # Submit workflow
342 self._kubectl.create_generic_object(
343 namespace="osm-workflows",
344 manifest_dict=yaml.safe_load(manifest),
345 api_group="argoproj.io",
346 api_plural="workflows",
347 api_version="v1alpha1",
348 )
garciadeblasadb81e82024-11-08 01:11:46 +0100349 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200350
351
352async def clone_ksu(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200353 self.logger.info(f"clone_ksu Enter. Operation {op_id}. Params: {op_params}")
354 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200355 workflow_name = f"clone-ksu-{content['_id']}"
garciadeblasadb81e82024-11-08 01:11:46 +0100356 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200357
358
359async def move_ksu(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200360 self.logger.info(f"move_ksu Enter. Operation {op_id}. Params: {op_params}")
361 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200362 workflow_name = f"move-ksu-{content['_id']}"
garciadeblasadb81e82024-11-08 01:11:46 +0100363 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200364
365
garciadeblasd8429852024-10-17 15:30:30 +0200366async def clean_items_ksu_create(self, op_id, op_params_list, content_list):
garciadeblasd8429852024-10-17 15:30:30 +0200367 self.logger.info(
garciadeblas9e532812024-10-22 14:04:36 +0200368 f"clean_items_ksu_create Enter. Operation {op_id}. Params: {op_params_list}"
garciadeblasd8429852024-10-17 15:30:30 +0200369 )
garciadeblas9e532812024-10-22 14:04:36 +0200370 # self.logger.debug(f"Content: {content_list}")
garciadeblasd8429852024-10-17 15:30:30 +0200371 try:
372 if len(content_list) > 1:
373 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
374 db_ksu = content_list[0]
375 ksu_name = db_ksu["git_name"].lower()
376 ksu_params = op_params_list[0]
377 oka_list = ksu_params["oka"]
378 if len(oka_list) > 1:
379 raise Exception(
380 "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
381 )
382 oka_item = oka_list[0]
383 oka_params = oka_item.get("transformation", {})
384 secret_values = oka_params.get("secret_values", "")
385 if secret_values:
386 items = {
387 "secrets": [
388 {
389 "name": f"ref-secret-{ksu_name}",
390 "namespace": "osm-workflows",
391 }
392 ]
393 }
394 await self.clean_items(items)
395 return True, "OK"
396 except Exception as e:
397 return False, f"Error while cleaning items: {e}"
398
399
400async def clean_items_ksu_update(self, op_id, op_params_list, content_list):
garciadeblasd8429852024-10-17 15:30:30 +0200401 self.logger.info(
garciadeblas9e532812024-10-22 14:04:36 +0200402 f"clean_items_ksu_update Enter. Operation {op_id}. Params: {op_params_list}"
garciadeblasd8429852024-10-17 15:30:30 +0200403 )
garciadeblas9e532812024-10-22 14:04:36 +0200404 # self.logger.debug(f"Content: {content_list}")
garciadeblasd8429852024-10-17 15:30:30 +0200405 return await self.clean_items_ksu_create(op_id, op_params_list, content_list)