Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure an up to date OpenAPI schema is included in the repo #252

Merged
merged 17 commits into from
Feb 3, 2025
Merged
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
10 changes: 9 additions & 1 deletion .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.10"
Expand All @@ -50,3 +50,11 @@ jobs:
run: poetry run alembic check
env:
DB_URL: postgresql+pg8000://postgres:password@localhost:5432/postgres
- name: Start the app
run: poetry run uvicorn test_observer.main:app --host 0.0.0.0 --port 30000 &
shell: bash
- name: Fetch OpenAPI schema
run: curl http://localhost:30000/openapi.json -o fetched_openapi.json
- name: Compare with schema in repository
run: diff fetched_openapi.json schemata/openapi.json

18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ Model Controller Cloud/Region Version SLA
test-observer-development juju-controller microk8s/localhost 3.1.2 unsupported 15:38:51+03:00

App Version Status Scale Charm Channel Rev Address Exposed Message
api active 1 test-observer-api latest/edge 15 10.152.183.182 no
api active 1 test-observer-api latest/edge 15 10.152.183.182 no
db 14.7 active 1 postgresql-k8s 14/stable 73 10.152.183.172 no Primary
frontend active 1 test-observer-frontend latest/edge 8 10.152.183.79 no
frontend active 1 test-observer-frontend latest/edge 8 10.152.183.79 no
ingress 25.3.0 active 1 nginx-ingress-integrator stable 59 10.152.183.103 no Ingress IP(s): 127.0.0.1, 127.0.0.1, Service IP(s): 10.152.183.72, 10.152.183.34

Unit Workload Agent Address Ports Message
api/0* active idle 10.1.131.142
api/0* active idle 10.1.131.142
db/0* active idle 10.1.131.132 Primary
frontend/0* active idle 10.1.131.169
frontend/0* active idle 10.1.131.169
ingress/0* active idle 10.1.131.167 Ingress IP(s): 127.0.0.1, 127.0.0.1, Service IP(s): 10.152.183.72, 10.152.183.34

Relation provider Requirer Interface Type Message
api:test-observer-rest-api frontend:test-observer-rest-api http regular
db:database api:database postgresql_client regular
db:database-peers db:database-peers postgresql_peers peer
db:restart db:restart rolling_op peer
ingress:nginx-route api:nginx-route nginx-route regular
api:test-observer-rest-api frontend:test-observer-rest-api http regular
db:database api:database postgresql_client regular
db:database-peers db:database-peers postgresql_peers peer
db:restart db:restart rolling_op peer
ingress:nginx-route api:nginx-route nginx-route regular
ingress:nginx-route frontend:nginx-route nginx-route regular
```

Expand Down
9 changes: 9 additions & 0 deletions backend/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: local
hooks:
- id: openapi-fetch-yaml
name: Fetch OpenAPI schema
entry: backend/scripts/fetch_openapi_schema.sh
language: script
files: 'backend/.*'
pass_filenames: false
2 changes: 2 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ RUN pip3 install poetry && \

COPY . .

HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl --fail http://localhost:30000/v1/version || exit 1
mz2 marked this conversation as resolved.
Show resolved Hide resolved

CMD [ "uvicorn", "test_observer.main:app", "--host", "0.0.0.0", "--port", "30000", "--reload" ]
1 change: 1 addition & 0 deletions backend/Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ RUN pip3 install poetry && \

COPY ./backend .

HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl --fail http://localhost:30000/v1/version || exit 1
CMD [ "uvicorn", "test_observer.main:app", "--host", "0.0.0.0", "--port", "30000" ]
8 changes: 8 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project supports [microk8s](https://microk8s.io/) development environment w
- Install [microk8s](https://microk8s.io/docs/getting-started) and setup permissions
- Install [Skaffold](https://skaffold.dev/docs/install/#standalone-binary)
- Install [Poetry](https://python-poetry.org/docs/#installation)
- Install [pre-commit](https://pre-commit.com) (best done using `sudo apt install pre-commit`, followed by `pre-commit install` in the `backend` directory)

### 2. Setup Skaffold and microk8s

Expand All @@ -25,6 +26,13 @@ While technically not required to run the code, it helps to have dependencies in

Linting is done using ruff, formatting using black and type checking using mypy. We have CI in place to make those checks, but it's probably a good idea to setup your editor to use these. Note that their settings are in `pyproject.toml`.

The project uses [pre-commit](https://pre-commit.com) to auto generate OpenAPI schema file. To set it up for your working copy of the repository:

```bash
sudo apt install pre-commit
pre-commit install # in the `backend` directory
```

### 4. Start the development environment

Assuming that your microk8s cluster is running, you can start the development environment by simply running `$ skaffold dev --no-prune=false --cache-artifacts=false`. This command will build the docker images and push them to your microk8s registry, then apply your k8s manifest to start the cluster and pull those images. Additionally, skaffold will watch for file changes and either sync them directly inside the running containers or rebuild and redeploy k8s cluster for you automatically.
Expand Down
1 change: 1 addition & 0 deletions backend/schemata/openapi.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions backend/scripts/fetch_openapi_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

poetry install >/dev/null 2>&1

echo "Starting a transient copy of test-observer-api to fetch the OpenAPI schema..."
trap 'kill $(jobs -p)' EXIT
poetry run uvicorn test_observer.main:app --host 0.0.0.0 --port 30000 &

for i in {1..5}; do
if curl --output /dev/null --silent --head --fail "http://localhost:30000/openapi.json"; then
curl --silent http://localhost:30000/openapi.json -o schemata/openapi.json
git add schemata/openapi.json
echo "OpenAPI schema fetched."
success=true
break
else
sleep 1
fi
done

if [ -z "$success" ]; then
echo "Failed to fetch /openapi.json after multiple attempts."
fi
Loading