Bug 1400: Fix stable repo urls that have changed
[osm/N2VC.git] / n2vc / k8s_helm_conn.py
index 2e74423..9a0908f 100644 (file)
@@ -41,6 +41,7 @@ class K8sHelmConnector(K8sConnector):
     ####################################################################################
     """
     service_account = "osm"
+    _STABLE_REPO_URL = "https://charts.helm.sh/stable"
 
     def __init__(
         self,
@@ -50,6 +51,7 @@ class K8sHelmConnector(K8sConnector):
         helm_command: str = "/usr/bin/helm",
         log: object = None,
         on_update_db=None,
+        vca_config: dict = None,
     ):
         """
 
@@ -80,9 +82,16 @@ class K8sHelmConnector(K8sConnector):
         self._helm_command = helm_command
         self._check_file_exists(filename=helm_command, exception_if_not_exists=True)
 
+        # obtain stable repo url from config or apply default
+        if not vca_config or not vca_config.get("stablerepourl"):
+            self._stable_repo_url = self._STABLE_REPO_URL
+        else:
+            self._stable_repo_url = vca_config.get("stablerepourl")
+
         # initialize helm client-only
         self.log.debug("Initializing helm client-only...")
-        command = "{} init --client-only".format(self._helm_command)
+        command = "{} init --client-only --stable-repo-url {}".format(
+            self._helm_command, self._stable_repo_url)
         try:
             asyncio.ensure_future(
                 self._local_async_exec(command=command, raise_exception_on_error=False)
@@ -176,8 +185,11 @@ class K8sHelmConnector(K8sConnector):
             _, _rc = await self._local_async_exec(command=command, raise_exception_on_error=False)
 
             command = ("{} --kubeconfig={} --tiller-namespace={} --home={} --service-account {} "
-                       "init").format(self._helm_command, config_filename, namespace, helm_dir,
-                                      self.service_account)
+                       " --stable-repo-url {} init").format(self._helm_command,
+                                                            config_filename,
+                                                            namespace, helm_dir,
+                                                            self.service_account,
+                                                            self._stable_repo_url)
             _, _rc = await self._local_async_exec(command=command, raise_exception_on_error=True)
             n2vc_installed_sw = True
         else:
@@ -187,14 +199,28 @@ class K8sHelmConnector(K8sConnector):
                 self.log.info("Initializing helm in client: {}".format(cluster_id))
                 command = (
                     "{} --kubeconfig={} --tiller-namespace={} "
-                    "--home={} init --client-only"
-                ).format(self._helm_command, config_filename, namespace, helm_dir)
+                    "--home={} init --client-only  --stable-repo-url {} "
+                ).format(self._helm_command, config_filename, namespace,
+                         helm_dir, self._stable_repo_url)
                 output, _rc = await self._local_async_exec(
                     command=command, raise_exception_on_error=True
                 )
             else:
                 self.log.info("Helm client already initialized")
 
+        # remove old stable repo and add new one
+        cluster_uuid = "{}:{}".format(namespace, cluster_id)
+        repo_list = await self.repo_list(cluster_uuid)
+        for repo in repo_list:
+            if repo["Name"] == "stable" and repo["URL"] != self._stable_repo_url:
+                self.log.debug("Add new stable repo url: {}")
+                await self.repo_remove(cluster_uuid,
+                                       "stable")
+                await self.repo_add(cluster_uuid,
+                                    "stable",
+                                    self._stable_repo_url)
+                break
+
         self.log.info("Cluster {} initialized".format(cluster_id))
 
         return cluster_uuid, n2vc_installed_sw
@@ -280,40 +306,39 @@ class K8sHelmConnector(K8sConnector):
     ) -> bool:
 
         namespace, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
-        self.log.debug(
-            "Resetting K8s environment. cluster uuid: {}".format(cluster_id)
-        )
+        self.log.debug("Resetting K8s environment. cluster uuid: {} uninstall={}"
+                       .format(cluster_id, uninstall_sw))
 
         # get kube and helm directories
         _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
             cluster_name=cluster_id, create_if_not_exist=False
         )
 
