From fd28d5b774dd975d8b3533e70af86d1322598177 Mon Sep 17 00:00:00 2001 From: Maarten de Waard Date: Mon, 26 Sep 2022 13:42:13 +0200 Subject: [PATCH] return domains of internal apps --- areas/apps/models.py | 14 +++++++++++--- cliapp/cliapp/cli.py | 2 +- helpers/kubernetes.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/areas/apps/models.py b/areas/apps/models.py index ea75d52..93c6847 100644 --- a/areas/apps/models.py +++ b/areas/apps/models.py @@ -33,14 +33,22 @@ class App(db.Model): if self.external: return self.url - # TODO: Get URL from Kubernetes - return "unknown" + # Get domain name from configmap + ks_config_map = k8s.get_kubernetes_config_map_data( + f"stackspin-{self.slug}-kustomization-variables", + "flux-system") + domain_key = f"{self.slug}_domain" + if ks_config_map is None or domain_key not in ks_config_map.keys(): + return None + + return ks_config_map[domain_key] + def get_status(self): """Returns a string that describes the app state in the cluster""" if self.external: - return("External app") + return "External app" # TODO: Get some kind of caching for those values, as this is called diff --git a/cliapp/cliapp/cli.py b/cliapp/cliapp/cli.py index bf0004e..5235627 100644 --- a/cliapp/cliapp/cli.py +++ b/cliapp/cliapp/cli.py @@ -108,7 +108,7 @@ def list_app(): apps = App.query.all() for obj in apps: - print(f"App name: {obj.name} \t Slug: {obj.slug}") + print(f"App name: {obj.name}\tSlug: {obj.slug},\tURL: {obj.get_url()}\tStatus: {obj.get_status()}") @app_cli.command( diff --git a/helpers/kubernetes.py b/helpers/kubernetes.py index 9d843e2..e146433 100644 --- a/helpers/kubernetes.py +++ b/helpers/kubernetes.py @@ -85,6 +85,21 @@ def get_kubernetes_secret_data(secret_name, namespace): return None return secret +def get_kubernetes_config_map_data(config_map_name, namespace): + """ + Returns the contents of a kubernetes config map. + + Returns None if the config map does not exist. + """ + api_instance = client.CoreV1Api() + try: + config_map = api_instance.read_namespaced_config_map(config_map_name, namespace).data + except ApiException as ex: + # 404 is expected when the optional secret does not exist. + if ex.status != 404: + raise ex + return None + return config_map def store_kubernetes_secret(secret_dict, namespace, update=False): """Stores either a new secret in the cluster, or updates an existing one."""