From e77541470ecbe935c910cd49467fce360385422b Mon Sep 17 00:00:00 2001 From: Ambra Mihu Date: Sat, 14 Mar 2026 20:24:41 +0100 Subject: [PATCH 1/4] Add amazing feature --- .github/workflows/markdown2pdf.yml | 2 +- .gitignore | 11 +++++++++++ README.md | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.github/workflows/markdown2pdf.yml b/.github/workflows/markdown2pdf.yml index fcb4671..4177b58 100644 --- a/.github/workflows/markdown2pdf.yml +++ b/.github/workflows/markdown2pdf.yml @@ -19,7 +19,7 @@ jobs: - name: Replace links run: | - cp README.md README_WITH_LINKS.md + cp README.md README_WITH_LINKS.md sed -i -e "s#\(^\!\[[^]]\+\](\)\(images/\)#\1$URL/\2#g" README_WITH_LINKS.md for file in sources/*; do sed -i -e "s#($file)#($URL/$file)#g" README_WITH_LINKS.md ; done diff --git a/.gitignore b/.gitignore index 09be0ef..c1db65f 100644 --- a/.gitignore +++ b/.gitignore @@ -205,3 +205,14 @@ cython_debug/ marimo/_static/ marimo/_lsp/ __marimo__/ + +minikube-darwin-arm64 +minikube-linux-arm64 +minikube-linux-amd64 +minikube-linux-s390x +minikube-linux-ppc64le +minikube-linux-riscv64 +minikube-linux-mips64le +minikube-linux-mips64 +minikube-linux-mips64le +minikube-linux-mips64le \ No newline at end of file diff --git a/README.md b/README.md index 79bb814..057f54d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ GitOps with FastAPI ***University of Amsterdam*** -# 1. Introduction +# 1. Introduction In this tutorial, we use GitOps practices with FastAPI, including CI/CD pipelines, code quality tools, and automated testing. @@ -35,7 +35,7 @@ In this tutorial, we use GitOps practices with FastAPI, including CI/CD pipeline -# 2. Tutorial +# 2. Tutorial The steps of this tutorial are as follows: - [Building REST APIs with FastAPI](#21-setting-up-the-project) @@ -60,17 +60,17 @@ Prerequisites: ``` * Set Up the Python Environmentt: - + ```bash # Create a virtual environment python -m venv venv - + # Activate the virtual environment # On Linux/MacOS: source venv/bin/activate # On Windows: venv\Scripts\activate - + # Install dependencies pip install -r requirements.txt ``` @@ -110,7 +110,7 @@ Prerequisites: ```bash # Check for issues ruff check app/ tests/ - + # Fix auto-fixable issues ruff check app/ tests/ --fix ``` @@ -120,7 +120,7 @@ Prerequisites: ```bash # Check formatting black --check app/ tests/ - + # Format code black app/ tests/ ``` @@ -134,7 +134,7 @@ Pre-commit hooks automatically run checks before each commit to ensure consisten ```bash # Install pre-commit pip install pre-commit - + # Install the git hooks pre-commit install ``` @@ -142,11 +142,11 @@ Pre-commit hooks automatically run checks before each commit to ensure consisten * Using Pre-commit: Pre-commit will now run automatically on `git commit`. You can also run it manually: - + ```bash # Run on all files pre-commit run --all-files - + # Run on staged files pre-commit run ``` @@ -200,14 +200,14 @@ This repository includes a Helm chart for deploying the application to Kubernete - Kubernetes 1.19+ - Helm 3.0+ -* Install the Helm Chart: +* Install the Helm Chart: ```bash helm install my-release ./helm/fastapi-gitops-starter ``` -* Uninstall the Helm Chart: - +* Uninstall the Helm Chart: + ```bash helm uninstall my-release ``` @@ -239,7 +239,7 @@ including host and paths. * To make sure we do not commit secrets * To check code style - + ## 3.2 Add a New Endpoint 1. Open `app/main.py` From 5c1c490a0054082cdcfed35d3625cadd038f313e Mon Sep 17 00:00:00 2001 From: Ambra Mihu Date: Sat, 14 Mar 2026 20:44:53 +0100 Subject: [PATCH 2/4] added pre-commit hooks --- .pre-commit-config.yaml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d337d10..95b1d07 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,3 +3,34 @@ repos: rev: v5.0.0 hooks: - id: trailing-whitespace + - id: check-added-large-files + - id: check-yaml + args: ['--unsafe'] + exclude: ^helm/ + + # sort imports in Python files + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + + # check for security issues + - repo: https://github.com/PyCQA/bandit + rev: 1.7.10 + hooks: + - id: bandit + args: ["-c", "pyproject.toml"] + + # prevent committing secrets + - repo: https://github.com/Yelp/detect-secrets + rev: v1.5.0 + hooks: + - id: detect-secrets + + # code style + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.4 + hooks: + - id: ruff + args: [--fix] + - id: ruff-format \ No newline at end of file From 0a14ed13add3589c5c12e691be0eedbe37991cee Mon Sep 17 00:00:00 2001 From: Ambra Mihu Date: Sat, 14 Mar 2026 21:16:35 +0100 Subject: [PATCH 3/4] setup for ci/cd --- .github/workflows/ci-cd.yml | 48 +++++++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 8 +++---- app/main.py | 11 +++++++++ tests/test_main.py | 11 +++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index a98b15e..a43bdba 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -19,6 +19,17 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install Ruff + run: pip install ruff==0.8.4 + + - name: Run Ruff linter + run: ruff check app/ tests/ + test: name: Run Tests runs-on: ubuntu-latest @@ -28,6 +39,17 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install dependencies + run: pip install -r requirements.txt && pip install pytest pytest-cov httpx + + - name: Run tests with coverage + run: pytest --cov=app --cov-fail-under=80 --cov-report=term-missing + build: name: Build Docker Image runs-on: ubuntu-latest @@ -40,3 +62,29 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=raw,value=latest,enable=${{ github.event_name == 'release' }} + type=semver,pattern={{version}},enable=${{ github.event_name == 'release' }} + type=sha,prefix=sha-,enable=${{ github.event_name == 'push' }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + file: docker/Dockerfile + push: ${{ github.event_name == 'release' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 95b1d07..65db981 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,11 +15,11 @@ repos: - id: isort # check for security issues - - repo: https://github.com/PyCQA/bandit - rev: 1.7.10 + - repo: https://github.com/returntocorp/semgrep + rev: v1.68.0 hooks: - - id: bandit - args: ["-c", "pyproject.toml"] + - id: semgrep + args: ['--config', 'p/python', '--error'] # prevent committing secrets - repo: https://github.com/Yelp/detect-secrets diff --git a/app/main.py b/app/main.py index ae8adc6..34b477e 100644 --- a/app/main.py +++ b/app/main.py @@ -49,5 +49,16 @@ async def get_item(item_id: int): } +@app.post("/api/items") +async def create_item(name: str, description: str): + """Create a new item.""" + return { + "id": 999, + "name": name, + "description": description, + "created": True, + } + + if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/tests/test_main.py b/tests/test_main.py index db89e2f..fac8a2e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -41,3 +41,14 @@ def test_get_item(): assert data["id"] == 5 assert data["name"] == "Item 5" assert "item number 5" in data["description"] + + +def test_create_item(): + """Test the create item endpoint.""" + response = client.post("/api/items?name=Item 6&description=Item 6 description") + assert response.status_code == 200 + data = response.json() + assert data["id"] == 999 + assert data["name"] == "Item 6" + assert data["description"] == "Item 6 description" + assert data["created"] From cf8c772e3b5830f3631c838928115088f3e86bdd Mon Sep 17 00:00:00 2001 From: Ambra Mihu Date: Sat, 14 Mar 2026 22:44:25 +0100 Subject: [PATCH 4/4] custom-vals.yaml and hpa --- custom-values.yaml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 custom-values.yaml diff --git a/custom-values.yaml b/custom-values.yaml new file mode 100644 index 0000000..d01f278 --- /dev/null +++ b/custom-values.yaml @@ -0,0 +1,24 @@ +image: + repository: fastapi-gitops-starter + tag: "latest" + # pullPolicy: Never + pullPolicy: IfNotPresent + + +registry: + createImagePullSecret: false + +imagePullSecrets: [] + +ingress: + hosts: + - host: minikube.test + paths: + - path: / + pathType: Prefix + +autoscaling: + enabled: true + minReplicas: 1 + maxReplicas: 10 + targetCPUUtilizationPercentage: 10 \ No newline at end of file