X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=n2vc%2Fk8s_juju_conn.py;h=29e6c1dd6530b48049db8d09eb3fa312473aacdf;hb=60a3a96717d7c36ba7a65573da59a6bc039f5e28;hp=149947ddefbb58523d4e300a16947d9235722c3f;hpb=05bccf7db9202bf50f7c9c7513c36082e73005e1;p=osm%2FN2VC.git diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 149947d..29e6c1d 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio +from typing import Union import os import uuid import yaml @@ -76,6 +77,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: @@ -128,7 +130,7 @@ class K8sJujuConnector(K8sConnector): cleanup_data.append( { "delete": kubectl.delete_cluster_role, - "args": (metadata_name), + "args": (metadata_name,), } ) @@ -139,7 +141,7 @@ class K8sJujuConnector(K8sConnector): cleanup_data.append( { "delete": kubectl.delete_service_account, - "args": (metadata_name), + "args": (metadata_name,), } ) @@ -150,7 +152,7 @@ class K8sJujuConnector(K8sConnector): cleanup_data.append( { "delete": kubectl.delete_service_account, - "args": (metadata_name), + "args": (metadata_name,), } ) token, client_cert_data = await kubectl.get_secret_data( @@ -302,6 +304,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 @@ -491,18 +497,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] - # self.log.debug("[uninstall] Model destroyed and disconnecting") - # await controller.disconnect() + 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 + + 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 +659,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.