| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 1 | ## |
| 2 | # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. |
| 3 | # This file is part of OSM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact with: nfvlabs@tid.es |
| 21 | ## |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 22 | import asyncio |
| Pedro Escaleira | a8980cc | 2022-04-05 17:32:13 +0100 | [diff] [blame] | 23 | from typing import Union |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 24 | from shlex import quote |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 25 | import os |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 26 | import yaml |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 27 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 28 | from n2vc.k8s_helm_base_conn import K8sHelmBaseConnector |
| 29 | from n2vc.exceptions import K8sException |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 30 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 31 | |
| 32 | class K8sHelmConnector(K8sHelmBaseConnector): |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 33 | |
| 34 | """ |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 35 | #################################################################################### |
| 36 | ################################### P U B L I C #################################### |
| 37 | #################################################################################### |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 38 | """ |
| 39 | |
| 40 | def __init__( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 41 | self, |
| 42 | fs: object, |
| 43 | db: object, |
| 44 | kubectl_command: str = "/usr/bin/kubectl", |
| 45 | helm_command: str = "/usr/bin/helm", |
| 46 | log: object = None, |
| 47 | on_update_db=None, |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 48 | ): |
| 49 | """ |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 50 | Initializes helm connector for helm v2 |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 51 | |
| 52 | :param fs: file system for kubernetes and helm configuration |
| 53 | :param db: database object to write current operation status |
| 54 | :param kubectl_command: path to kubectl executable |
| 55 | :param helm_command: path to helm executable |
| 56 | :param log: logger |
| 57 | :param on_update_db: callback called when k8s connector updates database |
| 58 | """ |
| 59 | |
| 60 | # parent class |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 61 | K8sHelmBaseConnector.__init__( |
| 62 | self, |
| 63 | db=db, |
| 64 | log=log, |
| 65 | fs=fs, |
| 66 | kubectl_command=kubectl_command, |
| 67 | helm_command=helm_command, |
| 68 | on_update_db=on_update_db, |
| 69 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 70 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 71 | self.log.info("Initializing K8S Helm2 connector") |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 72 | |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 73 | # initialize helm client-only |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 74 | self.log.debug("Initializing helm client-only...") |
| David Garcia | 4395cfa | 2021-05-28 16:21:51 +0200 | [diff] [blame] | 75 | command = "{} init --client-only {} ".format( |
| 76 | self._helm_command, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 77 | "--stable-repo-url {}".format(quote(self._stable_repo_url)) |
| David Garcia | 4395cfa | 2021-05-28 16:21:51 +0200 | [diff] [blame] | 78 | if self._stable_repo_url |
| 79 | else "--skip-repos", |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 80 | ) |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 81 | try: |
| Mark Beierl | 714d887 | 2023-05-18 15:08:06 -0400 | [diff] [blame] | 82 | asyncio.create_task( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 83 | self._local_async_exec(command=command, raise_exception_on_error=False) |
| 84 | ) |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 85 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 86 | self.warning( |
| 87 | msg="helm init failed (it was already initialized): {}".format(e) |
| 88 | ) |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 89 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 90 | self.log.info("K8S Helm2 connector initialized") |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 91 | |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 92 | async def install( |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 93 | self, |
| 94 | cluster_uuid: str, |
| 95 | kdu_model: str, |
| 96 | kdu_instance: str, |
| 97 | atomic: bool = True, |
| 98 | timeout: float = 300, |
| 99 | params: dict = None, |
| 100 | db_dict: dict = None, |
| 101 | kdu_name: str = None, |
| 102 | namespace: str = None, |
| 103 | **kwargs, |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 104 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 105 | """ |
| 106 | Deploys of a new KDU instance. It would implicitly rely on the `install` call |
| 107 | to deploy the Chart/Bundle properly parametrized (in practice, this call would |
| 108 | happen before any _initial-config-primitive_of the VNF is called). |
| 109 | |
| 110 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| garciadeblas | 0439319 | 2022-06-08 15:39:24 +0200 | [diff] [blame] | 111 | :param kdu_model: chart/reference (string), which can be either |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 112 | of these options: |
| 113 | - a name of chart available via the repos known by OSM |
| garciadeblas | 0439319 | 2022-06-08 15:39:24 +0200 | [diff] [blame] | 114 | (e.g. stable/openldap, stable/openldap:1.2.4) |
| 115 | - a path to a packaged chart (e.g. mychart.tgz) |
| 116 | - a path to an unpacked chart directory or a URL (e.g. mychart) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 117 | :param kdu_instance: Kdu instance name |
| 118 | :param atomic: If set, installation process purges chart/bundle on fail, also |
| 119 | will wait until all the K8s objects are active |
| 120 | :param timeout: Time in seconds to wait for the install of the chart/bundle |
| 121 | (defaults to Helm default timeout: 300s) |
| 122 | :param params: dictionary of key-value pairs for instantiation parameters |
| 123 | (overriding default values) |
| 124 | :param dict db_dict: where to write into database when the status changes. |
| 125 | It contains a dict with {collection: <str>, filter: {}, |
| 126 | path: <str>}, |
| 127 | e.g. {collection: "nsrs", filter: |
| 128 | {_id: <nsd-id>, path: "_admin.deployed.K8S.3"} |
| 129 | :param kdu_name: Name of the KDU instance to be installed |
| 130 | :param namespace: K8s namespace to use for the KDU instance |
| 131 | :param kwargs: Additional parameters (None yet) |
| 132 | :return: True if successful |
| 133 | """ |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 134 | self.log.debug("installing {} in cluster {}".format(kdu_model, cluster_uuid)) |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 135 | |
| 136 | # sync local dir |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 137 | self.fs.sync(from_path=cluster_uuid) |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 138 | |
| 139 | # init env, paths |
| 140 | paths, env = self._init_paths_env( |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 141 | cluster_name=cluster_uuid, create_if_not_exist=True |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 142 | ) |
| 143 | |
| David Garcia | c4da25c | 2021-02-23 11:47:29 +0100 | [diff] [blame] | 144 | await self._install_impl( |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 145 | cluster_uuid, |
| David Garcia | c4da25c | 2021-02-23 11:47:29 +0100 | [diff] [blame] | 146 | kdu_model, |
| 147 | paths, |
| 148 | env, |
| 149 | kdu_instance, |
| 150 | atomic=atomic, |
| 151 | timeout=timeout, |
| 152 | params=params, |
| 153 | db_dict=db_dict, |
| 154 | kdu_name=kdu_name, |
| 155 | namespace=namespace, |
| 156 | ) |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 157 | |
| 158 | # sync fs |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 159 | self.fs.reverse_sync(from_path=cluster_uuid) |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 160 | |
| 161 | self.log.debug("Returning kdu_instance {}".format(kdu_instance)) |
| David Garcia | c4da25c | 2021-02-23 11:47:29 +0100 | [diff] [blame] | 162 | return True |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 163 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 164 | async def inspect_kdu(self, kdu_model: str, repo_url: str = None) -> str: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 165 | self.log.debug( |
| 166 | "inspect kdu_model {} from (optional) repo: {}".format(kdu_model, repo_url) |
| 167 | ) |
| 168 | |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 169 | return await self._exec_inspect_command( |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 170 | inspect_command="", kdu_model=kdu_model, repo_url=repo_url |
| 171 | ) |
| 172 | |
| 173 | """ |
| 174 | #################################################################################### |
| 175 | ################################### P R I V A T E ################################## |
| 176 | #################################################################################### |
| 177 | """ |
| 178 | |
| 179 | def _init_paths_env(self, cluster_name: str, create_if_not_exist: bool = True): |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 180 | """ |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 181 | Creates and returns base cluster and kube dirs and returns them. |
| 182 | Also created helm3 dirs according to new directory specification, paths are |
| 183 | returned and also environment variables that must be provided to execute commands |
| 184 | |
| 185 | Helm 2 directory specification uses helm_home dir: |
| 186 | |
| 187 | The variables assigned for this paths are: |
| 188 | - Helm hone: $HELM_HOME |
| 189 | - helm kubeconfig: $KUBECONFIG |
| 190 | |
| 191 | :param cluster_name: cluster_name |
| 192 | :return: Dictionary with config_paths and dictionary with helm environment variables |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 193 | """ |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 194 | base = self.fs.path |
| 195 | if base.endswith("/") or base.endswith("\\"): |
| 196 | base = base[:-1] |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 197 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 198 | # base dir for cluster |
| 199 | cluster_dir = base + "/" + cluster_name |
| garciadeblas | 54771fa | 2019-12-13 13:39:03 +0100 | [diff] [blame] | 200 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 201 | # kube dir |
| 202 | kube_dir = cluster_dir + "/" + ".kube" |
| 203 | if create_if_not_exist and not os.path.exists(kube_dir): |
| 204 | self.log.debug("Creating dir {}".format(kube_dir)) |
| 205 | os.makedirs(kube_dir) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 206 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 207 | # helm home dir |
| 208 | helm_dir = cluster_dir + "/" + ".helm" |
| 209 | if create_if_not_exist and not os.path.exists(helm_dir): |
| 210 | self.log.debug("Creating dir {}".format(helm_dir)) |
| 211 | os.makedirs(helm_dir) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 212 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 213 | config_filename = kube_dir + "/config" |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 214 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 215 | # 2 - Prepare dictionary with paths |
| 216 | paths = { |
| 217 | "kube_dir": kube_dir, |
| 218 | "kube_config": config_filename, |
| 219 | "cluster_dir": cluster_dir, |
| 220 | "helm_dir": helm_dir, |
| 221 | } |
| 222 | |
| 223 | for file_name, file in paths.items(): |
| 224 | if "dir" in file_name and not os.path.exists(file): |
| 225 | err_msg = "{} dir does not exist".format(file) |
| 226 | self.log.error(err_msg) |
| 227 | raise K8sException(err_msg) |
| 228 | |
| 229 | # 3 - Prepare environment variables |
| 230 | env = {"HELM_HOME": helm_dir, "KUBECONFIG": config_filename} |
| 231 | |
| 232 | return paths, env |
| 233 | |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 234 | async def _get_services(self, cluster_id, kdu_instance, namespace, kubeconfig): |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 235 | # init config, env |
| 236 | paths, env = self._init_paths_env( |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 237 | cluster_name=cluster_id, create_if_not_exist=True |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 238 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 239 | |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 240 | command1 = "env KUBECONFIG={} {} get manifest {} ".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 241 | kubeconfig, self._helm_command, quote(kdu_instance) |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 242 | ) |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 243 | command2 = "{} get --namespace={} -f -".format( |
| 244 | self.kubectl_command, quote(namespace) |
| 245 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 246 | output, _rc = await self._local_async_exec_pipe( |
| 247 | command1, command2, env=env, raise_exception_on_error=True |
| 248 | ) |
| 249 | services = self._parse_services(output) |
| 250 | |
| 251 | return services |
| 252 | |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 253 | async def _cluster_init( |
| 254 | self, cluster_id: str, namespace: str, paths: dict, env: dict |
| 255 | ): |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 256 | """ |
| 257 | Implements the helm version dependent cluster initialization: |
| 258 | For helm2 it initialized tiller environment if needed |
| 259 | """ |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 260 | |
| 261 | # check if tiller pod is up in cluster |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 262 | command = "{} --kubeconfig={} --namespace={} get deployments".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 263 | self.kubectl_command, paths["kube_config"], quote(namespace) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 264 | ) |
| 265 | output, _rc = await self._local_async_exec( |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 266 | command=command, raise_exception_on_error=True, env=env |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 267 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 268 | |
| tierno | 119f723 | 2020-04-21 13:22:26 +0000 | [diff] [blame] | 269 | output_table = self._output_to_table(output=output) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 270 | |
| 271 | # find 'tiller' pod in all pods |
| 272 | already_initialized = False |
| 273 | try: |
| 274 | for row in output_table: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 275 | if row[0].startswith("tiller-deploy"): |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 276 | already_initialized = True |
| 277 | break |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 278 | except Exception: |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 279 | pass |
| 280 | |
| 281 | # helm init |
| 282 | n2vc_installed_sw = False |
| 283 | if not already_initialized: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 284 | self.log.info( |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 285 | "Initializing helm in client and server: {}".format(cluster_id) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 286 | ) |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 287 | command = "{} --kubeconfig={} --namespace kube-system create serviceaccount {}".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 288 | self.kubectl_command, paths["kube_config"], quote(self.service_account) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 289 | ) |
| 290 | _, _rc = await self._local_async_exec( |
| 291 | command=command, raise_exception_on_error=False, env=env |
| 292 | ) |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 293 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 294 | command = ( |
| 295 | "{} --kubeconfig={} create clusterrolebinding osm-tiller-cluster-rule " |
| 296 | "--clusterrole=cluster-admin --serviceaccount=kube-system:{}" |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 297 | ).format( |
| 298 | self.kubectl_command, paths["kube_config"], quote(self.service_account) |
| 299 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 300 | _, _rc = await self._local_async_exec( |
| 301 | command=command, raise_exception_on_error=False, env=env |
| 302 | ) |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 303 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 304 | command = ( |
| Gabriel Cuba | 05ad832 | 2022-07-05 15:07:33 -0500 | [diff] [blame] | 305 | "{} init --kubeconfig={} --tiller-namespace={} --home={} --service-account {} " |
| 306 | " {}" |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 307 | ).format( |
| 308 | self._helm_command, |
| 309 | paths["kube_config"], |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 310 | quote(namespace), |
| 311 | quote(paths["helm_dir"]), |
| 312 | quote(self.service_account), |
| 313 | "--stable-repo-url {}".format(quote(self._stable_repo_url)) |
| David Garcia | 4395cfa | 2021-05-28 16:21:51 +0200 | [diff] [blame] | 314 | if self._stable_repo_url |
| 315 | else "--skip-repos", |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 316 | ) |
| 317 | _, _rc = await self._local_async_exec( |
| 318 | command=command, raise_exception_on_error=True, env=env |
| 319 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 320 | n2vc_installed_sw = True |
| 321 | else: |
| 322 | # check client helm installation |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 323 | check_file = paths["helm_dir"] + "/repository/repositories.yaml" |
| 324 | if not self._check_file_exists( |
| 325 | filename=check_file, exception_if_not_exists=False |
| 326 | ): |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 327 | self.log.info("Initializing helm in client: {}".format(cluster_id)) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 328 | command = ( |
| Gabriel Cuba | 05ad832 | 2022-07-05 15:07:33 -0500 | [diff] [blame] | 329 | "{} init --kubeconfig={} --tiller-namespace={} " |
| 330 | "--home={} --client-only {} " |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 331 | ).format( |
| 332 | self._helm_command, |
| 333 | paths["kube_config"], |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 334 | quote(namespace), |
| 335 | quote(paths["helm_dir"]), |
| 336 | "--stable-repo-url {}".format(quote(self._stable_repo_url)) |
| David Garcia | 4395cfa | 2021-05-28 16:21:51 +0200 | [diff] [blame] | 337 | if self._stable_repo_url |
| 338 | else "--skip-repos", |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 339 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 340 | output, _rc = await self._local_async_exec( |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 341 | command=command, raise_exception_on_error=True, env=env |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 342 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 343 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 344 | self.log.info("Helm client already initialized") |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 345 | |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 346 | repo_list = await self.repo_list(cluster_id) |
| lloretgalleg | 83e5589 | 2020-12-17 12:42:11 +0000 | [diff] [blame] | 347 | for repo in repo_list: |
| 348 | if repo["name"] == "stable" and repo["url"] != self._stable_repo_url: |
| 349 | self.log.debug("Add new stable repo url: {}") |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 350 | await self.repo_remove(cluster_id, "stable") |
| David Garcia | 4395cfa | 2021-05-28 16:21:51 +0200 | [diff] [blame] | 351 | if self._stable_repo_url: |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 352 | await self.repo_add(cluster_id, "stable", self._stable_repo_url) |
| lloretgalleg | 83e5589 | 2020-12-17 12:42:11 +0000 | [diff] [blame] | 353 | break |
| 354 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 355 | return n2vc_installed_sw |
| lloretgalleg | e308c71 | 2020-09-02 09:40:38 +0000 | [diff] [blame] | 356 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 357 | async def _uninstall_sw(self, cluster_id: str, namespace: str): |
| 358 | # uninstall Tiller if necessary |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 359 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 360 | self.log.debug("Uninstalling tiller from cluster {}".format(cluster_id)) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 361 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 362 | # init paths, env |
| 363 | paths, env = self._init_paths_env( |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 364 | cluster_name=cluster_id, create_if_not_exist=True |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 365 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 366 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 367 | if not namespace: |
| 368 | # find namespace for tiller pod |
| 369 | command = "{} --kubeconfig={} get deployments --all-namespaces".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 370 | self.kubectl_command, quote(paths["kube_config"]) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 371 | ) |
| 372 | output, _rc = await self._local_async_exec( |
| 373 | command=command, raise_exception_on_error=False, env=env |
| 374 | ) |
| 375 | output_table = self._output_to_table(output=output) |
| 376 | namespace = None |
| 377 | for r in output_table: |
| 378 | try: |
| 379 | if "tiller-deploy" in r[1]: |
| 380 | namespace = r[0] |
| 381 | break |
| 382 | except Exception: |
| 383 | pass |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 384 | else: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 385 | msg = "Tiller deployment not found in cluster {}".format(cluster_id) |
| 386 | self.log.error(msg) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 387 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 388 | self.log.debug("namespace for tiller: {}".format(namespace)) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 389 | |
| tierno | 53555f6 | 2020-04-07 11:08:16 +0000 | [diff] [blame] | 390 | if namespace: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 391 | # uninstall tiller from cluster |
| 392 | self.log.debug("Uninstalling tiller from cluster {}".format(cluster_id)) |
| 393 | command = "{} --kubeconfig={} --home={} reset".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 394 | self._helm_command, |
| 395 | quote(paths["kube_config"]), |
| 396 | quote(paths["helm_dir"]), |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 397 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 398 | self.log.debug("resetting: {}".format(command)) |
| 399 | output, _rc = await self._local_async_exec( |
| 400 | command=command, raise_exception_on_error=True, env=env |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 401 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 402 | # Delete clusterrolebinding and serviceaccount. |
| 403 | # Ignore if errors for backward compatibility |
| 404 | command = ( |
| 405 | "{} --kubeconfig={} delete clusterrolebinding.rbac.authorization.k8s." |
| 406 | "io/osm-tiller-cluster-rule" |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 407 | ).format(self.kubectl_command, quote(paths["kube_config"])) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 408 | output, _rc = await self._local_async_exec( |
| 409 | command=command, raise_exception_on_error=False, env=env |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 410 | ) |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 411 | command = ( |
| 412 | "{} --kubeconfig={} --namespace {} delete serviceaccount/{}".format( |
| 413 | self.kubectl_command, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 414 | quote(paths["kube_config"]), |
| 415 | quote(namespace), |
| 416 | quote(self.service_account), |
| Pedro Escaleira | b41de17 | 2022-04-02 00:44:08 +0100 | [diff] [blame] | 417 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 418 | ) |
| 419 | output, _rc = await self._local_async_exec( |
| 420 | command=command, raise_exception_on_error=False, env=env |
| 421 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 422 | |
| 423 | else: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 424 | self.log.debug("namespace not found") |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 425 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 426 | async def _instances_list(self, cluster_id): |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 427 | # init paths, env |
| 428 | paths, env = self._init_paths_env( |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 429 | cluster_name=cluster_id, create_if_not_exist=True |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 430 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 431 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 432 | command = "{} list --output yaml".format(self._helm_command) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 433 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 434 | output, _rc = await self._local_async_exec( |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 435 | command=command, raise_exception_on_error=True, env=env |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 436 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 437 | |
| 438 | if output and len(output) > 0: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 439 | # parse yaml and update keys to lower case to unify with helm3 |
| 440 | instances = yaml.load(output, Loader=yaml.SafeLoader).get("Releases") |
| 441 | new_instances = [] |
| 442 | for instance in instances: |
| 443 | new_instance = dict((k.lower(), v) for k, v in instance.items()) |
| 444 | new_instances.append(new_instance) |
| 445 | return new_instances |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 446 | else: |
| 447 | return [] |
| 448 | |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 449 | def _get_inspect_command( |
| 450 | self, show_command: str, kdu_model: str, repo_str: str, version: str |
| 451 | ): |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 452 | inspect_command = "{} inspect {} {}{} {}".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 453 | self._helm_command, show_command, quote(kdu_model), repo_str, version |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 454 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 455 | return inspect_command |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 456 | |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 457 | def _get_get_command( |
| 458 | self, get_command: str, kdu_instance: str, namespace: str, kubeconfig: str |
| 459 | ): |
| 460 | get_command = "env KUBECONFIG={} {} get {} {} --output yaml".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 461 | kubeconfig, self._helm_command, get_command, quote(kdu_instance) |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 462 | ) |
| 463 | return get_command |
| 464 | |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 465 | async def _status_kdu( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 466 | self, |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 467 | cluster_id: str, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 468 | kdu_instance: str, |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 469 | namespace: str = None, |
| Pedro Escaleira | a8980cc | 2022-04-05 17:32:13 +0100 | [diff] [blame] | 470 | yaml_format: bool = False, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 471 | show_error_log: bool = False, |
| Pedro Escaleira | a8980cc | 2022-04-05 17:32:13 +0100 | [diff] [blame] | 472 | ) -> Union[str, dict]: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 473 | self.log.debug( |
| 474 | "status of kdu_instance: {}, namespace: {} ".format(kdu_instance, namespace) |
| 475 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 476 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 477 | # init config, env |
| 478 | paths, env = self._init_paths_env( |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 479 | cluster_name=cluster_id, create_if_not_exist=True |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 480 | ) |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 481 | command = ("env KUBECONFIG={} {} status {} --output yaml").format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 482 | paths["kube_config"], self._helm_command, quote(kdu_instance) |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 483 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 484 | output, rc = await self._local_async_exec( |
| 485 | command=command, |
| 486 | raise_exception_on_error=True, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 487 | show_error_log=show_error_log, |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 488 | env=env, |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 489 | ) |
| 490 | |
| Pedro Escaleira | a8980cc | 2022-04-05 17:32:13 +0100 | [diff] [blame] | 491 | if yaml_format: |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 492 | return str(output) |
| 493 | |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 494 | if rc != 0: |
| 495 | return None |
| 496 | |
| 497 | data = yaml.load(output, Loader=yaml.SafeLoader) |
| 498 | |
| 499 | # remove field 'notes' |
| 500 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 501 | del data.get("info").get("status")["notes"] |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 502 | except KeyError: |
| 503 | pass |
| 504 | |
| Pedro Escaleira | ed0ff05 | 2022-04-03 13:51:46 +0100 | [diff] [blame] | 505 | # parse the manifest to a list of dictionaries |
| 506 | if "manifest" in data: |
| 507 | manifest_str = data.get("manifest") |
| 508 | manifest_docs = yaml.load_all(manifest_str, Loader=yaml.SafeLoader) |
| 509 | |
| 510 | data["manifest"] = [] |
| 511 | for doc in manifest_docs: |
| 512 | data["manifest"].append(doc) |
| 513 | |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 514 | # parse field 'resources' |
| 515 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 516 | resources = str(data.get("info").get("status").get("resources")) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 517 | resource_table = self._output_to_table(resources) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 518 | data.get("info").get("status")["resources"] = resource_table |
| 519 | except Exception: |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 520 | pass |
| 521 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 522 | # set description to lowercase (unify with helm3) |
| 523 | try: |
| 524 | data.get("info")["description"] = data.get("info").pop("Description") |
| 525 | except KeyError: |
| 526 | pass |
| 527 | |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 528 | return data |
| 529 | |
| lloretgalleg | 095392b | 2020-11-20 11:28:08 +0000 | [diff] [blame] | 530 | def _get_helm_chart_repos_ids(self, cluster_uuid) -> list: |
| 531 | repo_ids = [] |
| 532 | cluster_filter = {"_admin.helm-chart.id": cluster_uuid} |
| 533 | cluster = self.db.get_one("k8sclusters", cluster_filter) |
| 534 | if cluster: |
| 535 | repo_ids = cluster.get("_admin").get("helm_chart_repos") or [] |
| 536 | return repo_ids |
| 537 | else: |
| 538 | raise K8sException( |
| 539 | "k8cluster with helm-id : {} not found".format(cluster_uuid) |
| 540 | ) |
| 541 | |
| tierno | a5728bf | 2020-06-25 15:48:52 +0000 | [diff] [blame] | 542 | async def _is_install_completed(self, cluster_id: str, kdu_instance: str) -> bool: |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 543 | # init config, env |
| 544 | paths, env = self._init_paths_env( |
| 545 | cluster_name=cluster_id, create_if_not_exist=True |
| 546 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 547 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 548 | status = await self._status_kdu( |
| Pedro Escaleira | a8980cc | 2022-04-05 17:32:13 +0100 | [diff] [blame] | 549 | cluster_id=cluster_id, kdu_instance=kdu_instance, yaml_format=False |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 550 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 551 | |
| 552 | # extract info.status.resources-> str |
| 553 | # format: |
| 554 | # ==> v1/Deployment |
| 555 | # NAME READY UP-TO-DATE AVAILABLE AGE |
| 556 | # halting-horse-mongodb 0/1 1 0 0s |
| 557 | # halting-petit-mongodb 1/1 1 0 0s |
| 558 | # blank line |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 559 | resources = K8sHelmBaseConnector._get_deep( |
| 560 | status, ("info", "status", "resources") |
| 561 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 562 | |
| 563 | # convert to table |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 564 | resources = K8sHelmBaseConnector._output_to_table(resources) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 565 | |
| 566 | num_lines = len(resources) |
| 567 | index = 0 |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 568 | ready = True |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 569 | while index < num_lines: |
| 570 | try: |
| 571 | line1 = resources[index] |
| 572 | index += 1 |
| 573 | # find '==>' in column 0 |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 574 | if line1[0] == "==>": |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 575 | line2 = resources[index] |
| 576 | index += 1 |
| 577 | # find READY in column 1 |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 578 | if line2[1] == "READY": |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 579 | # read next lines |
| 580 | line3 = resources[index] |
| 581 | index += 1 |
| 582 | while len(line3) > 1 and index < num_lines: |
| 583 | ready_value = line3[1] |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 584 | parts = ready_value.split(sep="/") |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 585 | current = int(parts[0]) |
| 586 | total = int(parts[1]) |
| 587 | if current < total: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 588 | self.log.debug("NOT READY:\n {}".format(line3)) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 589 | ready = False |
| 590 | line3 = resources[index] |
| 591 | index += 1 |
| 592 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 593 | except Exception: |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 594 | pass |
| 595 | |
| 596 | return ready |
| 597 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 598 | def _get_install_command( |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 599 | self, |
| 600 | kdu_model, |
| 601 | kdu_instance, |
| 602 | namespace, |
| 603 | params_str, |
| 604 | version, |
| 605 | atomic, |
| 606 | timeout, |
| 607 | kubeconfig, |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 608 | ) -> str: |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 609 | timeout_str = "" |
| 610 | if timeout: |
| 611 | timeout_str = "--timeout {}".format(timeout) |
| lloretgalleg | d99f3f2 | 2020-06-29 14:18:30 +0000 | [diff] [blame] | 612 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 613 | # atomic |
| 614 | atomic_str = "" |
| 615 | if atomic: |
| 616 | atomic_str = "--atomic" |
| 617 | # namespace |
| 618 | namespace_str = "" |
| 619 | if namespace: |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 620 | namespace_str = "--namespace {}".format(quote(namespace)) |
| lloretgalleg | d99f3f2 | 2020-06-29 14:18:30 +0000 | [diff] [blame] | 621 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 622 | # version |
| 623 | version_str = "" |
| 624 | if version: |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 625 | version_str = "--version {}".format(version) |
| lloretgalleg | d99f3f2 | 2020-06-29 14:18:30 +0000 | [diff] [blame] | 626 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 627 | command = ( |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 628 | "env KUBECONFIG={kubeconfig} {helm} install {atomic} --output yaml " |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 629 | "{params} {timeout} --name={name} {ns} {model} {ver}".format( |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 630 | kubeconfig=kubeconfig, |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 631 | helm=self._helm_command, |
| 632 | atomic=atomic_str, |
| 633 | params=params_str, |
| 634 | timeout=timeout_str, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 635 | name=quote(kdu_instance), |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 636 | ns=namespace_str, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 637 | model=quote(kdu_model), |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 638 | ver=version_str, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 639 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 640 | ) |
| 641 | return command |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 642 | |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 643 | def _get_upgrade_scale_command( |
| 644 | self, |
| 645 | kdu_model: str, |
| 646 | kdu_instance: str, |
| 647 | namespace: str, |
| 648 | scale: int, |
| 649 | version: str, |
| 650 | atomic: bool, |
| 651 | replica_str: str, |
| 652 | timeout: float, |
| 653 | resource_name: str, |
| 654 | kubeconfig: str, |
| 655 | ) -> str: |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 656 | """Generates the command to scale a Helm Chart release |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 657 | |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 658 | Args: |
| 659 | kdu_model (str): Kdu model name, corresponding to the Helm local location or repository |
| 660 | kdu_instance (str): KDU instance, corresponding to the Helm Chart release in question |
| 661 | namespace (str): Namespace where this KDU instance is deployed |
| 662 | scale (int): Scale count |
| 663 | version (str): Constraint with specific version of the Chart to use |
| 664 | atomic (bool): If set, upgrade process rolls back changes made in case of failed upgrade. |
| 665 | The --wait flag will be set automatically if --atomic is used |
| 666 | replica_str (str): The key under resource_name key where the scale count is stored |
| 667 | timeout (float): The time, in seconds, to wait |
| 668 | resource_name (str): The KDU's resource to scale |
| 669 | kubeconfig (str): Kubeconfig file path |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 670 | |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 671 | Returns: |
| 672 | str: command to scale a Helm Chart release |
| 673 | """ |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 674 | |
| 675 | # scale |
| 676 | if resource_name: |
| 677 | scale_dict = {"{}.{}".format(resource_name, replica_str): scale} |
| 678 | else: |
| 679 | scale_dict = {replica_str: scale} |
| 680 | |
| 681 | scale_str = self._params_to_set_option(scale_dict) |
| 682 | |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 683 | return self._get_upgrade_command( |
| 684 | kdu_model=kdu_model, |
| 685 | kdu_instance=kdu_instance, |
| 686 | namespace=namespace, |
| 687 | params_str=scale_str, |
| 688 | version=version, |
| 689 | atomic=atomic, |
| 690 | timeout=timeout, |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 691 | kubeconfig=kubeconfig, |
| 692 | ) |
| aktas | 867418c | 2021-10-19 18:26:13 +0300 | [diff] [blame] | 693 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 694 | def _get_upgrade_command( |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 695 | self, |
| 696 | kdu_model, |
| 697 | kdu_instance, |
| 698 | namespace, |
| 699 | params_str, |
| 700 | version, |
| 701 | atomic, |
| 702 | timeout, |
| 703 | kubeconfig, |
| garciadeblas | b2ac762 | 2024-02-08 13:05:27 +0100 | [diff] [blame^] | 704 | reset_values: bool = False, |
| 705 | reuse_values: bool = True, |
| 706 | reset_then_reuse_values: bool = False, |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 707 | force: bool = False, |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 708 | ) -> str: |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 709 | """Generates the command to upgrade a Helm Chart release |
| 710 | |
| 711 | Args: |
| 712 | kdu_model (str): Kdu model name, corresponding to the Helm local location or repository |
| 713 | kdu_instance (str): KDU instance, corresponding to the Helm Chart release in question |
| 714 | namespace (str): Namespace where this KDU instance is deployed |
| 715 | params_str (str): Params used to upgrade the Helm Chart release |
| 716 | version (str): Constraint with specific version of the Chart to use |
| 717 | atomic (bool): If set, upgrade process rolls back changes made in case of failed upgrade. |
| 718 | The --wait flag will be set automatically if --atomic is used |
| 719 | timeout (float): The time, in seconds, to wait |
| 720 | kubeconfig (str): Kubeconfig file path |
| garciadeblas | b2ac762 | 2024-02-08 13:05:27 +0100 | [diff] [blame^] | 721 | reset_values(bool): If set, helm resets values instead of reusing previous values. |
| 722 | reuse_values(bool): If set, helm reuses previous values. |
| 723 | reset_then_reuse_values(bool): If set, helm resets values, then apply the last release's values |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 724 | force (bool): If set, helm forces resource updates through a replacement strategy. This may recreate pods. |
| Pedro Escaleira | 0a2060c | 2022-07-07 22:18:35 +0100 | [diff] [blame] | 725 | Returns: |
| 726 | str: command to upgrade a Helm Chart release |
| 727 | """ |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 728 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 729 | timeout_str = "" |
| 730 | if timeout: |
| 731 | timeout_str = "--timeout {}".format(timeout) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 732 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 733 | # atomic |
| 734 | atomic_str = "" |
| 735 | if atomic: |
| 736 | atomic_str = "--atomic" |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 737 | |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 738 | # force |
| 739 | force_str = "" |
| 740 | if force: |
| 741 | force_str = "--force " |
| 742 | |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 743 | # version |
| 744 | version_str = "" |
| 745 | if version: |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 746 | version_str = "--version {}".format(quote(version)) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 747 | |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 748 | # namespace |
| 749 | namespace_str = "" |
| 750 | if namespace: |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 751 | namespace_str = "--namespace {}".format(quote(namespace)) |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 752 | |
| garciadeblas | b2ac762 | 2024-02-08 13:05:27 +0100 | [diff] [blame^] | 753 | # reset, reuse or reset_then_reuse values |
| 754 | on_values_str = "--reuse-values" |
| 755 | if reset_values: |
| 756 | on_values_str = "--reset-values" |
| 757 | elif reuse_values: |
| 758 | on_values_str = "--reuse-values" |
| 759 | elif reset_then_reuse_values: |
| 760 | on_values_str = "--reset-then-reuse-values" |
| 761 | |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 762 | command = ( |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 763 | "env KUBECONFIG={kubeconfig} {helm} upgrade {namespace} {atomic} --output yaml {params} {timeout} {force}" |
| garciadeblas | b2ac762 | 2024-02-08 13:05:27 +0100 | [diff] [blame^] | 764 | "{on_values} {name} {model} {ver}" |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 765 | ).format( |
| 766 | kubeconfig=kubeconfig, |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 767 | helm=self._helm_command, |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 768 | namespace=namespace_str, |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 769 | atomic=atomic_str, |
| Gabriel Cuba | 085fa8d | 2022-10-10 12:13:55 -0500 | [diff] [blame] | 770 | force=force_str, |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 771 | params=params_str, |
| 772 | timeout=timeout_str, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 773 | name=quote(kdu_instance), |
| garciadeblas | b2ac762 | 2024-02-08 13:05:27 +0100 | [diff] [blame^] | 774 | on_values=on_values_str, |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 775 | model=quote(kdu_model), |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 776 | ver=version_str, |
| 777 | ) |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 778 | return command |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 779 | |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 780 | def _get_rollback_command( |
| 781 | self, kdu_instance, namespace, revision, kubeconfig |
| 782 | ) -> str: |
| 783 | return "env KUBECONFIG={} {} rollback {} {} --wait".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 784 | kubeconfig, self._helm_command, quote(kdu_instance), revision |
| lloretgalleg | 1c83f2e | 2020-10-22 09:12:35 +0000 | [diff] [blame] | 785 | ) |
| quilesj | 26c78a4 | 2019-10-28 18:10:42 +0100 | [diff] [blame] | 786 | |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 787 | def _get_uninstall_command( |
| 788 | self, kdu_instance: str, namespace: str, kubeconfig: str |
| 789 | ) -> str: |
| 790 | return "env KUBECONFIG={} {} delete --purge {}".format( |
| Pedro Escaleira | febbaa0 | 2023-06-27 16:42:41 -0300 | [diff] [blame] | 791 | kubeconfig, self._helm_command, quote(kdu_instance) |
| bravof | 7bd5c6a | 2021-11-17 11:14:57 -0300 | [diff] [blame] | 792 | ) |