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