Fix OKA usage state while deleting cluster, profiles and KSU 00/15200/2
authoryshah <shahithya.y@tataelxsi.co.in>
Mon, 19 May 2025 12:29:01 +0000 (12:29 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 3 Jun 2025 09:29:41 +0000 (11:29 +0200)
Change-Id: Iab717e3a72f12d5fd8d77c83ff18cd983e14c32b
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
osm_lcm/k8s.py

index 21df2b1..83f57e4 100644 (file)
@@ -310,9 +310,39 @@ class GitOpsLcm(LcmBase):
         )
         return content_copy
 
+    def delete_ksu_dependency(self, _id, data):
+        used_oka = []
+        existing_oka = []
+
+        for oka_data in data["oka"]:
+            if oka_data.get("_id"):
+                used_oka.append(oka_data["_id"])
+
+        all_ksu_data = self.db.get_list("ksus", {})
+        for ksu_data in all_ksu_data:
+            if ksu_data["_id"] != _id:
+                for oka_data in ksu_data["oka"]:
+                    if oka_data.get("_id"):
+                        if oka_data["_id"] not in existing_oka:
+                            existing_oka.append(oka_data["_id"])
+
+        self.logger.info(f"Used OKA: {used_oka}")
+        self.logger.info(f"Existing OKA: {existing_oka}")
+
+        for oka_id in used_oka:
+            if oka_id not in existing_oka:
+                self.db.set_one(
+                    "okas", {"_id": oka_id}, {"_admin.usageState": "NOT_IN_USE"}
+                )
+
+        return
+
     def delete_profile_ksu(self, _id, profile_type):
         filter_q = {"profile": {"_id": _id, "profile_type": profile_type}}
         ksu_list = self.db.get_list("ksus", filter_q)
+        for ksu_data in ksu_list:
+            self.delete_ksu_dependency(ksu_data["_id"], ksu_data)
+
         if ksu_list:
             self.db.del_list("ksus", filter_q)
         return
@@ -2216,6 +2246,7 @@ class KsuLcm(GitOpsLcm):
 
             if resource_status:
                 db_ksu["state"] == "DELETED"
+                self.delete_ksu_dependency(db_ksu["_id"], db_ksu)
                 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
                 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})