版本:v1.6

CUE 操作符

这个文档介绍在工作流步骤的定义过程中,可以使用的 CUE 操作符。你需要引用 vela/op 包来使用这些操作符。

CUE 操作符 - 图1提示

在阅读本部分之前,请确保你已经了解如何自定义工作流 且学习掌握了 CUE 的基本知识

让该工作流步骤处于等待状态,直到条件被满足。

参数定义

  1. #ConditionalWait: {
  2. // +usage=If continue is false, the step will wait for continue to be true.
  3. continue: bool
  4. // +usage=Optional message that will be shown in workflow step status, note that the message might be override by other actions.
  5. message?: string
  6. }

用法示例

  1. import "vela/op"
  2. myRead: op.#Read & {
  3. value: {
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. metadata: name: "test-app"
  7. }
  8. }
  9. wait: op.#ConditionalWait & {
  10. continue: myRead.value.status.phase == "running"
  11. }

让该工作流步骤处于失败状态。

参数定义

  1. #Fail: {
  2. // +usage=Optional message that will be shown in workflow step status, note that the message might be override by other actions.
  3. message?: string
  4. }

用法示例

  1. import "vela/op"
  2. fail: op.#Fail & {
  3. message: "error in the step"
  4. }

在该步骤中输出日志,或者配置该步骤的日志来源。如果某个步骤定义中使用了 op.#Log,那么你可以使用 vela workflow logs <name> 来查看该步骤的日志。

参数定义

  1. #Log: {
  2. // +usage=The data to print in the controller logs
  3. data?: {...} | string
  4. // +usage=The log level of the data
  5. level: *3 | int
  6. // +usage=The log source of this step. You can specify it from a url or resources. Note that if you set source in multiple op.#Log, only the latest one will work
  7. source?: close({
  8. // +usage=Specify the log source url of this step
  9. url: string
  10. }) | close({
  11. // +usage=Specify the log resources of this step
  12. resources?: [...{
  13. // +usage=Specify the name of the resource
  14. name?: string
  15. // +usage=Specify the cluster of the resource
  16. cluster?: string
  17. // +usage=Specify the namespace of the resource
  18. namespace?: string
  19. // +usage=Specify the label selector of the resource
  20. labelSelector?: {...}
  21. }]
  22. })
  23. }

用法示例

  1. import "vela/op"
  2. myLog: op.#Log & {
  3. data: "my custom log"
  4. resources: [{
  5. labelsSelector: {"test-key": "test-value"}
  6. }]
  7. }

往该工作流步骤状态的 Message 中写入信息。

参数定义

  1. #Message: {
  2. // +usage=Optional message that will be shown in workflow step status, note that the message might be override by other actions.
  3. message?: string
  4. }

用法示例

  1. import "vela/op"
  2. msg: op.#Message & {
  3. message: "custom message"
  4. }

用来在 workflow 的上下文中保存或者读取用户定义的数据

参数定义

  1. #DoVar: {
  2. // +usage=The method to call on the variable
  3. method: *"Get" | "Put"
  4. // +usage=The path to the variable
  5. path: string
  6. // +usage=The value of the variable
  7. value?: _
  8. }

用法示例

  1. put: op.ws.#DoVar & {
  2. method: "Put"
  3. path: "foo.score"
  4. value: 100
  5. }
  6. // 用户可以通过get.value拿到上面保存的数据(100)
  7. get: op.ws.#DoVar & {
  8. method: "Get"
  9. path: "foo.score"
  10. }

向指定 URL 发送 HTTP 请求。

参数定义

  1. #HTTPDo: {
  2. // +usage=The method of HTTP request
  3. method: *"GET" | "POST" | "PUT" | "DELETE"
  4. // +usage=The url to request
  5. url: string
  6. // +usage=The request config
  7. request?: {
  8. // +usage=The timeout of this request
  9. timeout?: string
  10. // +usage=The request body
  11. body?: string
  12. // +usage=The header of the request
  13. header?: [string]: string
  14. // +usage=The trailer of the request
  15. trailer?: [string]: string
  16. // +usage=The rate limiter of the request
  17. ratelimiter?: {
  18. limit: int
  19. period: string
  20. }
  21. }
  22. // +usgae=The tls config of the request
  23. tls_config?: secret: string
  24. // +usage=The response of the request will be filled in this field after the action is executed
  25. response: {
  26. // +usage=The body of the response
  27. body: string
  28. // +usage=The header of the response
  29. header?: [string]: [...string]
  30. // +usage=The trailer of the response
  31. trailer?: [string]: [...string]
  32. // +usage=The status code of the response
  33. statusCode: int
  34. }
  35. }

