this is a demonstration app for the digital fieldlab seminar
  • Python 96.9%
  • Dockerfile 3.1%
Find a file
Jurriaan Cap d558c00b20
All checks were successful
ci-deploy / check-manifests (push) Successful in 4s
ci-deploy / test (push) Successful in 11s
ci-deploy / build-and-push (push) Successful in 12s
ci-deploy / deploy (push) Successful in 20s
Refactor health check to use port-forwarding instead of external IP for cluster access
2026-11-23 14:05:14 +01:00
.forgejo/workflows Refactor health check to use port-forwarding instead of external IP for cluster access 2026-11-23 14:05:14 +01:00
app Retry database connection on startup and return 503 from /health on errors 2026-11-23 11:28:33 +01:00
k8s Add HTTPS for the proeftuin demo via Caddy and sslip.io 2026-11-23 13:31:08 +01:00
tests Retry database connection on startup and return 503 from /health on errors 2026-11-23 11:28:33 +01:00
.dockerignore Add CI/CD workflow for demo-app to test, build, and deploy to Fundament 2026-11-22 14:29:08 +01:00
.env.example Add unique instance name generation and logging; update endpoints to display app name 2026-11-22 14:13:59 +01:00
.gitignore Ignore Fundament kubeconfig (.kube/) 2026-11-20 14:44:16 +01:00
.python-version setup basic app 2026-11-17 09:56:12 +01:00
AI-GROUNDING.md Add unique instance name generation and logging; update endpoints to display app name 2026-11-22 14:13:59 +01:00
Dockerfile Refactor Dockerfile and Kubernetes manifests for improved security and clarity; add non-root user, update uv version, and introduce config maps and service accounts. 2026-11-20 13:45:17 +01:00
pyproject.toml setup basic app 2026-11-17 09:56:12 +01:00
README.md Retry database connection on startup and return 503 from /health on errors 2026-11-23 11:28:33 +01:00
todo.md Add CI/CD workflow for demo-app to test, build, and deploy to Fundament 2026-11-22 14:29:08 +01:00
uv.lock Add CI/CD workflow for demo-app to test, build, and deploy to Fundament 2026-11-22 14:29:08 +01:00

demo-app

this is a demonstration app for the digital fieldlab seminar

Run

uv sync
uv run uvicorn app.main:app --reload

Health check: curl localhost:8000/health. The root / returns app_name and a short message that points to /health.

The response has five parts:

  • app_name: a unique instance name generated on startup (APP_NAME plus a random suffix, e.g. demo-app-GERTE1CV54), so you can see which instance answered behind a load balancer. It is also logged on startup.
  • status: error if any database component is error, otherwise ok.
  • components: localdb and remotedb. The inactive one is NA, the active one is ok or error.
  • records: per active database, the number of demo records (count) and the records themselves (items). The inactive one is NA.
  • config: the active settings. The password in REMOTE_DB_URL is masked as ***.

With DB_MODE=0 both components are NA.

/health returns HTTP 200 when status is ok and HTTP 503 when it is error; the body is the same in both cases. Kubernetes uses it as readiness probe, so a pod without a working database gets no traffic. /livez always returns 200 while the process runs and is the liveness probe, so a database outage does not restart the pods.

On startup the app tries to reach the database DB_CONNECT_ATTEMPTS times, waiting DB_CONNECT_DELAY seconds and doubling the wait each time (at most 10 s). If that still fails, every /health call tries again, so the app recovers on its own when the database comes back.

Configuration

Variable Default Purpose
APP_NAME demo-app Prefix for the instance name. A random 10-character suffix is added on every startup, so each instance or pod gets a unique name
DB_MODE 1 0 = no database, 1 = local SQLite, 2 = remote PostgreSQL
LOCAL_DB_PATH local.db Path to the SQLite file (mode 1)
REMOTE_DB_URL - PostgreSQL connection string (mode 2, contains credentials, keep it out of code)
DB_CONNECT_ATTEMPTS 10 Connection attempts on startup before giving up (then /health keeps retrying)
DB_CONNECT_DELAY 1.0 First wait in seconds between attempts, doubled each time, at most 10 s

Copy .env.example to .env to set these values. The app does not load .env on its own, so run it with uv run --env-file .env uvicorn app.main:app --reload.

On startup each enabled database is created if needed and seeded with 3 demo records when empty.

Tests

Run from the demo-app folder, not from the parent fieldlab folder:

cd demo-app
uv run pytest

Docker

Build the image from the demo-app folder:

docker build -t demo-app:latest .

Run it with the local SQLite database (mode 1) and expose port 8000:

docker run --rm -p 8000:8000 -e DB_MODE=1 -e LOCAL_DB_PATH=/tmp/local.db demo-app:latest

Check it from another terminal: curl localhost:8000/health

For DB_MODE=2, pass the remote URL as an environment variable and keep it out of the image:

docker run --rm -p 8000:8000 -e DB_MODE=2 -e REMOTE_DB_URL="postgresql://user:pass@host:5432/db" demo-app:latest

The image is built and tested in Docker Desktop: /health returns 200 with the seeded records, and the console shows the startup logs.

To test load balancing, run several instances and check which one answers. Each instance gets its own random name on startup:

docker run --rm -d -p 8001:8000 demo-app:latest
docker run --rm -d -p 8002:8000 demo-app:latest

On Kubernetes, raise replicas in the Deployment; every pod reports its own app_name.

If docker is not available in your WSL distro, enable WSL integration in Docker Desktop, or build with the Windows client (docker.exe).

Deploy

  • Dockerfile builds the app image. It runs as non-root user 10001 and pins uv 0.11.15.
  • .dockerignore keeps .env, local databases, tests, k8s/ and .forgejo/ out of the image.
  • k8s/base/ is a Kustomize base with a ServiceAccount, ConfigMap, Deployment and a LoadBalancer Service. The placeholder REPLACE_IMAGE_NAME:REPLACE_IMAGE_TAG is filled in by the deploy workflow.
  • The default is DB_MODE=1 (SQLite in /tmp), with one replica. Each pod has its own database, and its own app_name.
  • For DB_MODE=2, store REMOTE_DB_URL in a Kubernetes Secret and inject it into the Deployment. Do not put it in the manifest.

CI/CD (Forgejo Actions)

.forgejo/workflows/ci-deploy.yml runs the tests and a manifest check, builds and pushes the image tagged with the commit SHA, and deploys to Fundament. A push to main deploys to development. Production needs a manual run with environment=production and confirm_production=yes.

Create these in Forgejo under Settings → Actions. Never put the values in Git:

Name Type Level
REGISTRY_USER, REGISTRY_TOKEN Secret Organisation team-flevoland
FUNDAMENT_API_KEY_DEV (and _PROD) Secret Repository
IMAGE_NAME (demo-app), FUNDAMENT_ORG_ID, FUNDAMENT_CLUSTER_ID_DEV, K8S_NAMESPACE_DEV (and _PROD) Variable Repository

The API key must be made for this pipeline only, with an expiry, by a user who is project admin of this project (not organisation admin). The kubeconfig holds no credential: the workflow creates it from the key with functl.

After the first deploy: kubectl get service demo-app -n <namespace> shows the EXTERNAL-IP. Open http://<EXTERNAL-IP>/ and /health. Create a DNS A record yourself if you want a hostname.

The workflow has not run on the Fundament runner yet. See the assumptions at the top of the file.