Kubernetes event using the API Server Source

    1. You must have a Knative cluster running both the Serving and Eventing components. To learn how to install the required components, see Installing Knative.

    2. You can follow the steps below to create new files, or you clone a copy from the repo by running:

    Deployment Steps

    These instructions assume the namespace , which you can change to your preferred namespace. If you use a different namespace, you will need to modify all the YAML files deployed in this sample to point at that namespace.

    1. Create the default Broker in your namespace:

      1. kubectl create -f - <<EOF
      2. apiVersion: eventing.knative.dev/v1
      3. kind: Broker
      4. metadata:
      5. name: default
      6. EOF

    Service Account

    1. Create a Service Account that the ApiServerSource runs as. The ApiServerSource watches for Kubernetes events and forwards them to the Knative Eventing Broker. Create a file named serviceaccount.yaml and copy the code block below into it.

      1. apiVersion: v1
      2. kind: ServiceAccount
      3. metadata:
      4. name: events-sa
      5. namespace: default
      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. subjects:
      29. - kind: ServiceAccount
      30. name: events-sa
      31. namespace: default
    2. Enter the following command to create the service account from serviceaccount.yaml:

      1. kubectl apply --filename serviceaccount.yaml
    1. In order to receive events, you have to create a concrete Event Source for a specific namespace. Create a file named k8s-events.yaml and copy the code block below into it.

      If you want to consume events from a different namespace or use a different Service Account, you need to modify k8s-events.yaml accordingly.

    2. Enter the following command to create the event source:

    Trigger

    In order to check the ApiServerSource is fully working, we will create a simple Knative Service that dumps incoming messages to its log and creates a Trigger from the Broker to that Knative Service.

      1. apiVersion: eventing.knative.dev/v1
      2. kind: Trigger
      3. metadata:
      4. name: testevents-trigger
      5. namespace: default
      6. spec:
      7. broker: default
      8. subscriber:
      9. ref:
      10. apiVersion: serving.knative.dev/v1
      11. kind: Service
      12. name: event-display
      13. ---
      14. # This is a very simple Knative Service that writes the input request to its log.
      15. apiVersion: serving.knative.dev/v1
      16. kind: Service
      17. metadata:
      18. name: event-display
      19. namespace: default
      20. spec:
      21. template:
      22. spec:
      23. containers:
      24. - # This corresponds to
      25. # https://github.com/knative/eventing-contrib/tree/main/cmd/event_display/main.go
      26. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
    1. If the deployed ApiServerSource is pointing at a Broker other than default, modify trigger.yaml by adding spec.broker with the Broker’s name.

    2. Deploy trigger.yaml:

      1. kubectl apply --filename trigger.yaml

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

    Verify

    We will verify that the Kubernetes events were sent into the Knative eventing system by looking at our message dumper function logs. If you deployed the , continue using this section. If not, you will need to look downstream yourself:

    1. kubectl get pods
    2. kubectl logs -l serving.knative.dev/service=event-display -c user-container

    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. subject: /apis/v1/namespaces/default/events/testevents.15dd3050eb1e6f50
    7. time: 2020-07-28T16:35:14.172979816Z
    8. datacontenttype: application/json
    9. Extensions,
    10. kind: Event
    11. knativearrivaltime: 2020-07-28T16:35:14.173381505Z
    12. knativehistory: default-kne-trigger-kn-channel.default.svc.cluster.local
    13. name: busybox.1625f7cfa4cd12f8
    14. namespace: default
    15. Data,
    16. {
    17. "apiVersion": "v1",
    18. "count": 1,
    19. "eventTime": null,
    20. "firstTimestamp": "2020-07-28T16:35:14Z",
    21. "involvedObject": {
    22. "apiVersion": "v1",
    23. "fieldPath": "spec.containers{busybox}",
    24. "kind": "Pod",
    25. "name": "busybox",
    26. "namespace": "default",
    27. "resourceVersion": "28987493",
    28. "uid": "1efb342a-737b-11e9-a6c5-42010a8a00ed"
    29. },
    30. "kind": "Event",
    31. "lastTimestamp": "2020-07-28T16:35:14Z",
    32. "message": "Started container",
    33. "metadata": {
    34. "creationTimestamp": "2020-07-28T16:35:14Z",
    35. "name": "busybox.1625f7cfa4cd12f8",
    36. "namespace": "default",
    37. "resourceVersion": "506088",
    38. "selfLink": "/api/v1/namespaces/default/events/busybox.1625f7cfa4cd12f8",
    39. "uid": "7f841049-7979-48db-9cbc-93ed2346a1b5",
    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. }
    1. kubectl --namespace default delete --filename serviceaccount.yaml
    2. kubectl --namespace default delete --filename trigger.yaml