Version: v1.3

Built-in Trait Type

This documentation will walk through the built-in traits.

The gateway trait exposes a component to public Internet via a valid domain.

  • all component types
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
httpSpecify the mapping relationship between the http path and the workload portmap[string]inttrue
classSpecify the class of ingress to usestringtruenginx
classInSpecSet ingress class in ‘.spec.ingressClassName’ instead of ‘kubernetes.io/ingress.class’ annotation.boolfalsefalse
domainSpecify the domain you want to exposestringtrue
  1. # vela-app.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: first-vela-app
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. port: 8000
  13. traits:
  14. - type: gateway
  15. properties:
  16. domain: testsvc.example.com
  17. http:
  18. "/": 8000

The scaler trait allows you to change the replicas for the component.

  • webservice
  • worker
  • task
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
replicasSpecify the number of workloadinttrue1
  1. # sample.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: website
  6. spec:
  7. components:
  8. - name: frontend # This is the component I want to deploy
  9. type: webservice
  10. properties:
  11. image: nginx
  12. traits:
  13. - type: scaler # Set the replica to the specified value
  14. properties:
  15. replicas: 5

autoscaler trait use K8s HPA to control the replica of component.

Note: autoscaler trait is hidden by default in VelaUX, you can use it in CLI.

  • All component based on deployments.apps
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
minSpecify the minimal number of replicas to which the autoscaler can scale downinttrue1
maxSpecify the maximum number of of replicas to which the autoscaler can scale upinttrue10
cpuUtilSpecify the average cpu utilization, for example, 50 means the CPU usage is 50%inttrue50
  1. # sample.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: website
  6. spec:
  7. components:
  8. - name: frontend # This is the component I want to deploy
  9. type: webservice
  10. properties:
  11. image: nginx
  12. traits:
  13. - type: cpuscaler # Automatically scale the component by CPU usage after deployed
  14. properties:
  15. min: 1
  16. max: 10
  17. cpuPercent: 60

The storage trait allows you to manage storages for the component.

storage can help us create and bind storages like pvc, emptyDir, secret, or configMap for our component. For secret and configMap type storage, we can also bind it to the env.

