From 9e83dc314fd468b6d407618e3872edefebe704f8 Mon Sep 17 00:00:00 2001 From: Maarten de Waard Date: Tue, 27 Sep 2022 15:41:46 +0200 Subject: [PATCH] correctly return URLs for apps --- areas/apps/apps_service.py | 2 +- areas/apps/models.py | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/areas/apps/apps_service.py b/areas/apps/apps_service.py index e5897df..ff3e5d5 100644 --- a/areas/apps/apps_service.py +++ b/areas/apps/apps_service.py @@ -15,4 +15,4 @@ class AppsService: @staticmethod def get_app_roles(): app_roles = AppRole.query.all() - return [{"user_id": app_role.user_id, "app_id": app_role.app_id, "role_id": app_role.role_id} for app_role in app_roles] \ No newline at end of file + return [{"user_id": app_role.user_id, "app_id": app_role.app_id, "role_id": app_role.role_id} for app_role in app_roles] diff --git a/areas/apps/models.py b/areas/apps/models.py index 93c6847..4ac4dfd 100644 --- a/areas/apps/models.py +++ b/areas/apps/models.py @@ -1,6 +1,7 @@ """Everything to do with Apps""" import os +import base64 from sqlalchemy import ForeignKey, Integer, String, Boolean from sqlalchemy.orm import relationship @@ -11,6 +12,11 @@ import helpers.kubernetes as k8s #from .apps import APP_NOT_INSTALLED_STATUS APP_NOT_INSTALLED_STATUS = "Not installed" +DEFAULT_APP_SUBDOMAINS = { + "nextcloud": "files", + "wordpress": "www", + "monitoring": "grafana", +} class App(db.Model): """ @@ -22,13 +28,26 @@ class App(db.Model): name = db.Column(String(length=64)) slug = db.Column(String(length=64), unique=True) external = db.Column(Boolean, unique=False, nullable=False, default=True, server_default='0') + # The URL is only stored in the DB for external applications; otherwise the + # URL is stored in a configmap (see get_url) url = db.Column(String(length=128), unique=False) def __repr__(self): return f"{self.id} <{self.name}>" def get_url(self): - """Returns the URL where this application is running""" + """ + Returns the URL where this application is running + + For external applications: the URL is stored in the database + + For internal applications: the URL is stored in a configmap named + `stackspin-{self.slug}-kustomization-variables under + `{self.slug_domain}`. This function reads that configmap. If the + configmap does not contain a URL for the application (which is + possible, if the app is not installed yet, for example), we return a + default URL. + """ if self.external: return self.url @@ -38,10 +57,20 @@ class App(db.Model): f"stackspin-{self.slug}-kustomization-variables", "flux-system") domain_key = f"{self.slug}_domain" + # No config map found, or configmap not configured to contain the + # domain (yet). Return the default for this app if ks_config_map is None or domain_key not in ks_config_map.keys(): - return None - - return ks_config_map[domain_key] + domain_secret = k8s.get_kubernetes_secret_data( + "stackspin-cluster-variables", + "flux-system") + domain = base64.b64decode(domain_secret['domain']).decode() + # See if there is another default subdomain for this app than just + # "slug.{domain}" + if self.slug in DEFAULT_APP_SUBDOMAINS: + return f"https://{DEFAULT_APP_SUBDOMAINS[self.slug]}.{domain}" + # No default known + return f"https://{self.slug}.{domain}" + return f"https://{ks_config_map[domain_key]}" def get_status(self):