Add deleting lock to K8sJujuConnector
[osm/N2VC.git] / n2vc / k8s_juju_conn.py
index 149947d..737cac6 100644 (file)
@@ -13,6 +13,7 @@
 #     limitations under the License.
 
 import asyncio
+from typing import Union
 import os
 import uuid
 import yaml
@@ -20,6 +21,7 @@ import tempfile
 import binascii
 
 from n2vc.config import EnvironConfig
+from n2vc.definitions import RelationEndpoint
 from n2vc.exceptions import K8sException
 from n2vc.k8s_conn import K8sConnector
 from n2vc.kubectl import Kubectl
@@ -76,6 +78,7 @@ class K8sJujuConnector(K8sConnector):
         db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri")
         self._store = MotorStore(db_uri)
         self.loading_libjuju = asyncio.Lock(loop=self.loop)
+        self.uninstall_locks = {}
 
         self.log.debug("K8S Juju connector initialized")
         # TODO: Remove these commented lines:
@@ -121,14 +124,16 @@ class K8sJujuConnector(K8sConnector):
         # if it fails in the middle of the process
         cleanup_data = []
         try:
+            self.log.debug("Initializing K8s cluster for juju")
             kubectl.create_cluster_role(
                 name=metadata_name,
                 labels=labels,
             )
+            self.log.debug("Cluster role created")
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_cluster_role,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
 
@@ -136,10 +141,11 @@ class K8sJujuConnector(K8sConnector):
                 name=metadata_name,
                 labels=labels,
             )
+            self.log.debug("Service account created")
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_service_account,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
 
@@ -147,10 +153,11 @@ class K8sJujuConnector(K8sConnector):
                 name=metadata_name,
                 labels=labels,
             )
+            self.log.debug("Role binding created")
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_service_account,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
             token, client_cert_data = await kubectl.get_secret_data(
@@ -158,6 +165,7 @@ class K8sJujuConnector(K8sConnector):
             )
 
             default_storage_class = kubectl.get_default_storage_class()
+            self.log.debug("Default storage class: {}".format(default_storage_class))
             await libjuju.add_k8s(
                 name=cluster_uuid,
                 rbac_id=rbac_id,
@@ -167,9 +175,10 @@ class K8sJujuConnector(K8sConnector):
                 storage_class=default_storage_class,
                 credential_name=self._get_credential_name(cluster_uuid),
             )
+            self.log.debug("K8s cluster added to juju controller")
             return cluster_uuid, True
         except Exception as e:
-            self.log.error("Error initializing k8scluster: {}".format(e))
+            self.log.error("Error initializing k8scluster: {}".format(e), exc_info=True)
             if len(cleanup_data) > 0:
                 self.log.debug("Cleaning up created resources in k8s cluster...")
                 for item in cleanup_data:
@@ -186,6 +195,9 @@ class K8sJujuConnector(K8sConnector):
         name: str,
         url: str,
         _type: str = "charm",
+        cert: str = None,
+        user: str = None,
+        password: str = None,
     ):
         raise MethodNotImplemented()
 
@@ -302,6 +314,10 @@ class K8sJujuConnector(K8sConnector):
             raise K8sException("bundle must be set")
 
         if bundle.startswith("cs:"):
+            # For Juju Bundles provided by the Charm Store
+            pass
+        elif bundle.startswith("ch:"):
+            # For Juju Bundles provided by the Charm Hub (this only works for juju version >= 2.9)
             pass
         elif bundle.startswith("http"):
             # Download the file
@@ -359,8 +375,8 @@ class K8sJujuConnector(K8sConnector):
         """Scale an application in a model
 
         :param: kdu_instance str:        KDU instance name
-        :param: scale int:               Scale to which to set this application
-        :param: resource_name str:       Resource name (Application name)
+        :param: scale int:               Scale to which to set the application
+        :param: resource_name str:       The application name in the Juju Bundle
         :param: timeout float:           The time, in seconds, to wait for the install
                                          to finish
         :param kwargs:                   Additional parameters
@@ -393,12 +409,13 @@ class K8sJujuConnector(K8sConnector):
     ) -> int:
         """Get an application scale count
 
