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