| 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 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 19 | import yaml |
| 20 | import base64 |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 21 | import json |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def gather_age_key(cluster): |
| 25 | pubkey = cluster.get("age_pubkey") |
| 26 | privkey = cluster.get("age_privkey") |
| 27 | # return both public and private key |
| 28 | return pubkey, privkey |
| 29 | |
| 30 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 31 | async def create_cluster(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 32 | self.logger.info(f"create_cluster Enter. Operation {op_id}. Params: {op_params}") |
| 33 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 34 | |
| 35 | db_cluster = content["cluster"] |
| 36 | db_vim_account = content["vim_account"] |
| 37 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 38 | workflow_template = "launcher-create-crossplane-cluster-and-bootstrap.j2" |
| 39 | workflow_name = f"create-cluster-{db_cluster['_id']}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 40 | cluster_name = db_cluster["git_name"].lower() |
| 41 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 42 | # Get age key |
| 43 | public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 44 | # self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}") |
| 45 | # self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 46 | |
| 47 | # Test kubectl connection |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 48 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 49 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 50 | self.logger.debug( |
| 51 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 52 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 53 | self.logger.debug(self._kubectl._get_kubectl_version()) |
| 54 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 55 | # Create temporal secret with agekey |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 56 | secret_name = f"secret-age-{cluster_name}" |
| 57 | secret_namespace = "osm-workflows" |
| 58 | secret_key = "agekey" |
| 59 | secret_value = private_key_new_cluster |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 60 | try: |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 61 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 62 | self.logger.debug( |
| 63 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 64 | ) |
| 65 | self.logger.debug( |
| 66 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 67 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 68 | await self.create_secret( |
| 69 | secret_name, |
| 70 | secret_namespace, |
| 71 | secret_key, |
| 72 | secret_value, |
| 73 | ) |
| 74 | except Exception as e: |
| 75 | self.logger.info(f"Cannot create secret {secret_name}: {e}") |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 76 | return False, f"Cannot create secret {secret_name}: {e}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 77 | |
| 78 | # Additional params for the workflow |
| 79 | cluster_kustomization_name = cluster_name |
| 80 | osm_project_name = "osm_admin" # TODO: get project name from content |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 81 | vim_account_id = db_cluster["vim_account"] |
| 82 | providerconfig_name = f"{vim_account_id}-config" |
| 83 | vim_type = db_vim_account["vim_type"] |
| garciadeblas | 753b1e3 | 2024-11-06 12:56:33 +0100 | [diff] [blame] | 84 | if db_cluster.get("bootstrap", True): |
| 85 | skip_bootstrap = "false" |
| 86 | else: |
| 87 | skip_bootstrap = "true" |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 88 | if vim_type == "azure": |
| 89 | cluster_type = "aks" |
| 90 | elif vim_type == "aws": |
| 91 | cluster_type = "eks" |
| 92 | elif vim_type == "gcp": |
| 93 | cluster_type = "gke" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 94 | else: |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 95 | raise Exception("Not suitable VIM account to register cluster") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 96 | |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 97 | # Create configmap for subnet |
| 98 | configmap_name = None |
| 99 | data = {} |
| 100 | private_subnets = op_params.get("private_subnet") |
| 101 | public_subnets = op_params.get("public_subnet") |
| 102 | if private_subnets or public_subnets: |
| 103 | configmap_name = f"{cluster_name}-parameters" |
| 104 | configmap_namespace = "managed-resources" |
| 105 | data["private_subnets"] = f"{json.dumps(private_subnets)}" |
| 106 | data["public_subnets"] = f"{json.dumps(public_subnets)}" |
| 107 | try: |
| 108 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 109 | self.logger.debug( |
| 110 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 111 | ) |
| 112 | self.logger.debug( |
| 113 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 114 | ) |
| 115 | await self.create_configmap( |
| 116 | configmap_name, |
| 117 | configmap_namespace, |
| 118 | data, |
| 119 | ) |
| 120 | except Exception as e: |
| 121 | self.logger.info(f"Cannot create configmap {configmap_name}: {e}") |
| 122 | return False, f"Cannot create configmap {configmap_name}: {e}" |
| 123 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 124 | # Render workflow |
| 125 | # workflow_kwargs = { |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 126 | # "git_fleet_url": self._repo_fleet_url, |
| 127 | # "git_sw_catalogs_url": self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 128 | # } |
| 129 | # manifest = self.render_jinja_template( |
| 130 | # workflow_template, |
| 131 | # output_file=None, |
| 132 | # **workflow_kwargs |
| 133 | # ) |
| 134 | manifest = self.render_jinja_template( |
| 135 | workflow_template, |
| 136 | output_file=None, |
| 137 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 138 | git_fleet_url=self._repo_fleet_url, |
| 139 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 140 | cluster_name=cluster_name, |
| 141 | cluster_type=cluster_type, |
| 142 | cluster_kustomization_name=cluster_kustomization_name, |
| 143 | providerconfig_name=providerconfig_name, |
| 144 | public_key_mgmt=self._pubkey, |
| 145 | public_key_new_cluster=public_key_new_cluster, |
| 146 | secret_name_private_key_new_cluster=secret_name, |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 147 | vm_size=db_cluster.get("node_size", "default"), |
| 148 | node_count=db_cluster.get("node_count", "default"), |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 149 | k8s_version=db_cluster["k8s_version"], |
| 150 | cluster_location=db_cluster["region_name"], |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 151 | configmap_name=configmap_name if configmap_name else "default", |
| 152 | cluster_iam_role=db_cluster.get("iam_role", "default"), |
| 153 | cluster_private_subnets_id=db_cluster.get("private_subnet", "default"), |
| 154 | cluster_public_subnets_id=db_cluster.get("public_subnet", "default"), |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 155 | osm_project_name=osm_project_name, |
| garciadeblas | d84808e | 2024-11-18 17:10:00 +0100 | [diff] [blame] | 156 | rg_name=db_cluster.get("resource_group", "''"), |
| 157 | preemptible_nodes=db_cluster.get("preemptible_nodes", "false"), |
| garciadeblas | 753b1e3 | 2024-11-06 12:56:33 +0100 | [diff] [blame] | 158 | skip_bootstrap=skip_bootstrap, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 159 | workflow_debug=self._workflow_debug, |
| 160 | workflow_dry_run=self._workflow_dry_run, |
| 161 | ) |
| 162 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 163 | |
| 164 | # Submit workflow |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 165 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 166 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 167 | self.logger.debug( |
| 168 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 169 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 170 | self._kubectl.create_generic_object( |
| 171 | namespace="osm-workflows", |
| 172 | manifest_dict=yaml.safe_load(manifest), |
| 173 | api_group="argoproj.io", |
| 174 | api_plural="workflows", |
| 175 | api_version="v1alpha1", |
| 176 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 177 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 178 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 179 | |
| 180 | async def update_cluster(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 181 | self.logger.info(f"update_cluster Enter. Operation {op_id}. Params: {op_params}") |
| 182 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 183 | |
| 184 | db_cluster = content["cluster"] |
| 185 | db_vim_account = content["vim_account"] |
| garciadeblas | 73cd5a2 | 2024-11-06 10:45:22 +0100 | [diff] [blame] | 186 | cluster_name = db_cluster["git_name"].lower() |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 187 | |
| 188 | workflow_template = "launcher-update-crossplane-cluster.j2" |
| garciadeblas | c8c75d4 | 2024-11-13 12:36:13 +0100 | [diff] [blame] | 189 | workflow_name = f"update-cluster-{op_id}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 190 | # cluster_name = db_cluster["name"].lower() |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 191 | |
| 192 | # Get age key |
| 193 | public_key_cluster, private_key_cluster = gather_age_key(db_cluster) |
| 194 | self.logger.debug(f"public_key_new_cluster={public_key_cluster}") |
| 195 | self.logger.debug(f"private_key_new_cluster={private_key_cluster}") |
| 196 | |
| 197 | # Create secret with agekey |
| 198 | secret_name = f"secret-age-{cluster_name}" |
| 199 | secret_namespace = "osm-workflows" |
| 200 | secret_key = "agekey" |
| 201 | secret_value = private_key_cluster |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 202 | try: |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 203 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 204 | self.logger.debug( |
| 205 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 206 | ) |
| 207 | self.logger.debug( |
| 208 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 209 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 210 | await self.create_secret( |
| 211 | secret_name, |
| 212 | secret_namespace, |
| 213 | secret_key, |
| 214 | secret_value, |
| 215 | ) |
| 216 | except Exception as e: |
| 217 | self.logger.info(f"Cannot create secret {secret_name}: {e}") |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 218 | return False, f"Cannot create secret {secret_name}: {e}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 219 | |
| 220 | # Additional params for the workflow |
| 221 | cluster_kustomization_name = cluster_name |
| 222 | osm_project_name = "osm_admin" # TODO: get project name from db_cluster |
| 223 | vim_account_id = db_cluster["vim_account"] |
| 224 | providerconfig_name = f"{vim_account_id}-config" |
| 225 | vim_type = db_vim_account["vim_type"] |
| garciadeblas | 73cd5a2 | 2024-11-06 10:45:22 +0100 | [diff] [blame] | 226 | vm_size = op_params.get("node_size", db_cluster["node_size"]) |
| 227 | node_count = op_params.get("node_count", db_cluster["node_count"]) |
| 228 | k8s_version = op_params.get("k8s_version", db_cluster["k8s_version"]) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 229 | if vim_type == "azure": |
| 230 | cluster_type = "aks" |
| 231 | elif vim_type == "aws": |
| 232 | cluster_type = "eks" |
| 233 | elif vim_type == "gcp": |
| 234 | cluster_type = "gke" |
| 235 | else: |
| 236 | raise Exception("Not suitable VIM account to update cluster") |
| 237 | |
| 238 | # Render workflow |
| 239 | manifest = self.render_jinja_template( |
| 240 | workflow_template, |
| 241 | output_file=None, |
| 242 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 243 | git_fleet_url=self._repo_fleet_url, |
| 244 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 245 | cluster_name=cluster_name, |
| 246 | cluster_type=cluster_type, |
| 247 | cluster_kustomization_name=cluster_kustomization_name, |
| 248 | providerconfig_name=providerconfig_name, |
| 249 | public_key_mgmt=self._pubkey, |
| 250 | public_key_new_cluster=public_key_cluster, |
| 251 | secret_name_private_key_new_cluster=secret_name, |
| garciadeblas | 73cd5a2 | 2024-11-06 10:45:22 +0100 | [diff] [blame] | 252 | vm_size=vm_size, |
| 253 | node_count=node_count, |
| 254 | k8s_version=k8s_version, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 255 | cluster_location=db_cluster["region_name"], |
| 256 | osm_project_name=osm_project_name, |
| garciadeblas | d84808e | 2024-11-18 17:10:00 +0100 | [diff] [blame] | 257 | rg_name=db_cluster.get("resource_group", "''"), |
| 258 | preemptible_nodes=db_cluster.get("preemptible_nodes", "false"), |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 259 | workflow_debug=self._workflow_debug, |
| 260 | workflow_dry_run=self._workflow_dry_run, |
| 261 | ) |
| 262 | self.logger.info(manifest) |
| 263 | |
| 264 | # Submit workflow |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 265 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 266 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 267 | self.logger.debug( |
| 268 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 269 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 270 | self._kubectl.create_generic_object( |
| 271 | namespace="osm-workflows", |
| 272 | manifest_dict=yaml.safe_load(manifest), |
| 273 | api_group="argoproj.io", |
| 274 | api_plural="workflows", |
| 275 | api_version="v1alpha1", |
| 276 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 277 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 278 | |
| 279 | |
| 280 | async def delete_cluster(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 281 | self.logger.info(f"delete_cluster Enter. Operation {op_id}. Params: {op_params}") |
| 282 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 283 | |
| 284 | db_cluster = content["cluster"] |
| 285 | |
| 286 | workflow_template = "launcher-delete-cluster.j2" |
| 287 | workflow_name = f"delete-cluster-{db_cluster['_id']}" |
| 288 | # cluster_name = db_cluster["name"].lower() |
| 289 | cluster_name = db_cluster["git_name"].lower() |
| 290 | |
| 291 | # Additional params for the workflow |
| 292 | cluster_kustomization_name = cluster_name |
| 293 | osm_project_name = "osm_admin" # TODO: get project name from DB |
| 294 | |
| 295 | # Render workflow |
| 296 | manifest = self.render_jinja_template( |
| 297 | workflow_template, |
| 298 | output_file=None, |
| 299 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 300 | git_fleet_url=self._repo_fleet_url, |
| 301 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 302 | cluster_name=cluster_name, |
| 303 | cluster_kustomization_name=cluster_kustomization_name, |
| 304 | osm_project_name=osm_project_name, |
| 305 | workflow_debug=self._workflow_debug, |
| 306 | workflow_dry_run=self._workflow_dry_run, |
| 307 | ) |
| 308 | self.logger.info(manifest) |
| 309 | |
| 310 | # Submit workflow |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 311 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 312 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 313 | self.logger.debug( |
| 314 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 315 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 316 | self._kubectl.create_generic_object( |
| 317 | namespace="osm-workflows", |
| 318 | manifest_dict=yaml.safe_load(manifest), |
| 319 | api_group="argoproj.io", |
| 320 | api_plural="workflows", |
| 321 | api_version="v1alpha1", |
| 322 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 323 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 324 | |
| 325 | |
| 326 | async def register_cluster(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 327 | self.logger.info(f"register_cluster Enter. Operation {op_id}. Params: {op_params}") |
| 328 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 329 | |
| 330 | db_cluster = content["cluster"] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 331 | cluster_name = db_cluster["git_name"].lower() |
| 332 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 333 | workflow_template = "launcher-bootstrap-cluster.j2" |
| 334 | workflow_name = f"register-cluster-{db_cluster['_id']}" |
| 335 | |
| 336 | # Get age key |
| 337 | public_key_new_cluster, private_key_new_cluster = gather_age_key(db_cluster) |
| 338 | self.logger.debug(f"public_key_new_cluster={public_key_new_cluster}") |
| 339 | self.logger.debug(f"private_key_new_cluster={private_key_new_cluster}") |
| 340 | |
| 341 | # Create temporal secret with agekey |
| 342 | secret_name = f"secret-age-{cluster_name}" |
| 343 | secret_namespace = "osm-workflows" |
| 344 | secret_key = "agekey" |
| 345 | secret_value = private_key_new_cluster |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 346 | try: |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 347 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 348 | self.logger.debug( |
| 349 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 350 | ) |
| 351 | self.logger.debug( |
| 352 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 353 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 354 | await self.create_secret( |
| 355 | secret_name, |
| 356 | secret_namespace, |
| 357 | secret_key, |
| 358 | secret_value, |
| 359 | ) |
| 360 | except Exception as e: |
| 361 | self.logger.info( |
| 362 | f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}" |
| 363 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 364 | return ( |
| 365 | False, |
| 366 | f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}", |
| 367 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 368 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 369 | # Create secret with kubeconfig |
| 370 | secret_name2 = f"kubeconfig-{cluster_name}" |
| 371 | secret_namespace2 = "managed-resources" |
| 372 | secret_key2 = "kubeconfig" |
| 373 | secret_value2 = yaml.safe_dump( |
| garciadeblas | a82300f | 2024-11-18 10:24:26 +0100 | [diff] [blame] | 374 | db_cluster["credentials"], indent=2, default_flow_style=False, sort_keys=False |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 375 | ) |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 376 | try: |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 377 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 378 | self.logger.debug( |
| 379 | f"Testing kubectl configuration: {self._kubectl.configuration}" |
| 380 | ) |
| 381 | self.logger.debug( |
| 382 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 383 | ) |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 384 | await self.create_secret( |
| 385 | secret_name2, |
| 386 | secret_namespace2, |
| 387 | secret_key2, |
| 388 | secret_value2, |
| 389 | ) |
| 390 | except Exception as e: |
| 391 | self.logger.info( |
| 392 | f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}" |
| 393 | ) |
| garciadeblas | 41859ce | 2025-02-04 16:08:51 +0100 | [diff] [blame] | 394 | return ( |
| 395 | False, |
| 396 | f"Cannot create secret {secret_name} in namespace {secret_namespace}: {e}", |
| 397 | ) |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 398 | |
| 399 | # Additional params for the workflow |
| 400 | cluster_kustomization_name = cluster_name |
| 401 | osm_project_name = "osm_admin" # TODO: get project name from content |
| garciadeblas | 6504749 | 2025-09-17 23:33:13 +0200 | [diff] [blame] | 402 | if db_cluster.get("openshift", True): |
| 403 | templates_dir = "/sw-catalogs/sw-catalogs-osm/cloud-resources/flux-remote-bootstrap/cluster-base-openshift/templates" |
| 404 | self.logger.info( |
| 405 | "Rendering OpenShift bootstrap templates from %s", templates_dir |
| 406 | ) |
| 407 | else: |
| 408 | templates_dir = "/sw-catalogs/sw-catalogs-osm/cloud-resources/flux-remote-bootstrap/cluster-base/templates" |
| 409 | self.logger.info( |
| 410 | "Rendering Standard bootstrap templates from %s", templates_dir |
| 411 | ) |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 412 | |
| 413 | manifest = self.render_jinja_template( |
| 414 | workflow_template, |
| 415 | output_file=None, |
| 416 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 417 | git_fleet_url=self._repo_fleet_url, |
| 418 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 419 | cluster_name=cluster_name, |
| 420 | cluster_kustomization_name=cluster_kustomization_name, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 421 | public_key_mgmt=self._pubkey, |
| 422 | public_key_new_cluster=public_key_new_cluster, |
| 423 | secret_name_private_key_new_cluster=secret_name, |
| 424 | osm_project_name=osm_project_name, |
| garciadeblas | 6504749 | 2025-09-17 23:33:13 +0200 | [diff] [blame] | 425 | templates_dir=templates_dir, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 426 | workflow_debug=self._workflow_debug, |
| 427 | workflow_dry_run=self._workflow_dry_run, |
| 428 | ) |
| 429 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 430 | |
| 431 | # Submit workflow |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 432 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 433 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 434 | self.logger.debug( |
| 435 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 436 | ) |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 437 | self._kubectl.create_generic_object( |
| 438 | namespace="osm-workflows", |
| 439 | manifest_dict=yaml.safe_load(manifest), |
| 440 | api_group="argoproj.io", |
| 441 | api_plural="workflows", |
| 442 | api_version="v1alpha1", |
| 443 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 444 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 445 | |
| 446 | |
| 447 | async def deregister_cluster(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 448 | self.logger.info( |
| 449 | f"deregister_cluster Enter. Operation {op_id}. Params: {op_params}" |
| 450 | ) |
| 451 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 452 | |
| 453 | db_cluster = content["cluster"] |
| 454 | cluster_name = db_cluster["git_name"].lower() |
| 455 | |
| 456 | workflow_template = "launcher-disconnect-flux-remote-cluster.j2" |
| 457 | workflow_name = f"deregister-cluster-{db_cluster['_id']}" |
| 458 | |
| 459 | # Additional params for the workflow |
| 460 | cluster_kustomization_name = cluster_name |
| 461 | osm_project_name = "osm_admin" # TODO: get project name from DB |
| 462 | |
| 463 | # Render workflow |
| 464 | manifest = self.render_jinja_template( |
| 465 | workflow_template, |
| 466 | output_file=None, |
| 467 | workflow_name=workflow_name, |
| garciadeblas | 56c3aa8 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 468 | git_fleet_url=self._repo_fleet_url, |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 469 | cluster_kustomization_name=cluster_kustomization_name, |
| 470 | osm_project_name=osm_project_name, |
| 471 | workflow_debug=self._workflow_debug, |
| 472 | workflow_dry_run=self._workflow_dry_run, |
| 473 | ) |
| 474 | self.logger.info(manifest) |
| 475 | |
| 476 | # Submit workflow |
| garciadeblas | 6d8acf3 | 2025-02-06 13:34:37 +0100 | [diff] [blame] | 477 | self.logger.debug(f"Testing kubectl: {self._kubectl}") |
| 478 | self.logger.debug(f"Testing kubectl configuration: {self._kubectl.configuration}") |
| 479 | self.logger.debug( |
| 480 | f"Testing kubectl configuration Host: {self._kubectl.configuration.host}" |
| 481 | ) |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 482 | self._kubectl.create_generic_object( |
| 483 | namespace="osm-workflows", |
| 484 | manifest_dict=yaml.safe_load(manifest), |
| 485 | api_group="argoproj.io", |
| 486 | api_plural="workflows", |
| 487 | api_version="v1alpha1", |
| 488 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 489 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 490 | |
| 491 | |
| 492 | async def get_cluster_credentials(self, db_cluster): |
| 493 | """ |
| 494 | returns the kubeconfig file of a K8s cluster in a dictionary |
| 495 | """ |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 496 | self.logger.info("get_cluster_credentials Enter") |
| 497 | # self.logger.debug(f"Content: {db_cluster}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 498 | |
| 499 | secret_name = f"kubeconfig-{db_cluster['git_name'].lower()}" |
| 500 | secret_namespace = "managed-resources" |
| 501 | secret_key = "kubeconfig" |
| 502 | |
| 503 | self.logger.info(f"Checking content of secret {secret_name} ...") |
| 504 | try: |
| 505 | returned_secret_data = await self._kubectl.get_secret_content( |
| 506 | name=secret_name, |
| 507 | namespace=secret_namespace, |
| 508 | ) |
| 509 | returned_secret_value = base64.b64decode( |
| 510 | returned_secret_data[secret_key] |
| 511 | ).decode("utf-8") |
| 512 | return True, yaml.safe_load(returned_secret_value) |
| 513 | except Exception as e: |
| 514 | message = f"Not possible to get the credentials of the cluster. Exception: {e}" |
| 515 | self.logger.critical(message) |
| 516 | return False, message |
| 517 | |
| 518 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 519 | async def clean_items_cluster_create(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 520 | self.logger.info( |
| 521 | f"clean_items_cluster_create Enter. Operation {op_id}. Params: {op_params}" |
| 522 | ) |
| 523 | self.logger.debug(f"Content: {content}") |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 524 | items = { |
| 525 | "secrets": [ |
| 526 | { |
| 527 | "name": f"secret-age-{content['cluster']['git_name'].lower()}", |
| 528 | "namespace": "osm-workflows", |
| 529 | } |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 530 | ], |
| yshah | 4c0d1bc | 2025-09-23 09:53:26 +0000 | [diff] [blame] | 531 | # "configmaps": [ |
| 532 | # { |
| 533 | # "name": f"{content['cluster']['name']}-parameters", |
| 534 | # "namespace": "managed-resources", |
| 535 | # } |
| 536 | # ], |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 537 | } |
| 538 | try: |
| 539 | await self.clean_items(items) |
| 540 | return True, "OK" |
| 541 | except Exception as e: |
| 542 | return False, f"Error while cleaning items: {e}" |
| 543 | |
| 544 | |
| 545 | async def clean_items_cluster_update(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 546 | self.logger.info( |
| 547 | f"clean_items_cluster_update Enter. Operation {op_id}. Params: {op_params}" |
| 548 | ) |
| 549 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 550 | return await self.clean_items_cluster_create(op_id, op_params, content) |
| 551 | |
| 552 | |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 553 | async def clean_items_cluster_register(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 554 | self.logger.info( |
| 555 | f"clean_items_cluster_register Enter. Operation {op_id}. Params: {op_params}" |
| 556 | ) |
| 557 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 558 | # Clean secrets |
| 559 | cluster_name = content["cluster"]["git_name"].lower() |
| 560 | items = { |
| 561 | "secrets": [ |
| 562 | { |
| 563 | "name": f"secret-age-{cluster_name}", |
| 564 | "namespace": "osm-workflows", |
| 565 | }, |
| 566 | ] |
| 567 | } |
| 568 | |
| 569 | try: |
| 570 | await self.clean_items(items) |
| garciadeblas | 28d6e69 | 2024-10-15 13:14:39 +0200 | [diff] [blame] | 571 | return True, "OK" |
| garciadeblas | dde3a31 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 572 | except Exception as e: |
| 573 | return False, f"Error while cleaning items: {e}" |
| garciadeblas | 91bb2c4 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 574 | |
| 575 | |
| 576 | async def clean_items_cluster_deregister(self, op_id, op_params, content): |
| 577 | self.logger.info( |
| 578 | f"clean_items_cluster_deregister Enter. Operation {op_id}. Params: {op_params}" |
| 579 | ) |
| 580 | # self.logger.debug(f"Content: {content}") |
| 581 | # Clean secrets |
| 582 | self.logger.info("Cleaning kubeconfig") |
| 583 | cluster_name = content["cluster"]["git_name"].lower() |
| 584 | items = { |
| 585 | "secrets": [ |
| 586 | { |
| 587 | "name": f"kubeconfig-{cluster_name}", |
| 588 | "namespace": "managed-resources", |
| 589 | }, |
| 590 | ] |
| 591 | } |
| 592 | |
| 593 | try: |
| 594 | await self.clean_items(items) |
| 595 | return True, "OK" |
| 596 | except Exception as e: |
| 597 | return False, f"Error while cleaning items: {e}" |