Getting started

Prerequisites

Before you can create an API server source, you must install Knative Eventing and the kubectl CLI tool.

Create an API server source

  1. Optional: Create a namespace for the API server source instance:

    1. kubectl create namespace <namespace>

    where;

    • <namespace> is the name of the namespace that you want to create.

    Creating a namespace for your API server source and related components allows you to view changes and events for this workflow more easily, since these are isolated from the many other components that may exist in your default namespace.

    It also makes removing the source easier, since you can simply delete the namespace to remove all of the resources.

  2. Create a service account:

    1. kubectl create -f - <<EOF
    2. apiVersion: v1
    3. kind: ServiceAccount
    4. metadata:
    5. name: <service-account>
    6. namespace: <namespace>
    7. EOF

    where;

    • <service-account> is the name of the service account that you want to create.
    • <namespace> is the namespace that you created in step 1 above.
  3. Create a cluster role:

    1. kubectl create -f - <<EOF
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: ClusterRole
    4. metadata:
    5. name: <cluster-role>
    6. rules:
    7. - apiGroups:
    8. - ""
    9. resources:
    10. - events
    11. verbs:
    12. - get
    13. - list
    14. - watch
    15. EOF

    where;

    • <cluster-role> is the name of the cluster role that you want to create.
  4. Create a cluster role binding:

    1. kubectl create -f - <<EOF
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: ClusterRoleBinding
    4. metadata:
    5. name: <cluster-role-binding>
    6. roleRef:
    7. apiGroup: rbac.authorization.k8s.io
    8. kind: ClusterRole
    9. name: <cluster-role>
    10. subjects:
    11. - kind: ServiceAccount
    12. name: <service-account>
    13. namespace: <namespace>
    14. EOF

    where;

    • <cluster-role-binding> is the name of the cluster role binding that you want to create.
    • <cluster-role> is the name of the cluster role that you created in step 3 above.
    • <service-account> is the name of the service account that you created in step 2 above.
    • <namespace> is the name of the namespace that you created in step 1 above.
  5. Create an ApiServerSource object:

    1. kubectl create -f - <<EOF
    2. apiVersion: sources.knative.dev/v1
    3. kind: ApiServerSource
    4. metadata:
    5. name: <apiserversource>
    6. namespace: <namespace>
    7. spec:
    8. serviceAccountName: <service-account>
    9. mode: Resource
    10. resources:
    11. - apiVersion: v1
    12. kind: Event
    13. sink:
    14. ref:
    15. apiVersion: v1
    16. kind: Service
    17. name: <sink>
    18. EOF

    where;

    • <apiserversource> is the name of the source that you want to create.
    • <namespace> is the name of the namespace that you created in step 1 above.
    • <service-account> is the name of the service account that you created in step 2 above.
    • <sink> is the name of the Knative service that you want to use as a sink. A service is used here as an example, however you can use any supported PodSpecable object by updating the kind from Service to another object type.
    1. kn source apiserver create <apiserversource> \
    2. --namespace <namespace> \
    3. --mode "Resource" \
    4. --resource "Event:v1" \
    5. --service-account <service-account> \
    6. --sink <sink>

    where;

    • <apiserversource> is the name of the source that you want to create.
    • <namespace> is the name of the namespace that you created in step 1 above.
    • <service-account> is the name of the service account that you created in step 2 above.
    • <sink> is the name of the PodSpecable object that you want to use as a sink.
  6. Create events by launching a test pod in your namespace:

    1. kubectl run busybox --image=busybox --namespace=<namespace> --restart=Never -- ls

    where;

    • <namespace> is the name of the namespace that you created in step 1 above.
  7. Delete the test pod:

    1. kubectl --namespace=<namespace> delete pod busybox

    where;

    • <namespace> is the name of the namespace that you created in step 1 above.
  8. View the logs to verify that Kubernetes events were sent to the Knative Eventing system:

    1. kubectl logs --namespace=<namespace> -l app=<sink> --tail=100

    where;

    • <namespace> is the name of the namespace that you created in step 1 above.
    • <sink> is the name of the PodSpecable object that you used as a sink in step 5 above.

    Example log output:

    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. }

Delete the API server source

Deleting the namespace removes the API server source and all of the related resources that were created in this namespace:

  1. kubectl delete namespace <namespace>

where;

  • <namespace> is the name of the namespace that you created in step 1 above.