-        # uninstall releases if needed
-        releases = await self.instances_list(cluster_uuid=cluster_uuid)
-        if len(releases) > 0:
-            if force:
-                for r in releases:
-                    try:
-                        kdu_instance = r.get("Name")
-                        chart = r.get("Chart")
-                        self.log.debug(
-                            "Uninstalling {} -> {}".format(chart, kdu_instance)
-                        )
-                        await self.uninstall(
-                            cluster_uuid=cluster_uuid, kdu_instance=kdu_instance
-                        )
-                    except Exception as e:
-                        self.log.error(
-                            "Error uninstalling release {}: {}".format(kdu_instance, e)
-                        )
-            else:
-                msg = (
-                    "Cluster has releases and not force. Cannot reset K8s "
-                    "environment. Cluster uuid: {}"
-                ).format(cluster_id)
-                self.log.error(msg)
-                raise K8sException(msg)
+        # uninstall releases if needed.
+        if uninstall_sw:
+            releases = await self.instances_list(cluster_uuid=cluster_uuid)
+            if len(releases) > 0:
+                if force:
+                    for r in releases:
+                        try:
+                            kdu_instance = r.get("Name")
+                            chart = r.get("Chart")
+                            self.log.debug(
+                                "Uninstalling {} -> {}".format(chart, kdu_instance)
+                            )
+                            await self.uninstall(
+                                cluster_uuid=cluster_uuid, kdu_instance=kdu_instance
+                            )
+                        except Exception as e:
+                            self.log.error(
+                                "Error uninstalling release {}: {}".format(kdu_instance, e)
+                            )
+                else:
+                    msg = (
+                        "Cluster uuid: {} has releases and not force. Leaving K8s helm environment"
+                    ).format(cluster_id)
+                    self.log.warn(msg)
+                    uninstall_sw = False  # Allow to remove k8s cluster without removing Tiller
 
         if uninstall_sw:
 
@@ -833,10 +858,73 @@ class K8sHelmConnector(K8sConnector):
             return_text=True,
         )
 
+    async def get_services(self,
+                           cluster_uuid: str,
+                           kdu_instance: str,
+                           namespace: str) -> list:
+
+        _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
+        self.log.debug(
+            "get_services: cluster_uuid: {}, kdu_instance: {}".format(
+                cluster_uuid, kdu_instance
+            )
+        )
+
+        status = await self._status_kdu(
+            cluster_id, kdu_instance, return_text=False
+        )
+
+        service_names = self._parse_helm_status_service_info(status)
+        service_list = []
+        for service in service_names:
+            service = await self.get_service(cluster_uuid, service, namespace)
+            service_list.append(service)
+
+        return service_list
+
+    async def get_service(self,
+                          cluster_uuid: str,
+                          service_name: str,
+                          namespace: str) -> object:
+
+        self.log.debug(
+            "get service, service_name: {}, namespace: {}, cluster_uuid: {}".format(
+                service_name, namespace, cluster_uuid)
+        )
+
+        # get paths
+        _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
+        _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
+            cluster_name=cluster_id, create_if_not_exist=True
+        )
+
+        command = "{} --kubeconfig={} --namespace={} get service {} -o=yaml".format(
+            self.kubectl_command, config_filename, namespace, service_name
+        )
+
+        output, _rc = await self._local_async_exec(
+            command=command, raise_exception_on_error=True
+        )
+
+        data = yaml.load(output, Loader=yaml.SafeLoader)
+
+        service = {
+            "name": service_name,
+            "type": self._get_deep(data, ("spec", "type")),
+            "ports": self._get_deep(data, ("spec", "ports")),
+            "cluster_ip": self._get_deep(data, ("spec", "clusterIP"))
+        }
+        if service["type"] == "LoadBalancer":
+            ip_map_list = self._get_deep(data, ("status", "loadBalancer", "ingress"))
+            ip_list = [elem["ip"] for elem in ip_map_list]
+            service["external_ip"] = ip_list
+
+        return service
+
     async def synchronize_repos(self, cluster_uuid: str):
 
         _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