用法示例

  1. import "vela/op"
  2. myRequest: op.#HTTPDo & {
  3. method: "POST"
  4. url: "http://my-url.com"
  5. request: {
  6. body: {
  7. "hello": "body"
  8. }
  9. }
  10. }

向指定 URL 发送 HTTP GET 请求。

参数定义

同 HTTPDo,但是 method 已被指定为 GET。

用法示例

参考 HTTPDo。

向指定 URL 发送 HTTP POST 请求。

参数定义

同 HTTPDo,但是 method 已被指定为 POST。

用法示例

参考 HTTPDo。

向指定 URL 发送 HTTP PUT 请求。

参数定义

同 HTTPDo,但是 method 已被指定为 PUT。

用法示例

参考 HTTPDo。

向指定 URL 发送 HTTP DELETE 请求。

参数定义

同 HTTPDo,但是 method 已被指定为 DELETE。

用法示例

参考 HTTPDo。

发送邮件。

参数定义

  1. #SendEmail {
  2. // +usage=The info of the sender
  3. from: {
  4. // +usage=The address of the sender
  5. address: string
  6. // +usage=The alias of the sender
  7. alias?: string
  8. // +usage=The password of the sender
  9. password: string
  10. // +usage=The host of the sender server
  11. host: string
  12. // +usage=The port of the sender server
  13. port: int
  14. }
  15. // +usgae=The email address list of the recievers
  16. to: [...string]
  17. // +usage=The content of the email
  18. content: {
  19. // +usage=The subject of the email
  20. subject: string
  21. // +usage=The body of the email
  22. body: string
  23. }
  24. }

用法示例

  1. import "vela/op"
  2. myEmail: op.#SendEmail & {
  3. from: {
  4. address: "hello@mail.com"
  5. password: "password"
  6. host: "myhost"
  7. port: 465
  8. }
  9. to: ["world@mail.com", "next@workflow.com"]
  10. content: {
  11. subject: "Hello Vela"
  12. body: "Hello Vela, this is a test email"
  13. }
  14. }

在 Kubernetes 集群中创建或者更新资源。

参数定义

  1. #Apply: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The resource to apply
  5. value: {...}
  6. }

用法示例

  1. import "vela/op"
  2. myApply: op.#Apply & {
  3. value: {
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. metadata: name: "test-app"
  7. spec: {
  8. replicas: 2
  9. ...
  10. }
  11. }
  12. }

在 Kubernetes 集群中批量创建或者更新资源。

参数定义

  1. #ApplyInParallel: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The resources to apply in parallel
  5. value: [...{...}]
  6. }

用法示例

  1. import "vela/op"
  2. myApply: op.#ApplyInParallel & {
  3. value: [{
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. metadata: name: "test-app"
  7. spec: {
  8. replicas: 2
  9. ...
  10. }
  11. }, {
  12. kind: "Deployment"
  13. apiVersion: "apps/v1"
  14. metadata: name: "test-app2"
  15. spec: {
  16. replicas: 2
  17. ...
  18. }
  19. }]
  20. }

在 Kubernetes 集群中读取资源。

参数定义

  1. #Read: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The resource to read, this field will be filled with the resource read from the cluster after the action is executed
  5. value?: {...}
  6. ...
  7. }

用法示例

  1. import "vela/op"
  2. myRead: op.#Read & {
  3. value: {
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. metadata: name: "test-app"
  7. }
  8. }

在 Kubernetes 集群中列出资源。

参数定义

  1. #List: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The resource to list
  5. resource: {
  6. // +usage=The api version of the resource
  7. apiVersion: string
  8. // +usage=The kind of the resource
  9. kind: string
  10. }
  11. // +usage=The filter to list the resources
  12. filter?: {
  13. // +usage=The namespace to list the resources
  14. namespace?: *"" | string
  15. // +usage=The label selector to filter the resources
  16. matchingLabels?: {...}
  17. }
  18. // +usage=The listed resources will be filled in this field after the action is executed
  19. list?: {...}
  20. ...
  21. }

用法示例

  1. import "vela/op"
  2. myList: op.#List & {
  3. resource: {
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. }
  7. filter: {
  8. matchingLabels: {
  9. "mylabel": "myvalue"
  10. }
  11. }
  12. }

在 Kubernetes 集群中删除资源。

参数定义

  1. #Delete: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The resource to delete
  5. value: {
  6. // +usage=The api version of the resource
  7. apiVersion: string
  8. // +usage=The kind of the resource
  9. kind: string
  10. // +usage=The metadata of the resource
  11. metadata: {
  12. // +usage=The name of the resource
  13. name?: string
  14. // +usage=The namespace of the resource
  15. namespace: *"default" | string
  16. }
  17. }
  18. // +usage=The filter to delete the resources
  19. filter?: {
  20. // +usage=The namespace to list the resources
  21. namespace?: string
  22. // +usage=The label selector to filter the resources
  23. matchingLabels?: {...}
  24. }
  25. }

