X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fk8s_juju_conn.py;h=313e878fce927557bc5f1fc17b141060cfeb0987;hp=7a3bf273af4bbd46673c68ae67bfc4818ff994c1;hb=refs%2Fchanges%2F06%2F9606%2F1;hpb=f52cb7cfeb4e24febe7c66af3d5bb275a50d7f99 diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 7a3bf27..313e878 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -16,13 +16,14 @@ import asyncio import concurrent import os import uuid +import yaml import juju from juju.controller import Controller +from juju.model import Model from n2vc.exceptions import K8sException from n2vc.k8s_conn import K8sConnector -import yaml - +from n2vc.kubectl import Kubectl from .exceptions import MethodNotImplemented @@ -334,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:"): @@ -688,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 @@ -728,7 +756,7 @@ class K8sJujuConnector(K8sConnector): return True - async def add_model(self, model_name: str, cluster_uuid: str,) -> juju.model.Model: + async def add_model(self, model_name: str, cluster_uuid: str,) -> Model: """Adds a model to the controller Adds a new model to the Juju controller @@ -744,9 +772,12 @@ class K8sJujuConnector(K8sConnector): "Adding model '{}' to cluster_uuid '{}'".format(model_name, cluster_uuid) ) try: - model = await self.controller.add_model( - model_name, config={"authorized-keys": self.juju_public_key} - ) + if self.juju_public_key is not None: + model = await self.controller.add_model( + model_name, config={"authorized-keys": self.juju_public_key} + ) + else: + model = await self.controller.add_model(model_name) except Exception as ex: self.log.debug(ex) self.log.debug("Caught exception: {}".format(ex)) @@ -832,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 @@ -850,7 +887,7 @@ class K8sJujuConnector(K8sConnector): "Unable to locate configuration for cluster {}".format(cluster_uuid) ) - async def get_model(self, model_name: str, cluster_uuid: str,) -> juju.model.Model: + async def get_model(self, model_name: str, cluster_uuid: str,) -> Model: """Get a model from the Juju Controller. Note: Model objects returned must call disconnected() before it goes