Workflow Step Definition

    Before reading this section, make sure you have understood the concept of WorkflowStepDefinition in KubeVela and learned the .

    In this section, we will introduce how to customize the workflow step in Application by using CUE through WorkflowStepDefinition.

    We can generate WorkflowStepDefinition CUE file with command vela def <def-name> --type workflow-step > <def-name>.cue.

    Let’s take a custom step for sending an HTTP request as an example, first, initialize the Definition file:

    After initialization, we can see the following in request.cue:

    1. request: {
    2. alias: ""
    3. annotations: {}
    4. attributes: {}
    5. description: "Send request to the url"
    6. labels: {}
    7. type: "workflow-step"
    8. }
    9. template: {
    10. }

    Inside the template is the execution logic for this workflow step. We can define parameter in template to receive the parameters passed in by the user:

    At the same time, KubeVela also provides the vela/op package by default, which contains a series of built-in workflow CUE actions, such as: sending HTTP requests, operating K8s resources, printing logs, etc.

    Now we can import KubeVela’s built-in vela/op package and CUE’s official encoding/json, use op.#HTTPDo to send HTTP requests according to the user’s parameters, and use json.Marshal() to marshal the data.

    1. import (
    2. "vela/op"
    3. "encoding/json"
    4. )
    5. request: {
    6. alias: ""
    7. annotations: {}
    8. attributes: {}
    9. description: "Send request to the url"
    10. type: "workflow-step"
    11. template: {
    12. http: op.#HTTPDo & {
    13. method: parameter.method
    14. url: parameter.url
    15. request: {
    16. if parameter.body != _|_ {
    17. body: json.Marshal(parameter.body)
    18. }
    19. if parameter.header != _|_ {
    20. header: parameter.header
    21. }
    22. }
    23. }
    24. parameter: {
    25. url: string
    26. method: *"GET" | "POST" | "PUT" | "DELETE"
    27. body?: {...}
    28. header?: [string]: string
    29. }
    30. }

    If the HTTP request returns a status code greater than 400, we expect this step to be failed. Use op.#Fail to fail this step, and the definition is like:

    Use vela def apply -f request.cue to deploy this Definition to the cluster, then we can use this custom step directly in the Application.

    Deploy the following Application: The first step of the workflow will send an HTTP request to get the information of the KubeVela repository; at the same time, this step will use the star number of the KubeVela repository as the Output, the next step will use this Output as a parameter, and sent it as message to the Slack:

    tip

    1. apiVersion: core.oam.dev/v1beta1
    2. kind: Application
    3. metadata:
    4. name: request-http
    5. namespace: default
    6. spec:
    7. components: []
    8. steps:
    9. - name: request
    10. type: request
    11. properties:
    12. outputs:
    13. - name: stars
    14. valueFrom: |
    15. import "strconv"
    16. "Current star count: " + strconv.FormatInt(response["stargazers_count"], 10)
    17. - name: notification
    18. type: notification
    19. inputs:
    20. - from: stars
    21. parameterKey: slack.message.text
    22. properties:
    23. slack:
    24. url:
    25. value: <your slack url>

    If you want to wait a period of time in a workflow step until a certain condition is met, or until the status of a resource becomes ready, you can use op.#ConditionalWait.

    Take the status of waiting for a Deployment as an example, use op.#Apply to deploy a Deployment, and then use op.#ConditionalWait to wait for the status of the Deployment to become ready:

    KubeVela allows you to reference some runtime data via the context keyword.

    In a workflow step definition, you can use the following context data:

    KubeVela is fully programmable through CUE, while it leverages Kubernetes as a control plane and is consistent with the API in YAML.

    Therefore, the CUE Definition will be translated into the Kubernetes API when applied to the cluster.

    1. apiVersion: core.oam.dev/v1beta1
    2. kind: WorkflowStepDefinition
    3. metadata:
    4. annotations:
    5. definition.oam.dev/description: <Function description>
    6. spec:
    7. schematic:
    8. cue: # Details of workflow steps defined by CUE language
    9. template: <CUE format template>

    You can check the following resources for more examples:

    • Definitions defined in addons in the .