blob: 0292434b3384ab5802c8b7cbf04d6ab162fc487c [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):
31 self.logger.info("Create KSU workflow Enter")
32 self.logger.info(
33 f"Operation {op_id}. Params: {op_params_list}. Content: {content_list}"
34 )
35
36 if len(content_list) > 1:
37 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
38 db_ksu = content_list[0]
39 ksu_params = op_params_list[0]
40 oka_list = ksu_params["oka"]
41 if len(oka_list) > 1:
42 raise Exception(
43 "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
44 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020045 oka_item = oka_list[0]
garciadeblas1b2933c2024-10-16 11:43:36 +020046 oka_params = oka_item.get("transformation", {})
garciadeblasf7dfdb72024-09-25 12:15:40 +020047 if "sw_catalog_path" in oka_item:
48 oka_path = oka_item["sw_catalog_path"]
49 else:
50 oka_type = "infra-controllers"
51 oka_name = oka_item["git_name"]
52 oka_path = f"{oka_type}/{oka_name}/templates"
garciadeblas96b94f52024-07-08 16:18:21 +020053
54 workflow_template = "launcher-create-ksu-hr.j2"
55 workflow_name = f"create-ksus-{op_id}"
56 ksu_name = db_ksu["git_name"].lower()
57
58 # Additional params for the workflow
59 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
60 kustomization_name = ksu_name
61 helmrelease_name = ksu_name
garciadeblas96b94f52024-07-08 16:18:21 +020062 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +020063 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +020064 profile_name = ksu_params.get("profile", {}).get("name")
65 age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
garciadeblas1b2933c2024-10-16 11:43:36 +020066 target_ns = oka_params.get("namespace", "default")
67 substitute_environment = oka_params.get("substitute_environment", "false")
68 substitution_filter = oka_params.get("substitution_filter", "")
69 custom_env_vars = oka_params.get("custom_env_vars", "")
garciadeblas96b94f52024-07-08 16:18:21 +020070 if custom_env_vars:
71 custom_env_vars = "|\n" + "\n".join(
garciadeblasf7dfdb72024-09-25 12:15:40 +020072 [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
garciadeblas96b94f52024-07-08 16:18:21 +020073 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020074 else:
75 custom_env_vars = '""'
garciadeblas1b2933c2024-10-16 11:43:36 +020076 inline_values = oka_params.get("inline_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +020077 if inline_values:
78 yaml_string = yaml.safe_dump(
79 inline_values, sort_keys=False, default_flow_style=False
80 )
81 inline_values = "|\n" + "\n".join(
82 [" " * 8 + line for line in yaml_string.splitlines()]
83 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020084 else:
85 inline_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +020086 is_preexisting_cm = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +020087 cm_values = oka_params.get("configmap_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +020088 if cm_values:
89 yaml_string = yaml.safe_dump(
90 cm_values, sort_keys=False, default_flow_style=False
91 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020092 cm_values = "|\n" + "\n".join(
garciadeblas96b94f52024-07-08 16:18:21 +020093 [" " * 8 + line for line in yaml_string.splitlines()]
94 )
garciadeblasf7dfdb72024-09-25 12:15:40 +020095 values_configmap_name = f"cm-{ksu_name}"
96 cm_key = "values.yaml"
97 else:
98 values_configmap_name = ""
99 cm_key = ""
100 cm_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +0200101 is_preexisting_secret = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200102 secret_values = oka_params.get("secret_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200103 if secret_values:
104 values_secret_name = f"secret-{ksu_name}"
105 reference_secret_for_values = f"ref-secret-{ksu_name}"
106 reference_key_for_values = f"ref-key-{ksu_name}"
107 secret_values = yaml.safe_dump(
108 secret_values, sort_keys=False, default_flow_style=False
109 )
110 else:
111 values_secret_name = ""
garciadeblasf7dfdb72024-09-25 12:15:40 +0200112 reference_secret_for_values = "this-secret-does-not-exist"
113 reference_key_for_values = "this-key-does-not-exist"
garciadeblas96b94f52024-07-08 16:18:21 +0200114 sync = "true"
115
116 if secret_values:
117 secret_namespace = "osm-workflows"
118 # Create secret
119 await self.create_secret(
120 reference_secret_for_values,
121 secret_namespace,
122 reference_key_for_values,
123 secret_values,
124 )
125
126 # Render workflow
127 manifest = self.render_jinja_template(
128 workflow_template,
129 output_file=None,
130 workflow_name=workflow_name,
131 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
132 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
133 templates_path=oka_path,
134 substitute_environment=substitute_environment,
135 substitution_filter=substitution_filter,
136 custom_env_vars=custom_env_vars,
137 kustomization_name=kustomization_name,
138 helmrelease_name=helmrelease_name,
139 inline_values=inline_values,
140 is_preexisting_secret=is_preexisting_secret,
141 target_ns=target_ns,
142 age_public_key=age_public_key,
143 values_secret_name=values_secret_name,
144 reference_secret_for_values=reference_secret_for_values,
145 reference_key_for_values=reference_key_for_values,
146 is_preexisting_cm=is_preexisting_cm,
147 values_configmap_name=values_configmap_name,
garciadeblasf7dfdb72024-09-25 12:15:40 +0200148 cm_key=cm_key,
garciadeblas96b94f52024-07-08 16:18:21 +0200149 cm_values=cm_values,
150 ksu_name=ksu_name,
151 profile_name=profile_name,
152 profile_type=profile_type,
153 osm_project_name=osm_project_name,
154 sync=sync,
155 workflow_debug=self._workflow_debug,
156 workflow_dry_run=self._workflow_dry_run,
157 )
158 self.logger.debug(f"Workflow manifest: {manifest}")
159
160 # Submit workflow
161 self._kubectl.create_generic_object(
162 namespace="osm-workflows",
163 manifest_dict=yaml.safe_load(manifest),
164 api_group="argoproj.io",
165 api_plural="workflows",
166 api_version="v1alpha1",
167 )
168 return workflow_name
169
170
171async def update_ksus(self, op_id, op_params_list, content_list):
172 self.logger.info("Update KSU workflow Enter")
173 self.logger.info(
174 f"Operation {op_id}. Params: {op_params_list}. Content: {content_list}"
175 )
176
177 if len(content_list) > 1:
178 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
179 db_ksu = content_list[0]
180 ksu_params = op_params_list[0]
181 oka_list = ksu_params["oka"]
182 if len(oka_list) > 1:
183 raise Exception(
184 "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
185 )
garciadeblas1b2933c2024-10-16 11:43:36 +0200186 oka_item = oka_list[0]
187 oka_params = oka_item.get("transformation", {})
188 if "sw_catalog_path" in oka_item:
189 oka_path = oka_item["sw_catalog_path"]
190 else:
191 oka_type = "infra-controllers"
192 oka_name = oka_item["git_name"]
193 oka_path = f"{oka_type}/{oka_name}/templates"
garciadeblas96b94f52024-07-08 16:18:21 +0200194
195 workflow_template = "launcher-update-ksu-hr.j2"
196 workflow_name = f"update-ksus-{op_id}"
197 ksu_name = db_ksu["git_name"].lower()
198
199 # Additional params for the workflow
200 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
201 kustomization_name = ksu_name
202 helmrelease_name = ksu_name
garciadeblas96b94f52024-07-08 16:18:21 +0200203 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +0200204 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +0200205 profile_name = ksu_params.get("profile", {}).get("name")
206 age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
garciadeblas1b2933c2024-10-16 11:43:36 +0200207 target_ns = oka_params.get("namespace", "default")
208 substitute_environment = oka_params.get("substitute_environment", "false")
209 substitution_filter = oka_params.get("substitution_filter", "")
210 custom_env_vars = oka_params.get("custom_env_vars", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200211 if custom_env_vars:
212 custom_env_vars = "|\n" + "\n".join(
garciadeblasf7dfdb72024-09-25 12:15:40 +0200213 [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
garciadeblas96b94f52024-07-08 16:18:21 +0200214 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200215 else:
216 custom_env_vars = '""'
garciadeblas1b2933c2024-10-16 11:43:36 +0200217 inline_values = oka_params.get("inline_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200218 if inline_values:
219 yaml_string = yaml.safe_dump(
220 inline_values, sort_keys=False, default_flow_style=False
221 )
222 inline_values = "|\n" + "\n".join(
223 [" " * 8 + line for line in yaml_string.splitlines()]
224 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200225 else:
226 inline_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +0200227 is_preexisting_cm = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200228 cm_values = oka_params.get("configmap_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200229 if cm_values:
230 yaml_string = yaml.safe_dump(
231 cm_values, sort_keys=False, default_flow_style=False
232 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200233 cm_values = "|\n" + "\n".join(
garciadeblas96b94f52024-07-08 16:18:21 +0200234 [" " * 8 + line for line in yaml_string.splitlines()]
235 )
garciadeblasf7dfdb72024-09-25 12:15:40 +0200236 values_configmap_name = f"cm-{ksu_name}"
237 cm_key = "values.yaml"
238 else:
239 values_configmap_name = ""
240 cm_key = ""
241 cm_values = '""'
garciadeblas96b94f52024-07-08 16:18:21 +0200242 is_preexisting_secret = "false"
garciadeblas1b2933c2024-10-16 11:43:36 +0200243 secret_values = oka_params.get("secret_values", "")
garciadeblas96b94f52024-07-08 16:18:21 +0200244 if secret_values:
245 values_secret_name = f"secret-{ksu_name}"
246 reference_secret_for_values = f"ref-secret-{ksu_name}"
247 reference_key_for_values = f"ref-key-{ksu_name}"
248 secret_values = yaml.safe_dump(
249 secret_values, sort_keys=False, default_flow_style=False
250 )
251 else:
252 values_secret_name = ""
garciadeblasf7dfdb72024-09-25 12:15:40 +0200253 reference_secret_for_values = "this-secret-does-not-exist"
254 reference_key_for_values = "this-key-does-not-exist"
garciadeblas96b94f52024-07-08 16:18:21 +0200255
256 if secret_values:
257 secret_namespace = "osm-workflows"
258 # Create secret
259 await self.create_secret(
260 reference_secret_for_values,
261 secret_namespace,
262 reference_key_for_values,
263 secret_values,
264 )
265
266 # Render workflow
267 manifest = self.render_jinja_template(
268 workflow_template,
269 output_file=None,
270 workflow_name=workflow_name,
271 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
272 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
273 templates_path=oka_path,
274 substitute_environment=substitute_environment,
275 substitution_filter=substitution_filter,
276 custom_env_vars=custom_env_vars,
277 kustomization_name=kustomization_name,
278 helmrelease_name=helmrelease_name,
279 inline_values=inline_values,
280 is_preexisting_secret=is_preexisting_secret,
281 target_ns=target_ns,
282 age_public_key=age_public_key,
283 values_secret_name=values_secret_name,
284 reference_secret_for_values=reference_secret_for_values,
285 reference_key_for_values=reference_key_for_values,
286 is_preexisting_cm=is_preexisting_cm,
287 values_configmap_name=values_configmap_name,
garciadeblasf7dfdb72024-09-25 12:15:40 +0200288 cm_key=cm_key,
garciadeblas96b94f52024-07-08 16:18:21 +0200289 cm_values=cm_values,
290 ksu_name=ksu_name,
291 profile_name=profile_name,
292 profile_type=profile_type,
293 osm_project_name=osm_project_name,
294 workflow_debug=self._workflow_debug,
295 workflow_dry_run=self._workflow_dry_run,
296 )
297 self.logger.debug(f"Workflow manifest: {manifest}")
298
299 # Submit workflow
300 self._kubectl.create_generic_object(
301 namespace="osm-workflows",
302 manifest_dict=yaml.safe_load(manifest),
303 api_group="argoproj.io",
304 api_plural="workflows",
305 api_version="v1alpha1",
306 )
307 return workflow_name
308
309
310async def delete_ksus(self, op_id, op_params_list, content_list):
311 self.logger.info("Delete KSU workflow Enter")
312 self.logger.info(
313 f"Operation {op_id}. Params: {op_params_list}. Content: {content_list}"
314 )
315
316 if len(content_list) > 1:
317 raise Exception("There is no ODU workflow yet able to manage multiple KSUs")
318 db_ksu = content_list[0]
319 ksu_params = op_params_list[0]
320
321 workflow_template = "launcher-delete-ksu.j2"
322 workflow_name = f"delete-ksus-{op_id}"
323 ksu_name = db_ksu["git_name"].lower()
324
325 # Additional params for the workflow
326 osm_project_name = "osm_admin" # TODO: get project name from db_ksu
327 profile_name = ksu_params.get("profile", {}).get("name")
328 profile_type = ksu_params.get("profile", {}).get("profile_type")
garciadeblasf7dfdb72024-09-25 12:15:40 +0200329 profile_type = MAP_PROFILE[profile_type]
garciadeblas96b94f52024-07-08 16:18:21 +0200330
331 # Render workflow
332 manifest = self.render_jinja_template(
333 workflow_template,
334 output_file=None,
335 workflow_name=workflow_name,
336 git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git",
337 git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git",
338 ksu_name=ksu_name,
339 profile_name=profile_name,
340 profile_type=profile_type,
341 osm_project_name=osm_project_name,
342 workflow_debug=self._workflow_debug,
343 workflow_dry_run=self._workflow_dry_run,
344 )
345 self.logger.debug(f"Workflow manifest: {manifest}")
346
347 # Submit workflow
348 self._kubectl.create_generic_object(
349 namespace="osm-workflows",
350 manifest_dict=yaml.safe_load(manifest),
351 api_group="argoproj.io",
352 api_plural="workflows",
353 api_version="v1alpha1",
354 )
355 return workflow_name
356
357
358async def clone_ksu(self, op_id, op_params, content):
359 self.logger.info("Clone KSU workflow Enter")
360 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
361 workflow_name = f"clone-ksu-{content['_id']}"
362 return workflow_name
363
364
365async def move_ksu(self, op_id, op_params, content):
366 self.logger.info("Move KSU workflow Enter")
367 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
368 workflow_name = f"move-ksu-{content['_id']}"
369 return workflow_name
370
371
372async def check_create_ksus(self, op_id, op_params, content):
373 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
374 return True, "OK"
375
376
377async def check_update_ksus(self, op_id, op_params, content):
378 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
379 return True, "OK"
380
381
382async def check_delete_ksus(self, op_id, op_params, content):
383 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
384 return True, "OK"
385
386
387async def check_clone_ksu(self, op_id, op_params, content):
388 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
389 return True, "OK"
390
391
392async def check_move_ksu(self, op_id, op_params, content):
393 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
394 return True, "OK"