This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Este conteúdo não está disponível no idioma selecionado.
Chapter 11. Event sources
11.1. Getting started with event sources Copiar o linkLink copiado para a área de transferência!
An event source is an object that links an event producer with an event sink, or consumer. A sink can be a Knative service, channel, or broker that receives events from an event source.
Currently, OpenShift Serverless supports the following event source types:
- ApiServerSource
- Connects a sink to the Kubernetes API server.
- PingSource
- Periodically sends ping events with a constant payload. It can be used as a timer.
Sink binding is also supported, which allows you to connect core Kubernetes resources such as Deployment
, Job
, or StatefulSet
with a sink.
You can create and manage Knative event sources using the Developer perspective in the OpenShift Container Platform web console, the kn
CLI, or by applying YAML files.
11.1.1. Prerequisites Copiar o linkLink copiado para a área de transferência!
- You must have a current installation of OpenShift Serverless, including Knative Serving and Eventing, in your OpenShift Container Platform cluster. This can be installed by a cluster administrator.
11.1.2. Creating event sources Copiar o linkLink copiado para a área de transferência!
- Create an ApiServerSource object.
- Create a PingSource object.
11.1.3. Additional resources Copiar o linkLink copiado para a área de transferência!
- For more information about eventing workflows using OpenShift Serverless, see Knative Eventing architecture.
11.2. Using the Knative CLI to list event sources and event source types Copiar o linkLink copiado para a área de transferência!
You can use the kn
CLI to list and manage available event sources or event source types for use with Knative Eventing.
Currently, kn
supports management of the following event source types:
- API server source
-
Connects a sink to the Kubernetes API server by creating an
APIServerSource
object. - Ping source
-
Periodically sends ping events with a constant payload. It can be used as a timer, and is created as a
PingSource
object.
11.2.1. Listing available event source types using the Knative CLI Copiar o linkLink copiado para a área de transferência!
You can list the available event source types in the terminal by using the following command:
kn source list-types
$ kn source list-types
The default output for this command will look like:
TYPE NAME DESCRIPTION ApiServerSource apiserversources.sources.knative.dev Watch and send Kubernetes API events to a sink PingSource pingsources.sources.knative.dev Periodically send ping events to a sink SinkBinding sinkbindings.sources.knative.dev Binding for connecting a PodSpecable to a sink
TYPE NAME DESCRIPTION
ApiServerSource apiserversources.sources.knative.dev Watch and send Kubernetes API events to a sink
PingSource pingsources.sources.knative.dev Periodically send ping events to a sink
SinkBinding sinkbindings.sources.knative.dev Binding for connecting a PodSpecable to a sink
It is also possible to list available event source types in YAML format:
kn source list-types -o yaml
$ kn source list-types -o yaml
11.2.2. Listing available event sources using the Knative CLI Copiar o linkLink copiado para a área de transferência!
You can list the available event sources in the terminal by using the following command:
kn source list
$ kn source list
Example output
NAME TYPE RESOURCE SINK READY a1 ApiServerSource apiserversources.sources.knative.dev svc:eshow2 True b1 SinkBinding sinkbindings.sources.knative.dev svc:eshow3 False p1 PingSource pingsources.sources.knative.dev svc:eshow1 True
NAME TYPE RESOURCE SINK READY
a1 ApiServerSource apiserversources.sources.knative.dev svc:eshow2 True
b1 SinkBinding sinkbindings.sources.knative.dev svc:eshow3 False
p1 PingSource pingsources.sources.knative.dev svc:eshow1 True
You can list event sources of a specific type only, by using the --type
flag.
kn source list --type PingSource
$ kn source list --type PingSource
Example output
NAME TYPE RESOURCE SINK READY p1 PingSource pingsources.sources.knative.dev svc:eshow1 True
NAME TYPE RESOURCE SINK READY
p1 PingSource pingsources.sources.knative.dev svc:eshow1 True
11.2.3. Next steps Copiar o linkLink copiado para a área de transferência!
- See the documentation on Using the API server source.
- See the documentation on Using a ping source.
11.3. Using the API server source Copiar o linkLink copiado para a área de transferência!
An API server source is an event source that can be used to connect an event sink, such as a Knative service, to the Kubernetes API server. An API server source watches for Kubernetes events and forwards them to the Knative Eventing broker.
Both of the following procedures require you to create YAML files.
If you change the names of the YAML files from those used in the examples, you must ensure that you also update the corresponding CLI commands.
11.3.1. Using the API server source with the Knative CLI Copiar o linkLink copiado para a área de transferência!
This section describes the steps required to create an ApiServerSource
object using kn
commands.
Prerequisites
- Knative Serving and Eventing are installed on your cluster.
-
You have created the
default
broker in the same namespace that the API server source will be installed in. -
You have the
kn
CLI installed.
Procedure
Create a service account, role, and role binding for the
ApiServerSource
object.You can do this by creating a file named
authentication.yaml
and copying the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you want to reuse an existing service account with the appropriate permissions, you must modify the
authentication.yaml
file for that service account.Create the service account, role binding and cluster binding:
oc apply -f authentication.yaml
$ oc apply -f authentication.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create an
ApiServerSource
object that uses a broker as an event sink:kn source apiserver create <event_source_name> --sink broker:<broker_name> --resource "event:v1" --service-account <service_account_name> --mode Resource
$ kn source apiserver create <event_source_name> --sink broker:<broker_name> --resource "event:v1" --service-account <service_account_name> --mode Resource
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a Knative service that dumps incoming messages to its log:
kn service create <service_name> --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
$ kn service create <service_name> --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a trigger to filter events from the
default
broker to the service:kn trigger create <trigger_name> --sink svc:<service_name>
$ kn trigger create <trigger_name> --sink svc:<service_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create events by launching a Pod in the default namespace:
oc create deployment hello-node --image=quay.io/openshift-knative/knative-eventing-sources-event-display
$ oc create deployment hello-node --image=quay.io/openshift-knative/knative-eventing-sources-event-display
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the controller is mapped correctly by inspecting the output generated by the following command:
kn source apiserver describe testevents
$ kn source apiserver describe testevents
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
You can verify that the Kubernetes events were sent to Knative by looking at the message dumper function logs.
Get the pods:
oc get pods
$ oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the message dumper function logs for the pods:
oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.3.2. Deleting an API server source using the Knative CLI Copiar o linkLink copiado para a área de transferência!
This section describes the steps used to delete the ApiServerSource
object, trigger, service, service account, cluster role, and cluster binding using the kn
and oc
commands.
Prerequisites
-
You must have the
kn
CLI installed.
Procedure
Delete the trigger:
kn trigger delete <trigger_name>
$ kn trigger delete <trigger_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the service:
kn service delete <service_name>
$ kn service delete <service_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the API server source:
kn source apiserver delete <source_name>
$ kn source apiserver delete <source_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Delete the service account, cluster role, and cluster binding:
oc delete -f authentication.yaml
$ oc delete -f authentication.yaml
11.3.3. Creating an API server source using YAML files Copiar o linkLink copiado para a área de transferência!
This guide describes the steps required to create an ApiServerSource
object using YAML files.
Prerequisites
- Knative Serving and Eventing are installed on your cluster.
-
You have created the
default
broker in the same namespace as the one defined in theApiServerSource
object.
Procedure
To create a service account, role, and role binding for the API server source, create a file named
authentication.yaml
and copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you want to re-use an existing service account with the appropriate permissions, you must modify the
authentication.yaml
for that service account.After you have created the
authentication.yaml
file, apply it:oc apply -f authentication.yaml
$ oc apply -f authentication.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create an
ApiServerSource
object, create a file namedk8s-events.yaml
and copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
k8s-events.yaml
file, apply it:oc apply -f k8s-events.yaml
$ oc apply -f k8s-events.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To check that the API server source is set up correctly, create a Knative service that dumps incoming messages to its log.
Copy the following sample YAML into a file named
service.yaml
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
service.yaml
file, apply it:oc apply -f service.yaml
$ oc apply -f service.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create a trigger from the
default
broker that filters events to the service created in the previous step, create a file namedtrigger.yaml
and copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
trigger.yaml
file, apply it:oc apply -f trigger.yaml
$ oc apply -f trigger.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create events, launch a pod in the
default
namespace:oc create deployment hello-node --image=quay.io/openshift-knative/knative-eventing-sources-event-display
$ oc create deployment hello-node --image=quay.io/openshift-knative/knative-eventing-sources-event-display
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To check that the controller is mapped correctly, enter the following command and inspect the output:
oc get apiserversource.sources.knative.dev testevents -o yaml
$ oc get apiserversource.sources.knative.dev testevents -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
To verify that the Kubernetes events were sent to Knative, you can look at the message dumper function logs.
Get the pods:
oc get pods
$ oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the message dumper function logs for the pods:
oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.3.4. Deleting the API server source Copiar o linkLink copiado para a área de transferência!
This section describes how to delete the ApiServerSource
object, trigger, service, service account, cluster role, and cluster binding by deleting their YAML files.
Procedure
Delete the trigger:
oc delete -f trigger.yaml
$ oc delete -f trigger.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the service:
oc delete -f service.yaml
$ oc delete -f service.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the API server source:
oc delete -f k8s-events.yaml
$ oc delete -f k8s-events.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the service account, cluster role, and cluster binding:
oc delete -f authentication.yaml
$ oc delete -f authentication.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4. Using a ping source Copiar o linkLink copiado para a área de transferência!
A ping source is used to periodically send ping events with a constant payload to an event consumer. A ping source can be used to schedule sending events, similar to a timer, as shown in the example:
Example ping source
- 1
- The schedule of the event specified using CRON expression.
- 2
- The event message body expressed as a JSON encoded data string.
- 3
- These are the details of the event consumer. In this example, we are using a Knative service named
event-display
.
11.4.1. Creating a ping source using the Knative CLI Copiar o linkLink copiado para a área de transferência!
The following sections describe how to create, verify and remove a basic PingSource
object using the kn
CLI.
Prerequisites
- You have Knative Serving and Eventing installed.
-
You have the
kn
CLI installed.
Procedure
To verify that the ping source is working, create a simple Knative service that dumps incoming messages to the service’s logs:
kn service create event-display \ --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
$ kn service create event-display \ --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For each set of ping events that you want to request, create a
PingSource
object in the same namespace as the event consumer:kn source ping create test-ping-source \ --schedule "*/2 * * * *" \ --data '{"message": "Hello world!"}' \ --sink svc:event-display
$ kn source ping create test-ping-source \ --schedule "*/2 * * * *" \ --data '{"message": "Hello world!"}' \ --sink svc:event-display
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the controller is mapped correctly by entering the following command and inspecting the output:
kn source ping describe test-ping-source
$ kn source ping describe test-ping-source
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the logs of the sink pod.
By default, Knative services terminate their pods if no traffic is received within a 60 second period. The example shown in this guide creates a PingSource
object that sends a message every 2 minutes, so each message should be observed in a newly created pod.
Watch for new pods created:
watch oc get pods
$ watch oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Cancel watching the pods using
Ctrl+C
, then look at the logs of the created pod:oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4.1.1. Remove the ping source Copiar o linkLink copiado para a área de transferência!
Delete the
PingSource
object:kn delete pingsources.sources.knative.dev test-ping-source
$ kn delete pingsources.sources.knative.dev test-ping-source
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the
event-display
service:kn delete service.serving.knative.dev event-display
$ kn delete service.serving.knative.dev event-display
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4.2. Creating a ping source using YAML files Copiar o linkLink copiado para a área de transferência!
The following sections describe how to create, verify and remove a basic ping source using YAML files.
Prerequisites
- You have Knative Serving and Eventing installed.
The following procedure requires you to create YAML files.
If you change the names of the YAML files from those used in the examples, you must ensure that you also update the corresponding CLI commands.
Procedure
To verify that the ping source is working, create a simple Knative service that dumps incoming messages to the service’s logs.
Copy the example YAML into a file named
service.yaml
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
service.yaml
file:oc apply --filename service.yaml
$ oc apply --filename service.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
For each set of ping events that you want to request, create a
PingSource
object in the same namespace as the event consumer.Copy the example YAML into a file named
ping-source.yaml
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
ping-source.yaml
file:oc apply --filename ping-source.yaml
$ oc apply --filename ping-source.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Check that the controller is mapped correctly by entering the following command and observing the output:
oc get pingsource.sources.knative.dev test-ping-source -oyaml
$ oc get pingsource.sources.knative.dev test-ping-source -oyaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the logs of the sink pod.
By default, Knative services terminate their pods if no traffic is received within a 60 second period. The example shown in this guide creates a PingSource
object that sends a message every 2 minutes, so each message should be observed in a newly created pod.
Watch for new pods created:
watch oc get pods
$ watch oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Cancel watching the pods using
Ctrl+C
, then look at the logs of the created pod:oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4.2.1. Remove the PingSource Copiar o linkLink copiado para a área de transferência!
Delete the service by entering the following command:
oc delete --filename service.yaml
$ oc delete --filename service.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the
PingSource
object by entering the following command:oc delete --filename ping-source.yaml
$ oc delete --filename ping-source.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow