From 1db89e8567575739cd5de523bb05c50777e1aca3 Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Thu, 8 Feb 2024 13:05:27 +0100 Subject: [PATCH] Feature 11014: add option to reset values for upgrade operation on helm charts Change-Id: I61ca767d458985ecaa429eb3d0549634cf882da8 Signed-off-by: garciadeblas --- n2vc/k8s_conn.py | 6 ++++++ n2vc/k8s_helm3_conn.py | 17 ++++++++++++++++- n2vc/k8s_helm_base_conn.py | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/n2vc/k8s_conn.py b/n2vc/k8s_conn.py index 3a1a5ef..8fd8a7d 100644 --- a/n2vc/k8s_conn.py +++ b/n2vc/k8s_conn.py @@ -194,6 +194,9 @@ class K8sConnector(abc.ABC, Loggable): timeout: float = 300, params: dict = None, db_dict: dict = None, + reset_values: bool = False, + reuse_values: bool = True, + reset_then_reuse_values: bool = False, force: bool = False, ): """ @@ -214,6 +217,9 @@ class K8sConnector(abc.ABC, Loggable): path: }, e.g. {collection: "nsrs", filter: {_id: , path: "_admin.deployed.K8S.3"} + :param reset_values: force reseting values + :param reuse_values: force reusing values (default) + :param reset_then_reuse_values: forces reseting values, then apply the last release's values :param force: force recreation of resources if necessary :return: reference to the new revision number of the KDU instance """ diff --git a/n2vc/k8s_helm3_conn.py b/n2vc/k8s_helm3_conn.py index 14f7fe0..b267c75 100644 --- a/n2vc/k8s_helm3_conn.py +++ b/n2vc/k8s_helm3_conn.py @@ -549,6 +549,9 @@ class K8sHelm3Connector(K8sHelmBaseConnector): atomic: bool, timeout: float, kubeconfig: str, + reset_values: bool = False, + reuse_values: bool = True, + reset_then_reuse_values: bool = False, force: bool = False, ) -> str: """Generates the command to upgrade a Helm Chart release @@ -563,6 +566,9 @@ class K8sHelm3Connector(K8sHelmBaseConnector): The --wait flag will be set automatically if --atomic is used timeout (float): The time, in seconds, to wait kubeconfig (str): Kubeconfig file path + reset_values(bool): If set, helm resets values instead of reusing previous values. + reuse_values(bool): If set, helm reuses previous values. + reset_then_reuse_values(bool): If set, helm resets values, then apply the last release's values force (bool): If set, helm forces resource updates through a replacement strategy. This may recreate pods. Returns: str: command to upgrade a Helm Chart release @@ -592,9 +598,17 @@ class K8sHelm3Connector(K8sHelmBaseConnector): if namespace: namespace_str = "--namespace {}".format(quote(namespace)) + # reset, reuse or reset_then_reuse values + on_values_str = "--reuse-values" + if reset_values: + on_values_str = "--reset-values" + elif reuse_values: + on_values_str = "--reuse-values" + elif reset_then_reuse_values: + on_values_str = "--reset-then-reuse-values" command = ( "env KUBECONFIG={kubeconfig} {helm} upgrade {name} {model} {namespace} {atomic} {force}" - "--output yaml {params} {timeout} --reuse-values {ver}" + "--output yaml {params} {timeout} {on_values} {ver}" ).format( kubeconfig=kubeconfig, helm=self._helm_command, @@ -605,6 +619,7 @@ class K8sHelm3Connector(K8sHelmBaseConnector): params=params_str, timeout=timeout_str, model=quote(kdu_model), + on_values=on_values_str, ver=version_str, ) return command diff --git a/n2vc/k8s_helm_base_conn.py b/n2vc/k8s_helm_base_conn.py index 5f004b3..abf2d7e 100644 --- a/n2vc/k8s_helm_base_conn.py +++ b/n2vc/k8s_helm_base_conn.py @@ -499,6 +499,9 @@ class K8sHelmBaseConnector(K8sConnector): params: dict = None, db_dict: dict = None, namespace: str = None, + reset_values: bool = False, + reuse_values: bool = True, + reset_then_reuse_values: bool = False, force: bool = False, ): self.log.debug("upgrading {} in cluster {}".format(kdu_model, cluster_uuid)) @@ -539,6 +542,9 @@ class K8sHelmBaseConnector(K8sConnector): atomic, timeout, paths["kube_config"], + reset_values, + reuse_values, + reset_then_reuse_values, force, ) @@ -1364,6 +1370,9 @@ class K8sHelmBaseConnector(K8sConnector): atomic, timeout, kubeconfig, + reset_values, + reuse_values, + reset_then_reuse_values, force, ) -> str: """Generates the command to upgrade a Helm Chart release @@ -1378,6 +1387,9 @@ class K8sHelmBaseConnector(K8sConnector): The --wait flag will be set automatically if --atomic is used timeout (float): The time, in seconds, to wait kubeconfig (str): Kubeconfig file path + reset_values(bool): If set, helm resets values instead of reusing previous values. + reuse_values(bool): If set, helm reuses previous values. + reset_then_reuse_values(bool): If set, helm resets values, then apply the last release's values force (bool): If set, helm forces resource updates through a replacement strategy. This may recreate pods. Returns: str: command to upgrade a Helm Chart release -- 2.25.1