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