Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
feat: add knative serving and functions (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
zalsader authored Dec 20, 2023
1 parent 1439b5a commit 2c9eb3b
Show file tree
Hide file tree
Showing 27 changed files with 5,082 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ terraform.rc

# Generated tf.json
tf.json

# Generated from kubeone
koor-demo-*-kubeconfig
koor-demo-*.tar.gz
6 changes: 3 additions & 3 deletions ingress/dashboard-ingress-https.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: koor-dashboard-demo-ceph
namespace: koor-operator # namespace:cluster
namespace: rook-ceph # namespace:cluster
annotations:
cert-manager.io/issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/server-snippet: |
proxy_ssl_verify off;
# nginx.ingress.kubernetes.io/server-snippet: |
# proxy_ssl_verify off;
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
ingressClassName: "nginx"
Expand Down
4 changes: 2 additions & 2 deletions ingress/dashboard_ingress_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: koor-dashboard-demo
namespace: koor-operator # namespace:cluster
namespace: rook-ceph # namespace:cluster
annotations:
cert-manager.io/issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/use-regex: "true"
Expand Down Expand Up @@ -175,7 +175,7 @@ apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: koor-dashboard-demo-ceph
namespace: koor-operator # namespace:cluster
namespace: rook-ceph # namespace:cluster
annotations:
cert-manager.io/issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
Expand Down
2 changes: 1 addition & 1 deletion ingress/data-control-center.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: koor-dashboard-demo
namespace: koor-operator # namespace:cluster
namespace: rook-ceph # namespace:cluster
annotations:
cert-manager.io/issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/use-regex: "true"
Expand Down
89 changes: 89 additions & 0 deletions knative/README.md
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
```
5 changes: 5 additions & 0 deletions knative/consumer/.funcignore
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.
5 changes: 5 additions & 0 deletions knative/consumer/.gitignore
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
8 changes: 8 additions & 0 deletions knative/consumer/README.md
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
16 changes: 16 additions & 0 deletions knative/consumer/func.yaml
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
3 changes: 3 additions & 0 deletions knative/consumer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module function

go 1.21
64 changes: 64 additions & 0 deletions knative/consumer/handle.go
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)
}
70 changes: 70 additions & 0 deletions knative/deploy/cronjob.yaml
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
Loading

0 comments on commit 2c9eb3b

Please sign in to comment.