Merge pull request #606 from resouer/dev

Refactor workload type doc
This commit is contained in:
Hongchao Deng
2020-11-14 13:23:07 -08:00
committed by GitHub

View File

@@ -1,25 +1,81 @@
# Extending Traits in KubeVela
# Extending Workload Types in KubeVela
In the following tutorial, you will learn how to add a new workload and expose it to users via Appfile.
> WARNINIG: you are now reading a platform builder/administrator oriented documentation.
## Add A New Workload
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: Install OpenFaaS via Cap Center
## Step 1: Create Workload Definition
Add cap center that contains OpenFaaS:
To register OpenFaaS as a new workload type in KubeVela, the only thing needed is to create a OAM `WorkloadDefinition` object for it. A full example can be found in this [openfaas.yaml](https://github.com/oam-dev/catalog/blob/master/registry/openfaas.yaml). Several highlights are list below.
```bash
$ vela cap center config my-center https://github.com/oam-dev/catalog/tree/master/registry
$ vela cap center config my-center https://github.com/oam-dev/catalog/tree/openfaas/registry
successfully sync 2/2 from my-center remote center
Successfully configured capability center my-center and sync from remote
### 1. Describe The Workload Type
$ vela cap center sync my-center
successfully sync 2/2 from my-center remote center
sync finished
```yaml
...
annotations:
definition.oam.dev/description: "OpenFaaS function"
...
```
Create namespace and secret for OpenFaaS:
A one line description of this workload type. It will be shown in helper commands such as `$ vela workloads`.
### 2. Register API Resource
```yaml
...
spec:
definitionRef:
name: functions.openfaas.com
...
```
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
```yaml
...
extension:
install:
helm:
repo: openfaas
name: openfaas
namespace: openfaas
url: https://openfaas.github.io/faas-netes/
version: 6.1.2
...
```
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
```yaml
...
template: |
output: {
apiVersion: "openfaas.com/v1"
kind: "Function"
spec: {
handler: parameter.handler
image: parameter.image
name: context.name
}
}
parameter: {
image: string
handler: string
}
```
For a given capability, KubeVela leverages [CUElang](https://github.com/cuelang/cue/blob/master/doc/tutorial/kubernetes/README.md) 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](https://github.com/oam-dev/kubevela/tree/master/hack/vela-templates) of KubeVela.
Note that OpenFaaS also requires a namespace and secret configured before first-time usage:
<details>
```bash
# generate a random password
@@ -31,19 +87,17 @@ $ kubectl -n openfaas create secret generic basic-auth \
$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
```
</details>
Install OpenFaaS:
## Step 2: Register New Workload Type to KubeVela
As long as the definition file is ready, you just need to apply it to Kubernetes.
```bash
$ vela cap install my-center/openfaas
Installing workload capability openfaas
2020/11/13 15:50:07 [debug] creating 24 resource(s)
Successfully installed chart (openfaas) with release name (openfaas)
Successfully installed capability openfaas from my-center
$ kubectl apply -f openfaas.yaml
```
### Step 2: Verify OpenFaaS Workload Added
And the new workload type will immediately become available for developers to use in KubeVela.
```bash
$ vela workloads
@@ -57,7 +111,7 @@ webservice Long running service with network routes
worker Backend worker without ports exposed
```
### Step 3: Deploy OpenFaaS Workload via Appfile
## (Optional) Step 3: Deploy OpenFaaS Function via Appfile
Write an Appfile:
@@ -78,6 +132,10 @@ Deploy it:
$ vela up
```
(Optional) Verify the function is deployed and running.
<details>
Then you could find functions have been created:
```
@@ -102,4 +160,6 @@ password: $(echo $PASSWORD)
Then you can see the dashboard as below. The `nodeinfo` function is shown as well:
![Image of Kubewatch](../../resources/openfaas.jpg)
![alt](../../resources/openfaas.jpg)
</details>