Skip to content
Open
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
21 changes: 15 additions & 6 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ on:
- '**'

jobs:
lint:
name: Lint Code
ci-pipeline:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with: {python-version: '3.11'}
- name: Install deps
run: pip install -r requirements.txt
- name: Lint
run: ruff check .
- name: Test & Coverage
run: |
pytest --cov=app --cov-fail-under=80 # Step 3.3: Fail if <80% [cite: 1518]
- name: Build Docker
if: success()
run: docker build -t fastapi-gitops .

test:
name: Run Tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/markdown2pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -55,4 +55,4 @@ jobs:
id: upload-readme-file
with:
name: 'README_WITH_LINKS.md'
path: README_WITH_LINKS.md
path: README_WITH_LINKS.md
2 changes: 1 addition & 1 deletion .github/workflows/tests_md-urls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
with:
file_types: .md,yaml,json
retry_count: 3
exclude_patterns: http://IP:NODE_PORT,http://localhost,http://xxxxxxxxx.compute-1.amazonaws.com,http://IP,http://minikube.test,http://prometheus.monitoring:9090
exclude_patterns: http://IP:NODE_PORT,http://localhost,http://xxxxxxxxx.compute-1.amazonaws.com,http://IP,http://minikube.test,http://prometheus.monitoring:9090
30 changes: 27 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: check-yaml
exclude: ^helm/
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: ruff
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
args: ["-r", "app"]
exclude: ^tests/
additional_dependencies: ['pbr']
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
exclude: ^helm/|^external-services-values/
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand All @@ -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
```
Expand Down Expand Up @@ -110,7 +110,7 @@ Prerequisites:
```bash
# Check for issues
ruff check app/ tests/

# Fix auto-fixable issues
ruff check app/ tests/ --fix
```
Expand All @@ -120,7 +120,7 @@ Prerequisites:
```bash
# Check formatting
black --check app/ tests/

# Format code
black app/ tests/
```
Expand All @@ -134,19 +134,19 @@ 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
```

* 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
```
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -287,4 +287,4 @@ kubectl get hpa -n default -w
## 3.5 Questions

1. The auto-scaling did not work as expected. What could be the possible reasons?
2. How does Horizontal Pod Autoscaling (HPA) work in Kubernetes?
2. How does Horizontal Pod Autoscaling (HPA) work in Kubernetes?
13 changes: 12 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
uvicorn.run(app, host="0.0.0.0", port=8000) # nosec
2 changes: 0 additions & 2 deletions helm/fastapi-gitops-starter/example-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ autoscaling:
minReplicas: 1
maxReplicas: 3
targetCPUUtilizationPercentage: 10


2 changes: 0 additions & 2 deletions helm/fastapi-gitops-starter/secret-example-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,3 @@ autoscaling:
minReplicas: 1
maxReplicas: 3
targetCPUUtilizationPercentage: 10


12 changes: 6 additions & 6 deletions helm/fastapi-gitops-starter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
replicaCount: 1

image:
repository: ghcr.io/qcdis/fastapi-gitops-starter
repository: fastapi-gitops-starter
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: "v0.4"
tag: "latest"

registry:
createImagePullSecret: true
createImagePullSecret: false
secretName: image-pull-secret
server: ghcr.io
token: ""
Expand Down Expand Up @@ -96,10 +96,10 @@ readinessProbe:
failureThreshold: 3

autoscaling:
enabled: false
minReplicas: 2
enabled: true
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 80
targetCPUUtilizationPercentage: 10
targetMemoryUtilizationPercentage: 80

# Additional volumes on the output Deployment definition.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ def test_get_item():
assert data["id"] == 5
assert data["name"] == "Item 5"
assert "item number 5" in data["description"]

def test_create_item(client):
response = client.post("/api/items?name=FastAPI&description=Speedrun")
assert response.status_code == 200
assert response.json()["name"] == "FastAPI"