用法示例

  1. import "vela/op"
  2. myDelete: op.#Delete & {
  3. resource: {
  4. kind: "Deployment"
  5. apiVersion: "apps/v1"
  6. metadata: name: "my-app"
  7. }
  8. }

获取当前应用部署计划中所有组件对应的资源数据。

参数定义

  1. #Load: {
  2. // +usage=If specify `app`, use specified application to load its component resources otherwise use current application
  3. app?: string
  4. // +usage=The value of the components will be filled in this field after the action is executed, you can use value[componentName] to refer a specified component
  5. value?: {...}
  6. }

用法示例

  1. import "vela/op"
  2. // 该操作完成后,你可以使用 `load.value[componentName]` 来获取到对应组件的资源数据
  3. load: op.#Load & {}
  4. mycomp: load.value["my-comp"]

在 Kubernetes 集群中创建或者更新组件对应的所有资源。注意,在使用该操作前需要先用 Load 加载资源。

参数定义

  1. #ApplyComponent: {
  2. // +usage=The cluster to use
  3. cluster: *"" | string
  4. // +usage=The env to use
  5. env: *"" | string
  6. // +usage=The namespace to apply
  7. namespace: *"" | string
  8. // +usage=Whether to wait healthy of the applied component
  9. waitHealthy: *true | bool
  10. // +usage=The value of the component resource
  11. value: {...}
  12. // +usage=The patcher that will be applied to the resource, you can define the strategy of list merge through comments. Reference doc here: https://kubevela.io/docs/platform-engineers/traits/patch-trait#patch-in-workflow-step
  13. patch?: {...}
  14. }

用法示例

  1. import "vela/op"
  2. load: op.#Load & {}
  3. apply: op.#ApplyComponent & {
  4. value: load.value["my-comp"]
  5. }

在 Kubernetes 集群中创建或者更新应用对应的所有资源。

参数定义

  1. #ApplyApplication: {}

用法示例

  1. import "vela/op"
  2. apply: op.#ApplyApplication & {}

用于一组操作的组合,可以用于实现复杂的操作逻辑。

参数定义

  1. #Steps: {}

用法示例

  1. import "vela/op"
  2. env: "prod"
  3. app: op.#Steps & {
  4. if env == "prod" {
  5. load: op.#Load & {
  6. component: "component-name"
  7. }
  8. apply: op.#Apply & {
  9. value: load.value.workload
  10. }
  11. }
  12. if env != "prod" {
  13. request: op.#HTTPGet & {
  14. url: "http://my-url.com"
  15. }
  16. }
  17. }

向指定 Slack URL 发送请求。#Slack 实际上是对 #HTTPPost 的二次封装,我们将在下个版本废弃这个操作。你可以使用 #HTTPPost 替代,如:

  1. import (
  2. "vela/op"
  3. "encoding/json"
  4. )
  5. message: {
  6. "hello": "world"
  7. }
  8. mySlack: op.#HTTPPost & {
  9. url: "slackURL"
  10. request: {
  11. body: json.Marshal(message)
  12. header: "Content-Type": "application/json"
  13. }
  14. }

参数定义

  1. #Slack: {
  2. message: {...}
  3. slackUrl: string
  4. }

用法示例

  1. import "vela/op"
  2. myMessage: {
  3. "hello": "world"
  4. }
  5. myRequest: op.#Slack & {
  6. message: myMessage
  7. slackUrl: "slackURL"
  8. }

向指定 DingTalk URL 发送请求。#DingTalk 实际上是对 #HTTPPost 的二次封装,我们将在下个版本废弃这个操作。你可以使用 #HTTPPost 替代,请参考 Slack 操作中的例子。

参数定义

  1. #DingTalk: {
  2. message: {...}
  3. dingUrl: string
  4. }

用法示例

  1. import "vela/op"
  2. myMessage: {
  3. "hello": "world"
  4. }
  5. myRequest: op.#DingTalk & {
  6. message: myMessage
  7. dingUrl: "dingURL"
  8. }

向指定 Lark URL 发送请求。#Lark 实际上是对 #HTTPPost 的二次封装,我们将在下个版本废弃这个操作。你可以使用 #HTTPPost 替代,请参考 Slack 操作中的例子。

参数定义

  1. #Lark: {
  2. message: {...}
  3. larkUrl: string
  4. }

用法示例

  1. import "vela/op"
  2. myMessage: {
  3. "hello": "world"
  4. }
  5. myRequest: op.#Lark & {
  6. message: myMessage
  7. larkUrl: "larkURL"
  8. }

Last updated on 2022年12月1日 by Somefive