Bug 1609 fix
[osm/N2VC.git] / n2vc / k8s_helm3_conn.py
index 06e5788..edefc86 100644 (file)
@@ -42,7 +42,6 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
         helm_command: str = "/usr/bin/helm3",
         log: object = None,
         on_update_db=None,
-        vca_config: dict = None,
     ):
         """
         Initializes helm connector for helm v3
@@ -64,7 +63,6 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
             kubectl_command=kubectl_command,
             helm_command=helm_command,
             on_update_db=on_update_db,
-            vca_config=vca_config,
         )
 
         self.log.info("K8S Helm3 connector initialized")
@@ -112,9 +110,17 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
 
         # for helm3 if namespace does not exist must create it
         if namespace and namespace != "kube-system":
-            namespaces = await self._get_namespaces(cluster_id)
-            if namespace not in namespaces:
-                await self._create_namespace(cluster_id, namespace)
+            if not await self._namespace_exists(cluster_id, namespace):
+                try:
+                    await self._create_namespace(cluster_id, namespace)
+                except Exception as e:
+                    if not await self._namespace_exists(cluster_id, namespace):
+                        err_msg = (
+                            "namespace {} does not exist in cluster_id {} "
+                            "error message: ".format(namespace, e)
+                        )
+                        self.log.error(err_msg)
+                        raise K8sException(err_msg)
 
         await self._install_impl(
             cluster_id,
@@ -228,6 +234,15 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
 
         return paths, env
 
+    async def _namespace_exists(self, cluster_id, namespace) -> bool:
+        self.log.debug(
+            "checking if namespace {} exists cluster_id {}".format(
+                namespace, cluster_id
+            )
+        )
+        namespaces = await self._get_namespaces(cluster_id)
+        return namespace in namespaces if namespaces else False
+
     async def _get_namespaces(self, cluster_id: str):
 
         self.log.debug("get namespaces cluster_id {}".format(cluster_id))
@@ -300,12 +315,8 @@ class K8sHelm3Connector(K8sHelmBaseConnector):
         # If default repo is not included add
         cluster_uuid = "{}:{}".format(namespace, cluster_id)
         repo_list = await self.repo_list(cluster_uuid)
-        for repo in repo_list:
-            self.log.debug("repo")
-            if repo["name"] == "stable":
-                self.log.debug("Default repo already present")
-                break
-        else:
+        stable_repo = [repo for repo in repo_list if repo["name"] == "stable"]
+        if not stable_repo and self._stable_repo_url:
             await self.repo_add(cluster_uuid, "stable", self._stable_repo_url)
 
         # Returns False as no software needs to be uninstalled