ApiServerSource 参考¶
本主题提供有关 ApiServerSource 对象可配置字段的参考信息。
ApiServerSource¶
ApiServerSource 定义支持以下字段
字段 | 描述 | 必需或可选 |
---|---|---|
apiVersion |
指定 API 版本,例如 sources.knative.dev/v1 。 |
必需 |
kind |
将此资源对象识别为 ApiServerSource 对象。 | 必需 |
metadata |
指定唯一标识 ApiServerSource 对象的元数据。例如,一个 name 。 |
必需 |
spec |
指定此 ApiServerSource 对象的配置信息。 | 必需 |
spec.resources |
源跟踪的资源,以便它可以从 Kubernetes ApiServer 发送相关的生命周期事件。包括一个可选的标签选择器以帮助过滤。 | 必需 |
spec.mode |
EventMode 控制事件的格式。设置为 Reference 以发送正在监视的资源的 dataref 事件类型。事件有效负载中只包含对资源的引用。设置为 Resource 以在有效负载中包含完整的资源生命周期事件。默认为 Reference 。 |
可选 |
spec.owner |
ResourceOwner 是一个额外的过滤器,用于仅跟踪由特定资源类型拥有的资源。如果 ResourceOwner 与 Resources[n] 匹配,则 Resources[n] 被允许通过 ResourceOwner 过滤器。 | 可选 |
spec.serviceAccountName |
用于运行此源的 ServiceAccount 的名称。如果未设置,则默认为 default 。 |
可选 |
spec.sink |
对解析为 URI 以用作接收器的对象的引用。 | 必需 |
spec.ceOverrides |
定义覆盖以控制输出格式和对发送到接收器的事件的修改。 | 可选 |
spec.namespaceSelector |
指定标签选择器以跟踪多个命名空间。如果未指定,将跟踪 ApiServerSource 的命名空间。 | 可选 |
Resources 参数¶
resources
参数指定源跟踪的资源,以便它可以从 Kubernetes ApiServer 发送相关的生命周期事件。该参数包括一个可选的标签选择器以帮助过滤。
resources
定义支持以下字段
字段 | 描述 | 必需或可选 |
---|---|---|
apiVersion |
要监视的资源的 API 版本。 | 必需 |
kind |
要监视的资源的种类。 | 必需 |
selector |
LabelSelector 将此源过滤到那些通过标签选择器的对象的资源。 | 可选 |
selector.matchExpressions |
标签选择器要求的列表。这些要求是 ANDed。 | 使用 matchExpressions 或 matchLabels 之一 |
selector.matchExpressions.key |
选择器应用到的标签键。 | 如果使用 matchExpressions ,则为必需 |
selector.matchExpressions.operator |
表示键与一组值的关系。有效操作符为 In 、NotIn 、Exists 和 DoesNotExist 。 |
如果使用 matchExpressions ,则为必需 |
selector.matchExpressions.values |
字符串值的数组。如果 operator 为 In 或 NotIn ,则 values 数组必须是非空的。如果 operator 为 Exists 或 DoesNotExist ,则 values 数组必须为空。此数组在策略性合并修补期间被替换。 |
如果使用 matchExpressions ,则为必需 |
selector.matchLabels |
键值对的映射。matchLabels 映射中的每个键值对都等效于 matchExpressions 的一个元素,其中键字段为 matchLabels.<key> ,operator 为 In ,而 values 数组只包含“matchLabels”。 |
使用 matchExpressions 或 matchLabels 之一 |
示例:Resources 参数¶
鉴于以下 YAML,ApiServerSource 对象接收命名空间中所有 Pod 和 Deployment 的事件
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
# ...
resources:
- apiVersion: v1
kind: Pod
- apiVersion: apps/v1
kind: Deployment
示例:使用 matchExpressions 的 Resources 参数¶
鉴于以下 YAML,ApiServerSource 对象接收命名空间中所有具有标签 app=myapp
或 app=yourapp
的 Pod 的事件
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
# ...
resources:
- apiVersion: v1
kind: Pod
selector:
matchExpressions:
- key: app
operator: In
values:
- myapp
- yourapp
示例:使用 matchLabels 的 Resources 参数¶
鉴于以下 YAML,ApiServerSource 对象接收命名空间中所有具有标签 app=myapp
的 Pod 的事件
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
# ...
resources:
- apiVersion: v1
kind: Pod
selector:
matchLabels:
app: myapp
ServiceAccountName 参数¶
ServiceAccountName 是对 Kubernetes 服务帐户的引用。
要跟踪指定 resources
的生命周期事件,您必须为 ApiServerSource 对象分配适当的权限。
示例:跟踪 Pod¶
以下 YAML 文件创建了一个 ServiceAccount、Role 和 RoleBinding,并授予在 apiserversource-example
命名空间中为 ApiServerSource 获取、列出和监视 Pod 资源的权限。
示例 ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
name: test-service-account
namespace: apiserversource-example
示例 Role,具有获取、列出和监视 Pod 资源的权限
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: test-role
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
示例 RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: test-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: test-role
subjects:
- kind: ServiceAccount
name: test-service-account
namespace: apiserversource-example
示例 ApiServerSource,使用 test-service-account
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: test-apiserversource
namespace: apiserversource-example
spec:
# ...
serviceAccountName: test-service-account
...
Owner 参数¶
ResourceOwner 是一个额外的过滤器,用于仅跟踪由特定资源类型拥有的资源。如果 ResourceOwner 与 Resources[n] 匹配,则 Resources[n] 被允许通过 ResourceOwner 过滤器。
owner
定义支持以下字段
字段 | 描述 | 必需或可选 |
---|---|---|
apiVersion |
要监视的资源的 API 版本。 | 必需 |
kind |
要监视的资源的种类。 | 必需 |
示例:Owner 参数¶
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
...
owner:
apiVersion: apps/v1
kind: Deployment
...
NamespaceSelector 参数¶
NamespaceSelector 是一个可选的标签选择器,可用于定位多个命名空间。如果选择器未设置,将跟踪 ApiServerSource 的命名空间。
namespaceSelector
支持以下字段
字段 | 描述 | 必需或可选 |
---|---|---|
matchExpressions |
标签选择器要求的列表。这些要求是 ANDed。 | 使用 matchExpressions 或 matchLabels 之一 |
matchExpressions.key |
选择器应用到的标签键。 | 如果使用 matchExpressions ,则为必需 |
matchExpressions.operator |
表示键与一组值的关系。有效操作符为 In 、NotIn 、Exists 和 DoesNotExist 。 |
如果使用 matchExpressions ,则为必需 |
matchExpressions.values |
字符串值的数组。如果 operator 为 In 或 NotIn ,则 values 数组必须是非空的。如果 operator 为 Exists 或 DoesNotExist ,则 values 数组必须为空。此数组在策略性合并修补期间被替换。 |
如果使用 matchExpressions ,则为必需 |
matchLabels |
键值对的映射。matchLabels 映射中的每个键值对都等效于 matchExpressions 的一个元素,其中键字段为 matchLabels.<key> ,operator 为 In ,而 values 数组只包含“matchLabels”。 |
使用 matchExpressions 或 matchLabels 之一 |
示例:使用 matchExpressions 目标多个命名空间¶
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
...
namespaceSelector:
matchExpressions:
- key: environment
operator: In
values:
- production
- development
...
示例:使用 matchLabels 目标多个命名空间¶
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
...
namespaceSelector:
matchLabels:
environment: production
...
示例:使用空选择器目标所有命名空间¶
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
...
namespaceSelector: {}
...
CloudEvent 覆盖¶
CloudEvent 覆盖定义覆盖以控制输出格式和对发送到接收器的事件的修改。
ceOverrides
定义支持以下字段
字段 | 描述 | 必需或可选 |
---|---|---|
extensions |
指定在出站事件上添加或覆盖哪些属性。每个 extensions 键值对在事件上独立设置,作为属性扩展。 |
可选 |
注意
只有有效的 CloudEvent 属性名称 才能作为扩展使用。您不能从扩展覆盖配置中设置规范定义的属性。例如,您不能修改 type
属性。
示例:CloudEvent 覆盖¶
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: <apiserversource>
namespace: <namespace>
spec:
...
ceOverrides:
extensions:
extra: this is an extra attribute
additional: 42
契约
这会导致 K_CE_OVERRIDES
环境变量在接收器容器上设置如下
{ "extensions": { "extra": "this is an extra attribute", "additional": "42" } }