Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions app/routers/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from app.database.db import get_db
from app.database.models.processing_job import ProcessingJobRecord
from fastapi.responses import JSONResponse
from fastapi import status as http_status

router = APIRouter()

Expand Down Expand Up @@ -31,7 +33,17 @@ def check_db_status(db: Session) -> dict:

@router.get("/health")
async def health(db: Session = Depends(get_db)):
return {
"status": "ok",
"database": check_db_status(db),
}
db_status = check_db_status(db)
general_status = "ok" if db_status["status"] == "ok" else "error"
status_code = (
http_status.HTTP_200_OK
if general_status == "ok"
else http_status.HTTP_503_SERVICE_UNAVAILABLE
)
return JSONResponse(
status_code=status_code,
content={
"status": general_status,
"database": db_status,
},
)
23 changes: 23 additions & 0 deletions deploy/apex-dispatch-api/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions deploy/apex-dispatch-api/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: apex-dispatch-api
description: APEx Dispatch API
type: application
version: 0.1.0
appVersion: "latest"
62 changes: 62 additions & 0 deletions deploy/apex-dispatch-api/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "apex-dispatch-api.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "apex-dispatch-api.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "apex-dispatch-api.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "apex-dispatch-api.labels" -}}
helm.sh/chart: {{ include "apex-dispatch-api.chart" . }}
{{ include "apex-dispatch-api.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "apex-dispatch-api.selectorLabels" -}}
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "apex-dispatch-api.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "apex-dispatch-api.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
10 changes: 10 additions & 0 deletions deploy/apex-dispatch-api/templates/configmap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "apex-dispatch-api.fullname" . }}-config
data:
APP_NAME: {{ .Values.app.name | quote }}
APP_ENV: {{ .Values.app.environment | quote }}
CORS_ALLOWED_ORIGINS: {{ .Values.app.corsAllowedOrigins | quote }}
KEYCLOAK_HOST: {{ .Values.keycloak.host | quote }}
KEYCLOAK_REALM: {{ .Values.keycloak.realm | quote }}
58 changes: 58 additions & 0 deletions deploy/apex-dispatch-api/templates/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "apex-dispatch-api.fullname" . }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- name: apex-dispatch-api
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.service.targetPort }}
envFrom:
- configMapRef:
name: {{ include "apex-dispatch-api.fullname" . }}-config
env:
- name: KEYCLOAK_CLIENT_ID
valueFrom:
secretKeyRef:
name: {{ .Values.secrets.name }}
key: KEYCLOAK_CLIENT_ID
- name: KEYCLOAK_CLIENT_SECRET
valueFrom:
secretKeyRef:
name: {{ .Values.secrets.name }}
key: KEYCLOAK_CLIENT_SECRET
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: {{ .Values.secrets.name }}
key: DATABASE_URL
- name: OPENEO_BACKENDS
valueFrom:
secretKeyRef:
name: {{ .Values.secrets.name }}
key: OPENEO_BACKENDS
readinessProbe:
httpGet:
path: /health
port: {{ .Values.service.targetPort }}
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: {{ .Values.service.targetPort }}
initialDelaySeconds: 10
periodSeconds: 20
34 changes: 34 additions & 0 deletions deploy/apex-dispatch-api/templates/ingress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "apex-dispatch-api.fullname" . }}-ingress
labels:
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
annotations:
{{ toYaml .Values.ingress.annotations | indent 4 }}
spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts: {{ toYaml .hosts | trim | indent 8 }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host }}
http:
paths:
{{- range .paths }}
- path: {{ .path | default "/" }}
pathType: {{ .pathType | default "Prefix" }}
backend:
service:
name: {{ include "apex-dispatch-api.fullname" $ }}
port:
number: {{ $.Values.service.port }}
{{- end }}
{{- end }}
{{- end }}
15 changes: 15 additions & 0 deletions deploy/apex-dispatch-api/templates/secrets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- if .Values.secrets.create }}
apiVersion: v1
kind: Secret
metadata:
name: {{ .Values.secrets.name }}
labels:
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
type: Opaque
stringData:
KEYCLOAK_CLIENT_ID: {{ .Values.secrets.KEYCLOAK_CLIENT_ID | quote }}
KEYCLOAK_CLIENT_SECRET: {{ .Values.secrets.KEYCLOAK_CLIENT_SECRET | quote }}
DATABASE_URL: {{ .Values.secrets.DATABASE_URL | quote }}
OPENEO_BACKENDS: {{ .Values.secrets.OPENEO_BACKENDS | quote }}
{{- end }}
14 changes: 14 additions & 0 deletions deploy/apex-dispatch-api/templates/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "apex-dispatch-api.fullname" . }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
protocol: TCP
name: http
selector:
app.kubernetes.io/name: {{ include "apex-dispatch-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
44 changes: 44 additions & 0 deletions deploy/apex-dispatch-api/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
replicaCount: 1

image:
repository: apex-dispatch-api
tag: latest
pullPolicy: IfNotPresent

service:
type: ClusterIP
port: 8000
targetPort: 8000

ingress:
enabled: true
annotations: {}
hosts:
- host: apex-dispatch-api.local
paths:
- path: /
pathType: Prefix
tls: []

app:
name: "APEx Dispatch API"
environment: "development"
corsAllowedOrigins: "*"

keycloak:
host: "auth.dev.apex.esa.int"
realm: "apex"

secrets:
create: false
name: apex-dispatch-api-secrets
KEYCLOAK_CLIENT_ID: ""
KEYCLOAK_CLIENT_SECRET: ""
DATABASE_URL: ""
OPENEO_BACKENDS: ""

nodeSelector: {}

tolerations: []

affinity: {}
73 changes: 73 additions & 0 deletions deploy/deploy_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
set -euo pipefail

RELEASE_NAME="${1:-apex-dispatch-api}"
CLUSTER_NAME="${2:-local}"
IMAGE_NAME="${3:-apex-dispatch-api:latest}"
HOST="${4:-apex-dispatch-api.local}"
NAMESPACE="${5:-apex-dispatch-api-local}"
ENV_FILE="${6:-../.env}"
SECRET_NAME="${7:-apex-dispatch-api-secrets}"

echo "(1) Create kind cluster (if missing)"
if ! kind get clusters | grep -q "^${CLUSTER_NAME}$"; then
kind create cluster --name "${CLUSTER_NAME}"
else
echo "Cluster ${CLUSTER_NAME} already exists"
fi

echo "(2) Load local image into kind"
kind load docker-image "${IMAGE_NAME}" --name "${CLUSTER_NAME}"

echo "(3) Install ingress-nginx "
kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=90s

# Create namespace up front so we can create secrets there
echo "(4) Creating namespace ${NAMESPACE}"
if ! kubectl get namespace "${NAMESPACE}" >/dev/null 2>&1; then
kubectl create namespace "${NAMESPACE}"
else
echo "Namespace ${NAMESPACE} exists"
fi


echo "(5) Creating/updating Kubernetes Secret ${SECRET_NAME} in namespace ${NAMESPACE}"

_extract_env() {
local key="$1"
local val
if [ ! -f "${ENV_FILE}" ]; then
echo ""
return
fi
val=$(sed -n -E "s/^${key}=(.*)/\1/p" "${ENV_FILE}" | sed -E 's/^"(.*)"$/\1/' | sed -E "s/^'(.*)'$/\1/")
echo "${val}"
}

KEYCLOAK_CLIENT_ID=$(_extract_env "KEYCLOAK_CLIENT_ID")
KEYCLOAK_CLIENT_SECRET=$(_extract_env "KEYCLOAK_CLIENT_SECRET")
DATABASE_URL=$(_extract_env "DATABASE_URL")
OPENEO_BACKENDS=$(_extract_env "OPENEO_BACKENDS")

# Create or update secret using kubectl apply via dry-run
kubectl create secret generic "${SECRET_NAME}" \
--namespace "${NAMESPACE}" \
--from-literal=KEYCLOAK_CLIENT_ID="${KEYCLOAK_CLIENT_ID}" \
--from-literal=KEYCLOAK_CLIENT_SECRET="${KEYCLOAK_CLIENT_SECRET}" \
--from-literal=DATABASE_URL="${DATABASE_URL}" \
--from-literal=OPENEO_BACKENDS="${OPENEO_BACKENDS}" \
--dry-run=client -o yaml | kubectl apply -f -


echo "(6) Install/upgrade apex-dispatch-api chart with ingress enabled"
helm upgrade --install "${RELEASE_NAME}" ./apex-dispatch-api -f ./apex-dispatch-api/values.yaml \
--namespace "${NAMESPACE}" --create-namespace \
--wait


echo "(7) Enabling port forwarding to test locally"
kubectl -n apex-dispatch-api-local port-forward svc/apex-dispatch-api 8000:8000
Loading