Bug 2065 fixed 62/12162/6
authorPedro Escaleira <escaleira@av.it.pt>
Sat, 4 Jun 2022 18:14:11 +0000 (19:14 +0100)
committercalvinosanc1 <guillermo.calvino@canonical.com>
Wed, 28 Sep 2022 16:03:14 +0000 (18:03 +0200)
The method _exec_inspect_command is only obtaining the repo name
where an URL was passed: this will allow to scale KDUs based on local
Helm Charts; did also some refactor work: the _split_repo method now
returns both the Chart's name and Repo's name, in order to avoid using
similar blocks of code in other portions (with this, made some changes
to the _exec_inspect_command and _find_repo methods, to use this method,
instead of a similar block of code); added some information to the
description of some changed methods; other small changes.

Change-Id: I7302623ad2bfeef9be3b7c17376cab67f5eda630
Signed-off-by: Pedro Escaleira <escaleira@av.it.pt>
n2vc/k8s_helm3_conn.py
n2vc/k8s_helm_base_conn.py

index 74a8947..0a8f7e0 100644 (file)
@@ -351,7 +351,7 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
             return []
 
     def _get_inspect_command(
-        self, inspect_command: str, kdu_model: str, repo_str: str, version: str
+        self, show_command: str, kdu_model: str, repo_str: str, version: str
     ):
         """Generates the command to obtain the information about an Helm Chart package
             (┬┤helm show ...┬┤ command)
@@ -367,7 +367,7 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
         """
 
         inspect_command = "{} show {} {}{} {}".format(
-            self._helm_command, inspect_command, kdu_model, repo_str, version
+            self._helm_command, show_command, kdu_model, repo_str, version
         )
         return inspect_command
 
index afd9d5e..afb7bc2 100644 (file)
@@ -404,7 +404,7 @@ class K8sHelmBaseConnector(K8sConnector):
         # version
         kdu_model, version = self._split_version(kdu_model)
 
-        repo = self._split_repo(kdu_model)
+        _, repo = self._split_repo(kdu_model)
         if repo:
             await self.repo_update(cluster_id, repo)
 
@@ -508,7 +508,7 @@ class K8sHelmBaseConnector(K8sConnector):
         # version
         kdu_model, version = self._split_version(kdu_model)
 
-        repo = self._split_repo(kdu_model)
+        _, repo = self._split_repo(kdu_model)
         if repo:
             await self.repo_update(cluster_uuid, repo)
 
@@ -1715,10 +1715,8 @@ class K8sHelmBaseConnector(K8sConnector):
         if repo_url:
             repo_str = " --repo {}".format(repo_url)
 
-        idx = kdu_model.find("/")
-        if idx >= 0:
-            idx += 1
-            kdu_model = kdu_model[idx:]
+            # Obtain the Chart's name and store it in the var kdu_model
+            kdu_model, _ = self._split_repo(kdu_model=kdu_model)
 
         kdu_model, version = self._split_version(kdu_model)
         if version:
@@ -1727,10 +1725,13 @@ class K8sHelmBaseConnector(K8sConnector):
             version_str = ""
 
         full_command = self._get_inspect_command(
-            inspect_command, kdu_model, repo_str, version_str
+            show_command=inspect_command,
+            kdu_model=kdu_model,
+            repo_str=repo_str,
+            version=version_str,
         )
 
-        output, _rc = await self._local_async_exec(command=full_command)
+        output, _ = await self._local_async_exec(command=full_command)
 
         return output
 
@@ -1999,12 +2000,27 @@ class K8sHelmBaseConnector(K8sConnector):
                 kdu_model = parts[0]
         return kdu_model, version
 
-    def _split_repo(self, kdu_model: str) -> str:
+    def _split_repo(self, kdu_model: str) -> (str, str):
+        """Obtain the Helm Chart's repository and Chart's names from the KDU model
+
+        Args:
+            kdu_model (str): Associated KDU model
+
+        Returns:
+            (str, str): Tuple with the Chart name in index 0, and the repo name
+                        in index 2; if there was a problem finding them, return None
+                        for both
+        """
+
+        chart_name = None
         repo_name = None
+
         idx = kdu_model.find("/")
         if idx >= 0:
+            chart_name = kdu_model[idx + 1 :]
             repo_name = kdu_model[:idx]
-        return repo_name
+
+        return chart_name, repo_name
 
     async def _find_repo(self, kdu_model: str, cluster_uuid: str) -> str:
         """Obtain the Helm repository for an Helm Chart
@@ -2017,12 +2033,15 @@ class K8sHelmBaseConnector(K8sConnector):
             str: the repository URL; if Helm Chart is a local one, the function returns None
         """
 
+        _, repo_name = self._split_repo(kdu_model=kdu_model)
+
         repo_url = None
-        idx = kdu_model.find("/")
-        if idx >= 0:
-            repo_name = kdu_model[:idx]
+        if repo_name:
             # Find repository link
             local_repo_list = await self.repo_list(cluster_uuid)
             for repo in local_repo_list:
-                repo_url = repo["url"] if repo["name"] == repo_name else None
+                if repo["name"] == repo_name:
+                    repo_url = repo["url"]
+                    break  # it is not necessary to continue the loop if the repo link was found...
+
         return repo_url