// Module included in the following assemblies:
//
// serverless/event_workflows/serverless-listing-event-sources.adoc

[id="apiserversource-kn_context"]
= Using the ApiServerSource with the Knative CLI (`kn`)

This section describes the steps required to create an ApiServerSource using `kn` commands.

.Prerequisites

* You must have {ServerlessProductName}, the Knative Serving and Eventing components, and the `kn` CLI installed.

.Procedure

. Create an ApiServerSource that uses a broker as an event sink:
+
[source,terminal]
----
$ kn source apiserver create <event_source_name> --sink broker:<broker_name> --resource "event:v1" --service-account <service_account_name> --mode Resource
----

. To check that the ApiServerSource is set up correctly, create a Knative service that dumps incoming messages to its log:
+
[source,terminal]
----
$ kn service create <service_name> --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
----

. Create a trigger to filter events from the `default` broker to the service:
+
[source,terminal]
----
$ kn trigger create <trigger_name> --sink ksvc:<service_name>
----


. Create events by launching a Pod in the default namespace:
+
[source,terminal]
----
$ oc create deployment hello-node --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
----

. Check that the controller is mapped correctly by inspecting the output generated by the following command:
+
[source,terminal]
----
$ kn source apiserver describe <source_name>
----
+
.Example output
+
[source,terminal]
----
Name:                mysource
Namespace:           default
Annotations:         sources.knative.dev/creator=developer, sources.knative.dev/lastModifier=developer
Age:                 3m
ServiceAccountName:  events-sa
Mode:                Resource
Sink:
  Name:       default
  Namespace:  default
  Kind:       Broker (eventing.knative.dev/v1)
Resources:
  Kind:        event (v1)
  Controller:  false
Conditions:
  OK TYPE                     AGE REASON
  ++ Ready                     3m
  ++ Deployed                  3m
  ++ SinkProvided              3m
  ++ SufficientPermissions     3m
  ++ EventTypesProvided        3m
----

.Verification steps

You can verify that the Kubernetes events were sent to Knative by looking at the message dumper function logs.

. Get the pods:
+
[source,terminal]
----
$ oc get pods
----

. View the message dumper function logs for the pods:
+
[source,terminal]
----
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
----
+
.Example output
[source,terminal]
----
☁️  cloudevents.Event
Validation: valid
Context Attributes,
  specversion: 1.0
  type: dev.knative.apiserver.resource.update
  datacontenttype: application/json
  ...
Data,
  {
    "apiVersion": "v1",
    "involvedObject": {
      "apiVersion": "v1",
      "fieldPath": "spec.containers{hello-node}",
      "kind": "Pod",
      "name": "hello-node",
      "namespace": "default",
       .....
    },
    "kind": "Event",
    "message": "Started container",
    "metadata": {
      "name": "hello-node.159d7608e3a3572c",
      "namespace": "default",
      ....
    },
    "reason": "Started",
    ...
  }
----
