This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add knative serving and functions (#14)
- Loading branch information
Showing
27 changed files
with
5,082 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,7 @@ terraform.rc | |
|
||
# Generated tf.json | ||
tf.json | ||
|
||
# Generated from kubeone | ||
koor-demo-*-kubeconfig | ||
koor-demo-*.tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Knative integration | ||
|
||
Idea: File buffer using producer and consumer | ||
Producer creates files with random sizes. Limited to a number of files. | ||
Consumer picks random file, calculates md5 and size then deletes the file, sends that to output. | ||
|
||
This could be intermediate files in your image processing pipeline or [find applications] | ||
|
||
## Install knative operator | ||
```bash | ||
kubectl apply -f deploy/operator.yaml | ||
kubectl apply -f deploy/serving.yaml | ||
kubectl patch service/kourier \ | ||
-n knative-serving \ | ||
--type merge -p '{"metadata": {"annotations": {"load-balancer.hetzner.cloud/name": "koor-demo-staging-kourier" }}}' | ||
|
||
``` | ||
|
||
## Install knative func | ||
```bash | ||
wget -O kn-func https://github.com/knative/func/releases/download/knative-v1.12.0/func_$(go env GOOS)_$(go env GOARCH) | ||
chmod +x kn-func | ||
sudo mv kn-func /usr/local/bin | ||
kn func version | ||
``` | ||
|
||
## Create pvc | ||
```bash | ||
kubectl apply -f deploy/pvc.yaml | ||
``` | ||
|
||
## Create kantive functions | ||
```bash | ||
kn func create -l go producer | ||
kn func create -l go consumer | ||
kn func create -l go list | ||
``` | ||
|
||
## Fill in code | ||
... | ||
|
||
## Enable PVC usage and configure the functions to use the pvc | ||
https://knative.dev/docs/serving/configuration/feature-flags/#kubernetes-persistentvolumeclaim-pvc | ||
|
||
```yaml | ||
features: | ||
kubernetes.podspec-persistent-volume-claim: "enabled" | ||
kubernetes.podspec-persistent-volume-write: "enabled" | ||
kubernetes.podspec-securitycontext: "enabled" | ||
``` | ||
## Add pvc to funcs | ||
```bash | ||
kn func config volumes add | ||
``` | ||
|
||
or add this to `func.yaml` | ||
```bash | ||
run: | ||
volumes: | ||
- presistentVolumeClaim: | ||
claimName: knative-pc-cephfs | ||
path: /files | ||
``` | ||
|
||
## Build and push kantive function | ||
```bash | ||
cd producer | ||
kn func build --registry docker.io/<your_username> | ||
kn func deploy | ||
|
||
# this is to fix permission issues | ||
kubectl patch services.serving/producer --type merge \ | ||
-p '{"spec": {"template": {"spec": {"securityContext": {"fsGroup":1000}}}}}' | ||
``` | ||
do the same for consumer. | ||
|
||
## Invoke function | ||
```console | ||
$ kn func invoke | ||
TODO result | ||
``` | ||
|
||
## Undeploy function | ||
```console | ||
$ kn func delete consumer | ||
Removing Knative Service: consumer | ||
Removing Knative Service 'consumer' and all dependent resources | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
# Use the .funcignore file to exclude files which should not be | ||
# tracked in the image build. To instruct the system not to track | ||
# files in the image build, add the regex pattern or file information | ||
# to this file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
# Functions use the .func directory for local runtime data which should | ||
# generally not be tracked in source control. To instruct the system to track | ||
# .func in source control, comment the following line (prefix it with '# '). | ||
/.func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Consumer function | ||
When called, this function: | ||
1. Reads the files in the pvc | ||
2. Chooses a random file | ||
3. Reads the file | ||
4. Calculates md5 | ||
5. Deletes the file | ||
6. Outputs the file name, size and md5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
specVersion: 0.35.0 | ||
name: consumer | ||
runtime: go | ||
registry: docker.io/zuhairkoor | ||
image: docker.io/zuhairkoor/consumer:latest | ||
imageDigest: sha256:d671445b1eb0feba973cbdd5ddb3f186789ddf96ab675670070c809266c991ac | ||
created: 2023-12-13T00:39:05.888786906-05:00 | ||
build: | ||
builder: pack | ||
run: | ||
volumes: | ||
- presistentVolumeClaim: | ||
claimName: knative-pc-cephfs | ||
path: /files | ||
deploy: | ||
namespace: default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module function | ||
|
||
go 1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package function | ||
|
||
import ( | ||
"context" | ||
"crypto/md5" | ||
"fmt" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
const FILES_DIR = "/files" | ||
|
||
// Handle an HTTP Request. | ||
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) { | ||
fmt.Println("Reading directory") | ||
files, err := os.ReadDir(FILES_DIR) | ||
if err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
fmt.Println(err) | ||
fmt.Fprint(res, "Error reading files directory") | ||
return | ||
} | ||
fmt.Printf("Found %d files\n", len(files)) | ||
|
||
if len(files) == 0 { | ||
msg := "No files to consume" | ||
fmt.Println(msg) | ||
fmt.Fprint(res, msg) | ||
return | ||
} | ||
|
||
// Choose a random file | ||
fileEntry := files[rand.Intn(len(files))] | ||
fileName := fileEntry.Name() | ||
filePath := FILES_DIR + "/" + fileName | ||
|
||
fmt.Printf("Reading file %s\n", fileName) | ||
contents, err := os.ReadFile(filePath) | ||
if err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
fmt.Println(err) | ||
fmt.Fprintf(res, "Error reading file %s", fileName) | ||
return | ||
} | ||
|
||
fileSize := len(contents) | ||
fmt.Printf("Read %d bytes\n", fileSize) | ||
|
||
md5sum := md5.Sum(contents) | ||
fmt.Printf("The md5 sum is %x\n", md5sum) | ||
|
||
fmt.Printf("Removing file %s\n", fileName) | ||
err = os.Remove(filePath) | ||
if err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
fmt.Println(err) | ||
fmt.Fprintf(res, "Error removing file %s", fileName) | ||
return | ||
} | ||
fmt.Println("File removed") | ||
|
||
fmt.Fprintf(res, "Consumed file %s \nFile size is %d \nThe md5 sum is %x", fileName, fileSize, md5sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: busybot | ||
spec: | ||
schedule: "* * * * *" | ||
successfulJobsHistoryLimit: 10 | ||
failedJobsHistoryLimit: 5 | ||
concurrencyPolicy: Replace | ||
jobTemplate: | ||
metadata: | ||
labels: | ||
app: busybot | ||
cron: busybot | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: busybot-cont | ||
image: alpine:3.18.4 | ||
imagePullPolicy: IfNotPresent | ||
env: | ||
- name: STORE | ||
value: /store | ||
- name: COUNT | ||
value: "5" | ||
- name: DEL_COUNT | ||
value: "2" | ||
- name: SIZE | ||
value: 50M | ||
volumeMounts: | ||
- name: knative-pc-cephfs | ||
mountPath: /store | ||
command: | ||
- /bin/sh | ||
- -c | ||
- | | ||
echo Reading directory | ||
echo "Number of files: $(ls $STORE | wc -l)" | ||
# Read $COUNT random files from store and calculate md5 | ||
ls $STORE | shuf -n $COUNT | while read file | ||
do | ||
md5sum $STORE/$file | ||
done | ||
# Write $COUNT random files from store | ||
shuf -n $COUNT -i 0-99 | while read file | ||
do | ||
dd if=/dev/urandom of=/tmp/file bs=$SIZE count=1 | ||
md5=$(md5sum /tmp/file | awk '{ print $1 }') | ||
filename=$(printf "%02d" $file) | ||
echo Writing $filename | ||
rm -f $STORE/$filename-* | ||
mv /tmp/file $STORE/$filename-$md5 | ||
done | ||
# Delete $DEL_COUNT random files from store | ||
ls $STORE | shuf -n $DEL_COUNT | while read file | ||
do | ||
echo Deleting $file | ||
rm $STORE/$file | ||
done | ||
restartPolicy: Never | ||
volumes: | ||
- name: knative-pc-cephfs | ||
persistentVolumeClaim: | ||
claimName: knative-pc-cephfs | ||
readOnly: false |
Oops, something went wrong.