diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index a98b15e..e33899d 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 + + - name: Lint with Ruff + 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 + + - name: Run tests with coverage + run: pytest --cov=app --cov-fail-under=80 + build: name: Build Docker Image runs-on: ubuntu-latest @@ -40,3 +62,25 @@ 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: Build Docker image + run: | + OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') + docker build -t ghcr.io/${OWNER}/fastapi-gitops-starter:${{ github.sha }} -f docker/Dockerfile . + + - name: Tag and push release image + if: github.event_name == 'release' + run: | + OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') + VERSION=${{ github.event.release.tag_name }} + docker tag ghcr.io/${OWNER}/fastapi-gitops-starter:${{ github.sha }} ghcr.io/${OWNER}/fastapi-gitops-starter:${VERSION} + docker tag ghcr.io/${OWNER}/fastapi-gitops-starter:${{ github.sha }} ghcr.io/${OWNER}/fastapi-gitops-starter:latest + docker push ghcr.io/${OWNER}/fastapi-gitops-starter:${VERSION} + docker push ghcr.io/${OWNER}/fastapi-gitops-starter:latest diff --git a/.github/workflows/markdown2pdf.yml b/.github/workflows/markdown2pdf.yml index fcb4671..dd5035d 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 @@ -55,4 +55,4 @@ jobs: id: upload-readme-file with: name: 'README_WITH_LINKS.md' - path: README_WITH_LINKS.md \ No newline at end of file + path: README_WITH_LINKS.md diff --git a/.github/workflows/tests_md-urls.yml b/.github/workflows/tests_md-urls.yml index f61463f..402cf3e 100644 --- a/.github/workflows/tests_md-urls.yml +++ b/.github/workflows/tests_md-urls.yml @@ -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 \ No newline at end of file + exclude_patterns: http://IP:NODE_PORT,http://localhost,http://xxxxxxxxx.compute-1.amazonaws.com,http://IP,http://minikube.test,http://prometheus.monitoring:9090 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d337d10..8a37f3b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,3 +3,32 @@ repos: rev: v5.0.0 hooks: - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-added-large-files + args: ['--maxkb=500'] + - id: check-yaml + exclude: ^helm/ + + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + args: ['--profile', 'black'] + + - repo: https://github.com/PyCQA/bandit + rev: 1.7.8 + hooks: + - id: bandit + args: ['-r', '--skip', 'B101'] + + - repo: https://github.com/Yelp/detect-secrets + rev: v1.4.0 + hooks: + - id: detect-secrets + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.4 + hooks: + - id: ruff + args: ['--fix'] + - id: ruff-format diff --git a/README.md b/README.md index 79bb814..a289605 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` @@ -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? \ No newline at end of file +2. How does Horizontal Pod Autoscaling (HPA) work in Kubernetes? diff --git a/app/main.py b/app/main.py index ae8adc6..30abb95 100644 --- a/app/main.py +++ b/app/main.py @@ -49,5 +49,11 @@ 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/external-services-values/argo-cd-values.yaml b/external-services-values/argo-cd-values.yaml index 6453246..08af7d8 100644 --- a/external-services-values/argo-cd-values.yaml +++ b/external-services-values/argo-cd-values.yaml @@ -3,8 +3,8 @@ global: configs: secret: - argocdServerAdminPassword: "$2a$10$f4euqaB7AZX41WFALuxIEu7FakyF03ir.zh48m0oVRi9B7zttHztq" - dex.config: | + argocdServerAdminPassword: "$2a$10$f4euqaB7AZX41WFALuxIEu7FakyF03ir.zh48m0oVRi9B7zttHztq" #pragma: allowlist secret + dex.config: | #pragma: allowlist secret connectors: - type: github id: github diff --git a/external-services-values/monitoring-values.yaml b/external-services-values/monitoring-values.yaml index d161e49..d8339ff 100644 --- a/external-services-values/monitoring-values.yaml +++ b/external-services-values/monitoring-values.yaml @@ -2,7 +2,7 @@ grafana: enabled: true initChownData: enabled: false - adminPassword: password + adminPassword: password #pragma: allowlist secret grafana.ini: server: domain: naavre-dev.minikube.test diff --git a/helm/fastapi-gitops-starter/custom-values.yaml b/helm/fastapi-gitops-starter/custom-values.yaml new file mode 100644 index 0000000..8b9fad8 --- /dev/null +++ b/helm/fastapi-gitops-starter/custom-values.yaml @@ -0,0 +1,26 @@ +image: + repository: fastapi-gitops-starter + tag: latest + pullPolicy: Never + +autoscaling: + enabled: true + minReplicas: 1 + maxReplicas: 10 + targetCPUUtilizationPercentage: 10 + +resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 200m + memory: 256Mi + +ingress: + enabled: true + hosts: + - host: minikube.test + paths: + - path: /GitOps-Starter + pathType: Prefix diff --git a/helm/fastapi-gitops-starter/example-canary-with-analysis-values.yaml b/helm/fastapi-gitops-starter/example-canary-with-analysis-values.yaml index d5b2aba..a65968e 100644 --- a/helm/fastapi-gitops-starter/example-canary-with-analysis-values.yaml +++ b/helm/fastapi-gitops-starter/example-canary-with-analysis-values.yaml @@ -13,7 +13,7 @@ image: registry: createImagePullSecret: true - secretName: image-pull-secret + secretName: image-pull-secret #pragma: allowlist secret server: ghcr.io token: "" diff --git a/helm/fastapi-gitops-starter/example-values.yaml b/helm/fastapi-gitops-starter/example-values.yaml index 2a02efb..1b7fe00 100644 --- a/helm/fastapi-gitops-starter/example-values.yaml +++ b/helm/fastapi-gitops-starter/example-values.yaml @@ -28,5 +28,3 @@ autoscaling: minReplicas: 1 maxReplicas: 3 targetCPUUtilizationPercentage: 10 - - diff --git a/helm/fastapi-gitops-starter/secret-example-canary-values.yaml b/helm/fastapi-gitops-starter/secret-example-canary-values.yaml index 39aa040..3795b8a 100644 --- a/helm/fastapi-gitops-starter/secret-example-canary-values.yaml +++ b/helm/fastapi-gitops-starter/secret-example-canary-values.yaml @@ -13,9 +13,9 @@ image: registry: createImagePullSecret: true - secretName: image-pull-secret + secretName: image-pull-secret #pragma: allowlist secret server: ghcr.io - token: "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" + token: "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" #pragma: allowlist secret # Enable Argo Rollouts with Canary strategy and automated analysis rollout: diff --git a/helm/fastapi-gitops-starter/secret-example-values.yaml b/helm/fastapi-gitops-starter/secret-example-values.yaml index 68c2a0e..6899b75 100644 --- a/helm/fastapi-gitops-starter/secret-example-values.yaml +++ b/helm/fastapi-gitops-starter/secret-example-values.yaml @@ -9,9 +9,9 @@ image: registry: createImagePullSecret: true - secretName: image-pull-secret + secretName: image-pull-secret #pragma: allowlist secret server: ghcr.io - token: "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" + token: "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" #pragma: allowlist secret imagePullSecrets: - name: image-pull-secret @@ -34,5 +34,3 @@ autoscaling: minReplicas: 1 maxReplicas: 3 targetCPUUtilizationPercentage: 10 - - diff --git a/helm/fastapi-gitops-starter/values.yaml b/helm/fastapi-gitops-starter/values.yaml index 20c08be..c1f0fb6 100644 --- a/helm/fastapi-gitops-starter/values.yaml +++ b/helm/fastapi-gitops-starter/values.yaml @@ -12,7 +12,7 @@ image: registry: createImagePullSecret: true - secretName: image-pull-secret + secretName: image-pull-secret #pragma: allowlist secret server: ghcr.io token: "" diff --git a/tests/test_main.py b/tests/test_main.py index db89e2f..a004895 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=Test Item&description=A test item") + assert response.status_code == 200 + data = response.json() + assert data["id"] == 999 + assert data["name"] == "Test Item" + assert data["description"] == "A test item" + assert data["created"] is True