-        :param: resource_name str:       Resource name (Application name)
+        :param: resource_name str:       The application name in the Juju Bundle
         :param: kdu_instance str:        KDU instance name
         :param kwargs:                   Additional parameters
                                             vca_id (str): VCA ID
         :return: Return application instance count
         """
+
         try:
             libjuju = await self._get_libjuju(kwargs.get("vca_id"))
             status = await libjuju.get_model_status(kdu_instance)
@@ -491,18 +508,30 @@ class K8sJujuConnector(K8sConnector):
         """
 
         self.log.debug("[uninstall] Destroying model")
-        libjuju = await self._get_libjuju(kwargs.get("vca_id"))
 
-        await libjuju.destroy_model(kdu_instance, total_timeout=3600)
+        will_not_delete = False
+        if kdu_instance not in self.uninstall_locks:
+            self.uninstall_locks[kdu_instance] = asyncio.Lock(loop=self.loop)
+        delete_lock = self.uninstall_locks[kdu_instance]
+
+        while delete_lock.locked():
+            will_not_delete = True
+            await asyncio.sleep(0.1)
+
+        if will_not_delete:
+            self.log.info("Model {} deleted by another worker.".format(kdu_instance))
+            return True
 
-        # self.log.debug("[uninstall] Model destroyed and disconnecting")
-        # await controller.disconnect()
+        try:
+            async with delete_lock:
+                libjuju = await self._get_libjuju(kwargs.get("vca_id"))
+
+                await libjuju.destroy_model(kdu_instance, total_timeout=3600)
+        finally:
+            self.uninstall_locks.pop(kdu_instance)
 
+        self.log.debug(f"[uninstall] Model {kdu_instance} destroyed")
         return True
-        # TODO: Remove these commented lines
-        # if not self.authenticated:
-        #     self.log.debug("[uninstall] Connecting to controller")
-        #     await self.login(cluster_uuid)
 
     async def exec_primitive(
         self,
@@ -641,7 +670,7 @@ class K8sJujuConnector(K8sConnector):
         complete_status: bool = False,
         yaml_format: bool = False,
         **kwargs,
-    ) -> dict:
+    ) -> Union[str, dict]:
         """Get the status of the KDU
 
         Get the current status of the KDU instance.
@@ -673,6 +702,53 @@ class K8sJujuConnector(K8sConnector):
 
         return status
 
+    async def add_relation(
+        self,
+        provider: RelationEndpoint,
+        requirer: RelationEndpoint,
+    ):
+        """
+        Add relation between two charmed endpoints
+
+        :param: provider: Provider relation endpoint
+        :param: requirer: Requirer relation endpoint
+        """
+        self.log.debug(f"adding new relation between {provider} and {requirer}")
+        cross_model_relation = (
+            provider.model_name != requirer.model_name
+            or requirer.vca_id != requirer.vca_id
+        )
+        try:
+            if cross_model_relation:
+                # Cross-model relation
+                provider_libjuju = await self._get_libjuju(provider.vca_id)
+                requirer_libjuju = await self._get_libjuju(requirer.vca_id)
+                offer = await provider_libjuju.offer(provider)
+                if offer:
+                    saas_name = await requirer_libjuju.consume(
+                        requirer.model_name, offer, provider_libjuju
+                    )
+                    await requirer_libjuju.add_relation(
+                        requirer.model_name,
+                        requirer.endpoint,
+                        saas_name,
+                    )
+            else:
+                # Standard relation
+                vca_id = provider.vca_id
+                model = provider.model_name
+                libjuju = await self._get_libjuju(vca_id)
+                # add juju relations between two applications
+                await libjuju.add_relation(
+                    model_name=model,
+                    endpoint_1=provider.endpoint,
+                    endpoint_2=requirer.endpoint,
+                )
+        except Exception as e:
+            message = f"Error adding relation between {provider} and {requirer}: {e}"
+            self.log.error(message)
+            raise Exception(message=message)
+
     async def update_vca_status(self, vcastatus: dict, kdu_instance: str, **kwargs):
         """
         Add all configs, actions, executed actions of all applications in a model to vcastatus dict