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:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: err-with-no-ifnamespace: defaultspec:components:- name: express-servertype: webserviceproperties:image: oamdev/hello-worldports:- port: 8000workflow:steps:- name: apply-errtype: apply-objectproperties:value:test: err- name: apply-comptype: apply-componentproperties:component: express-server
Use vela status to check the status of the Application:
$ vela status err-with-no-ifAbout:Name: err-with-no-ifNamespace: defaultCreated at: 2022-06-24 18:14:46 +0800 CSTStatus: workflowTerminatedWorkflow:mode: StepByStepfinished: trueSuspend: falseTerminated: trueSteps- id:bztlmifsjlname:apply-errtype:apply-objectphase:failedmessage:step apply: run step(provider=kube,do=apply): Object 'Kind' is missing in '{"test":"err"}'- id:el8quwh8jhname:apply-comptype:apply-componentphase:skippedmessage: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:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: err-with-alwaysnamespace: defaultspec:components:- name: invalidtype: webserviceproperties:image: invalidports:- port: 8000workflow:steps:- name: comptype: apply-componenttimeout: 5soutputs:- name: statusvalueFrom: output.status.conditions[0].type + output.status.conditions[0].statusproperties:component: invalid- name: notificationtype: notificationinputs:- from: statusparameterKey: slack.message.textif: alwaysproperties:slack:url:value: <your slack url>
Use vela status to check the status of the Application:
$ vela status err-with-alwaysAbout:Name: err-with-alwaysNamespace: defaultCreated at: 2022-06-27 17:30:29 +0800 CSTStatus: workflowTerminatedWorkflow:mode: StepByStepfinished: trueSuspend: falseTerminated: trueSteps- id:loeqr6dlcnname:comptype:apply-componentphase:failedmessage:- id:hul9tayu82name:notificationtype:notificationphase:succeededmessage:Services:- Name: invalidCluster: local Namespace: defaultType: webserviceUnhealthy Ready:0/1No 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:
status:statuscontains status information for all workflow steps. You can usestatus.<step-name>.phase == "succeeded"to determine the status of a step, or you can use the simplifiedstatus.<step-name>.succeededto determine.inputs:inputscontains all the inputs parameters of the step. You can useinputs.<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:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: custom-ifnamespace: defaultspec:components:- name: comp-custom-iftype: webserviceproperties:image: crccheck/hello-worldport: 8000traits:workflow:steps:- name: applytype: apply-componentproperties:component: comp-custom-ifoutputs:- name: comp-outputvalueFrom: context.name- name: notificationtype: notificationinputs:- from: comp-outputparameterKey: slack.message.textif: inputs["comp-output"] == "custom-if"properties:slack:url:value: <your slack url>- name: notification-skiptype: notificationif: status.notification.failedproperties:slack:url:value: <your slack url>message:text: this notification should be skipped- name: notification-succeededtype: notificationif: status.notification.succeededproperties:slack:url:value: <your slack url>message:text: the notification is succeeded
Use vela status to check the status of the Application:
$ vela status custom-ifAbout:Name: custom-ifNamespace: defaultCreated at: 2022-06-25 00:37:14 +0800 CSTStatus: runningWorkflow:mode: StepByStepfinished: trueSuspend: falseTerminated: falseSteps- id:un1zd8qc6hname:applytype:apply-componentphase:succeededmessage:- id:n5xbtgsi68name:notificationtype:notificationphase:succeededmessage:- id:2ufd3v6n78name:notification-skiptype:notificationphase:skippedmessage:- id:h644x6o8mbname:notification-succeededtype:notificationphase:succeededmessage:Services:- Name: comp-custom-ifCluster: local Namespace: defaultType: webserviceHealthy Ready:1/1No 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 Feb 9, 2023 by dependabot[bot]