CEL 表达式

发布时间 2023-12-05 17:43:47作者: 小吉猫

字符串函数

'refs/heads/main'.split('/') // result = list ['refs', 'heads', 'main']
['refs', 'heads', 'main'].join('/') // result = string 'refs/heads/main'
'my place'.replace('my ',' ') // result = string 'place'
'this that another'.replace('th ',' ', 2) // result = 'is at another'

CEL 表达式 数字操作

定义json格式数据

{
  "count": 2,
  "measure": 1.7
}

CEL 转换数据

可以显式转换数字,或添加另一个双精度值
interceptors:
  - cel:
      overlays:
        - key: count_plus_1
          expression: "body.count + 1.0"
        - key: count_plus_2
          expression: "int(body.count) + 2"
        - key: measure_times_3
          expression: "body.measure * 3.0"

返回结果

{
  "count_plus_1": 2,
  "count_plus_2": 3,
  "measure_times_3": 5.1
}

CEL 表达式示例

定义json格式数据

{
  "labels": [
    {
      "name": "test-a"
    },
    {
      "name":"test-b"
    }
  ]
}

定义 filter

filter: body.labels.exists(x, x.name == 'test-b') is true
filter: body.labels.exists(x, x.name == 'test-c') is false
filter: body.labels.exists_one(x, x.name.endsWith('-b')) is true
filter: body.labels.exists_one(x, x.name.startsWith('test-')) is false
filter: body.labels.all(x, x.name.startsWith('test-')) is true
filter: body.labels.all(x, x.name.endsWith('-b')) is false

CEL 扩展列表

http.Request 值的正文被解码为 JSON 并公开,并且标头也可用。
Symbol Type Description Example
body map(string, dynamic) 这是来自传入 http.Request 的解码 JSON 正文,公开为字符串键到任何值类型的映射。
body.value == 'test'
header map(string, list(string)) 这是请求标头。
header['X-Test'][0] == 'test-value'
requestURL string 这是传入 HTTP 请求的 URL。
requestURL.parseURL().path

CEL 函数列表

这列出了可从 CEL interceptor中的 CEL 表达式使用的自定义函数。
Symbol Type Description Example
match header.match(string, string) -> bool 使用 Go 的 http.Request 中的规范标头匹配来将标头与值进行匹配。
header.match('x-test', 'test-value')
canonical header.canonical(string) -> string 使用 Go 的 http.Request 中匹配的规范标头来获取提供的标头名称。
header.canonical('x-test')
truncate
<string>.truncate(uint) -> string
将字符串截断为不超过指定长度。
body.commit.sha.truncate(5)
split
<string>.split(string) -> list(string)
根据提供的分隔符值拆分字符串。
body.ref.split('/')
join
<list(string)>.join(string) -> string
在提供的分隔符值上连接字符串列表。
['body', 'refs', 'main'].join('/')
decodeb64 **deprecated: please use base64.decode**
<string>.decodeb64() -> string
解码 Base64 编码的字符串。
body.message.data.decodeb64()
compareSecret
<string>.compareSecret(string, string, string) -> bool
Constant-time comparison of strings against secrets, this will fetch the secret using the combination of namespace/name and compare the token key to the string using a cryptographic constant-time comparison..

The event-listener service account must have access to the secret. The parameters to the function are 1. the key within the secret, 2. the secret name, and 3. the namespace for the secret (optional, defaults to the namespace of the EventListener).

header.canonical('X-Secret-Token').compareSecret('', 'secret-name', 'namespace')
compareSecret
<string>.compareSecret(string, string) -> bool
This is almost identical to the version above, but only requires two arguments, the namespace is assumed to be the namespace for the event-listener.
header.canonical('X-Secret-Token').compareSecret('key', 'secret-name')
parseJSON()
<string>.parseJSON() -> map<string, dyn>
这会将包含 JSON 主体的字符串解析为随后可在其他表达式中使用的映射。
'{"testing":"value"}'.parseJSON().testing == "value"
parseYAML()
<string>.parseYAML() -> map<string, dyn>
这会将包含 YAML 正文的字符串解析为随后可在其他表达式中使用的映射。
'key1: value1\nkey2: value2\n'.parseYAML().key1 == "value"
parseURL()
<string>.parseURL() -> map<string, dyn>
这会将包含 URL 的字符串解析为包含 URL 元素键的映射。 生成的映射将包含此 URL "https://user:pass@example.com/test/path?s=testing#first"
Field Example
scheme https
host example.com
path /test/path
rawQuery s=testing
fragment first
query {"s": "testing"}
queryStrings {"s": ["testing"]}
auth {"username": "user", "password": "pass"}
请注意查询和查询字符串之间的区别,在查询中,具有相同名称的多个查询参数将以逗号分隔,对于提供单个字符串的情况,这将只是单个字符串值。对于 queryString,查询参数值以列表形式提供,可以通过索引进行访问。
'https://example.com/test?query=testing'.parseURL().query['query'] == "testing"
marshalJSON()
<jsonObjectOrList>.marshalJSON() -> <string>
以字符串形式返回“jsonObjectOrList”的 JSON 编码。
{"testing":"value"}.marshalJSON() == "{\"testing\": \"value\"}"

参考文档

https://tekton.dev/docs/triggers/cel_expressions/

https://github.com/google/cel-spec/