|
| 1 | +--- |
| 2 | +title: "WasmEdge Integration" |
| 3 | +linkTitle: "WasmEdge Integration" |
| 4 | +weight: 3380 |
| 5 | +description: |
| 6 | +--- |
| 7 | + |
| 8 | +## WasmEdge Integration |
| 9 | + |
| 10 | +`WasmEdge` is a lightweight, high-performance, and extensible WebAssembly runtime for cloud native, edge, and decentralized applications. It powers serverless apps, embedded functions, microservices, smart contracts, and IoT devices. |
| 11 | + |
| 12 | +OpenFunction now supports building and running wasm functions with `WasmEdge` as the workload runtime. |
| 13 | + |
| 14 | +> You can find the WasmEdge Integration proposal [here](https://github.com/OpenFunction/OpenFunction/blob/main/docs/proposals/20230223-wasmedge-integration.md) |
| 15 | +
|
| 16 | +### Function Build |
| 17 | + |
| 18 | +When the value of the `spec.workloadRuntime` field is `wasmedge` or the annotations of the Function CR contains `module.wasm.image/variant: compat-smart`, |
| 19 | +`spec.build.shipwright.strategy` will be automatically generated based on the `ClusterBuildStrategy` named `wasmedge`. |
| 20 | + |
| 21 | +### Function Serving |
| 22 | + |
| 23 | +When the value of the `spec.workloadRuntime` field is `wasmedge` or the annotations of the Function CR contains `module.wasm.image/variant: compat-smart`: |
| 24 | +- If `spec.serving.annotations` does not contain `module.wasm.image/variant`, `module.wasm.image/variant: compat-smart` will be automatically generated into `spec.serving.annotations` |
| 25 | +- If `spec.serving.template.runtimeClassName` field is not set, the value of this field will be automatically set to `openfunction-crun` |
| 26 | + |
| 27 | +> If your kubernetes cluster is in a public cloud, such as `azure`, you can set `spec.serving.template.runtimeClassName` to override the default `runtimeClassName`. |
| 28 | +
|
| 29 | +## Build and run wasm functions |
| 30 | + |
| 31 | +> To setup `WasmEdge` workload runtime in kubernetes cluster and push images to a container registry, |
| 32 | +> please refer to the [prerequisites](../../getting-started/Quickstarts/prerequisites) section for more info. |
| 33 | +
|
| 34 | +1. Create a wasm function |
| 35 | + |
| 36 | +```shell |
| 37 | +cat <<EOF | kubectl apply -f - |
| 38 | +apiVersion: core.openfunction.io/v1beta1 |
| 39 | +kind: Function |
| 40 | +metadata: |
| 41 | + name: wasmedge-http-server |
| 42 | +spec: |
| 43 | + workloadRuntime: wasmedge |
| 44 | + image: openfunctiondev/wasmedge_http_server:0.1.0 |
| 45 | + imageCredentials: |
| 46 | + name: push-secret |
| 47 | + build: |
| 48 | + dockerfile: Dockerfile |
| 49 | + srcRepo: |
| 50 | + revision: main |
| 51 | + sourceSubPath: functions/knative/wasmedge/http-server |
| 52 | + url: https://github.com/OpenFunction/samples |
| 53 | + port: 8080 |
| 54 | + route: |
| 55 | + rules: |
| 56 | + - matches: |
| 57 | + - path: |
| 58 | + type: PathPrefix |
| 59 | + value: /echo |
| 60 | + serving: |
| 61 | + runtime: knative |
| 62 | + scaleOptions: |
| 63 | + minReplicas: 0 |
| 64 | + template: |
| 65 | + containers: |
| 66 | + - command: |
| 67 | + - /wasmedge_hyper_server.wasm |
| 68 | + imagePullPolicy: IfNotPresent |
| 69 | + livenessProbe: |
| 70 | + initialDelaySeconds: 3 |
| 71 | + periodSeconds: 30 |
| 72 | + tcpSocket: |
| 73 | + port: 8080 |
| 74 | + name: function |
| 75 | +EOF |
| 76 | +``` |
| 77 | + |
| 78 | +2. Check the wasm function status |
| 79 | + |
| 80 | +```shell |
| 81 | +kubectl get functions.core.openfunction.io -w |
| 82 | +NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE |
| 83 | +wasmedge-http-server Succeeded Running builder-4p2qq serving-lrd8c http://wasmedge-http-server.default.svc.cluster.local/echo 12m |
| 84 | + ``` |
| 85 | + |
| 86 | +3. Access the wasm function |
| 87 | + |
| 88 | +Once the `BUILDSTATE` becomes `Succeeded` and the `SERVINGSTATE` becomes `Running`, you can access this function through the address in the `ADDRESS` field: |
| 89 | + |
| 90 | +```shell |
| 91 | +kubectl run curl --image=radial/busyboxplus:curl -i --tty |
| 92 | +curl http://wasmedge-http-server.default.svc.cluster.local/echo -X POST -d "WasmEdge" |
| 93 | +WasmEdge |
| 94 | +``` |
0 commit comments