If you don’t want to create the storages automatically, you can set mountOnly to true.

  • All component based on deployments.apps
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
volumeModestringtrueFilesystem
mountPathstringtrue
mountOnlybooltruefalse
accessModes[…]true
volumeNamestringfalse
storageClassNamestringfalse
resourcesresourcesfalse
dataSourceRefdataSourceReffalse
dataSourcedataSourcefalse
selectorselectorfalse
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
mediumstringtrueempty
mountPathstringtrue
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
defaultModeinttrue420
items[]itemsfalse
mountPathstringtrue
mountToEnvmountToEnvfalse
mountOnlybooltruefalse
datamap[string]{nullboolstring
stringDatamap[string]{nullboolstring
readOnlybooltruefalse
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
defaultModeinttrue420
items[]itemsfalse
mountPathstringtrue
mountToEnvmountToEnvfalse
mountOnlybooltruefalse
datamap[string]{nullboolstring
readOnlybooltruefalse
  1. # sample.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: storage-app
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. ports:
  13. - port: 8000
  14. traits:
  15. - type: storage
  16. properties:
  17. # PVC type storage
  18. pvc:
  19. - name: test1
  20. mountPath: /test/mount/pvc
  21. # EmptyDir type storage
  22. emptyDir:
  23. - name: test1
  24. mountPath: /test/mount/emptydir
  25. # ConfigMap type storage
  26. configMap:
  27. - name: test1
  28. mountPath: /test/mount/cm
  29. # Mount ConfigMap to Env
  30. mountToEnv:
  31. envName: TEST_ENV
  32. configMapKey: key1
  33. data:
  34. key1: value1
  35. key2: value2
  36. # Secret type storage
  37. secret:
  38. - name: test1
  39. mountPath: /test/mount/secret
  40. # Mount Secret to Env
  41. mountToEnv:
  42. envName: TEST_SECRET
  43. secretKey: key1
  44. data:
  45. key1: dmFsdWUx
  46. key2: dmFsdWUy

labels trait allow us to mark labels on Pod for workload.

Note: labels trait are hidden by default in VelaUX, you can use them in CLI.

  • all component types
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
-map[string]stringtrue

They’re all string Key-Value pairs.

  1. # myapp.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: myapp
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. port: 8000
  13. traits:
  14. - type: labels
  15. properties:
  16. "release": "stable"

annotations trait allow us to mark annotations on Pod for workload.

Note: annotations trait are hidden by default in VelaUX, you can use them in CLI.

  • all component types
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
-map[string]stringtrue

They’re all string Key-Value pairs.

  1. # myapp.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: myapp
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. port: 8000
  13. traits:
  14. - type: annotations
  15. properties:
  16. "description": "web application"

Trait kustomize-patch will patch on the Kustomize component.

Note: To use kustomize trait, you must enable fluxcd addon first.

  • kustomize
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
patchesa list of StrategicMerge or JSON6902 patch to selected target[]patchestrue
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
patchInline patch string, in yaml stylestringtrue
targetSpecify the target the patch should be applied totargettrue
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringfalse
groupstringfalse
versionstringfalse
kindstringfalse
namespacestringfalse
annotationSelectorstringfalse
labelSelectorstringfalse
  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-patch
  12. properties:
  13. patches:
  14. - patch: |-
  15. apiVersion: v1
  16. kind: Pod
  17. metadata:
  18. name: not-used
  19. labels:
  20. app.kubernetes.io/part-of: test-app
  21. target:
  22. labelSelector: "app=podinfo"

In this example, the kustomize-patch will patch the content for all Pods with label app=podinfo.

You could use this trait in JSON6902 format to patch for the kustomize component.

  • kustomize
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
patchesJsonA list of JSON6902 patch.[]patchesJsontrue
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
patchpatchtrue
targettargettrue
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringfalse
groupstringfalse
versionstringfalse
kindstringfalse
namespacestringfalse
annotationSelectorstringfalse
labelSelectorstringfalse
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
pathstringtrue
opstringtrue
valuestringfalse
  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-json-patch
  12. properties:
  13. patchesJson:
  14. - target:
  15. version: v1
  16. kind: Deployment
  17. name: podinfo
  18. patch:
  19. - op: add
  20. path: /metadata/annotations/key
  21. value: value

kustomize-strategy-merge trait provide strategy merge patch for kustomize component.

  • kustomize
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
patchesStrategicMergea list of strategicmerge, defined as inline yaml objects.[]patchesStrategicMergetrue

+—————-+——————-+————————————————————————————+—————+————-+ | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT | +—————-+——————-+————————————————————————————+—————+————-+ | undefined | | map[string]{null|bool|string|bytes|{…}|[…]|number} | true | |

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-strategy-merge
  12. properties:
  13. patchesStrategicMerge:
  14. - apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: podinfo
  18. spec:
  19. template:
  20. spec:
  21. serviceAccount: custom-service-account

Service binding trait will bind data from Kubernetes Secret to the application container’s ENV.

  • webservice
  • worker
  • task
  • cron-task
NameDescriptionTypeRequiredDefault
envMappingsThe mapping of environment variables to secretmap[string]#KeySecrettrue
NameDescriptionTypeRequiredDefault
keyif key is empty, we will use envMappings key insteadstringfalse
secretKubernetes secret namestringtrue
  1. Prepare a Kubernetes Secret

The secret can be manually created, or generated by other component or external system.

For example, we have a secret db-conn-example whose data is as below:

  1. endpoint: https://xxx.com
  2. password: 123
  3. username: myname
  1. Bind the Secret into your component by service-binding trait

For example, we have a webservice component who needs to consume a database. The database connection string should be set to Pod environments: endpoint, username and DB_PASSWORD.

We can set the properties for envMappings as below. For each environment, secret represents the secret name, and key represents the key of the secret.

Here is the complete properties for the trait.

  1. traits:
  2. - type: service-binding
  3. properties:
  4. envMappings:
  5. DB_PASSWORD:
  6. secret: db-conn-example
  7. key: password
  8. endpoint:
  9. secret: db-conn-example
  10. key: endpoint
  11. username:
  12. secret: db-conn-example
  13. key: username

In particular, if the environment name, like endpoint, is same to the key of the secret, we can omit the key. So we can simplify the properties as below.

  1. traits:
  2. - type: service-binding
  3. properties:
  4. envMappings:
  5. DB_PASSWORD:
  6. secret: db-conn-example
  7. key: password
  8. endpoint:
  9. secret: db-conn-example
  10. username:
  11. secret: db-conn-example

We can finally prepare an Application for the business component binding-test-comp to consume the secret, which is a representative of a database cloud resource.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: webapp
  5. spec:
  6. components:
  7. - name: binding-test-comp
  8. type: webservice
  9. properties:
  10. image: zzxwill/flask-web-application:v0.3.1-crossplane
  11. ports: 80
  12. traits:
  13. - type: service-binding
  14. properties:
  15. envMappings:
  16. # environments refer to db-conn secret
  17. DB_PASSWORD:
  18. secret: db-conn-example
  19. key: password
  20. endpoint:
  21. secret: db-conn-example
  22. username:
  23. secret: db-conn-example

Deploy this YAML and the Secret db-conn-example will be binding into environment of workload.

The sidecar trait allows you to attach a sidecar container to the component.

  • webservice
  • worker
  • task
  • cron-task

+————-+————————————————————-+———————————-+—————+————-+ | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT | +————-+————————————————————-+———————————-+—————+————-+ | name | Specify the name of sidecar container | string | true | | | cmd | Specify the commands run in the sidecar | []string | false | | | image | Specify the image of sidecar container | string | true | | | volumes | Specify the shared volume path | []volumes | false | | +————-+————————————————————-+———————————-+—————+————-+

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
pathstringtrue
  1. # app.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: vela-app-with-sidecar
  6. spec:
  7. components:
  8. - name: log-gen-worker
  9. type: worker
  10. properties:
  11. image: busybox
  12. cmd:
  13. - /bin/sh
  14. - -c
  15. - >
  16. i=0;
  17. while true;
  18. do
  19. echo "$i: $(date)" >> /var/log/date.log;
  20. i=$((i+1));
  21. sleep 1;
  22. done
  23. volumes:
  24. - name: varlog
  25. mountPath: /var/log
  26. type: emptyDir
  27. traits:
  28. - type: sidecar
  29. properties:
  30. name: count-log
  31. image: busybox
  32. cmd: [ /bin/sh, -c, 'tail -n+1 -f /var/log/date.log']
  33. volumes:
  34. - name: varlog
  35. path: /var/log

Last updated on Nov 1, 2022 by Tianxin Dong