Creating an ApiServerSource object

version

This topic describes how to create an ApiServerSource object.

Before you begin

Before you can create an ApiServerSource object:

  • You must have Knative Eventing installed on your cluster.
  • You must install the kubectl CLI tool.
  • Optional: If you want to use the kn commands, install the kn tool.

Create an ApiServerSource object

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

    1. kubectl create namespace <namespace>

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

    Note

    Creating a namespace for your ApiServerSource 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 ServiceAccount by running the command:

    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 ServiceAccount that you want to create.
    • <namespace> is the namespace that you created in step 1 above.
  3. Create a ClusterRole by running the command:

    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 ClusterRole that you want to create.

  4. Create a ClusterRoleBinding by running the command:

    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 ClusterRoleBinding that you want to create.
    • <cluster-role> is the name of the ClusterRole that you created in step 3 above.
    • <service-account> is the name of the ServiceAccount that you created in step 2 above.
    • <namespace> is the name of the namespace that you created in step 1 above.
  5. Create the ApiServerSource object by running the command:

    kn

    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 ServiceAccount that you created in step 2 above.
    • <sink> is the name of the PodSpecable object that you want to use as a sink. A PodSpecable is an object that describes a PodSpec.

    YAML

    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: <event-mode>
    10. resources:
    11. - apiVersion: v1
    12. kind: Event
    13. sink:
    14. ref:
    15. apiVersion: v1
    16. kind: <sink-kind>
    17. name: <sink-name>
    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 ServiceAccount that you created in step 2 above.
    • <event-mode> is either Resource or Reference. If set to Resource, the event payload contains the entire resource that the event is for. If set to Reference, the event payload only contains a reference to the resource that the event is for. The default is Reference.
    • <sink-kind> is any supported PodSpecable object that you want to use as a sink, for example, Service or Deployment. A PodSpecable is an object that describes a PodSpec.
    • <sink-name> is the name of your sink.

Verify the ApiServerSource object

  1. Make the Kubernetes API server create events by launching a test pod in your namespace by running the command:

    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.

  2. Delete the test pod by running the command:

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

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

  3. View the logs to verify that Kubernetes events were sent to the sink by the Knative Eventing system by running the command:

    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 ApiServerSource

To remove the ApiServerSource object and all of the related resources:

  • Delete the namespace by running the command:

    1. kubectl delete namespace <namespace>

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