Synchronize helm repos on ns instantiation instead of creation 24/8624/6
authorlloretgalleg <illoret@indra.es>
Thu, 20 Feb 2020 10:50:50 +0000 (11:50 +0100)
committerlloretgalleg <illoret@indra.es>
Thu, 12 Mar 2020 21:55:43 +0000 (22:55 +0100)
Change-Id: I9f1e7931df739c78637be8a222b5fcf698d5f4de
Signed-off-by: lloretgalleg <illoret@indra.es>
osm_lcm/ns.py
osm_lcm/tests/test_ns.py
osm_lcm/vim_sdn.py

index 9bc0dba..c9bcc9d 100644 (file)
@@ -2076,9 +2076,28 @@ class NsLcm(LcmBase):
                     except Exception:
                         # it is not a file
                         pass
+
+                    step = "Prepare instantiate KDU {} in k8s cluster {}".format(
+                        kdur["kdu-name"], kdur["k8s-cluster"]["id"])
+
                     try:
                         if not error_text:
                             cluster_uuid = _get_cluster_id(kdur["k8s-cluster"]["id"], k8sclustertype_full)
+
+                            updated_cluster_list = []
+                            if k8sclustertype == "chart" and cluster_uuid not in updated_cluster_list:
+                                del_repo_list, added_repo_dict = await asyncio.ensure_future(
+                                    self.k8sclusterhelm.synchronize_repos(cluster_uuid=cluster_uuid))
+                                if del_repo_list or added_repo_dict:
+                                    unset = {'_admin.helm_charts_added.' + item: None for item in del_repo_list}
+                                    updated = {'_admin.helm_charts_added.' +
+                                               item: name for item, name in added_repo_dict.items()}
+                                    self.logger.debug("repos synchronized, to_delete: {}, to_add: {}".
+                                                      format(del_repo_list, added_repo_dict))
+                                    self.db.set_one("k8sclusters", {"_id": kdur["k8s-cluster"]["id"]},
+                                                    updated, unset=unset)
+                            updated_cluster_list.append(cluster_uuid)
+
                     except LcmException as e:
                         error_text = str(e)
                         deployed_ok = False
@@ -2097,6 +2116,7 @@ class NsLcm(LcmBase):
 
                     db_dict = {"collection": "nsrs", "filter": {"_id": nsr_id}, "path": "_admin.deployed.K8s."
                                                                                         "{}".format(index)}
+
                     if k8sclustertype == "chart":
                         task = asyncio.ensure_future(
                             self.k8sclusterhelm.install(cluster_uuid=cluster_uuid, kdu_model=kdumodel, atomic=True,
index 9ddaa4b..8b70003 100644 (file)
@@ -492,6 +492,7 @@ class TestMyNS(asynctest.TestCase):
         db_vnfds = {db_vnfd["_id"]: db_vnfd}
         logging_text = "KDU"
         self.my_ns.k8sclusterhelm.install = asynctest.CoroutineMock(return_value="k8s_id")
+        self.my_ns.k8sclusterhelm.synchronize_repos = asynctest.CoroutineMock(return_value=("", ""))
         await self.my_ns.deploy_kdus(logging_text, nsr_id, db_nsr, db_vnfrs, db_vnfds)
         db_nsr = self.db.get_list("nsrs")[1]
         self.assertIn("K8s", db_nsr["_admin"]["deployed"], "K8s entry not created at '_admin.deployed'")
index b07c551..6b1a338 100644 (file)
@@ -16,7 +16,6 @@
 # under the License.
 ##
 
-import asyncio
 import yaml
 import logging
 import logging.handlers
@@ -1012,32 +1011,12 @@ class K8sClusterLcm(LcmBase):
                 else:
                     self.logger.error(logging_text + "Failing init juju-bundle: {}".format(e), exc_info=True)
 
-            step = "Getting the list of repos"
-            if k8s_hc_id:
-                self.logger.debug(logging_text + step)
-                task_list = []
-                db_k8srepo_list = self.db.get_list("k8srepos", {"type": "helm-chart"})
-                for repo in db_k8srepo_list:
-                    step = "Adding repo {} to cluster: {}".format(repo["name"], k8s_hc_id)
-                    self.logger.debug(logging_text + step)
-                    task = asyncio.ensure_future(self.helm_k8scluster.repo_add(cluster_uuid=k8s_hc_id,
-                                                 name=repo["name"], url=repo["url"],
-                                                 repo_type="chart"))
-                    task_list.append(task)
-                    repo_k8scluster_list = deep_get(repo, ("_admin", "cluster-inserted")) or []
-                    repo_k8scluster_list.append(k8s_hc_id)
-                    self.update_db_2("k8srepos", repo["_id"], {"_admin.cluster-inserted": repo_k8scluster_list})
-
-                if task_list:
-                    self.logger.debug(logging_text + 'Waiting for terminate tasks of repo_add')
-                    done, pending = await asyncio.wait(task_list, timeout=3600)
-                    if pending:
-                        self.logger.error(logging_text + 'There are pending tasks: {}'.format(pending))
-
             # mark as an error if both helm-chart and juju-bundle have been failed
             if k8s_hc_id or k8s_jb_id:
+                self.logger.debug(logging_text + " successfully created")
                 db_k8scluster_update["_admin.operationalState"] = "ENABLED"
             else:
+                self.logger.debug(logging_text + " successfully created with errors")
                 db_k8scluster_update["_admin.operationalState"] = "ERROR"
                 db_k8scluster_update["_admin.detailed-status"] = ";".join(error_text_list)
 
@@ -1102,6 +1081,7 @@ class K8sClusterLcm(LcmBase):
                 uninstall_sw = uninstall_sw or False
                 cluster_removed = await self.juju_k8scluster.reset(cluster_uuid=k8s_jb_id, uninstall_sw=uninstall_sw)
 
+            # Try to remove from cluster_inserted to clean old versions
             if k8s_hc_id and cluster_removed:
                 step = "Removing k8scluster='{}' from k8srepos".format(k8scluster_id)
                 self.logger.debug(logging_text + step)
@@ -1193,31 +1173,6 @@ class K8sRepoLcm(LcmBase):
             step = "Getting k8srepo-id='{}' from db".format(k8srepo_id)
             self.logger.debug(logging_text + step)
             db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id})
