1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 21:46:22 +01:00
Files
openshift-docs/modules/apiserversource-kn.adoc
2023-10-30 10:13:25 -04:00

157 lines
4.2 KiB
Plaintext

// Module included in the following assemblies:
//
// * serverless/eventing/event-sources/serverless-apiserversource.adoc
// * serverless/reference/kn-eventing-ref.adoc
:_mod-docs-content-type: PROCEDURE
[id="apiserversource-kn_{context}"]
= Creating an API server source by using the Knative CLI
You can use the `kn source apiserver create` command to create an API server source by using the `kn` CLI. Using the `kn` CLI to create an API server source provides a more streamlined and intuitive user interface than modifying YAML files directly.
.Prerequisites
* The {ServerlessOperatorName} and Knative Eventing are installed on the cluster.
* You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in {product-title}.
* You have installed the OpenShift CLI (`oc`).
* You have installed the Knative (`kn`) CLI.
.Procedure
include::snippets/serverless-service-account-apiserversource.adoc[]
. Create an API server source that has an event sink. In the following example, the sink is a broker:
+
[source,terminal]
----
$ kn source apiserver create <event_source_name> --sink broker:<broker_name> --resource "event:v1" --service-account <service_account_name> --mode Resource
----
// need to revisit these docs and give better tutorial examples with different sinks; out of scope for the current PR
. To check that the API server source 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
----
. If you used a broker as an event sink, 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
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",
...
}
----
.Deleting the API server source
. Delete the trigger:
+
[source,terminal]
----
$ kn trigger delete <trigger_name>
----
. Delete the event source:
+
[source,terminal]
----
$ kn source apiserver delete <source_name>
----
. Delete the service account, cluster role, and cluster binding:
+
[source,terminal]
----
$ oc delete -f authentication.yaml
----