条件判断
本节将介绍如何在 KubeVela 中为工作流步骤添加条件判断。
在 KubeVela 工作流中,每个步骤都可以指定一个 if,你可以使用 if 来确定是否应该执行该步骤。
不指定 If
在步骤没有指定 If 的情况下,KubeVela 会根据先前步骤的状态来判断是否应该执行该步骤。默认步骤的执行条件是:在该步骤前的所有步骤状态均为成功。
这也意味着,如果步骤 A 执行失败,那么步骤 A 之后的步骤 B 会被跳过,不会被执行。
部署如下例子:
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
使用 vela status 命令查看应用状态:
$ 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:
可以看到,步骤 apply-err 会因为尝试部署一个非法的资源而导致失败,同时,因为之前的步骤失败了,步骤 apply-comp 将被跳过。
If Always
如果你希望一个步骤无论如何都应该被执行,那么,你可以为这个步骤指定 if 为 always。
部署如下例子:
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>
使用 vela status 命令查看应用状态:
$ 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
可以看到,步骤 comp 会去尝试部署一个镜像为 invalid 的组件,而组件因为拉取不到镜像,所以会在五秒后因为超时而失败,同时,这个步骤将组件的 status 作为 outputs 传出。而步骤 notification 因为指定了 if: always,所以一定会被执行,同时,消息通知的内容为上一个步骤中组件的状态,因此,我们可以在 slack 中看到携带状态信息的消息通知。
自定义 If 条件判断
注意:你需要升级到 1.5 及以上版本来使用自定义 If 条件判断。
你也可以编写自己的判断逻辑来确定是否应该执行该步骤。注意: if 里的值将作为 CUE 代码执行。KubeVela 在 if 中提供了一些内置变量,它们是:
status:status 中包含了所有工作流步骤的状态信息。你可以使用status.<step-name>.phase == "succeeded"来判断步骤的状态,也可以使用简化方式status.<step-name>.succeeded来进行判断。inputs:inputs 中包含了该步骤的所有 inputs 参数。你可以使用inputs.<input-name> == "value"来获取判断步骤的输入。
注意,如果你的步骤名或者 inputs 名并不是一个有效的 CUE 变量名(如:包含
-,或者以数字开头等),你可以用如下方式引用:status["invalid-name"].failed
部署如下例子:
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
使用 vela status 命令查看应用状态:
$ 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
可以看到,第一个步骤 apply 成功后,会输出一个 outputs。第二个步骤 notification 中引用第一个步骤的 outputs 作为 inputs 并且进行判断,满足条件后成功发送通知。第三个步骤 notification-skip 判断第二个步骤是否为失败状态,条件不满足,这个步骤跳过。第四个步骤 notification-succeeded 判断第二个步骤是否成功,条件满足,该步骤成功执行。