ApiServerSource reference

version

This topic provides reference information about the configurable fields for the ApiServerSource object.

ApiServerSource

An ApiServerSource definition supports the following fields:

FieldDescriptionRequired or optional
apiVersionSpecifies the API version, for example sources.knative.dev/v1.Required
kindIdentifies this resource object as an ApiServerSource object.Required
metadataSpecifies metadata that uniquely identifies the ApiServerSource object. For example, a name.Required
specSpecifies the configuration information for this ApiServerSource object.Required
spec.resourcesThe resources that the source tracks so it can send related lifecycle events from the Kubernetes ApiServer. Includes an optional label selector to help filter.Required
spec.modeEventMode controls the format of the event. Set to Reference to send a dataref event type for the resource being watched. Only a reference to the resource is included in the event payload. Set to Resource to have the full resource lifecycle event in the payload. Defaults to Reference.Optional
spec.ownerResourceOwner is an additional filter to only track resources that are owned by a specific resource type. If ResourceOwner matches Resources[n] then Resources[n] is allowed to pass the ResourceOwner filter.Optional
spec.serviceAccountNameThe name of the ServiceAccount to use to run this source. Defaults to default if not set.Optional
spec.sinkA reference to an object that resolves to a URI to use as the sink.Required
spec.ceOverridesDefines overrides to control the output format and modifications to the event sent to the sink.Optional
spec.namespaceSelectorSpecifies a label selector to track multiple namespaces. If unspecified, the namespace of the ApiServerSource will be tracked.Optional

Resources parameter

The resources parameter specifies the resources that the source tracks so that it can send related lifecycle events from the Kubernetes ApiServer. The parameter includes an optional label selector to help filter.

A resources definition supports the following fields:

FieldDescriptionRequired or optional
apiVersionAPI version of the resource to watch.Required
kindKind of the resource to watch.Required
selectorLabelSelector filters this source to objects to those resources pass the label selector.Optional
selector.matchExpressionsA list of label selector requirements. The requirements are ANDed.Use one of matchExpressions or matchLabels
selector.matchExpressions.keyThe label key that the selector applies to.Required if using matchExpressions
selector.matchExpressions.operatorRepresents a key’s relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.Required if using matchExpressions
selector.matchExpressions.valuesAn array of string values. If operator is In or NotIn, the values array must be non-empty. If operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.Required if using matchExpressions
selector.matchLabelsA map of key-value pairs. Each key-value pair in the matchLabels map is equivalent to an element of matchExpressions, where the key field is matchLabels.<key>, the operator is In, and the values array contains only “matchLabels.“. The requirements are ANDed.Use one of matchExpressions or matchLabels

Example: Resources parameter

Given the following YAML, the ApiServerSource object receives events for all Pods and Deployments in the namespace:

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. # ...
  8. resources:
  9. - apiVersion: v1
  10. kind: Pod
  11. - apiVersion: apps/v1
  12. kind: Deployment

Example: Resources parameter using matchExpressions

Given the following YAML, ApiServerSource object receives events for all Pods in the namespace that have a label app=myapp or app=yourapp:

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. # ...
  8. resources:
  9. - apiVersion: v1
  10. kind: Pod
  11. selector:
  12. matchExpressions:
  13. - key: app
  14. operator: In
  15. values:
  16. - myapp
  17. - yourapp

Example: Resources parameter using matchLabels

Given the following YAML, the ApiServerSource object receives events for all Pods in the namespace that have a label app=myapp:

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. # ...
  8. resources:
  9. - apiVersion: v1
  10. kind: Pod
  11. selector:
  12. matchLabels:
  13. app: myapp

ServiceAccountName parameter

ServiceAccountName is a reference to a Kubernetes service account.

To track the lifecycle events of the specified resources, you must assign the proper permissions to the ApiServerSource object.

Example: tracking Pods

