Version: v1.8

If Conditions

This section introduces how to add if conditions to workflow steps.

In the KubeVela workflow, each step can specify an if, in which you can determine whether the step should be executed.

In the case where a step does not specify an If, KubeVela will determine whether to execute the step based on the status of the previous steps. In default, if all the previous steps are succeeded, the step will be executed.

This also means that if step A fails, step B after step A will be skipped and will not be executed.

Apply the following example:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: err-with-no-if
  5. namespace: default
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. ports:
  13. - port: 8000
  14. workflow:
  15. steps:
  16. - name: apply-err
  17. type: apply-object
  18. properties:
  19. value:
  20. test: err
  21. - name: apply-comp
  22. type: apply-component
  23. properties:
  24. component: express-server

Use vela status to check the status of the Application:

  1. $ vela status err-with-no-if
  2. About:
  3. Name: err-with-no-if
  4. Namespace: default
  5. Created at: 2022-06-24 18:14:46 +0800 CST
  6. Status: workflowTerminated
  7. Workflow:
  8. mode: StepByStep
  9. finished: true
  10. Suspend: false
  11. Terminated: true
  12. Steps
  13. - id:bztlmifsjl
  14. name:apply-err
  15. type:apply-object
  16. phase:failed
  17. message:step apply: run step(provider=kube,do=apply): Object 'Kind' is missing in '{"test":"err"}'
  18. - id:el8quwh8jh
  19. name:apply-comp
  20. type:apply-component
  21. phase:skipped
  22. message:
  23. Services:

As you can see, the step apply-err will fail due to an attempt to deploy an invalid resource, and the step apply-comp will be skipped because the previous step failed.

If you want a step to be executed anyway, you can specify if to always for this step.

Apply the following example:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: err-with-always
  5. namespace: default
  6. spec:
  7. components:
  8. - name: invalid
  9. type: webservice
  10. properties:
  11. image: invalid
  12. ports:
  13. - port: 8000
  14. workflow:
  15. steps:
  16. - name: comp
  17. type: apply-component
  18. timeout: 5s
  19. outputs:
  20. - name: status
  21. valueFrom: output.status.conditions[0].type + output.status.conditions[0].status
  22. properties:
  23. component: invalid
  24. - name: notification
  25. type: notification
  26. inputs:
  27. - from: status
  28. parameterKey: slack.message.text
  29. if: always
  30. properties:
  31. slack:
  32. url:
  33. value: <your slack url>

Use vela status to check the status of the Application:

  1. $ vela status err-with-always
  2. About:
  3. Name: err-with-always
  4. Namespace: default
  5. Created at: 2022-06-27 17:30:29 +0800 CST
  6. Status: workflowTerminated
  7. Workflow:
  8. mode: StepByStep
  9. finished: true
  10. Suspend: false
  11. Terminated: true
  12. Steps
  13. - id:loeqr6dlcn
  14. name:comp
  15. type:apply-component
  16. phase:failed
  17. message:
  18. - id:hul9tayu82
  19. name:notification
  20. type:notification
  21. phase:succeeded
  22. message:
  23. Services:
  24. - Name: invalid
  25. Cluster: local Namespace: default
  26. Type: webservice
  27. Unhealthy Ready:0/1
  28. No trait applied

You can see that step comp will try to deploy a component whose image is invalid, and the component will fail due to timeout after five seconds because the image cannot be pulled. In the meanwhile, this step passes the component’s status as outputs. The step notification will be executed because if: always is specified. At the same time, the content of the message notification is the status of the component in the previous step. Therefore, we can see the message notification carrying the status information in slack.

Note: You need to upgrade to version 1.5 or above to use custom If conditions.

You can also write your own judgment logic to determine whether the step should be executed. Note: The value in if will be executed as CUE codes. KubeVela provides some built-in variables in if, they are:

  • statusstatus contains status information for all workflow steps. You can use status.<step-name>.phase == "succeeded" to determine the status of a step, or you can use the simplified status.<step-name>.succeeded to determine.
  • inputsinputs contains all the inputs parameters of the step. You can use inputs.<input-name> == "value" to get input for the step.

Note that if your step name or inputs name is not a valid CUE variable name (eg: contains -, or starts with a number, etc.), you can refer to it as follows: status["invalid-name"].failed

Apply the following example:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: custom-if
  5. namespace: default
  6. spec:
  7. components:
  8. - name: comp-custom-if
  9. type: webservice
  10. properties:
  11. image: crccheck/hello-world
  12. port: 8000
  13. traits:
  14. workflow:
  15. steps:
  16. - name: apply
  17. type: apply-component
  18. properties:
  19. component: comp-custom-if
  20. outputs:
  21. - name: comp-output
  22. valueFrom: context.name
  23. - name: notification
  24. type: notification
  25. inputs:
  26. - from: comp-output
  27. parameterKey: slack.message.text
  28. if: inputs["comp-output"] == "custom-if"
  29. properties:
  30. slack:
  31. url:
  32. value: <your slack url>
  33. - name: notification-skip
  34. type: notification
  35. if: status.notification.failed
  36. properties:
  37. slack:
  38. url:
  39. value: <your slack url>
  40. message:
  41. text: this notification should be skipped
  42. - name: notification-succeeded
  43. type: notification
  44. if: status.notification.succeeded
  45. properties:
  46. slack:
  47. url:
  48. value: <your slack url>
  49. message:
  50. text: the notification is succeeded

Use vela status to check the status of the Application:

  1. $ vela status custom-if
  2. About:
  3. Name: custom-if
  4. Namespace: default
  5. Created at: 2022-06-25 00:37:14 +0800 CST
  6. Status: running
  7. Workflow:
  8. mode: StepByStep
  9. finished: true
  10. Suspend: false
  11. Terminated: false
  12. Steps
  13. - id:un1zd8qc6h
  14. name:apply
  15. type:apply-component
  16. phase:succeeded
  17. message:
  18. - id:n5xbtgsi68
  19. name:notification
  20. type:notification
  21. phase:succeeded
  22. message:
  23. - id:2ufd3v6n78
  24. name:notification-skip
  25. type:notification
  26. phase:skipped
  27. message:
  28. - id:h644x6o8mb
  29. name:notification-succeeded
  30. type:notification
  31. phase:succeeded
  32. message:
  33. Services:
  34. - Name: comp-custom-if
  35. Cluster: local Namespace: default
  36. Type: webservice
  37. Healthy Ready:1/1
  38. No trait applied

As you can see, after the first step apply succeeded, the outputs comp-output will be output. The second step notification refers to the outputs of the first step as inputs and makes a judgment. After the condition is met, the notification is successfully sent. The third step notification-skip judges whether the second step is in a failed state. If the condition is not met, this step is skipped. The fourth step notification-succeeded judges whether the second step is successful, if the condition is met, the step is successfully executed.

Last updated on May 6, 2023 by Tianxin Dong