Feature 10983: new post-renderer script and new function migrate() in k8s_helm3_conn

Change-Id: I75564fd51e9d9cc3a16efc4e7b732292ba763fba
Signed-off-by: Pedro Pereira <pedrocjdpereira@av.it.pt>
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/n2vc/k8s_helm3_conn.py b/n2vc/k8s_helm3_conn.py
index ed0bdaa..8d5e4a0 100644
--- a/n2vc/k8s_helm3_conn.py
+++ b/n2vc/k8s_helm3_conn.py
@@ -155,6 +155,42 @@
         self.log.debug("Returning kdu_instance {}".format(kdu_instance))
         return True
 
+    async def migrate(self, nsr_id, target):
+        db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
+
+        # check if it has k8s deployed kdus
+        if len(db_nsr["_admin"]["deployed"]["K8s"]) < 1:
+            err_msg = "INFO: No deployed KDUs"
+            self.log.error(err_msg)
+            raise K8sException(err_msg)
+
+        kdu_id = target["vdu"]["vduId"]
+        for index, kdu in enumerate(db_nsr["_admin"]["deployed"]["K8s"]):
+            if kdu["kdu-instance"] == kdu_id:
+                namespace = kdu["namespace"]
+                cluster_uuid = kdu["k8scluster-uuid"]
+                kdu_model = kdu["kdu-model"]
+                db_dict = {
+                    "collection": "nsrs",
+                    "filter": {"_id": nsr_id},
+                    "path": "_admin.deployed.K8s.{}".format(index),
+                }
+
+                await self.upgrade(
+                    cluster_uuid,
+                    kdu_instance=kdu_id,
+                    kdu_model=kdu_model,
+                    namespace=namespace,
+                    targetHostK8sLabels=target["targetHostK8sLabels"],
+                    atomic=True,
+                    db_dict=db_dict,
+                    force=True,
+                )
+
+                return True
+
+        self.log.debug("ERROR: Unable to retrieve kdu from the database")
+
     async def inspect_kdu(self, kdu_model: str, repo_url: str = None) -> str:
         self.log.debug(
             "inspect kdu_model {} from (optional) repo: {}".format(kdu_model, repo_url)
@@ -581,6 +617,7 @@
         atomic: bool,
         timeout: float,
         kubeconfig: str,
+        targetHostK8sLabels: dict = None,
         reset_values: bool = False,
         reuse_values: bool = True,
         reset_then_reuse_values: bool = False,
@@ -652,6 +689,20 @@
                 )
             )
 
+        # migration
+        if targetHostK8sLabels and self.nodeSelector_post_renderer_path:
+            post_renderer_args.append(
+                "{}={}".format(
+                    self.nodeSelector_post_renderer_path,
+                    " ".join(
+                        [
+                            "{}:{}".format(key, value)
+                            for key, value in targetHostK8sLabels.items()
+                        ]
+                    ),
+                )
+            )
+
         if len(post_renderer_args) > 0 and self.main_post_renderer_path:
             post_renderer_str = "--post-renderer {}".format(
                 self.main_post_renderer_path,