Manage Definition
tip
Before reading this part, please make sure you’ve learned the Definition Concept of KubeVela.
In KubeVela CLI, vela def command group provides a series of convenient definition writing tools. With these commands, users only need to write CUE files to generate and edit definitions, instead of composing Kubernetes YAML object with mixed CUE string.
vela def init is a command that helps users bootstrap new definitions. To create an empty trait definition, run:
vela def init my-trait -t trait --desc "My trait description."
It will generate the following scaffold:
"my-trait": {annotations: {}attributes: {appliesToWorkloads: []conflictsWith: []definitionRef: ""podDisruptive: falseworkloadRefPath: ""}description: "My trait description."labels: {}type: "trait"}template: patch: {}
You can also initiate definitions interactively like below:
vela def init my-comp --interactive
expected output
Please choose one definition type from the following values: component, trait, policy, workload, scope, workflow-step> Definition type: component> Definition description: My component definition.Please enter the location the template YAML file to build definition. Leave it empty to generate default template.> Definition template filename:Please enter the output location of the generated definition. Leave it empty to print definition to stdout.> Definition output filename: my-component.cueDefinition written to my-component.cue
In addition, users can create definitions from existing YAML files. For example, if a user want to create a ComponentDefinition which is designed to generate a deployment, and this deployment has already been created elsewhere, he/she can use the --template-yaml flag to complete the transformation. The YAML file is as below
# my-deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: hello-worldspec:replicas: 1selector:matchLabels:app.kubernetes.io/name: hello-worldtemplate:metadata:labels:app.kubernetes.io/name: hello-worldspec:containers:- name: hello-worldimage: somefive/hello-worldports:- name: httpcontainerPort: 80protocol: TCP---apiVersion: v1kind: Servicemetadata:name: hello-world-servicespec:selector:app: hello-worldports:- name: httpprotocol: TCPport: 80targetPort: 8080type: LoadBalancer
Running the following command to get the CUE-format ComponentDefinition:
vela def init my-comp -t component --desc "My component." --template-yaml ./my-deployment.yaml
The expect output will be:
"my-comp": {annotations: {}attributes: workload: definition: {apiVersion: "<change me> apps/v1"kind: "<change me> Deployment"}description: "My component."labels: {}type: "component"}template: {output: {metadata: name: "hello-world"spec: {replicas: 1selector: matchLabels: "app.kubernetes.io/name": "hello-world"template: {metadata: labels: "app.kubernetes.io/name": "hello-world"spec: containers: [{name: "hello-world"image: "somefive/hello-world"ports: [{name: "http"containerPort: 80protocol: "TCP"}]}]}}apiVersion: "apps/v1"kind: "Deployment"}outputs: "hello-world-service": {metadata: name: "hello-world-service"spec: {ports: [{name: "http"protocol: "TCP"port: 80targetPort: 8080}]selector: app: "hello-world"type: "LoadBalancer"}apiVersion: "v1"kind: "Service"}parameter: {}}
Then the user can make further modifications based on the definition file above, like removing <change me> in workload.definition。
After initializing definition files, run vela def vet <my-def.cue> to validate if there are any syntax error in the definition file.
vela def vet my-comp.cue
It can be used to detect some simple errors such as missing brackets.
After confirming the definition file has correct syntax, users can apply this definition into control plane by:
vela def apply my-comp.cue --namespace my-namespace
The definition will be applied to the namespace named my-namespace, by default the namespace is vela-system。
tip
When definition applied to vela-system, the definition can be used for all applications in this cluster. If definitions are applied in some other namespaces, only the application in the same namespace can use that. It’s useful when in multi-tenant cases.
If you want to check the transformed Kubernetes API format, you can use --dry-run can achieve that.
vela def apply my-comp.cue --dry-run
expected output
apiVersion: core.oam.dev/v1beta1kind: ComponentDefinitionmetadata:annotations:definition.oam.dev/description: My component.labels: {}name: my-compnamespace: vela-systemspec:schematic:cue:template: |output: {metadata: name: "hello-world"spec: {replicas: 1selector: matchLabels: "app.kubernetes.io/name": "hello-world"template: {metadata: labels: "app.kubernetes.io/name": "hello-world"spec: containers: [{name: "hello-world"image: "somefive/hello-world"ports: [{name: "http"containerPort: 80protocol: "TCP"}]}]}}apiVersion: "apps/v11"kind: "Deployment"}outputs: "hello-world-service": {metadata: name: "hello-world-service"spec: {ports: [{name: "http"protocol: "TCP"port: 80targetPort: 8080}]selector: app: "hello-world"type: "LoadBalancer"}apiVersion: "v1"kind: "Service"}parameter: {}workload:definition:apiVersion: apps/v1kind: Deployment
While you can use native kubectl tools to confirm the results of the apply command, as mentioned above, the YAML object mixed with raw CUE template string is complex. Using vela def get will automatically convert the YAML object into the CUE-format definition.
vela def get my-comp
You can list all definitions installed through:
vela def list
You can filter type by:
List component only:
vela def list -t component
List trait only:
vela def list -t trait
List workflow step only:
vela def list -t workflow-step
List policy only:
vela def list -t policy
Using vela def edit to edit definitions in pure CUE-format. The transformation between CUE-format definition and YAML object is done by the command. Besides, you can specify the EDITOR environment variable to use your favorite editor.
EDITOR=vim vela def edit my-comp
vela def del can be utilized to delete existing definitions.
$ vela def del my-comp -n my-namespaceAre you sure to delete the following definition in namespace my-namespace?ComponentDefinition my-comp: My component.[yes|no] > yesComponentDefinition my-comp in namespace my-namespace deleted.
A brief quick view for these commands are shown below.

