APIServerSource

version

An APIServerSource brings Kubernetes API server events into Knative.

Installation

The APIServerSource source type is enabled by default when you install Knative Eventing.

Example

This example shows how to create an APIServerSource that listens to Kubernetes Events and send CloudEvents to the Event Display Service.

Creating a namespace

Create a new namespace called apiserversource-example by entering the following command:

  1. kubectl create namespace apiserversource-example

Creating the Event Display Service

In this step, you create one event consumer, event-display to verify that APIServerSource is properly working.

To deploy the event-display consumer to your cluster, run the following command:

  1. kubectl -n apiserversource-example apply -f - << EOF
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: event-display
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels: &labels
  10. app: event-display
  11. template:
  12. metadata:
  13. labels: *labels
  14. spec:
  15. containers:
  16. - name: event-display
  17. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  18. ---
  19. kind: Service
  20. apiVersion: v1
  21. metadata:
  22. name: event-display
  23. spec:
  24. selector:
  25. app: event-display
  26. ports:
  27. - protocol: TCP
  28. port: 80
  29. targetPort: 8080
  30. EOF

Creating a Service Account

Create a Service Account that the ApiServerSource runs as. The ApiServerSource watches for Kubernetes events and forwards them to the Knative Eventing Broker.

  1. kubectl -n apiserversource-example apply -f - << EOF
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: events-sa
  6. namespace: apiserversource-example
  7. ---
  8. apiVersion: rbac.authorization.k8s.io/v1
  9. kind: ClusterRole
  10. metadata:
  11. name: event-watcher
  12. rules:
  13. - apiGroups:
  14. - ""
  15. resources:
  16. - events
  17. verbs:
  18. - get
  19. - list
  20. - watch
  21. ---
  22. apiVersion: rbac.authorization.k8s.io/v1
  23. kind: ClusterRoleBinding
  24. metadata:
  25. name: k8s-ra-event-watcher
  26. roleRef:
  27. apiGroup: rbac.authorization.k8s.io
  28. kind: ClusterRole
  29. name: event-watcher
  30. subjects:
  31. - kind: ServiceAccount
  32. name: events-sa
  33. namespace: apiserversource-example
  34. EOF

Creating the APIServerSource

In order to receive kubernetes events, you need to create a concrete APIServerSource for the namespace.

  1. kubectl -n apiserversource-example apply -f - << EOF
  2. apiVersion: sources.knative.dev/v1
  3. kind: ApiServerSource
  4. metadata:
  5. name: testevents
  6. namespace: apiserversource-example
  7. spec:
  8. serviceAccountName: events-sa
  9. mode: Resource
  10. resources:
  11. - apiVersion: v1
  12. kind: Event
  13. sink:
  14. ref:
  15. apiVersion: v1
  16. kind: Service
  17. name: event-display
  18. EOF
  1. kn source apiserver create testevents \
  2. --namespace apiserversource-example \
  3. --mode "Resource" \
  4. --resource "Event:v1" \
  5. --service-account events-sa \
  6. --sink --sink http://event-display.svc.cluster.local

Creating Events

Create events by launching a pod in the default namespace. Create a busybox container and immediately delete it:

  1. kubectl -n apiserversource-example run busybox --image=busybox --restart=Never -- ls
  2. kubectl -n apiserversource-example delete pod busybox

Verify

We will verify that the Kubernetes events were sent into the Knative eventing system by looking at our message dumper function logs.

  1. kubectl -n apiserversource-example logs -l app=event-display --tail=100

You should see log lines similar to:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.apiserver.resource.update
  6. source: https://10.96.0.1:443
  7. subject: /apis/v1/namespaces/apiserversource-example/events/testevents.15dd3050eb1e6f50
  8. id: e0447eb7-36b5-443b-9d37-faf4fe5c62f0
  9. time: 2020-07-28T19:14:54.719501054Z
  10. datacontenttype: application/json
  11. Extensions,
  12. kind: Event
  13. name: busybox.1626008649e617e3
  14. namespace: apiserversource-example
  15. Data,
  16. {
  17. "apiVersion": "v1",
  18. "count": 1,
  19. "eventTime": null,
  20. "firstTimestamp": "2020-07-28T19:14:54Z",
  21. "involvedObject": {
  22. "apiVersion": "v1",
  23. "fieldPath": "spec.containers{busybox}",
  24. "kind": "Pod",
  25. "name": "busybox",
  26. "namespace": "apiserversource-example",
  27. "resourceVersion": "28987493",
  28. "uid": "1efb342a-737b-11e9-a6c5-42010a8a00ed"
  29. },
  30. "kind": "Event",
  31. "lastTimestamp": "2020-07-28T19:14:54Z",
  32. "message": "Started container",
  33. "metadata": {
  34. "creationTimestamp": "2020-07-28T19:14:54Z",
  35. "name": "busybox.1626008649e617e3",
  36. "namespace": "default",
  37. "resourceVersion": "506088",
  38. "selfLink": "/api/v1/namespaces/apiserversource-example/events/busybox.1626008649e617e3",
  39. "uid": "2005af47-737b-11e9-a6c5-42010a8a00ed"
  40. },
  41. "reason": "Started",
  42. "reportingComponent": "",
  43. "reportingInstance": "",
  44. "source": {
  45. "component": "kubelet",
  46. "host": "gke-knative-auto-cluster-default-pool-23c23c4f-xdj0"
  47. },
  48. "type": "Normal"
  49. }

Cleanup

Delete the apiserversource-example namespace and all of its resources from your cluster by entering the following command:

  1. kubectl delete namespace apiserversource-example

Reference Documentation

See the APIServerSource specification.

Contact

For any inquiries about this source, please reach out on to the Knative users group.

Feedback

Was this page helpful?

Glad to hear it! Please tell us how we can improve.

Sorry to hear that. Please tell us how we can improve.