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