Needed for feature 10938: extract_prometheus_scrape_jobs populating prom template... 96/12196/6
authorPedro Escaleira <escaleira@av.it.pt>
Sat, 11 Jun 2022 20:17:26 +0000 (21:17 +0100)
committercubag <gcuba@whitestack.com>
Thu, 15 Dec 2022 21:20:19 +0000 (22:20 +0100)
Change-Id: I227a6c8581d0b9e4f949b2ff46d13a5e900c978b
Signed-off-by: Pedro Escaleira <escaleira@av.it.pt>
osm_lcm/ns.py

index b6cc9a9..3e6bafc 100644 (file)
@@ -1782,6 +1782,7 @@ class NsLcm(LcmBase):
         vdu_id,
         kdu_name,
         vdu_index,
+        kdu_index,
         config_descriptor,
         deploy_params,
         base_folder,
@@ -2204,6 +2205,11 @@ class NsLcm(LcmBase):
                     vnfr_id=vnfr_id,
                     nsr_id=nsr_id,
                     target_ip=rw_mgmt_ip,
+                    vnf_member_index=db_vnfr.get("member-vnf-index-ref", ""),
+                    vdu_id=vdu_id,
+                    vdu_index=vdu_index,
+                    kdu_name=kdu_name,
+                    kdu_index=kdu_index,
                 )
                 if prometheus_jobs:
                     self.update_db_2(
@@ -2640,6 +2646,7 @@ class NsLcm(LcmBase):
                 vdu_index = 0
                 vdu_name = None
                 kdu_name = None
+                kdu_index = None
 
                 # Get additional parameters
                 deploy_params = {"OSM": get_osm_params(db_vnfr)}
@@ -2663,6 +2670,7 @@ class NsLcm(LcmBase):
                         kdu_name=kdu_name,
                         member_vnf_index=member_vnf_index,
                         vdu_index=vdu_index,
+                        kdu_index=kdu_index,
                         vdu_name=vdu_name,
                         deploy_params=deploy_params,
                         descriptor_config=descriptor_config,
@@ -2695,6 +2703,7 @@ class NsLcm(LcmBase):
                     if descriptor_config:
                         vdu_name = None
                         kdu_name = None
+                        kdu_index = None
                         for vdu_index in range(vdud_count):
                             # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
                             self._deploy_n2vc(
@@ -2710,6 +2719,7 @@ class NsLcm(LcmBase):
                                 vnfd_id=vnfd_id,
                                 vdu_id=vdu_id,
                                 kdu_name=kdu_name,
+                                kdu_index=kdu_index,
                                 member_vnf_index=member_vnf_index,
                                 vdu_index=vdu_index,
                                 vdu_name=vdu_name,
@@ -2726,8 +2736,10 @@ class NsLcm(LcmBase):
                         vdu_id = None
                         vdu_index = 0
                         vdu_name = None
-                        kdur = next(
-                            x for x in db_vnfr["kdur"] if x["kdu-name"] == kdu_name
+                        kdu_index, kdur = next(
+                            x
+                            for x in enumerate(db_vnfr["kdur"])
+                            if x[1]["kdu-name"] == kdu_name
                         )
                         deploy_params_kdu = {"OSM": get_osm_params(db_vnfr)}
                         if kdur.get("additionalParams"):
@@ -2747,6 +2759,7 @@ class NsLcm(LcmBase):
                             kdu_name=kdu_name,
                             member_vnf_index=member_vnf_index,
                             vdu_index=vdu_index,
+                            kdu_index=kdu_index,
                             vdu_name=vdu_name,
                             deploy_params=deploy_params_kdu,
                             descriptor_config=descriptor_config,
@@ -2763,6 +2776,7 @@ class NsLcm(LcmBase):
                 member_vnf_index = None
                 vdu_id = None
                 kdu_name = None
+                kdu_index = None
                 vdu_index = 0
                 vdu_name = None
 
@@ -2785,6 +2799,7 @@ class NsLcm(LcmBase):
                     kdu_name=kdu_name,
                     member_vnf_index=member_vnf_index,
                     vdu_index=vdu_index,
+                    kdu_index=kdu_index,
                     vdu_name=vdu_name,
                     deploy_params=deploy_params,
                     descriptor_config=descriptor_config,
@@ -3762,6 +3777,7 @@ class NsLcm(LcmBase):
         kdu_name,
         member_vnf_index,
         vdu_index,
+        kdu_index,
         vdu_name,
         deploy_params,
         descriptor_config,
@@ -3888,6 +3904,7 @@ class NsLcm(LcmBase):
                     vdu_id=vdu_id,
                     kdu_name=kdu_name,
                     vdu_index=vdu_index,
+                    kdu_index=kdu_index,
                     deploy_params=deploy_params,
                     config_descriptor=descriptor_config,
                     base_folder=base_folder,
@@ -7029,6 +7046,7 @@ class NsLcm(LcmBase):
                                 vdu_id = None
                                 vdu_name = None
                                 kdu_name = None
+                                kdu_index = None
                                 self._deploy_n2vc(
                                     logging_text=logging_text
                                     + "member_vnf_index={} ".format(member_vnf_index),
@@ -7040,6 +7058,7 @@ class NsLcm(LcmBase):
                                     vnfd_id=vnfd_id,
                                     vdu_id=vdu_id,
                                     kdu_name=kdu_name,
+                                    kdu_index=kdu_index,
                                     member_vnf_index=member_vnf_index,
                                     vdu_index=vdu_index,
                                     vdu_name=vdu_name,
@@ -7066,6 +7085,7 @@ class NsLcm(LcmBase):
                             if descriptor_config:
                                 vdu_name = None
                                 kdu_name = None
+                                kdu_index = None
                                 stage[
                                     1
                                 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
@@ -7088,6 +7108,7 @@ class NsLcm(LcmBase):
                                     kdu_name=kdu_name,
                                     member_vnf_index=member_vnf_index,
                                     vdu_index=vdu_index,
+                                    kdu_index=kdu_index,
                                     vdu_name=vdu_name,
                                     deploy_params=deploy_params_vdu,
                                     descriptor_config=descriptor_config,
@@ -7488,8 +7509,42 @@ class NsLcm(LcmBase):
             )
 
     async def extract_prometheus_scrape_jobs(
-        self, ee_id, artifact_path, ee_config_descriptor, vnfr_id, nsr_id, target_ip
-    ):
+        self,
+        ee_id: str,
+        artifact_path: str,
+        ee_config_descriptor: dict,
+        vnfr_id: str,
+        nsr_id: str,
+        target_ip: str,
+        vnf_member_index: str = "",
+        vdu_id: str = "",
+        vdu_index: int = None,
+        kdu_name: str = "",
+        kdu_index: int = None,
+    ) -> dict:
+        """Method to extract prometheus scrape jobs from EE's Prometheus template job file
+            This method will wait until the corresponding VDU or KDU is fully instantiated
+
+        Args:
+            ee_id (str): Execution Environment ID
+            artifact_path (str): Path where the EE's content is (including the Prometheus template file)
+            ee_config_descriptor (dict): Execution Environment's configuration descriptor
+            vnfr_id (str): VNFR ID where this EE applies
+            nsr_id (str): NSR ID where this EE applies
+            target_ip (str): VDU/KDU instance IP address
+            vnf_member_index (str, optional): VNF index where this EE applies. Defaults to "".
+            vdu_id (str, optional): VDU ID where this EE applies. Defaults to "".
+            vdu_index (int, optional): VDU index where this EE applies. Defaults to None.
+            kdu_name (str, optional): KDU name where this EE applies. Defaults to "".
+            kdu_index (int, optional): KDU index where this EE applies. Defaults to None.
+
+        Raises:
+            LcmException: When the VDU or KDU instance was not found in an hour
+
+        Returns:
+            _type_: Prometheus jobs
+        """
+        self.logger.debug(f"KDU: {kdu_name}; KDU INDEX: {kdu_index}")
         # look if exist a file called 'prometheus*.j2' and
         artifact_content = self.fs.dir_ls(artifact_path)
         job_file = next(
@@ -7505,6 +7560,52 @@ class NsLcm(LcmBase):
         with self.fs.file_open((artifact_path, job_file), "r") as f:
             job_data = f.read()
 
+        vdur_name = ""
+        kdur_name = ""
+        for r in range(360):
+            db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
+            if vdu_id and vdu_index is not None:
+                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
+                        )
+                    ),
+                    {},
+                )
+                if vdur.get("name"):
+                    vdur_name = vdur.get("name")
+                    break
+            if kdu_name and kdu_index is not None:
+                kdur = next(
+                    (
+                        x
+                        for x in get_iterable(db_vnfr, "kdur")
+                        if (
+                            x.get("kdu-name") == kdu_name
+                            and x.get("count-index") == kdu_index
+                        )
+                    ),
+                    {},
+                )
+                if kdur.get("name"):
+                    kdur_name = kdur.get("name")
+                    break
+
+            await asyncio.sleep(10, loop=self.loop)
+        else:
+            if vdu_id and vdu_index is not None:
+                raise LcmException(
+                    f"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
+                )
+            if kdu_name and kdu_index is not None:
+                raise LcmException(
+                    f"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
+                )
+
         # TODO get_service
         _, _, service = ee_id.partition(".")  # remove prefix   "namespace."
         host_name = "{}-{}".format(service, ee_config_descriptor["metric-service"])
@@ -7515,6 +7616,10 @@ class NsLcm(LcmBase):
             "TARGET_IP": target_ip,
             "EXPORTER_POD_IP": host_name,
             "EXPORTER_POD_PORT": host_port,
+            "NSR_ID": nsr_id,
+            "VNF_MEMBER_INDEX": vnf_member_index,
+            "VDUR_NAME": vdur_name,
+            "KDUR_NAME": kdur_name,
         }
         job_list = parse_job(job_data, variables)
         # ensure job_name is using the vnfr_id. Adding the metadata nsr_id