APIServerSource

    An APIServerSource brings Kubernetes API server events into Knative.

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

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

    Create a new namespace called by entering the following command:

    Creating the Event Display Service

    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

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

    Creating the APIServerSource

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

    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

    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

    You should see log lines similar to:

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

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

    1. kubectl delete namespace apiserversource-example

    See the .

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