The following YAML files create a ServiceAccount, Role and RoleBinding and grant the permission to get, list and watch Pod resources in the namespace apiserversource-example for the ApiServerSource.

Example ServiceAccount:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: test-service-account
  5. namespace: apiserversource-example

Example Role with permission to get, list and watch Pod resources:

  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: Role
  3. metadata:
  4. name: test-role
  5. rules:
  6. - apiGroups:
  7. - ""
  8. resources:
  9. - pods
  10. verbs:
  11. - get
  12. - list
  13. - watch

Example RoleBinding:

  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: RoleBinding
  3. metadata:
  4. name: test-role-binding
  5. roleRef:
  6. apiGroup: rbac.authorization.k8s.io
  7. kind: Role
  8. name: test-role
  9. subjects:
  10. - kind: ServiceAccount
  11. name: test-service-account
  12. namespace: apiserversource-example

Example ApiServerSource using test-service-account:

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: test-apiserversource
  5. namespace: apiserversource-example
  6. spec:
  7. # ...
  8. serviceAccountName: test-service-account
  9. ...

Owner parameter

ResourceOwner is an additional filter to only track resources that are owned by a specific resource type. If ResourceOwner matches Resources[n] then Resources[n] is allowed to pass the ResourceOwner filter.

An owner definition supports the following fields:

FieldDescriptionRequired or optional
apiVersionAPI version of the resource to watch.Required
kindKind of the resource to watch.Required

Example: Owner parameter

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. ...
  8. owner:
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. ...

NamespaceSelector parameter

The NamespaceSelector is an optional label selector that can be utilized to target more than one namespace. If the selector is unset, the namespace of the ApiServerSource will be tracked.

A namespaceSelector supports the following fields:

FieldDescriptionRequired or optional
matchExpressionsA list of label selector requirements. The requirements are ANDed.Use one of matchExpressions or matchLabels
matchExpressions.keyThe label key that the selector applies to.Required if using matchExpressions
matchExpressions.operatorRepresents a key’s relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.Required if using matchExpressions
matchExpressions.valuesAn array of string values. If operator is In or NotIn, the values array must be non-empty. If operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.Required if using matchExpressions
matchLabelsA map of key-value pairs. Each key-value pair in the matchLabels map is equivalent to an element of matchExpressions, where the key field is matchLabels.<key>, the operator is In, and the values array contains only “matchLabels.“. The requirements are ANDed.Use one of matchExpressions or matchLabels

Example: Target multiple namespaces with matchExpressions

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. ...
  8. namespaceSelector:
  9. matchExpressions:
  10. - key: environment
  11. operator: In
  12. values:
  13. - production
  14. - development
  15. ...

Example: Target multiple namespaces with matchLabels

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. ...
  8. namespaceSelector:
  9. matchLabels:
  10. environment: production
  11. ...

Example: Target all namespaces with an empty selector

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. ...
  8. namespaceSelector: {}
  9. ...

CloudEvent Overrides

CloudEvent Overrides defines overrides to control the output format and modifications of the event sent to the sink.

A ceOverrides definition supports the following fields:

FieldDescriptionRequired or optional
extensionsSpecifies which attributes are added or overridden on the outbound event. Each extensions key-value pair is set independently on the event as an attribute extension.Optional

Note

Only valid CloudEvent attribute names are allowed as extensions. You cannot set the spec defined attributes from the extensions override configuration. For example, you can not modify the type attribute.

Example: CloudEvent Overrides

  1. apiVersion: sources.knative.dev/v1
  2. kind: ApiServerSource
  3. metadata:
  4. name: <apiserversource>
  5. namespace: <namespace>
  6. spec:
  7. ...
  8. ceOverrides:
  9. extensions:
  10. extra: this is an extra attribute
  11. additional: 42

Contract

This results in the K_CE_OVERRIDES environment variable being set on the sink container as follows:

  1. { "extensions": { "extra": "this is an extra attribute", "additional": "42" } }