From 5d542b5a382558bd5f83a8b173840ea2c953aaa5 Mon Sep 17 00:00:00 2001 From: Pedro Escaleira Date: Sat, 4 Jun 2022 22:21:57 +0100 Subject: [PATCH] Bug 2107 fixed Proper verification of the replica count in get_scale_count, _get_replica_count_instance and _get_replica_count_url methods Change-Id: I0df7b164b690daf171b78dbed19629047db199fe Signed-off-by: Pedro Escaleira --- n2vc/k8s_helm_base_conn.py | 62 ++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/n2vc/k8s_helm_base_conn.py b/n2vc/k8s_helm_base_conn.py index 51eb475..d627859 100644 --- a/n2vc/k8s_helm_base_conn.py +++ b/n2vc/k8s_helm_base_conn.py @@ -742,7 +742,7 @@ class K8sHelmBaseConnector(K8sConnector): raise K8sException("kdu_instance {} not found".format(kdu_instance)) # init env, paths - paths, env = self._init_paths_env( + paths, _ = self._init_paths_env( cluster_name=cluster_uuid, create_if_not_exist=True ) @@ -753,8 +753,15 @@ class K8sHelmBaseConnector(K8sConnector): resource_name=resource_name, ) + self.log.debug( + f"Number of replicas of the KDU instance {kdu_instance} and resource {resource_name} obtained: {replicas}" + ) + # Get default value if scale count is not found from provided values - if not replicas: + # Important note: this piece of code shall only be executed in the first scaling operation, + # since it is expected that the _get_replica_count_instance is able to obtain the number of + # replicas when a scale operation was already conducted previously for this KDU/resource! + if replicas is None: repo_url = await self._find_repo( kdu_model=kdu_model, cluster_uuid=cluster_uuid ) @@ -762,10 +769,15 @@ class K8sHelmBaseConnector(K8sConnector): kdu_model=kdu_model, repo_url=repo_url, resource_name=resource_name ) - if not replicas: - msg = "Replica count not found. Cannot be scaled" - self.log.error(msg) - raise K8sException(msg) + self.log.debug( + f"Number of replicas of the Helm Chart package for KDU instance {kdu_instance} and resource " + f"{resource_name} obtained: {replicas}" + ) + + if replicas is None: + msg = "Replica count not found. Cannot be scaled" + self.log.error(msg) + raise K8sException(msg) return int(replicas) @@ -1762,7 +1774,7 @@ class K8sHelmBaseConnector(K8sConnector): kdu_model: str, repo_url: str = None, resource_name: str = None, - ): + ) -> (int, str): """Get the replica count value in the Helm Chart Values. Args: @@ -1771,7 +1783,9 @@ class K8sHelmBaseConnector(K8sConnector): resource_name: Resource name Returns: - True if replicas, False replicaCount + A tuple with: + - The number of replicas of the specific instance; if not found, returns None; and + - The string corresponding to the replica count key in the Helm values """ kdu_values = yaml.load( @@ -1779,6 +1793,8 @@ class K8sHelmBaseConnector(K8sConnector): Loader=yaml.SafeLoader, ) + self.log.debug(f"Obtained the Helm package values for the KDU: {kdu_values}") + if not kdu_values: raise K8sException( "kdu_values not found for kdu_model {}".format(kdu_model) @@ -1799,10 +1815,10 @@ class K8sHelmBaseConnector(K8sConnector): replica_str = "" replicas = None - if kdu_values.get("replicaCount", None): + if kdu_values.get("replicaCount") is not None: replicas = kdu_values["replicaCount"] replica_str = "replicaCount" - elif kdu_values.get("replicas", None): + elif kdu_values.get("replicas") is not None: duplicate_check = True replicas = kdu_values["replicas"] replica_str = "replicas" @@ -1841,7 +1857,7 @@ class K8sHelmBaseConnector(K8sConnector): namespace: str, kubeconfig: str, resource_name: str = None, - ): + ) -> int: """Get the replica count value in the instance. Args: @@ -1851,7 +1867,7 @@ class K8sHelmBaseConnector(K8sConnector): resource_name: Resource name Returns: - True if replicas, False replicaCount + The number of replicas of the specific instance; if not found, returns None """ kdu_values = yaml.load( @@ -1859,23 +1875,23 @@ class K8sHelmBaseConnector(K8sConnector): Loader=yaml.SafeLoader, ) + self.log.debug(f"Obtained the Helm values for the KDU instance: {kdu_values}") + replicas = None if kdu_values: resource_values = ( kdu_values.get(resource_name, None) if resource_name else None ) - replicas = ( - ( - resource_values.get("replicaCount", None) - or resource_values.get("replicas", None) - ) - if resource_values - else ( - kdu_values.get("replicaCount", None) - or kdu_values.get("replicas", None) - ) - ) + + for replica_str in ("replicaCount", "replicas"): + if resource_values: + replicas = resource_values.get(replica_str) + else: + replicas = kdu_values.get(replica_str) + + if replicas is not None: + break return replicas -- 2.25.1