跳至內容

ApiServerSource 參考

version

本主題提供有關 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 的。 使用 matchExpressionsmatchLabels 其中一個
selector.matchExpressions.key 選擇器套用的標籤鍵。 如果使用 matchExpressions 則為必要
selector.matchExpressions.operator 表示鍵與一組值之間的關係。有效的運算子為 InNotInExistsDoesNotExist 如果使用 matchExpressions 則為必要
selector.matchExpressions.values 字串值陣列。如果 operatorInNotIn,則 values 陣列必須為非空。如果 operatorExistsDoesNotExist,則 values 陣列必須為空。此陣列在策略合併修補期間會被取代。 如果使用 matchExpressions 則為必要
selector.matchLabels 鍵值對的對應。matchLabels 對應中的每個鍵值對都相當於 matchExpressions 的元素,其中鍵欄位為 matchLabels.<key>operatorIn,且 values 陣列僅包含 "matchLabels."。需求是 ANDed 的。 使用 matchExpressionsmatchLabels 其中一個

範例: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=myappapp=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 中取得、列出和監看 Pod 資源的權限給 ApiServerSource。

範例 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 的。 使用 matchExpressionsmatchLabels 其中一個
matchExpressions.key 選擇器套用的標籤鍵。 如果使用 matchExpressions 則為必要
matchExpressions.operator 表示鍵與一組值之間的關係。有效的運算子為 InNotInExistsDoesNotExist 如果使用 matchExpressions 則為必要
matchExpressions.values 字串值陣列。如果 operatorInNotIn,則 values 陣列必須為非空。如果 operatorExistsDoesNotExist,則 values 陣列必須為空。此陣列在策略合併修補期間會被取代。 如果使用 matchExpressions 則為必要
matchLabels 鍵值對的對應。matchLabels 對應中的每個鍵值對都相當於 matchExpressions 的元素,其中鍵欄位為 matchLabels.<key>operatorIn,且 values 陣列僅包含 "matchLabels."。需求是 ANDed 的。 使用 matchExpressionsmatchLabels 其中一個

範例:使用 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" } }

我們使用分析和 Cookie 來了解網站流量。您對我們網站的使用資訊會與 Google 分享,以達到此目的。了解更多。