Bug 2066 fixed 63/12163/12
authorPedro Escaleira <escaleira@av.it.pt>
Sat, 4 Jun 2022 21:21:57 +0000 (22:21 +0100)
committercalvinosanc1 <guillermo.calvino@canonical.com>
Tue, 8 Nov 2022 16:13:28 +0000 (17:13 +0100)
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 <escaleira@av.it.pt>
n2vc/k8s_helm_base_conn.py

index afb7bc2..6ddf118 100644 (file)
@@ -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)
 
@@ -1740,7 +1752,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:
@@ -1749,7 +1761,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(
@@ -1757,6 +1771,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)
@@ -1777,10 +1793,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"
@@ -1819,7 +1835,7 @@ class K8sHelmBaseConnector(K8sConnector):
         namespace: str,
         kubeconfig: str,
         resource_name: str = None,
-    ):
+    ) -> int:
         """Get the replica count value in the instance.
 
         Args:
@@ -1829,7 +1845,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(
@@ -1837,23 +1853,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