Delete KSU from DB while deleting default and non-default profiles 80/14980/5
authoryshah <shahithya.y@tataelxsi.co.in>
Tue, 11 Feb 2025 12:37:04 +0000 (12:37 +0000)
committeryshah <shahithya.y@tataelxsi.co.in>
Fri, 28 Feb 2025 09:02:01 +0000 (09:02 +0000)
Change-Id: Ia50335512cb5a39f881cea4c2f49af8acf40662c
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
osm_lcm/k8s.py

index 3a0e2d0..58337ac 100644 (file)
@@ -50,6 +50,13 @@ class GitOpsLcm(LcmBase):
         "app_profiles": "k8sapp",
     }
 
+    profile_type_mapping = {
+        "infra-controllers": "infra_controller_profiles",
+        "infra-configs": "infra_config_profiles",
+        "managed-resources": "resource_profiles",
+        "applications": "app_profiles",
+    }
+
     def __init__(self, msg, lcm_tasks, config):
         self.logger = logging.getLogger("lcm.gitops")
         self.lcm_tasks = lcm_tasks
@@ -274,6 +281,13 @@ class GitOpsLcm(LcmBase):
         )
         return content_copy
 
+    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)
+        if ksu_list:
+            self.db.del_list("ksus", filter_q)
+        return
+
     def cluster_kubectl(self, db_cluster):
         cluster_kubeconfig = db_cluster["credentials"]
         kubeconfig_path = f"/tmp/{db_cluster['_id']}_kubeconfig.yaml"
@@ -725,6 +739,7 @@ class ClusterLcm(GitOpsLcm):
                         "the db_profile name is :{}".format(db_profile["name"])
                     )
                     if db_cluster["name"] == db_profile["name"]:
+                        self.delete_profile_ksu(profile_id, profile_type)
                         self.db.del_one(db_collection, {"_id": profile_id})
                     else:
                         profile_ids.remove(profile_id)
@@ -1520,6 +1535,8 @@ class K8sAppLcm(GitOpsLcm):
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(f"App Delete Exit with resource status: {resource_status}")
@@ -1598,6 +1615,8 @@ class K8sResourceLcm(GitOpsLcm):
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(
@@ -1678,6 +1697,8 @@ class K8sInfraControllerLcm(GitOpsLcm):
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(
@@ -1758,6 +1779,8 @@ class K8sInfraConfigLcm(GitOpsLcm):
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(