fix issues with k8scluster and repos
[osm/LCM.git] / osm_lcm / ns.py
index e6dec82..8705823 100644 (file)
@@ -615,7 +615,8 @@ class NsLcm(LcmBase):
                 if vnf_RO.get("ip_address"):
                     db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"].split(";")[0]
                 elif not db_vnfr.get("ip-address"):
-                    raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index))
+                    if db_vnfr.get("vdur"):   # if not VDUs, there is not ip_address
+                        raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index))
 
                 for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")):
                     vdur_RO_count_index = 0
@@ -731,7 +732,6 @@ class NsLcm(LcmBase):
         RO_descriptor_number = 0   # number of descriptors created at RO
         vnf_index_2_RO_id = {}    # map between vnfd/nsd id to the id used at RO
         start_deploy = time()
-        vdu_flag = False  # If any of the VNFDs has VDUs
         ns_params = db_nslcmop.get("operationParams")
         if ns_params and ns_params.get("timeout_ns_deploy"):
             timeout_ns_deploy = ns_params["timeout_ns_deploy"]
@@ -748,8 +748,6 @@ class NsLcm(LcmBase):
         for c_vnf in nsd.get("constituent-vnfd", ()):
             member_vnf_index = c_vnf["member-vnf-index"]
             vnfd = db_vnfds_ref[c_vnf['vnfd-id-ref']]
-            if vnfd.get("vdu"):
-                vdu_flag = True
             vnfd_ref = vnfd["id"]
             step = db_nsr_update["_admin.deployed.RO.detailed-status"] = "Creating vnfd='{}' member_vnf_index='{}' at" \
                                                                          " RO".format(vnfd_ref, member_vnf_index)
@@ -894,8 +892,7 @@ class NsLcm(LcmBase):
                 elif ns_status == "ACTIVE":
                     step = detailed_status = "Waiting for management IP address reported by the VIM. Updating VNFRs"
                     try:
-                        if vdu_flag:
-                            self.ns_update_vnfr(db_vnfrs, desc)
+                        self.ns_update_vnfr(db_vnfrs, desc)
                         break
                     except LcmExceptionNoMgmtIP:
                         pass
@@ -967,12 +964,13 @@ class NsLcm(LcmBase):
                     vdur = next((x for x in get_iterable(db_vnfr, "vdur")
                                  if x.get("vdu-id-ref") == vdu_id and x.get("count-index") == vdu_index), None)
 
+                if not vdur and len(db_vnfr.get("vdur", ())) == 1:  # If only one, this should be the target vdu
+                    vdur = db_vnfr["vdur"][0]
                 if not vdur:
-                    raise LcmException("Not found vnfr_id={}, vdu_index={}, vdu_index={}".format(
-                        vnfr_id, vdu_id, vdu_index
-                    ))
+                    raise LcmException("Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(vnfr_id, vdu_id,
+                                                                                              vdu_index))
 
-                if vdur.get("status") == "ACTIVE":
+                if vdur.get("pdu-type") or vdur.get("status") == "ACTIVE":
                     ip_address = vdur.get("ip-address")
                     if not ip_address:
                         continue
@@ -983,11 +981,12 @@ class NsLcm(LcmBase):
             if not target_vdu_id:
                 continue
 
-            # self.logger.debug(logging_text + "IP address={}".format(ip_address))
-
             # inject public key into machine
             if pub_key and user:
                 # self.logger.debug(logging_text + "Inserting RO key")
+                if vdur.get("pdu-type"):
+                    self.logger.error(logging_text + "Cannot inject ssh-ky to a PDU")
+                    return ip_address
                 try:
                     ro_vm_id = "{}-{}".format(db_vnfr["member-vnf-index-ref"], target_vdu_id)  # TODO add vdu_index
                     result_dict = await self.RO.create_action(
@@ -1805,6 +1804,7 @@ class NsLcm(LcmBase):
                                  exc_info=True)
         finally:
             if exc:
+                instantiated_ok = False
                 if db_nsr:
                     db_nsr_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
                     db_nsr_update["operational-status"] = "failed"
@@ -2079,9 +2079,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(logging_text + "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
@@ -2100,6 +2119,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,
@@ -2124,7 +2144,7 @@ class NsLcm(LcmBase):
                                                                  return_when=asyncio.FIRST_COMPLETED)
                     if not done_list:   # timeout
                         for task in pending_list:
-                            db_nsr_update[pending_tasks(task) + "detailed-status"] = "Timeout"
+                            db_nsr_update[pending_tasks[task] + "detailed-status"] = "Timeout"
                             deployed_ok = False
                         break
                     for task in done_list: