extend kubevela: add openfaas workload

This commit is contained in:
Hongchao Deng
2020-11-13 16:14:33 -08:00
parent 48052e59ee
commit 6486b7878f
6 changed files with 137 additions and 63 deletions
+6 -5
View File
@@ -59,11 +59,12 @@ type Capability struct {
}
type Chart struct {
Repo string `json:"repo"`
URL string `json:"url"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Version string `json:"version"`
Repo string `json:"repo"`
URL string `json:"url"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Version string `json:"version"`
Values map[string]interface{} `json:"values"`
}
type Installation struct {
+1 -1
View File
@@ -21,8 +21,8 @@
- [Configuring data/env in Application](/en/developers/config-app.md)
- [Alternative Commands](/en/developers/alternative-cmd.md)
- Extending KubeVela
- [Add Workload Type](/en/platform-engineers/workload-type.md)
- [Add Trait](/en/platform-engineers/trait.md)
<!-- - [Add Workload Type](/en/platform-engineers/workload-type.md) -->
<!-- - [Add Cloud Services](/en/platform-engineers/cloud-resource.md) -->
- Roadmap
+24 -56
View File
@@ -2,68 +2,45 @@
In the following tutorial, you will learn how to add a new trait and expose it to users via Appfile.
## Add A New Trait
### Step 1: Install KubeWatch via Cap Center
Prerequisites:
- [helm v3](https://helm.sh/docs/intro/install/)
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
### Step 1: Install KubeWatch
Add cap center that contains KubeWatch:
```bash
$ helm repo add vela-demo https://wonderflow.info/kubewatch/archives/
$ helm install kubewatch vela-demo/kubewatch --version 0.1.0
$ vela cap center config my-center https://github.com/oam-dev/catalog/tree/master/registry
successfully sync 2/2 from my-center remote center
Successfully configured capability center my-center and sync from remote
$ vela cap center sync my-center
successfully sync 2/2 from my-center remote center
sync finished
```
### Step 2: Add Trait Definition with CUE template
Install KubeWatch:
```bash
$ cat << EOF | kubectl apply -f -
apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
name: kubewatch
annotations:
definition.oam.dev/description: "Add a watch for resource"
spec:
appliesToWorkloads:
- "*"
workloadRefPath: spec.workloadRef
definitionRef:
name: kubewatches.labs.bitnami.com
extension:
template: |
output: {
apiVersion: "labs.bitnami.com/v1alpha1"
kind: "KubeWatch"
spec: handler: webhook: url: parameter.webhook
}
parameter: {
webhook: string
}
EOF
$ vela cap install my-center/kubewatch
Installing trait capability kubewatch
...
Successfully installed chart (kubewatch) with release name (kubewatch)
Successfully installed capability kubewatch from my-center
```
That's it! Once you have applied the definition file the feature will be automatically registered in Vela Server and exposed to users.
### Step 2: Verify Kubewatch Trait Added
### Step 3: Verify Kubewatch Trait Installed
```bash
$ vela traits
Synchronizing capabilities from cluster⌛ ...
Sync capabilities successfully ✅ Add(1) Update(0) Delete(0)
TYPE CATEGORY DESCRIPTION
+kubewatch trait Add a watch for resource
Listing trait capabilities ...
NAME DESCRIPTION APPLIES TO
kubewatch Add a watch for resource
Sync capabilities successfully ✅ (no changes)
TYPE CATEGORY DESCRIPTION
kubewatch trait Add a watch for resource
...
```
### Step 4: Adding Kubewatch Trait to The App
### Step 3: Adding Kubewatch Trait to The App
Write an Appfile:
@@ -84,18 +61,9 @@ Deploy it:
```bash
$ vela up
...
✅ App has been deployed 🚀🚀🚀
Port forward: vela port-forward testapp
SSH: vela exec testapp
Logging: vela logs testapp
App status: vela status testapp
Service status: vela status testapp --svc testsvc
```
#### Verify
You can now add `kubewatch` config to Appfile to attach the newly added kubewatch trait to the App:
Now add `kubewatch` config to Appfile:
```bash
$ cat << EOF >> vela.yaml
@@ -104,7 +72,7 @@ $ cat << EOF >> vela.yaml
EOF
```
Deploy it:
Update deployment:
```
$ vela up
+105
View File
@@ -0,0 +1,105 @@
# Extending Traits in KubeVela
In the following tutorial, you will learn how to add a new workload and expose it to users via Appfile.
## Add A New Workload
### Step 1: Install OpenFaaS via Cap Center
Add cap center that contains OpenFaaS:
```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
$ vela cap center sync my-center
successfully sync 2/2 from my-center remote center
sync finished
```
Create namespace and secret for OpenFaaS:
```bash
# generate a random password
$ PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)
$ kubectl -n openfaas create secret generic basic-auth \
--from-literal=basic-auth-user=admin \
--from-literal=basic-auth-password="$PASSWORD"
$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
```
Install OpenFaaS:
```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
```
### Step 2: Verify OpenFaaS Workload Added
```bash
$ vela workloads
Automatically discover capabilities successfully ✅ Add(0) Update(1) Delete(0)
TYPE CATEGORY DESCRIPTION
*openfaas workload OpenFaaS function workload
NAME DESCRIPTION
openfaas OpenFaaS function workload
task One-time task/job
webservice Long running service with network routes
worker Backend worker without ports exposed
```
### Step 3: Deploy OpenFaaS Workload via Appfile
Write an Appfile:
```bash
$ cat << EOF > vela.yaml
name: testapp
services:
nodeinfo:
type: openfaas
image: functions/nodeinfo
handler: "node main.js"
EOF
```
Deploy it:
```bash
$ vela up
```
Then you could find functions have been created:
```
$ kubectl get functions
NAME AGE
nodeinfo 33s
```
Port-forward the OpenFaaS Gateway:
```
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.
```
username: admin
password: $(echo $PASSWORD)
```
Then you can see the dashboard as below. The `nodeinfo` function is shown as well:
![Image of Kubewatch](../../resources/openfaas.jpg)
Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

+1 -1
View File
@@ -168,7 +168,7 @@ func GetSyncedCapabilities(repoName, addonName string) (types.Capability, error)
}
func InstallHelmChart(ioStreams cmdutil.IOStreams, c types.Chart) error {
return helm.Install(ioStreams, c.Repo, c.URL, c.Name, c.Version, c.Namespace, c.Name, nil)
return helm.Install(ioStreams, c.Repo, c.URL, c.Name, c.Version, c.Namespace, c.Name, c.Values)
}
func ListCapabilityCenters() ([]apis.CapabilityCenterMeta, error) {