Wasm Functions

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.

OpenFunction now supports building and running wasm functions with WasmEdge as the workload runtime.

You can find the WasmEdge Integration proposal here

Wasm container images

The wasm image containing the wasm binary is a special container image without the OS layer. An special annotation module.wasm.image/variant: compat-smart should be added to this wasm container image for a wasm runtime like WasmEdge to recognize it. This is handled automatically in OpenFunction and users only need to specify the workloadRuntime as wasmedge.

The build phase of the wasm container images

If function.spec.workloadRuntime is set to wasmedge or the function’s annotation contains module.wasm.image/variant: compat-smart, function.spec.build.shipwright.strategy will be automatically generated based on the ClusterBuildStrategy named wasmedge in order to build a wasm container image with the module.wasm.image/variant: compat-smart annotation.

The serving phase of the wasm container images

When function.spec.workloadRuntime is set to wasmedge or the function’s annotation contains module.wasm.image/variant: compat-smart:

  • If function.spec.serving.annotations does not contain module.wasm.image/variant, module.wasm.image/variant: compat-smart will be automatically added to function.spec.serving.annotations.
  • If function.spec.serving.template.runtimeClassName is not set, this runtimeClassName will be automatically set to the default openfunction-crun

If your kubernetes cluster is in a public cloud like Azure, you can set spec.serving.template.runtimeClassName manually to override the default runtimeClassName.

Build and run wasm functions

To setup WasmEdge workload runtime in kubernetes cluster and push images to a container registry, please refer to the prerequisites section for more info.

You can find more info about this sample Function here.

  1. Create a wasm function
  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: core.openfunction.io/v1beta2
  3. kind: Function
  4. metadata:
  5. name: wasmedge-http-server
  6. spec:
  7. workloadRuntime: wasmedge
  8. image: openfunctiondev/wasmedge_http_server:0.1.0
  9. imageCredentials:
  10. name: push-secret
  11. build:
  12. dockerfile: Dockerfile
  13. srcRepo:
  14. revision: main
  15. sourceSubPath: functions/knative/wasmedge/http-server
  16. url: https://github.com/OpenFunction/samples
  17. serving:
  18. scaleOptions:
  19. minReplicas: 0
  20. template:
  21. containers:
  22. - command:
  23. - /wasmedge_hyper_server.wasm
  24. imagePullPolicy: IfNotPresent
  25. livenessProbe:
  26. initialDelaySeconds: 3
  27. periodSeconds: 30
  28. tcpSocket:
  29. port: 8080
  30. name: function
  31. triggers:
  32. http:
  33. port: 8080
  34. route:
  35. rules:
  36. - matches:
  37. - path:
  38. type: PathPrefix
  39. value: /echo
  40. EOF
  1. Check the wasm function status
  1. kubectl get functions.core.openfunction.io -w
  2. NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE
  3. wasmedge-http-server Succeeded Running builder-4p2qq serving-lrd8c http://wasmedge-http-server.default.svc.cluster.local/echo 12m
  1. Access the wasm function

Once the BUILDSTATE becomes Succeeded and the SERVINGSTATE becomes Running, you can access this function through the address in the ADDRESS field:

  1. kubectl run curl --image=radial/busyboxplus:curl -i --tty
  2. curl http://wasmedge-http-server.default.svc.cluster.local/echo -X POST -d "WasmEdge"
  3. WasmEdge