+ def _look_for_k8scluster(self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback):
+ """
+ Look for an available k8scluster for all the kuds in the vnfd matching version and cni requirements.
+ Fills vnfr.kdur with the selected k8scluster
+
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param rollback: list with the database modifications to rollback if needed
+ :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
+ :param vim_account: vim_account where this vnfr should be deployed
+ :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
+ :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
+ of the changed vnfr is needed
+
+ :return: List of KDU interfaces that are connected to an existing VIM network. Each item contains:
+ "vim-network-name": used at VIM
+ "name": interface name
+ "vnf-vld-id": internal VNFD vld where this interface is connected, or
+ "ns-vld-id": NSD vld where this interface is connected.
+ NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
+ """
+
+ ifaces_forcing_vim_network = []
+ if not vnfr.get("kdur"):
+ return ifaces_forcing_vim_network
+
+ kdu_filter = self._get_project_filter(session)
+ kdu_filter["vim_account"] = vim_account
+ # TODO kdu_filter["_admin.operationalState"] = "ENABLED"
+ available_k8sclusters = self.db.get_list("k8sclusters", kdu_filter)
+
+ k8s_requirements = {} # just for logging
+ for k8scluster in available_k8sclusters:
+ if not vnfr.get("k8s-cluster"):
+ break
+ # restrict by cni
+ if vnfr["k8s-cluster"].get("cni"):
+ k8s_requirements["cni"] = vnfr["k8s-cluster"]["cni"]
+ if not set(vnfr["k8s-cluster"]["cni"]).intersection(k8scluster.get("cni", ())):
+ continue
+ # restrict by version
+ if vnfr["k8s-cluster"].get("version"):
+ k8s_requirements["version"] = vnfr["k8s-cluster"]["version"]
+ if k8scluster.get("k8s_version") not in vnfr["k8s-cluster"]["version"]:
+ continue
+ # restrict by number of networks
+ if vnfr["k8s-cluster"].get("nets"):
+ k8s_requirements["networks"] = len(vnfr["k8s-cluster"]["nets"])
+ if not k8scluster.get("nets") or len(k8scluster["nets"]) < len(vnfr["k8s-cluster"]["nets"]):
+ continue
+ break
+ else:
+ raise EngineException("No k8scluster with requirements='{}' at vim_account={} found for member_vnf_index={}"
+ .format(k8s_requirements, vim_account, vnfr["member-vnf-index-ref"]))
+
+ for kdur_index, kdur in enumerate(get_iterable(vnfr.get("kdur"))):
+ # step 3. Fill vnfr info by filling kdur
+ kdu_text = "kdur.{}.".format(kdur_index)
+ vnfr_update_rollback[kdu_text + "k8s-cluster.id"] = None
+ vnfr_update[kdu_text + "k8s-cluster.id"] = k8scluster["_id"]
+
+ # step 4. Check VIM networks that forces the selected k8s_cluster
+ if vnfr.get("k8s-cluster") and vnfr["k8s-cluster"].get("nets"):
+ k8scluster_net_list = list(k8scluster.get("nets").keys())
+ for net_index, kdur_net in enumerate(vnfr["k8s-cluster"]["nets"]):
+ # get a network from k8s_cluster nets. If name matches use this, if not use other
+ if kdur_net["id"] in k8scluster_net_list: # name matches
+ vim_net = k8scluster["nets"][kdur_net["id"]]
+ k8scluster_net_list.remove(kdur_net["id"])
+ else:
+ vim_net = k8scluster["nets"][k8scluster_net_list[0]]
+ k8scluster_net_list.pop(0)
+ vnfr_update_rollback["k8s-cluster.nets.{}.vim_net".format(net_index)] = None
+ vnfr_update["k8s-cluster.nets.{}.vim_net".format(net_index)] = vim_net
+ if vim_net and (kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id")):
+ ifaces_forcing_vim_network.append({
+ "name": kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id"),
+ "vnf-vld-id": kdur_net.get("vnf-vld-id"),
+ "ns-vld-id": kdur_net.get("ns-vld-id"),
+ "vim-network-name": vim_net, # TODO can it be vim-network-id ???
+ })
+ # TODO check that this forcing is not incompatible with other forcing
+ return ifaces_forcing_vim_network
+