X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fk8s_juju_conn.py;h=a0bf3cfabfe6a7d7ae7cc85569a8e584757e1ca7;hp=1f9c896a1a8dc84d4160891f72efc4f2bf92ddb4;hb=refs%2Fchanges%2F07%2F9607%2F1;hpb=06bc9df8077a546587aaa6ebca8b9a3dfe512a44 diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 1f9c896..a0bf3cf 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -23,7 +23,7 @@ from juju.controller import Controller from juju.model import Model from n2vc.exceptions import K8sException from n2vc.k8s_conn import K8sConnector - +from n2vc.kubectl import Kubectl from .exceptions import MethodNotImplemented @@ -335,8 +335,10 @@ class K8sJujuConnector(K8sConnector): in the package> - """ - - previous_workdir = os.getcwd() + try: + previous_workdir = os.getcwd() + except FileNotFoundError: + previous_workdir = "/app/storage" bundle = kdu_model if kdu_model.startswith("cs:"): @@ -689,6 +691,31 @@ class K8sJujuConnector(K8sConnector): return status + async def get_services( + self, cluster_uuid: str, kdu_instance: str, namespace: str + ) -> list: + """Return a list of services of a kdu_instance""" + + config_file = self.get_config_file(cluster_uuid=cluster_uuid) + kubectl = Kubectl(config_file=config_file) + return kubectl.get_services( + field_selector="metadata.namespace={}".format(kdu_instance) + ) + + async def get_service( + self, cluster_uuid: str, service_name: str, namespace: str + ) -> object: + """Return data for a specific service inside a namespace""" + + config_file = self.get_config_file(cluster_uuid=cluster_uuid) + kubectl = Kubectl(config_file=config_file) + + return kubectl.get_services( + field_selector="metadata.name={},metadata.namespace={}".format( + service_name, namespace + ) + )[0] + # Private methods async def add_k8s(self, cloud_name: str, credentials: str,) -> bool: """Add a k8s cloud to Juju @@ -836,6 +863,12 @@ class K8sJujuConnector(K8sConnector): if "already exists" not in stderr: raise Exception(stderr) + def get_config_file(self, cluster_uuid: str) -> str: + """ + Get Cluster Kubeconfig location + """ + return "{}/{}/.kube/config".format(self.fs.path, cluster_uuid) + def get_config(self, cluster_uuid: str,) -> dict: """Get the cluster configuration @@ -913,14 +946,13 @@ class K8sJujuConnector(K8sConnector): :param credentials dict: A dictionary containing the k8s credentials :returns: A boolean if the cluster is running locally """ + creds = yaml.safe_load(credentials) - if os.getenv("OSMLCM_VCA_APIPROXY"): - host_ip = os.getenv("OSMLCM_VCA_APIPROXY") - if creds and host_ip: + if creds and os.getenv("OSMLCM_VCA_APIPROXY"): for cluster in creds["clusters"]: if "server" in cluster["cluster"]: - if host_ip in cluster["cluster"]["server"]: + if os.getenv("OSMLCM_VCA_APIPROXY") in cluster["cluster"]["server"]: return True return False