| 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", |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 24 | "infra_config_profiles": "infra-configs", |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 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): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 31 | self.logger.info(f"create_ksus Enter. Operation {op_id}. Params: {op_params_list}") |
| 32 | # self.logger.debug(f"Content: {content_list}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 33 | |
| 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 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 43 | oka_item = oka_list[0] |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 44 | oka_params = oka_item.get("transformation", {}) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 45 | if "sw_catalog_path" in oka_item: |
| 46 | oka_path = oka_item["sw_catalog_path"] |
| 47 | else: |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 48 | oka_type = MAP_PROFILE[ |
| 49 | oka_item.get("profile_type", "infra_controller_profiles") |
| 50 | ] |
| garciadeblas | 29f8bcf | 2025-01-24 14:24:41 +0100 | [diff] [blame] | 51 | oka_name = oka_item["git_name"].lower() |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 52 | oka_path = f"{oka_type}/{oka_name}/templates" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 53 | |
| 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 |
| 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") |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 66 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 67 | target_ns = oka_params.get("namespace", "default") |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 68 | substitute_environment = oka_params.get("substitute_environment", "true").lower() |
| 69 | custom_env_vars = oka_params.get("custom_env_vars", {}) |
| garciadeblas | 58a6aee | 2025-12-18 12:14:58 +0100 | [diff] [blame] | 70 | if custom_env_vars is None: |
| 71 | custom_env_vars = {} |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 72 | if "APPNAME" not in custom_env_vars: |
| 73 | custom_env_vars["APPNAME"] = ksu_name |
| 74 | if "TARGET_NS" not in custom_env_vars: |
| 75 | custom_env_vars["TARGET_NS"] = target_ns |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 76 | if "KUSTOMIZATION_NS" not in custom_env_vars: |
| 77 | custom_env_vars["KUSTOMIZATION_NS"] = kustomization_ns |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 78 | custom_env_vars_str = "|\n" |
| 79 | substitution_filter_list = [] |
| 80 | for k, v in custom_env_vars.items(): |
| 81 | custom_env_vars_str += " " * 10 + f"{k}={v}\n" |
| 82 | substitution_filter_list.append(f"${k}") |
| 83 | substitution_filter = ",".join(substitution_filter_list) |
| 84 | # TODO: add additional substitution filters |
| 85 | # substitution_filter = ( |
| 86 | # f"{substitution_filter},{oka_params.get('substitution_filter', '')}".strip(",") |
| 87 | # ) |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 88 | inline_values = oka_params.get("inline_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 89 | if inline_values: |
| 90 | yaml_string = yaml.safe_dump( |
| 91 | inline_values, sort_keys=False, default_flow_style=False |
| 92 | ) |
| 93 | inline_values = "|\n" + "\n".join( |
| 94 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 95 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 96 | else: |
| 97 | inline_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 98 | is_preexisting_cm = "false" |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 99 | cm_values = oka_params.get("configmap_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 100 | if cm_values: |
| 101 | yaml_string = yaml.safe_dump( |
| 102 | cm_values, sort_keys=False, default_flow_style=False |
| 103 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 104 | cm_values = "|\n" + "\n".join( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 105 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 106 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 107 | values_configmap_name = f"cm-{ksu_name}" |
| 108 | cm_key = "values.yaml" |
| 109 | else: |
| 110 | values_configmap_name = "" |
| 111 | cm_key = "" |
| 112 | cm_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 113 | is_preexisting_secret = "false" |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 114 | secret_values = oka_params.get("secret_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 115 | if secret_values: |
| 116 | values_secret_name = f"secret-{ksu_name}" |
| 117 | reference_secret_for_values = f"ref-secret-{ksu_name}" |
| 118 | reference_key_for_values = f"ref-key-{ksu_name}" |
| 119 | secret_values = yaml.safe_dump( |
| 120 | secret_values, sort_keys=False, default_flow_style=False |
| 121 | ) |
| 122 | else: |
| 123 | values_secret_name = "" |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 124 | reference_secret_for_values = "this-secret-does-not-exist" |
| 125 | reference_key_for_values = "this-key-does-not-exist" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 126 | sync = "true" |
| 127 | |
| 128 | if secret_values: |
| 129 | secret_namespace = "osm-workflows" |
| 130 | # Create secret |
| 131 | await self.create_secret( |
| 132 | reference_secret_for_values, |
| 133 | secret_namespace, |
| 134 | reference_key_for_values, |
| 135 | secret_values, |
| 136 | ) |
| 137 | |
| 138 | # Render workflow |
| 139 | manifest = self.render_jinja_template( |
| 140 | workflow_template, |
| 141 | output_file=None, |
| 142 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 143 | git_fleet_url=self._repo_fleet_url, |
| 144 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 145 | templates_path=oka_path, |
| 146 | substitute_environment=substitute_environment, |
| 147 | substitution_filter=substitution_filter, |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 148 | custom_env_vars=custom_env_vars_str, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 149 | kustomization_name=kustomization_name, |
| 150 | helmrelease_name=helmrelease_name, |
| 151 | inline_values=inline_values, |
| 152 | is_preexisting_secret=is_preexisting_secret, |
| 153 | target_ns=target_ns, |
| 154 | age_public_key=age_public_key, |
| 155 | values_secret_name=values_secret_name, |
| 156 | reference_secret_for_values=reference_secret_for_values, |
| 157 | reference_key_for_values=reference_key_for_values, |
| 158 | is_preexisting_cm=is_preexisting_cm, |
| 159 | values_configmap_name=values_configmap_name, |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 160 | cm_key=cm_key, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 161 | cm_values=cm_values, |
| 162 | ksu_name=ksu_name, |
| 163 | profile_name=profile_name, |
| 164 | profile_type=profile_type, |
| 165 | osm_project_name=osm_project_name, |
| 166 | sync=sync, |
| 167 | workflow_debug=self._workflow_debug, |
| 168 | workflow_dry_run=self._workflow_dry_run, |
| 169 | ) |
| 170 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 171 | |
| 172 | # Submit workflow |
| 173 | self._kubectl.create_generic_object( |
| 174 | namespace="osm-workflows", |
| 175 | manifest_dict=yaml.safe_load(manifest), |
| 176 | api_group="argoproj.io", |
| 177 | api_plural="workflows", |
| 178 | api_version="v1alpha1", |
| 179 | ) |
| garciadeblas | 61a4c69 | 2025-07-17 13:04:13 +0200 | [diff] [blame^] | 180 | return True, workflow_name, None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 181 | |
| 182 | |
| 183 | async def update_ksus(self, op_id, op_params_list, content_list): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 184 | self.logger.info(f"update_ksus Enter. Operation {op_id}. Params: {op_params_list}") |
| 185 | # self.logger.debug(f"Content: {content_list}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 186 | |
| 187 | if len(content_list) > 1: |
| 188 | raise Exception("There is no ODU workflow yet able to manage multiple KSUs") |
| 189 | db_ksu = content_list[0] |
| 190 | ksu_params = op_params_list[0] |
| 191 | oka_list = ksu_params["oka"] |
| 192 | if len(oka_list) > 1: |
| 193 | raise Exception( |
| 194 | "There is no ODU workflow yet able to manage multiple OKAs for a KSU" |
| 195 | ) |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 196 | oka_item = oka_list[0] |
| 197 | oka_params = oka_item.get("transformation", {}) |
| 198 | if "sw_catalog_path" in oka_item: |
| 199 | oka_path = oka_item["sw_catalog_path"] |
| 200 | else: |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 201 | oka_type = MAP_PROFILE[ |
| 202 | oka_item.get("profile_type", "infra_controller_profiles") |
| 203 | ] |
| garciadeblas | 29f8bcf | 2025-01-24 14:24:41 +0100 | [diff] [blame] | 204 | oka_name = oka_item["git_name"].lower() |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 205 | oka_path = f"{oka_type}/{oka_name}/templates" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 206 | |
| 207 | workflow_template = "launcher-update-ksu-hr.j2" |
| 208 | workflow_name = f"update-ksus-{op_id}" |
| 209 | ksu_name = db_ksu["git_name"].lower() |
| 210 | |
| 211 | # Additional params for the workflow |
| 212 | osm_project_name = "osm_admin" # TODO: get project name from db_ksu |
| 213 | kustomization_name = ksu_name |
| 214 | helmrelease_name = ksu_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 215 | profile_type = ksu_params.get("profile", {}).get("profile_type") |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 216 | profile_type = MAP_PROFILE[profile_type] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 217 | profile_name = ksu_params.get("profile", {}).get("name") |
| 218 | age_public_key = ksu_params.get("profile", {}).get("age_pubkey") |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 219 | kustomization_ns = oka_params.get("kustomization_namespace", "flux-system") |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 220 | target_ns = oka_params.get("namespace", "default") |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 221 | substitute_environment = oka_params.get("substitute_environment", "true").lower() |
| 222 | custom_env_vars = oka_params.get("custom_env_vars", {}) |
| garciadeblas | 58a6aee | 2025-12-18 12:14:58 +0100 | [diff] [blame] | 223 | if custom_env_vars is None: |
| 224 | custom_env_vars = {} |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 225 | if "APPNAME" not in custom_env_vars: |
| 226 | custom_env_vars["APPNAME"] = ksu_name |
| 227 | if "TARGET_NS" not in custom_env_vars: |
| 228 | custom_env_vars["TARGET_NS"] = target_ns |
| garciadeblas | 167dde3 | 2025-02-14 00:44:58 +0100 | [diff] [blame] | 229 | if "KUSTOMIZATION_NS" not in custom_env_vars: |
| 230 | custom_env_vars["KUSTOMIZATION_NS"] = kustomization_ns |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 231 | custom_env_vars_str = "|\n" |
| 232 | substitution_filter_list = [] |
| 233 | for k, v in custom_env_vars.items(): |
| 234 | custom_env_vars_str += " " * 10 + f"{k}={v}\n" |
| 235 | substitution_filter_list.append(f"${k}") |
| 236 | substitution_filter = ",".join(substitution_filter_list) |
| 237 | # TODO: add additional substitution filters |
| 238 | # substitution_filter = ( |
| 239 | # f"{substitution_filter},{oka_params.get('substitution_filter', '')}".strip(",") |
| 240 | # ) |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 241 | inline_values = oka_params.get("inline_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 242 | if inline_values: |
| 243 | yaml_string = yaml.safe_dump( |
| 244 | inline_values, sort_keys=False, default_flow_style=False |
| 245 | ) |
| 246 | inline_values = "|\n" + "\n".join( |
| 247 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 248 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 249 | else: |
| 250 | inline_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 251 | is_preexisting_cm = "false" |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 252 | cm_values = oka_params.get("configmap_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 253 | if cm_values: |
| 254 | yaml_string = yaml.safe_dump( |
| 255 | cm_values, sort_keys=False, default_flow_style=False |
| 256 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 257 | cm_values = "|\n" + "\n".join( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 258 | [" " * 8 + line for line in yaml_string.splitlines()] |
| 259 | ) |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 260 | values_configmap_name = f"cm-{ksu_name}" |
| 261 | cm_key = "values.yaml" |
| 262 | else: |
| 263 | values_configmap_name = "" |
| 264 | cm_key = "" |
| 265 | cm_values = '""' |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 266 | is_preexisting_secret = "false" |
| garciadeblas | 1b2933c | 2024-10-16 11:43:36 +0200 | [diff] [blame] | 267 | secret_values = oka_params.get("secret_values", "") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 268 | if secret_values: |
| 269 | values_secret_name = f"secret-{ksu_name}" |
| 270 | reference_secret_for_values = f"ref-secret-{ksu_name}" |
| 271 | reference_key_for_values = f"ref-key-{ksu_name}" |
| 272 | secret_values = yaml.safe_dump( |
| 273 | secret_values, sort_keys=False, default_flow_style=False |
| 274 | ) |
| 275 | else: |
| 276 | values_secret_name = "" |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 277 | reference_secret_for_values = "this-secret-does-not-exist" |
| 278 | reference_key_for_values = "this-key-does-not-exist" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 279 | |
| 280 | if secret_values: |
| 281 | secret_namespace = "osm-workflows" |
| 282 | # Create secret |
| 283 | await self.create_secret( |
| 284 | reference_secret_for_values, |
| 285 | secret_namespace, |
| 286 | reference_key_for_values, |
| 287 | secret_values, |
| 288 | ) |
| 289 | |
| 290 | # Render workflow |
| 291 | manifest = self.render_jinja_template( |
| 292 | workflow_template, |
| 293 | output_file=None, |
| 294 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 295 | git_fleet_url=self._repo_fleet_url, |
| 296 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 297 | templates_path=oka_path, |
| 298 | substitute_environment=substitute_environment, |
| 299 | substitution_filter=substitution_filter, |
| garciadeblas | 6347c8a | 2025-01-17 01:29:37 +0100 | [diff] [blame] | 300 | custom_env_vars=custom_env_vars_str, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 301 | kustomization_name=kustomization_name, |
| 302 | helmrelease_name=helmrelease_name, |
| 303 | inline_values=inline_values, |
| 304 | is_preexisting_secret=is_preexisting_secret, |
| 305 | target_ns=target_ns, |
| 306 | age_public_key=age_public_key, |
| 307 | values_secret_name=values_secret_name, |
| 308 | reference_secret_for_values=reference_secret_for_values, |
| 309 | reference_key_for_values=reference_key_for_values, |
| 310 | is_preexisting_cm=is_preexisting_cm, |
| 311 | values_configmap_name=values_configmap_name, |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 312 | cm_key=cm_key, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 313 | cm_values=cm_values, |
| 314 | ksu_name=ksu_name, |
| 315 | profile_name=profile_name, |
| 316 | profile_type=profile_type, |
| 317 | osm_project_name=osm_project_name, |
| 318 | workflow_debug=self._workflow_debug, |
| 319 | workflow_dry_run=self._workflow_dry_run, |
| 320 | ) |
| 321 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 322 | |
| 323 | # Submit workflow |
| 324 | self._kubectl.create_generic_object( |
| 325 | namespace="osm-workflows", |
| 326 | manifest_dict=yaml.safe_load(manifest), |
| 327 | api_group="argoproj.io", |
| 328 | api_plural="workflows", |
| 329 | api_version="v1alpha1", |
| 330 | ) |
| garciadeblas | 61a4c69 | 2025-07-17 13:04:13 +0200 | [diff] [blame^] | 331 | return True, workflow_name, None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 332 | |
| 333 | |
| 334 | async def delete_ksus(self, op_id, op_params_list, content_list): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 335 | self.logger.info(f"delete_ksus Enter. Operation {op_id}. Params: {op_params_list}") |
| 336 | # self.logger.debug(f"Content: {content_list}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 337 | |
| 338 | if len(content_list) > 1: |
| 339 | raise Exception("There is no ODU workflow yet able to manage multiple KSUs") |
| 340 | db_ksu = content_list[0] |
| 341 | ksu_params = op_params_list[0] |
| 342 | |
| 343 | workflow_template = "launcher-delete-ksu.j2" |
| 344 | workflow_name = f"delete-ksus-{op_id}" |
| 345 | ksu_name = db_ksu["git_name"].lower() |
| 346 | |
| 347 | # Additional params for the workflow |
| 348 | osm_project_name = "osm_admin" # TODO: get project name from db_ksu |
| 349 | profile_name = ksu_params.get("profile", {}).get("name") |
| 350 | profile_type = ksu_params.get("profile", {}).get("profile_type") |
| garciadeblas | f7dfdb7 | 2024-09-25 12:15:40 +0200 | [diff] [blame] | 351 | profile_type = MAP_PROFILE[profile_type] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 352 | |
| 353 | # Render workflow |
| 354 | manifest = self.render_jinja_template( |
| 355 | workflow_template, |
| 356 | output_file=None, |
| 357 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 358 | git_fleet_url=self._repo_fleet_url, |
| 359 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 360 | ksu_name=ksu_name, |
| 361 | profile_name=profile_name, |
| 362 | profile_type=profile_type, |
| 363 | osm_project_name=osm_project_name, |
| 364 | workflow_debug=self._workflow_debug, |
| 365 | workflow_dry_run=self._workflow_dry_run, |
| 366 | ) |
| 367 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 368 | |
| 369 | # Submit workflow |
| 370 | self._kubectl.create_generic_object( |
| 371 | namespace="osm-workflows", |
| 372 | manifest_dict=yaml.safe_load(manifest), |
| 373 | api_group="argoproj.io", |
| 374 | api_plural="workflows", |
| 375 | api_version="v1alpha1", |
| 376 | ) |
| garciadeblas | 61a4c69 | 2025-07-17 13:04:13 +0200 | [diff] [blame^] | 377 | return True, workflow_name, None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 378 | |
| 379 | |
| 380 | async def clone_ksu(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 381 | self.logger.info(f"clone_ksu Enter. Operation {op_id}. Params: {op_params}") |
| 382 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 383 | workflow_name = f"clone-ksu-{content['_id']}" |
| garciadeblas | 61a4c69 | 2025-07-17 13:04:13 +0200 | [diff] [blame^] | 384 | return True, workflow_name, None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 385 | |
| 386 | |
| 387 | async def move_ksu(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 388 | self.logger.info(f"move_ksu Enter. Operation {op_id}. Params: {op_params}") |
| 389 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 390 | workflow_name = f"move-ksu-{content['_id']}" |
| garciadeblas | 61a4c69 | 2025-07-17 13:04:13 +0200 | [diff] [blame^] | 391 | return True, workflow_name, None |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 392 | |
| 393 | |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 394 | async def clean_items_ksu_create(self, op_id, op_params_list, content_list): |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 395 | self.logger.info( |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 396 | f"clean_items_ksu_create Enter. Operation {op_id}. Params: {op_params_list}" |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 397 | ) |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 398 | # self.logger.debug(f"Content: {content_list}") |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 399 | try: |
| 400 | if len(content_list) > 1: |
| 401 | raise Exception("There is no ODU workflow yet able to manage multiple KSUs") |
| 402 | db_ksu = content_list[0] |
| 403 | ksu_name = db_ksu["git_name"].lower() |
| 404 | ksu_params = op_params_list[0] |
| 405 | oka_list = ksu_params["oka"] |
| 406 | if len(oka_list) > 1: |
| 407 | raise Exception( |
| 408 | "There is no ODU workflow yet able to manage multiple OKAs for a KSU" |
| 409 | ) |
| 410 | oka_item = oka_list[0] |
| 411 | oka_params = oka_item.get("transformation", {}) |
| 412 | secret_values = oka_params.get("secret_values", "") |
| 413 | if secret_values: |
| 414 | items = { |
| 415 | "secrets": [ |
| 416 | { |
| 417 | "name": f"ref-secret-{ksu_name}", |
| 418 | "namespace": "osm-workflows", |
| 419 | } |
| 420 | ] |
| 421 | } |
| 422 | await self.clean_items(items) |
| 423 | return True, "OK" |
| 424 | except Exception as e: |
| 425 | return False, f"Error while cleaning items: {e}" |
| 426 | |
| 427 | |
| 428 | async def clean_items_ksu_update(self, op_id, op_params_list, content_list): |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 429 | self.logger.info( |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 430 | f"clean_items_ksu_update Enter. Operation {op_id}. Params: {op_params_list}" |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 431 | ) |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 432 | # self.logger.debug(f"Content: {content_list}") |
| garciadeblas | d842985 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 433 | return await self.clean_items_ksu_create(op_id, op_params_list, content_list) |
| garciadeblas | b23d2dc | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 434 | |
| 435 | |
| 436 | async def clean_items_ksu_delete(self, op_id, op_params_list, content_list): |
| 437 | self.logger.info( |
| 438 | f"clean_items_ksu_delete Enter. Operation {op_id}. Params: {op_params_list}" |
| 439 | ) |
| 440 | # self.logger.debug(f"Content: {content_list}") |
| 441 | return True, "OK" |