Manage X-Definition

    In KubeVela CLI (>= v1.1.0), 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."

    Or you can use vela def init my-comp --interactive to initiate definitions interactively.

    1. $ vela def init my-comp --interactive
    2. Please choose one definition type from the following values: component, trait, policy, workload, scope, workflow-step
    3. > Definition type: component
    4. > Definition description: My component definition.
    5. Please enter the location the template YAML file to build definition. Leave it empty to generate default template.
    6. > Definition template filename:
    7. Please enter the output location of the generated definition. Leave it empty to print definition to stdout.
    8. > Definition output filename: my-component.cue
    9. Definition written to my-component.cue
    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: hello-world
    5. spec:
    6. replicas: 1
    7. selector:
    8. matchLabels:
    9. app.kubernetes.io/name: hello-world
    10. template:
    11. metadata:
    12. labels:
    13. app.kubernetes.io/name: hello-world
    14. spec:
    15. containers:
    16. - name: hello-world
    17. image: somefive/hello-world
    18. ports:
    19. - name: http
    20. containerPort: 80
    21. protocol: TCP
    22. ---
    23. apiVersion: v1
    24. kind: Service
    25. metadata:
    26. name: hello-world-service
    27. spec:
    28. selector:
    29. app: hello-world
    30. ports:
    31. - name: http
    32. protocol: TCP
    33. port: 80
    34. targetPort: 8080
    35. type: LoadBalancer

    Running vela def init my-comp -t component --desc "My component." --template-yaml ./my-deployment.yaml to get the CUE-format ComponentDefinition

    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-comp.cue to validate if there are any syntax error in the definition file. It can be used to detect some simple errors such as missing brackets.

    1. $ vela def vet my-comp.cue
    2. Validation succeed.
    1. apiVersion: core.oam.dev/v1beta1
    2. kind: ComponentDefinition
    3. metadata:
    4. annotations:
    5. definition.oam.dev/description: My component.
    6. labels: {}
    7. name: my-comp
    8. namespace: vela-system
    9. spec:
    10. schematic:
    11. cue:
    12. template: |
    13. output: {
    14. metadata: name: "hello-world"
    15. spec: {
    16. replicas: 1
    17. selector: matchLabels: "app.kubernetes.io/name": "hello-world"
    18. template: {
    19. metadata: labels: "app.kubernetes.io/name": "hello-world"
    20. spec: containers: [{
    21. name: "hello-world"
    22. image: "somefive/hello-world"
    23. ports: [{
    24. name: "http"
    25. }]
    26. }]
    27. }
    28. }
    29. apiVersion: "apps/v11"
    30. kind: "Deployment"
    31. }
    32. outputs: "hello-world-service": {
    33. metadata: name: "hello-world-service"
    34. spec: {
    35. ports: [{
    36. name: "http"
    37. protocol: "TCP"
    38. port: 80
    39. targetPort: 8080
    40. }]
    41. selector: app: "hello-world"
    42. type: "LoadBalancer"
    43. }
    44. apiVersion: "v1"
    45. kind: "Service"
    46. }
    47. parameter: {}
    48. workload:
    49. definition:
    50. apiVersion: apps/v1
    51. kind: 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.

    1. $ vela def get my-comp -t component

    Or you can list all defintions installed through vela def list

    1. $ vela def list -n my-namespace -t component
    2. NAME TYPE NAMESPACE DESCRIPTION
    3. my-comp ComponentDefinition my-namespace My component.

    Similarly, 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 favourate editor.

    1. $ vela def del my-comp -n my-namespace
    2. Are you sure to delete the following definition in namespace my-namespace?
    3. ComponentDefinition my-comp: My component.
    4. [yes|no] > yes