Skip to content

Commit f268421

Browse files
New Python environment based on FastAPI framework (#388)
* feat: Add python-fastapi-env * Fix some issues with fastapi environment * Cosmetic changes to fastapi environment * Add python-fastapi-env support to github workflows * Upgrade upload-artifact version to v4 * Fix typos in python-fastapi tests * Fix python-fastapi CI failure * Fix python-fastapi tests and examples * Add sourcepkg example for python-fastapi environment * Add multifile example for python-fastapi environment * Add requestdata and statuscode examples for python-fastapi environment * Add guestbook example for python-fastapi environment * Remove redis and other packages from python-fastapi core environment --------- Signed-off-by: Md Soharab Ansari <[email protected]> Co-authored-by: Jacob Chen <[email protected]>
1 parent a4021e2 commit f268421

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+779
-6
lines changed

.github/workflows/environment.yaml

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
command -v fission && fission support dump
6161
- name: Archive fission dump
6262
if: ${{ failure() }}
63-
uses: actions/upload-artifact@v2
63+
uses: actions/upload-artifact@v4
6464
with:
6565
name: fission-dump
6666
path: fission-dump/*.zip
@@ -101,7 +101,7 @@ jobs:
101101
command -v fission && fission support dump
102102
- name: Archive fission dump
103103
if: ${{ failure() }}
104-
uses: actions/upload-artifact@v2
104+
uses: actions/upload-artifact@v4
105105
with:
106106
name: fission-dump
107107
path: fission-dump/*.zip
@@ -191,7 +191,7 @@ jobs:
191191
command -v fission && fission support dump
192192
- name: Archive fission dump
193193
if: ${{ failure() }}
194-
uses: actions/upload-artifact@v2
194+
uses: actions/upload-artifact@v4
195195
with:
196196
name: fission-dump
197197
path: fission-dump/*.zip
@@ -262,7 +262,7 @@ jobs:
262262
python:
263263
runs-on: ubuntu-latest
264264
needs: check
265-
if: contains( needs.check.outputs.packages, 'python' )
265+
if: contains( needs.check.outputs, 'python' )
266266
steps:
267267
- name: Checkout sources
268268
uses: actions/checkout@v2
@@ -312,7 +312,62 @@ jobs:
312312
command -v fission && fission support dump
313313
- name: Archive fission dump
314314
if: ${{ failure() }}
315-
uses: actions/upload-artifact@v2
315+
uses: actions/upload-artifact@v4
316+
with:
317+
name: fission-dump
318+
path: fission-dump/*.zip
319+
retention-days: 5
320+
python-fastapi:
321+
runs-on: ubuntu-latest
322+
needs: check
323+
if: contains( needs.check.outputs.packages, 'python-fastapi' )
324+
steps:
325+
- name: Checkout sources
326+
uses: actions/checkout@v2
327+
- uses: actions/setup-python@v2
328+
with:
329+
python-version: '3.11'
330+
- name: Unit test
331+
run: |
332+
pip3 install virtualenv
333+
sudo apt-get update -y && sudo apt-get install -y libev-dev
334+
pushd python-fastapi/
335+
export USERFUNCVOL=/tmp
336+
export RUNTIME_PORT=8882
337+
./tests/local_test.sh
338+
unset RUNTIME_PORT
339+
popd
340+
- name: Helm
341+
uses: Azure/setup-helm@5119fcb9089d432beecbf79bb2c7915207344b78 # v3.5
342+
with:
343+
version: v3.13.0
344+
- name: Kind Clutser
345+
uses: engineerd/[email protected]
346+
with:
347+
image: kindest/node:v1.25.16
348+
version: v0.23.0
349+
config: kind.yaml
350+
- name: Base Setup
351+
run: |
352+
make verify-kind-cluster
353+
make install-fission-cli
354+
make install-skaffold
355+
make create-crds
356+
- name: Fission and Test images
357+
run: |
358+
SKAFFOLD_PROFILE=python-fastapi make skaffold-run
359+
make python-fastapi-test-images
360+
make router-port-forward
361+
- name: python-fastapi-tests
362+
run: |
363+
./test_utils/run_test.sh ./python-fastapi/tests/test_python_fastapi_env.sh
364+
- name: Collect Fission Dump
365+
if: ${{ always() }}
366+
run: |
367+
command -v fission && fission support dump
368+
- name: Archive fission dump
369+
if: ${{ failure() }}
370+
uses: actions/upload-artifact@v4
316371
with:
317372
name: fission-dump
318373
path: fission-dump/*.zip

.github/workflows/filters/filters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ perl:
1818
- 'perl/**'
1919
python:
2020
- 'python/**'
21+
python-fastapi:
22+
- 'python-fastapi/**'
2123
ruby:
2224
- 'ruby/**'
2325
tensorflow-serving:

.github/workflows/filters/version_filter.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ perl:
1818
- 'perl/envconfig.json'
1919
python:
2020
- 'python/envconfig.json'
21+
python-fastapi:
22+
- 'python-fastapi/envconfig.json'
2123
ruby:
2224
- 'ruby/envconfig.json'
2325
tensorflow-serving:

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
FISSION_ENVS := nodejs-envs \
1818
go-envs \
1919
python-envs \
20+
python-fastapi-envs \
2021
perl-envs \
2122
jvm-envs \
2223
php7-envs \
@@ -86,5 +87,9 @@ python-test-images:
8687
@kind load docker-image python-env
8788
@kind load docker-image python-builder
8889

90+
python-fastapi-test-images:
91+
@kind load docker-image python-fastapi-env
92+
@kind load docker-image python-fastapi-builder
93+
8994
router-port-forward:
9095
@kubectl port-forward svc/router 8888:80 -nfission &

python-fastapi/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG PY_BASE_IMG=3.11-alpine
2+
3+
FROM python:${PY_BASE_IMG}
4+
5+
WORKDIR /app
6+
7+
RUN pip install --no-cache-dir --upgrade pip
8+
9+
COPY requirements.txt /app
10+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
11+
12+
COPY *.py /app
13+
14+
ENTRYPOINT ["python3"]
15+
CMD ["server.py"]

python-fastapi/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-include ../rules.mk
2+
3+
.PHONY: all
4+
all: python-fastapi-env-img
5+
6+
python-fastapi-env-img-buildargs := --build-arg PY_BASE_IMG=3.11-alpine
7+
8+
python-fastapi-env-img: Dockerfile

python-fastapi/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Fission: Python FastAPI Environment
2+
3+
This is the Python environment for Fission based on FastAPI framework.
4+
5+
It's a Docker image containing a Python 3.11 runtime, along with a
6+
dynamic loader. A few common dependencies are included in the
7+
requirements.txt file.
8+
9+
Looking for ready-to-run examples? See the [Python FastAPI examples directory](./examples).
10+
11+
## Customizing this image
12+
13+
To add package dependencies, edit requirements.txt to add what you
14+
need, and rebuild this image (instructions below).
15+
16+
You also may want to customize what's available to the function in its
17+
request context. You can do this by editing server.py (see the
18+
comment in that file about customizing request context).
19+
20+
## Rebuilding and pushing the image
21+
22+
You'll need access to a Docker registry to push the image: you can
23+
sign up for Docker hub at hub.docker.com, or use registries from
24+
gcr.io, quay.io, etc. Let's assume you're using a docker hub account
25+
called USER. Build and push the image to the the registry:
26+
27+
```
28+
docker build -t USER/python-fastapi-env --build-arg PY_BASE_IMG=3.11-alpine . && docker push USER/python-fastapi-env
29+
```
30+
31+
## Using the image in fission
32+
33+
You can add this customized image to fission with "fission env create":
34+
35+
```
36+
fission env create --name python --image USER/python-fastapi-env
37+
```
38+
39+
Or, if you already have an environment, you can update its image:
40+
41+
```
42+
fission env update --name python --image USER/python-fastapi-env
43+
```
44+
45+
After this, fission functions that have the env parameter set to the
46+
same environment name as this command will use this environment.
47+
48+
## Web Server Framework
49+
50+
Python environment build and start a ASGI server, to support high HTTP
51+
traffic. It provides Uvicorn server framework.

python-fastapi/builder/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ARG BUILDER_IMAGE=fission/builder
2+
ARG PY_BASE_IMG=3.11-alpine
3+
4+
FROM ${BUILDER_IMAGE}
5+
FROM python:${PY_BASE_IMG}
6+
7+
COPY --from=0 /builder /builder
8+
RUN apk add --update --no-cache python3-dev build-base gcc bash
9+
10+
ADD defaultBuildCmd /usr/local/bin/build
11+
12+
EXPOSE 8001

python-fastapi/builder/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-include ../../rules.mk
2+
3+
.PHONY: all
4+
all: python-fastapi-builder-img
5+
6+
python-fastapi-builder-img-buildargs := --build-arg PY_BASE_IMG=3.11-alpine
7+
8+
python-fastapi-builder-img: Dockerfile
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
set -euxo pipefail
3+
4+
if [ -f ${SRC_PKG}/requirements.txt ]
5+
then
6+
pip3 install -r ${SRC_PKG}/requirements.txt -t ${SRC_PKG}
7+
fi
8+
cp -r ${SRC_PKG} ${DEPLOY_PKG}

0 commit comments

Comments
 (0)