When we want to debug definition with application, we can use vela dry-run --definitions or -d for short, to specify local definitions.
Dry run will help you to understand what are the real resources which will to be expanded and deployed to the Kubernetes cluster. In other words, it will mock to run the same logic as KubeVela’s controller and output the results locally.
For example, let’s dry-run the following application:
# app.yamlapiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: vela-appspec:components:- name: express-servertype: my-comp
Use dry run for test:
vela dry-run -f app.yaml -d my-comp.cue
caution
Before KubeVela CLI 1.6.2, you can only use definition in Kubernetes API format.
vela dry-run -f app.yaml -d my-comp.yaml
expected output
---# Application(vela-app) -- Component(express-server)---apiVersion: apps/v1kind: Deploymentmetadata:annotations: {}labels:app.oam.dev/appRevision: ""app.oam.dev/component: express-serverapp.oam.dev/name: vela-appapp.oam.dev/namespace: defaultapp.oam.dev/resourceType: WORKLOADworkload.oam.dev/type: my-compname: hello-worldnamespace: defaultspec:replicas: 1selector:matchLabels:app.kubernetes.io/name: hello-worldtemplate:metadata:labels:app.kubernetes.io/name: hello-worldspec:containers:- image: somefive/hello-worldname: hello-worldports:- containerPort: 80name: httpprotocol: TCP---## From the auxiliary workloadapiVersion: v1kind: Servicemetadata:annotations: {}labels:app.oam.dev/appRevision: ""app.oam.dev/component: express-serverapp.oam.dev/name: vela-appapp.oam.dev/namespace: defaultapp.oam.dev/resourceType: TRAITtrait.oam.dev/resource: hello-world-servicetrait.oam.dev/type: AuxiliaryWorkloadname: hello-world-servicenamespace: defaultspec:ports:- name: httpport: 80protocol: TCPtargetPort: 8080selector:app: hello-worldtype: LoadBalancer---
- Learn more about defining customized component.
- Learn more about defining customized trait.
Last updated on Feb 9, 2023 by dependabot[bot]