-        self.log.debug("syncronize repos for cluster helm-id: {}",)
+        self.log.debug("syncronize repos for cluster helm-id: {}".format(cluster_id))
         try:
             update_repos_timeout = (
                 300  # max timeout to sync a single repos, more than this is too much
@@ -854,8 +942,8 @@ class K8sHelmConnector(K8sConnector):
                 # elements that must be deleted
                 deleted_repo_list = []
                 added_repo_dict = {}
-                self.log.debug("helm_chart_repos: {}".format(nbi_repo_list))
-                self.log.debug("helm_charts_added: {}".format(cluster_repo_dict))
+                self.log.debug("helm_chart_repos: {}".format(nbi_repo_list))
+                self.log.debug("helm_charts_added: {}".format(cluster_repo_dict))
 
                 # obtain repos to add: registered by nbi but not added
                 repos_to_add = [
@@ -872,7 +960,8 @@ class K8sHelmConnector(K8sConnector):
                 # delete repos: must delete first then add because there may be
                 # different repos with same name but
                 # different id and url
-                self.log.debug("repos to delete: {}".format(repos_to_delete))
+                if repos_to_delete:
+                    self.log.debug("repos to delete: {}".format(repos_to_delete))
                 for repo_id in repos_to_delete:
                     # try to delete repos
                     try:
@@ -895,7 +984,8 @@ class K8sHelmConnector(K8sConnector):
                     deleted_repo_list.append(repo_id)
 
                 # add repos
-                self.log.debug("repos to add: {}".format(repos_to_add))
+                if repos_to_add:
+                    self.log.debug("repos to add: {}".format(repos_to_add))
                 for repo_id in repos_to_add:
                     # obtain the repo data from the db
                     # if there is an error getting the repo in the database we will
@@ -1074,6 +1164,7 @@ class K8sHelmConnector(K8sConnector):
         db_dict: dict = None,
         run_once: bool = False,
     ):
+        previous_exception = None
         while True:
             try:
                 await asyncio.sleep(check_every)
@@ -1097,8 +1188,10 @@ class K8sHelmConnector(K8sConnector):
                 self.log.debug("Task cancelled")
                 return
             except Exception as e:
-                self.log.debug("_store_status exception: {}".format(str(e)))
-                pass
+                # log only once in the while loop
+                if str(previous_exception) != str(e):
+                    self.log.debug("_store_status exception: {}".format(str(e)))
+                previous_exception = e
             finally:
                 if run_once:
                     return
@@ -1152,6 +1245,37 @@ class K8sHelmConnector(K8sConnector):
 
         return ready
 
+    def _parse_helm_status_service_info(self, status):
+
+        # extract info.status.resources-> str
+        # format:
+        #       ==> v1/Deployment
+        #       NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
+        #       halting-horse-mongodb   0/1     1            0           0s
+        #       halting-petit-mongodb   1/1     1            0           0s
+        # blank line
+        resources = K8sHelmConnector._get_deep(status, ("info", "status", "resources"))
+
+        service_list = []
+        first_line_skipped = service_found = False
+        for line in resources:
+            if not service_found:
+                if len(line) >= 2 and line[0] == "==>" and line[1] == "v1/Service":
+                    service_found = True
+                    continue
+            else:
+                if len(line) >= 2 and line[0] == "==>":
+                    service_found = first_line_skipped = False
+                    continue
+                if not line:
+                    continue
+                if not first_line_skipped:
+                    first_line_skipped = True
+                    continue
+                service_list.append(line[0])
+
+        return service_list
+
     @staticmethod
     def _get_deep(dictionary: dict, members: tuple):
         target = dictionary