correctly return URLs for apps

This commit is contained in:
Maarten de Waard 2022-09-27 15:41:46 +02:00
parent fd28d5b774
commit 9e83dc314f
No known key found for this signature in database
GPG key ID: 1D3E893A657CC8DA
2 changed files with 34 additions and 5 deletions

View file

@ -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):