| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 19 | import yaml |
| 20 | |
| 21 | |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 22 | MAP_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 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 30 | async 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 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 45 | oka_item = oka_list[0] |
| 46 | if "sw_catalog_path" in oka_item: |
| 47 | oka_path = oka_item["sw_catalog_path"] |
| 48 | else: |
| 49 | oka_type = "infra-controllers" |
| 50 | oka_name = oka_item["git_name"] |
| 51 | oka_path = f"{oka_type}/{oka_name}/templates" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 52 | |
| 53 | workflow_template = "launcher-create-ksu-hr.j2" |
| 54 | workflow_name = f"create-ksus-{op_id}" |
| 55 | ksu_name = db_ksu["git_name"].lower() |
| 56 | |
| 57 | # Additional params for the workflow |
| 58 | osm_project_name = "osm_admin" # TODO: get project name from db_ksu |
| 59 | kustomization_name = ksu_name |
| 60 | helmrelease_name = ksu_name |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 61 | target_ns = ksu_params.get("namespace", "default") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 62 | profile_type = ksu_params.get("profile", {}).get("profile_type") |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 63 | profile_type = MAP_PROFILE[profile_type] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 64 | profile_name = ksu_params.get("profile", {}).get("name") |
| 65 | age_public_key = ksu_params.get("profile", {}).get("age_pubkey") |
| 66 | substitute_environment = ksu_params.get("substitute_environment", "false") |
| 67 | substitution_filter = ksu_params.get("substitution_filter", "") |
| 68 | custom_env_vars = ksu_params.get("custom_env_vars", "") |
| 69 | if custom_env_vars: |
| 70 | custom_env_vars = "|\n" + "\n".join( |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 71 | [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 72 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 73 | else: |
| 74 | custom_env_vars = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 75 | inline_values = ksu_params.get("inline_values", "") |
| 76 | if inline_values: |
| 77 | yaml_string = yaml.safe_dump( |
| 78 | inline_values, sort_keys=False, default_flow_style=False |
| 79 | ) |
| 80 | inline_values = "|\n" + "\n".join( |
| 81 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 82 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 83 | else: |
| 84 | inline_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 85 | is_preexisting_cm = "false" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 86 | cm_values = ksu_params.get("configmap_values", "") |
| 87 | if cm_values: |
| 88 | yaml_string = yaml.safe_dump( |
| 89 | cm_values, sort_keys=False, default_flow_style=False |
| 90 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 91 | cm_values = "|\n" + "\n".join( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 92 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 93 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 94 | values_configmap_name = f"cm-{ksu_name}" |
| 95 | cm_key = "values.yaml" |
| 96 | else: |
| 97 | values_configmap_name = "" |
| 98 | cm_key = "" |
| 99 | cm_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 100 | is_preexisting_secret = "false" |
| 101 | secret_values = ksu_params.get("secret_values", "") |
| 102 | if secret_values: |
| 103 | values_secret_name = f"secret-{ksu_name}" |
| 104 | reference_secret_for_values = f"ref-secret-{ksu_name}" |
| 105 | reference_key_for_values = f"ref-key-{ksu_name}" |
| 106 | secret_values = yaml.safe_dump( |
| 107 | secret_values, sort_keys=False, default_flow_style=False |
| 108 | ) |
| 109 | else: |
| 110 | values_secret_name = "" |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 111 | reference_secret_for_values = "this-secret-does-not-exist" |
| 112 | reference_key_for_values = "this-key-does-not-exist" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 113 | sync = "true" |
| 114 | |
| 115 | if secret_values: |
| 116 | secret_namespace = "osm-workflows" |
| 117 | # Create secret |
| 118 | await self.create_secret( |
| 119 | reference_secret_for_values, |
| 120 | secret_namespace, |
| 121 | reference_key_for_values, |
| 122 | secret_values, |
| 123 | ) |
| 124 | |
| 125 | # Render workflow |
| 126 | manifest = self.render_jinja_template( |
| 127 | workflow_template, |
| 128 | output_file=None, |
| 129 | workflow_name=workflow_name, |
| 130 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 131 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 132 | templates_path=oka_path, |
| 133 | substitute_environment=substitute_environment, |
| 134 | substitution_filter=substitution_filter, |
| 135 | custom_env_vars=custom_env_vars, |
| 136 | kustomization_name=kustomization_name, |
| 137 | helmrelease_name=helmrelease_name, |
| 138 | inline_values=inline_values, |
| 139 | is_preexisting_secret=is_preexisting_secret, |
| 140 | target_ns=target_ns, |
| 141 | age_public_key=age_public_key, |
| 142 | values_secret_name=values_secret_name, |
| 143 | reference_secret_for_values=reference_secret_for_values, |
| 144 | reference_key_for_values=reference_key_for_values, |
| 145 | is_preexisting_cm=is_preexisting_cm, |
| 146 | values_configmap_name=values_configmap_name, |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 147 | cm_key=cm_key, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 148 | cm_values=cm_values, |
| 149 | ksu_name=ksu_name, |
| 150 | profile_name=profile_name, |
| 151 | profile_type=profile_type, |
| 152 | osm_project_name=osm_project_name, |
| 153 | sync=sync, |
| 154 | workflow_debug=self._workflow_debug, |
| 155 | workflow_dry_run=self._workflow_dry_run, |
| 156 | ) |
| 157 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 158 | |
| 159 | # Submit workflow |
| 160 | self._kubectl.create_generic_object( |
| 161 | namespace="osm-workflows", |
| 162 | manifest_dict=yaml.safe_load(manifest), |
| 163 | api_group="argoproj.io", |
| 164 | api_plural="workflows", |
| 165 | api_version="v1alpha1", |
| 166 | ) |
| 167 | return workflow_name |
| 168 | |
| 169 | |
| 170 | async def update_ksus(self, op_id, op_params_list, content_list): |
| 171 | self.logger.info("Update KSU workflow Enter") |
| 172 | self.logger.info( |
| 173 | f"Operation {op_id}. Params: {op_params_list}. Content: {content_list}" |
| 174 | ) |
| 175 | |
| 176 | if len(content_list) > 1: |
| 177 | raise Exception("There is no ODU workflow yet able to manage multiple KSUs") |
| 178 | db_ksu = content_list[0] |
| 179 | ksu_params = op_params_list[0] |
| 180 | oka_list = ksu_params["oka"] |
| 181 | if len(oka_list) > 1: |
| 182 | raise Exception( |
| 183 | "There is no ODU workflow yet able to manage multiple OKAs for a KSU" |
| 184 | ) |
| 185 | oka_path = oka_list[0]["sw_catalog_path"] |
| 186 | |
| 187 | workflow_template = "launcher-update-ksu-hr.j2" |
| 188 | workflow_name = f"update-ksus-{op_id}" |
| 189 | ksu_name = db_ksu["git_name"].lower() |
| 190 | |
| 191 | # Additional params for the workflow |
| 192 | osm_project_name = "osm_admin" # TODO: get project name from db_ksu |
| 193 | kustomization_name = ksu_name |
| 194 | helmrelease_name = ksu_name |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 195 | target_ns = ksu_params.get("namespace", "default") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 196 | profile_type = ksu_params.get("profile", {}).get("profile_type") |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 197 | profile_type = MAP_PROFILE[profile_type] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 198 | profile_name = ksu_params.get("profile", {}).get("name") |
| 199 | age_public_key = ksu_params.get("profile", {}).get("age_pubkey") |
| 200 | substitute_environment = ksu_params.get("substitute_environment", "false") |
| 201 | substitution_filter = ksu_params.get("substitution_filter", "") |
| 202 | custom_env_vars = ksu_params.get("custom_env_vars", "") |
| 203 | if custom_env_vars: |
| 204 | custom_env_vars = "|\n" + "\n".join( |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 205 | [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 206 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 207 | else: |
| 208 | custom_env_vars = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 209 | inline_values = ksu_params.get("inline_values", "") |
| 210 | if inline_values: |
| 211 | yaml_string = yaml.safe_dump( |
| 212 | inline_values, sort_keys=False, default_flow_style=False |
| 213 | ) |
| 214 | inline_values = "|\n" + "\n".join( |
| 215 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 216 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 217 | else: |
| 218 | inline_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 219 | is_preexisting_cm = "false" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 220 | cm_values = ksu_params.get("configmap_values", "") |
| 221 | if cm_values: |
| 222 | yaml_string = yaml.safe_dump( |
| 223 | cm_values, sort_keys=False, default_flow_style=False |
| 224 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 225 | cm_values = "|\n" + "\n".join( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 226 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 227 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 228 | values_configmap_name = f"cm-{ksu_name}" |
| 229 | cm_key = "values.yaml" |
| 230 | else: |
| 231 | values_configmap_name = "" |
| 232 | cm_key = "" |
| 233 | cm_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 234 | is_preexisting_secret = "false" |
| 235 | secret_values = ksu_params.get("secret_values", "") |
| 236 | if secret_values: |
| 237 | values_secret_name = f"secret-{ksu_name}" |
| 238 | reference_secret_for_values = f"ref-secret-{ksu_name}" |
| 239 | reference_key_for_values = f"ref-key-{ksu_name}" |
| 240 | secret_values = yaml.safe_dump( |
| 241 | secret_values, sort_keys=False, default_flow_style=False |
| 242 | ) |
| 243 | else: |
| 244 | values_secret_name = "" |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 245 | reference_secret_for_values = "this-secret-does-not-exist" |
| 246 | reference_key_for_values = "this-key-does-not-exist" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 247 | |
| 248 | if secret_values: |
| 249 | secret_namespace = "osm-workflows" |
| 250 | # Create secret |
| 251 | await self.create_secret( |
| 252 | reference_secret_for_values, |
| 253 | secret_namespace, |
| 254 | reference_key_for_values, |
| 255 | secret_values, |
| 256 | ) |
| 257 | |
| 258 | # Render workflow |
| 259 | manifest = self.render_jinja_template( |
| 260 | workflow_template, |
| 261 | output_file=None, |
| 262 | workflow_name=workflow_name, |
| 263 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 264 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 265 | templates_path=oka_path, |
| 266 | substitute_environment=substitute_environment, |
| 267 | substitution_filter=substitution_filter, |
| 268 | custom_env_vars=custom_env_vars, |
| 269 | kustomization_name=kustomization_name, |
| 270 | helmrelease_name=helmrelease_name, |
| 271 | inline_values=inline_values, |
| 272 | is_preexisting_secret=is_preexisting_secret, |
| 273 | target_ns=target_ns, |
| 274 | age_public_key=age_public_key, |
| 275 | values_secret_name=values_secret_name, |
| 276 | reference_secret_for_values=reference_secret_for_values, |
| 277 | reference_key_for_values=reference_key_for_values, |
| 278 | is_preexisting_cm=is_preexisting_cm, |
| 279 | values_configmap_name=values_configmap_name, |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 280 | cm_key=cm_key, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 281 | cm_values=cm_values, |
| 282 | ksu_name=ksu_name, |
| 283 | profile_name=profile_name, |
| 284 | profile_type=profile_type, |
| 285 | osm_project_name=osm_project_name, |
| 286 | workflow_debug=self._workflow_debug, |
| 287 | workflow_dry_run=self._workflow_dry_run, |
| 288 | ) |
| 289 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 290 | |
| 291 | # Submit workflow |
| 292 | self._kubectl.create_generic_object( |
| 293 | namespace="osm-workflows", |
| 294 | manifest_dict=yaml.safe_load(manifest), |
| 295 | api_group="argoproj.io", |
| 296 | api_plural="workflows", |
| 297 | api_version="v1alpha1", |
| 298 | ) |
| 299 | return workflow_name |
| 300 | |
| 301 | |
| 302 | async def delete_ksus(self, op_id, op_params_list, content_list): |
| 303 | self.logger.info("Delete KSU workflow Enter") |
| 304 | self.logger.info( |
| 305 | f"Operation {op_id}. Params: {op_params_list}. Content: {content_list}" |
| 306 | ) |
| 307 | |
| 308 | if len(content_list) > 1: |
| 309 | raise Exception("There is no ODU workflow yet able to manage multiple KSUs") |
| 310 | db_ksu = content_list[0] |
| 311 | ksu_params = op_params_list[0] |
| 312 | |
| 313 | workflow_template = "launcher-delete-ksu.j2" |
| 314 | workflow_name = f"delete-ksus-{op_id}" |
| 315 | ksu_name = db_ksu["git_name"].lower() |
| 316 | |
| 317 | # Additional params for the workflow |
| 318 | osm_project_name = "osm_admin" # TODO: get project name from db_ksu |
| 319 | profile_name = ksu_params.get("profile", {}).get("name") |
| 320 | profile_type = ksu_params.get("profile", {}).get("profile_type") |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 321 | profile_type = MAP_PROFILE[profile_type] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 322 | |
| 323 | # Render workflow |
| 324 | manifest = self.render_jinja_template( |
| 325 | workflow_template, |
| 326 | output_file=None, |
| 327 | workflow_name=workflow_name, |
| 328 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 329 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 330 | ksu_name=ksu_name, |
| 331 | profile_name=profile_name, |
| 332 | profile_type=profile_type, |
| 333 | osm_project_name=osm_project_name, |
| 334 | workflow_debug=self._workflow_debug, |
| 335 | workflow_dry_run=self._workflow_dry_run, |
| 336 | ) |
| 337 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 338 | |
| 339 | # Submit workflow |
| 340 | self._kubectl.create_generic_object( |
| 341 | namespace="osm-workflows", |
| 342 | manifest_dict=yaml.safe_load(manifest), |
| 343 | api_group="argoproj.io", |
| 344 | api_plural="workflows", |
| 345 | api_version="v1alpha1", |
| 346 | ) |
| 347 | return workflow_name |
| 348 | |
| 349 | |
| 350 | async def clone_ksu(self, op_id, op_params, content): |
| 351 | self.logger.info("Clone KSU workflow Enter") |
| 352 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 353 | workflow_name = f"clone-ksu-{content['_id']}" |
| 354 | return workflow_name |
| 355 | |
| 356 | |
| 357 | async def move_ksu(self, op_id, op_params, content): |
| 358 | self.logger.info("Move KSU workflow Enter") |
| 359 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 360 | workflow_name = f"move-ksu-{content['_id']}" |
| 361 | return workflow_name |
| 362 | |
| 363 | |
| 364 | async def check_create_ksus(self, op_id, op_params, content): |
| 365 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 366 | return True, "OK" |
| 367 | |
| 368 | |
| 369 | async def check_update_ksus(self, op_id, op_params, content): |
| 370 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 371 | return True, "OK" |
| 372 | |
| 373 | |
| 374 | async def check_delete_ksus(self, op_id, op_params, content): |
| 375 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 376 | return True, "OK" |
| 377 | |
| 378 | |
| 379 | async def check_clone_ksu(self, op_id, op_params, content): |
| 380 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 381 | return True, "OK" |
| 382 | |
| 383 | |
| 384 | async def check_move_ksu(self, op_id, op_params, content): |
| 385 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 386 | return True, "OK" |