-            step = "Getting k8scluster_list from db"
-            self.logger.debug(logging_text + step)
-            db_k8scluster_list = self.db.get_list("k8sclusters", {})
-            db_k8srepo_update["_admin.cluster-inserted"] = []
-            task_list = []
-            for k8scluster in db_k8scluster_list:
-                hc_id = deep_get(k8scluster, ("_admin", "helm-chart", "id"))
-                if hc_id:
-                    step = "Adding repo to cluster: {}".format(hc_id)
-                    self.logger.debug(logging_text + step)
-                    task = asyncio.ensure_future(self.k8srepo.repo_add(cluster_uuid=hc_id,
-                                                                       name=db_k8srepo["name"], url=db_k8srepo["url"],
-                                                                       repo_type="chart"))
-                    task_list.append(task)
-                    db_k8srepo_update["_admin.cluster-inserted"].append(hc_id)
-
-            done = None
-            pending = None
-            if len(task_list) > 0:
-                self.logger.debug('Waiting for terminate pending tasks...')
-                done, pending = await asyncio.wait(task_list, timeout=3600)
-                if not pending:
-                    self.logger.debug('All tasks finished...')
-                else:
-                    self.logger.info('There are pending tasks: {}'.format(pending))
             db_k8srepo_update["_admin.operationalState"] = "ENABLED"
         except Exception as e:
             self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
@@ -1264,27 +1219,6 @@ class K8sRepoLcm(LcmBase):
             step = "Getting k8srepo-id='{}' from db".format(k8srepo_id)
             self.logger.debug(logging_text + step)
             db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id})
-            step = "Getting k8scluster_list from db"
-            self.logger.debug(logging_text + step)
-            db_k8scluster_list = self.db.get_list("k8sclusters", {})
-
-            task_list = []
-            for k8scluster in db_k8scluster_list:
-                hc_id = deep_get(k8scluster, ("_admin", "helm-chart", "id"))
-                if hc_id:
-                    task = asyncio.ensure_future(self.k8srepo.repo_remove(cluster_uuid=hc_id,
-                                                                          name=db_k8srepo["name"]))
-                task_list.append(task)
-            done = None
-            pending = None
-            if len(task_list) > 0:
-                self.logger.debug('Waiting for terminate pending tasks...')
-                done, pending = await asyncio.wait(task_list, timeout=3600)
-                if not pending:
-                    self.logger.debug('All tasks finished...')
-                else:
-                    self.logger.info('There are pending tasks: {}'.format(pending))
-            self.db.del_one("k8srepos", {"_id": k8srepo_id})
 
         except Exception as e:
             self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)