Extending Workload Types in KubeVela

WARNINIG: you are now reading a platform builder/administrator oriented documentation.

In the following tutorial, you will learn how to add OpenFaaS Function a new workload type and expose it to users via Appfile.

Step 1: Create Workload Definition

To register OpenFaaS as a new workload type in KubeVela, the only thing needed is to create an OAM WorkloadDefinition object for it. A full example can be found in this openfaas.yaml. Several highlights are list below.

1. Describe The Workload Type

  1. ...
  2. annotations:
  3. definition.oam.dev/description: "OpenFaaS function"
  4. ...

A one line description of this workload type. It will be shown in helper commands such as $ vela workloads.

2. Register API Resource

  1. ...
  2. spec:
  3. definitionRef:
  4. name: functions.openfaas.com
  5. ...

This is how you register OpenFaaS Function’s API resource (functions.openfaas.com) as the workload type. KubeVela uses Kubernetes API resource discovery mechanism to manage all registered capabilities.

3. Configure Installation Dependency

  1. ...
  2. extension:
  3. install:
  4. helm:
  5. repo: openfaas
  6. name: openfaas
  7. namespace: openfaas
  8. url: https://openfaas.github.io/faas-netes/
  9. version: 6.1.2
  10. ...

The extension.install field is used by KubeVela to automatically install the dependency (if any) when the new workload type is added to KubeVela. The dependency is described by a Helm chart custom resource. We highly recommend you to configure this field since otherwise, users will have to install dependencies like OpenFaaS operator manually later to user your new workload type.

4. Define User Parameters

  1. ...
  2. template: |
  3. output: {
  4. apiVersion: "openfaas.com/v1"
  5. kind: "Function"
  6. spec: {
  7. handler: parameter.handler
  8. image: parameter.image
  9. name: context.name
  10. }
  11. }
  12. parameter: {
  13. image: string
  14. handler: string
  15. }

For a given capability, KubeVela leverages CUElang to define the parameters that the end users could configure in the Appfile. In nutshell, parameter.* expected to be filled by users, and context.name will be filled by KubeVela as the service name in Appfile.

In the upcoming release, we will publish a detailed guide about defining CUE templates in KubeVela. For now, the best samples to learn about this section is the built-in templates of KubeVela.

Note that OpenFaaS also requires a namespace and secret configured before first-time usage:

  1. # generate a random password
  2. $ PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)
  3. $ kubectl -n openfaas create secret generic basic-auth \
  4. --from-literal=basic-auth-user=admin \
  5. --from-literal=basic-auth-password="$PASSWORD"
  6. $ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

Step 2: Register New Workload Type to KubeVela

As long as the definition file is ready, you just need to apply it to Kubernetes.

  1. $ kubectl apply -f https://raw.githubusercontent.com/oam-dev/catalog/master/registry/openfaas.yaml

And the new workload type will immediately become available for developers to use in KubeVela. It may take some time to be available as the dependency(helm chart) need to install.

  1. $ vela workloads
  2. Successfully installed chart (openfaas) with release name (openfaas)
  3. "my-repo" has been added to your repositories
  4. Automatically discover capabilities successfully Add(1) Update(0) Delete(0)
  5. TYPE CATEGORY DESCRIPTION
  6. +openfaas workload OpenFaaS function workload
  7. NAME DESCRIPTION
  8. openfaas OpenFaaS function workload
  9. task One-off task to run a piece of code or script to completion
  10. webservice Long-running scalable service with stable endpoint to receive external traffic
  11. worker Long-running scalable backend worker without network endpoint

(Optional) Step 3: Deploy OpenFaaS Function via Appfile

Write an Appfile:

  1. $ cat << EOF > vela.yaml
  2. name: testapp
  3. services:
  4. nodeinfo:
  5. type: openfaas
  6. image: functions/nodeinfo
  7. handler: "node main.js"
  8. EOF

Deploy it:

  1. $ vela up

(Optional) Verify the function is deployed and running.

Then you could find functions have been created:

  1. $ kubectl get functions
  2. NAME AGE
  3. nodeinfo 33s

Port-forward the OpenFaaS Gateway:

  1. kubectl port-forward -n openfaas svc/gateway 31112:8080

Now you can visit OpenFaas dashboard via http://127.0.0.1:31112 .

Here is the login credential. Username is admin, and the password is set in previous step via PASSWORD env.

  1. username: admin
  2. password: $(echo $PASSWORD)

Then you can see the dashboard as below. The nodeinfo function is shown as well:

alt