跳至内容

新的触发器过滤器

标志名称: new-apiserversource-filters

阶段: Alpha,默认情况下禁用

跟踪问题: #7791

概述

此功能在 APIServerSource 中启用了一个新的 filters 字段,该字段符合 CloudEvents Subscriptions API 中定义的过滤器 API 字段。它允许用户指定一组功能强大的过滤器表达式,其中每个表达式都会针对每个事件计算为真或假。

以下示例显示了使用新 filters 字段的 APIServerSource

---
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
 name: my-apiserversource
 namespace: default
spec:
  filters:
  - any:
    - exact:
        type: dev.knative.apiserver.ref.add

  serviceAccountName: apiserversource
  mode: Reference
  resources: ...
  sink: ...

关于 filters 字段

  • 一个过滤器表达式的数组,计算为真或假。如果数组中的任何过滤器表达式计算为假,则不会将事件发送到 sink
  • 每个过滤器表达式都遵循一种方言,该方言定义了过滤器的类型以及过滤器表达式中允许的一组附加属性。

支持的过滤器方言

filters 字段支持以下方言

exact

CloudEvent 属性字符串值必须与指定的字符串值完全匹配。匹配区分大小写。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - exact:
        type: com.github.push

prefix

CloudEvent 属性字符串值必须以指定的字符串值开头。匹配区分大小写。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - prefix:
        type: com.github.

suffix

CloudEvent 属性字符串值必须以指定的字符串值结尾。匹配区分大小写。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - suffix:
        type: .created

all

所有嵌套的过滤器表达式都必须计算为真。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - all:
        - exact:
            type: com.github.push
        - exact:
            subject: https://github.com/cloudevents/spec

any

至少一个嵌套的过滤器表达式必须计算为真。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - any:
        - exact:
            type: com.github.push
        - exact:
            subject: https://github.com/cloudevents/spec

not

计算的嵌套表达式必须计算为假。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
      - not:
          exact:
              type: com.github.push

cesql

提供的 CloudEvents SQL 表达式 必须计算为真。

apiVersion: sources.knative.dev/v1
kind: APIServerSource
metadata:
  ...
spec:
  ...
  filters:
    - cesql: "source LIKE '%commerce%' AND type IN ('order.created', 'order.updated', 'order.canceled')"

常见问题解答

APIServerSource 提供哪些事件类型?

  • dev.knative.apiserver.resource.add
  • dev.knative.apiserver.resource.update
  • dev.knative.apiserver.resource.delete
  • dev.knative.apiserver.ref.add
  • dev.knative.apiserver.ref.update
  • dev.knative.apiserver.ref.delete

我们使用分析和 Cookie 来了解网站流量。有关您使用我们网站的信息将与 Google 共享,以实现该目的。 了解更多。