参考
https://kubevela.io/zh/docs/platform-engineers/cue/basic/
CUE 的执行模型:声明式而非命令式
CUE 是一种声明式语言,而不是传统的命令式编程语言(如 Python 或 Go)。这意味着:
代码分析
import (
"vela/op"
"encoding/json"
)
request: {
alias: ""
annotations: {}
attributes: {}
description: "Send request to the url"
labels: {}
type: "workflow-step"
}
template: {
http: op.#HTTPDo & {
method: parameter.method
url: parameter.url
request: {
if parameter.body != _|_ {
body: json.Marshal(parameter.body)
}
if parameter.header != _|_ {
header: parameter.header
}
}
}
fail: op.#Steps & {
if http.response.statusCode > 400 {
requestFail: op.#Fail & {
message: "request of \\\\(parameter.url) is fail: \\\\(http.response.statusCode)"
}
}
}
response: json.Unmarshal(http.response.body)
parameter: {
url: string
method: *"GET" | "POST" | "PUT" | "DELETE"
body?: {...}
header?: [string]: string
}
}
CUE 的求值过程
request
和 template
的定义。http
、fail
、response
、parameter
)的约束合并,检查是否一致。fail
依赖 http.response
),CUE 会根据这些依赖确定求值顺序。template
的完整结构。代码中的“执行顺序”
尽管 CUE 本身没有严格的“从上往下”执行,但在 KubeVela 工作流中,这些字段会被解释为一个逻辑步骤序列。具体到代码,在 KubeVela 的运行时(而不是 CUE 的求值时),逻辑顺序如下: