From a897606f947f0ddfe474a73eb926f91e70356230 Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Mon, 28 Apr 2025 11:00:15 +0200 Subject: [PATCH 01/17] Some fixes for the container and helm build/install --- .idea/misc.xml | 2 +- .idea/mrmat-python-api-fastapi.iml | 2 +- Makefile | 8 ++++++-- README.md | 2 +- var/container/Dockerfile | 5 +++-- var/helm/values.yaml | 2 +- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index be83902..aaa3def 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/mrmat-python-api-fastapi.iml b/.idea/mrmat-python-api-fastapi.iml index 24ec1fd..674ba84 100644 --- a/.idea/mrmat-python-api-fastapi.iml +++ b/.idea/mrmat-python-api-fastapi.iml @@ -7,7 +7,7 @@ - + diff --git a/Makefile b/Makefile index 7578cfa..086b525 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,12 @@ GIT_SHA := $(shell git rev-parse --short HEAD) VERSION ?= 0.0.0-dev0.${GIT_SHA} PYTHON_VERSION := $(shell echo "${VERSION}" | sed -e 's/-dev0\./-dev0+/') +WHEEL_VERSION := $(shell echo "${VERSION}" | sed -e 's/-dev0\./.dev0+/') + +ROOT_PATH := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) PYTHON_SOURCES := $(shell find src/mrmat_python_api_fastapi -name '*.py') -PYTHON_TARGET := dist/mrmat_python_api_fastapi-${PYTHON_VERSION}-py3-none-any.whl +PYTHON_TARGET := dist/mrmat_python_api_fastapi-${WHEEL_VERSION}-py3-none-any.whl CONTAINER_SOURCES := $(shell find var/container) HELM_SOURCES := $(shell find var/helm) HELM_TARGET := dist/mrmat-python-api-fastapi-$(VERSION).tgz @@ -31,7 +34,8 @@ container: $(PYTHON_TARGET) $(CONTAINER_SOURCES) -f var/container/Dockerfile \ -t localhost:5001/mrmat-python-api-fastapi:$(VERSION) \ --build-arg MRMAT_VERSION=$(VERSION) \ - . + --build-arg WHEEL=$(PYTHON_TARGET) \ + $(ROOT_PATH) docker push localhost:5001/mrmat-python-api-fastapi:$(VERSION) helm-install: $(HELM_TARGET) diff --git a/README.md b/README.md index 2e615c5..99e2157 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,5 @@ [![Build](https://github.com/MrMatOrg/mrmat-python-api-fastapi/actions/workflows/build.yml/badge.svg)](https://github.com/MrMatOrg/mrmat-python-api-fastapi/actions/workflows/build.yml) -Boilerplate (and playground) for a code-first Python FastAPI API, with all the bells and whistles we've come to expect +Boilerplate (and playground) for a code-first Python FastAPI API, with all the bells and whistles we've come to expect. diff --git a/var/container/Dockerfile b/var/container/Dockerfile index 6a64184..6e9ed49 100644 --- a/var/container/Dockerfile +++ b/var/container/Dockerfile @@ -1,7 +1,8 @@ FROM python:3.12-alpine AS build ARG MRMAT_VERSION="0.0.0.dev0" -ADD dist/mrmat_python_api_fastapi-*-py3-none-any.whl / -RUN pip install --user /mrmat_python_api_fastapi-*-py3-none-any.whl +ARG WHEEL="" +ADD "$WHEEL" / +RUN pip install --user /mrmat_python_api_fastapi-*.whl FROM python:3.12-alpine ARG MRMAT_VERSION="0.0.0.dev0" diff --git a/var/helm/values.yaml b/var/helm/values.yaml index 25a7a29..fbcb4e5 100644 --- a/var/helm/values.yaml +++ b/var/helm/values.yaml @@ -11,7 +11,7 @@ serviceAccount: pod: name: mrmat-python-api-fastapi replicas: 2 - repository: mrmat-registry:5000/mrmat-python-api-fastapi + repository: registry:5000/mrmat-python-api-fastapi imagePullPolicy: IfNotPresent port: 8000 From fb71728bffc1586d98b63050d69082029283f98a Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sat, 3 May 2025 13:50:50 +0200 Subject: [PATCH 02/17] Deployment and Prometheus Metrics --- Makefile | 1 + requirements.txt | 1 + src/mrmat_python_api_fastapi/app.py | 3 +++ var/container/Dockerfile | 4 ++-- var/helm/templates/deployment.yaml | 5 +++++ var/helm/templates/httproute.yaml | 2 +- var/helm/values.yaml | 4 ++-- 7 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 086b525..2defa13 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ helm-install: $(HELM_TARGET) mrmat-python-api-fastapi \ ${HELM_TARGET} \ --install \ + --force \ --create-namespace \ --namespace mrmat-python-api-fastapi diff --git a/requirements.txt b/requirements.txt index 7e66d3c..9044626 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ fastapi==0.115.11 # MIT sqlalchemy[asyncio]==2.0.40 # MIT uvicorn==0.34.0 # BSD 3-Clause pydantic==2.10.6 # MIT +prometheus-fastapi-instrumentator==7.1.0 # ISC diff --git a/src/mrmat_python_api_fastapi/app.py b/src/mrmat_python_api_fastapi/app.py index 0f00470..277d4a2 100644 --- a/src/mrmat_python_api_fastapi/app.py +++ b/src/mrmat_python_api_fastapi/app.py @@ -22,6 +22,8 @@ from fastapi import FastAPI +from prometheus_fastapi_instrumentator import Instrumentator + from mrmat_python_api_fastapi.apis.healthz import api_healthz from mrmat_python_api_fastapi.apis.greeting import api_greeting_v1, api_greeting_v2, api_greeting_v3 from mrmat_python_api_fastapi.apis.platform import api_platform_v1 @@ -33,6 +35,7 @@ app.include_router(api_greeting_v3, prefix='/api/greeting/v3', tags=['greeting']) app.include_router(api_platform_v1, prefix='/api/platform/v1', tags=['platform']) +Instrumentator().instrument(app).expose(app) @app.get('/') def index(): diff --git a/var/container/Dockerfile b/var/container/Dockerfile index 6e9ed49..f84e626 100644 --- a/var/container/Dockerfile +++ b/var/container/Dockerfile @@ -14,5 +14,5 @@ RUN chown -R 1000:1000 /home/app/.local USER app:app EXPOSE 8000 -#CMD ["/home/app/.local/bin/uvicorn", "--host", "0.0.0.0", "--port", "8000", "mrmat_python_api_fastapi.app:app"] -CMD ["/home/app/.local/bin/mrmat-python-api-fastapi"] +CMD ["/home/app/.local/bin/uvicorn", "--host", "0.0.0.0", "--port", "8000", "mrmat_python_api_fastapi.app:app"] +#CMD ["/home/app/.local/bin/mrmat-python-api-fastapi"] diff --git a/var/helm/templates/deployment.yaml b/var/helm/templates/deployment.yaml index c20f14c..6f9e461 100644 --- a/var/helm/templates/deployment.yaml +++ b/var/helm/templates/deployment.yaml @@ -19,6 +19,11 @@ spec: version: {{ .Chart.AppVersion }} app.kubernetes.io/name: mrmat-python-api-fastapi app.kubernetes.io/part-of: mrmat-python-api-fastapi + annotations: + prometheus.io/scrape: "true" + prometheus.io/scheme: http + prometheus.io/port: "8000" + prometheus.io/path: /metrics spec: serviceAccountName: {{ .Values.serviceAccount.name }} containers: diff --git a/var/helm/templates/httproute.yaml b/var/helm/templates/httproute.yaml index 7c76989..3cbd9d1 100644 --- a/var/helm/templates/httproute.yaml +++ b/var/helm/templates/httproute.yaml @@ -13,13 +13,13 @@ spec: {{- range .Values.httproute.hostnames }} - {{ . | quote }} {{- end }} - - mrmat-python-api-fastapi.local parentRefs: {{- range .Values.httproute.parents }} - group: gateway.networking.k8s.io kind: Gateway name: {{ .name }} namespace: {{ .namespace }} + sectionName: mrmat-python-api-fastapi {{- end }} rules: - backendRefs: diff --git a/var/helm/values.yaml b/var/helm/values.yaml index fbcb4e5..24f286c 100644 --- a/var/helm/values.yaml +++ b/var/helm/values.yaml @@ -23,9 +23,9 @@ httproute: enabled: true name: mrmat-python-api-fastapi-httproute hostnames: - - mrmat-python-api-fastapi.local + - mrmat-python-api-fastapi.covenant.local parents: - - name: istio-ingress + - name: edge-ingress namespace: edge ingress: From 422bddca81292bfb4a38e2275ed49fadd289813e Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sat, 3 May 2025 13:52:49 +0200 Subject: [PATCH 03/17] Added the helm-uninstall convenience target --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 2defa13..6d060b9 100644 --- a/Makefile +++ b/Makefile @@ -47,5 +47,8 @@ helm-install: $(HELM_TARGET) --create-namespace \ --namespace mrmat-python-api-fastapi +helm-uninstall: + helm delete -n mrmat-python-api-fastapi mrmat-python-api-fastapi + clean: rm -rf build dist From 669176c51d5099865e03995759fda5824b12d4b0 Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sun, 4 May 2025 10:02:36 +0200 Subject: [PATCH 04/17] We can take configuration from the environment --- src/mrmat_python_api_fastapi/config.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/mrmat_python_api_fastapi/config.py b/src/mrmat_python_api_fastapi/config.py index a7b2691..5698b1c 100644 --- a/src/mrmat_python_api_fastapi/config.py +++ b/src/mrmat_python_api_fastapi/config.py @@ -30,14 +30,18 @@ class Config: A class to deal with application configuration """ secret_key: str = secrets.token_urlsafe(16) - db_url: str = 'sqlite://' + db_url: str = 'sqlite:///' @staticmethod - def from_json_file(file: str | None = os.getenv('APP_CONFIG')): + def from_context(file: str | None = os.getenv('APP_CONFIG')): runtime_config = Config() if file and os.path.exists(file): with open(file, 'r', encoding='UTF-8') as c: file_config = json.load(c) - runtime_config.secret_key = file_config.get_owner('secret_key', secrets.token_urlsafe(16)) - runtime_config.db_url = file_config.get_owner('db_url', 'sqlite://') + runtime_config.secret_key = file_config.get('secret_key', secrets.token_urlsafe(16)) + runtime_config.db_url = file_config.get('db_url', 'sqlite:///') + if 'APP_CONFIG_SECRET_KEY' in os.environ: + runtime_config.secret_key = os.getenv('APP_CONFIG_SECRET_KEY', secrets.token_urlsafe(16)) + if 'APP_CONFIG_DB_URL' in os.environ: + runtime_config.db_url = os.getenv('APP_CONFIG_DB_URL', '') return runtime_config From fc755aa735ee06ba524a3d767672d220833fce40 Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sun, 4 May 2025 10:02:48 +0200 Subject: [PATCH 05/17] A little HTTP client --- var/client.http | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 var/client.http diff --git a/var/client.http b/var/client.http new file mode 100644 index 0000000..c7d7dd0 --- /dev/null +++ b/var/client.http @@ -0,0 +1,19 @@ +### GET Owners +GET http://localhost:8000/api/platform/v1/owners + +### GET resources +GET http://localhost:8000/api/platform/v1/resources + +### Create Owner +POST http://localhost:8000/api/platform/v1/owners +Content-Type: application/json + +{ + "name": "MrMat" +} + +> {% + const owner_uid = jsonPath(response.body, ".owner_uid") + %} + +### From 1036ef263df0e027d3b4e4ea66848e8d03929dd3 Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sun, 4 May 2025 10:02:57 +0200 Subject: [PATCH 06/17] Configuration of the local database --- .idea/dataSources.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 22e22e1..fbe0e20 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -11,5 +11,18 @@ $ProjectFileDir$ + + postgresql + true + Kube Eng :: PostgreSQL + org.postgresql.Driver + jdbc:postgresql://localhost:5432/postgres + + + + + + $ProjectFileDir$ + \ No newline at end of file From 9c24337bf83d6a88586c787f1aed998a8843119b Mon Sep 17 00:00:00 2001 From: Mathieu Imfeld Date: Sun, 4 May 2025 10:03:17 +0200 Subject: [PATCH 07/17] Pointing the run configuration to the local config file --- .idea/runConfigurations/run.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.idea/runConfigurations/run.xml b/.idea/runConfigurations/run.xml index 028572a..98d83e2 100644 --- a/.idea/runConfigurations/run.xml +++ b/.idea/runConfigurations/run.xml @@ -5,6 +5,9 @@