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.
SinkBinding 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.
- Create an PingSource.
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 kn 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:
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.
11.2.1. Listing available event source types using kn Copiar o linkLink copiado para a área de transferência!
Procedure
List the available event source types in the terminal:
kn source list-types
$ kn source list-types
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can also list available event source types in YAML format:
kn source list-types -o yaml
$ kn source list-types -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.2.2. Listing available event sources using kn Copiar o linkLink copiado para a área de transferência!
List available event sources by entering the following command:
kn source list
$ kn source list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.2.2.1. Listing event sources of a specific type only Copiar o linkLink copiado para a área de transferência!
You can list event sources of a specific type only, by using the --type
flag.
List available event sources of type
PingSource
by entering the following command:kn source list --type PingSource
$ kn source list --type PingSource
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.2.3. Next steps Copiar o linkLink copiado para a área de transferência!
- See the documentation on Using ApiServerSource.
- See the documentation on Using PingSource.
11.3. Using ApiServerSource Copiar o linkLink copiado para a área de transferência!
ApiServerSource is an event source that can be used to connect an event sink, such as a Knative service, to the Kubernetes API server. ApiServerSource 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 ApiServerSource with the Knative CLI (kn) Copiar o linkLink copiado para a área de transferência!
This section describes the steps required to create an ApiServerSource using kn
commands.
Prerequisites
-
You must have OpenShift Serverless, the Knative Serving and Eventing components, and the
kn
CLI installed.
Procedure
Create a service account, role, and role binding for the ApiServerSource.
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 re-use an existing service account with the appropriate permissions, you must modify the
authentication.yaml
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 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 To check that the ApiServerSource is set up correctly, 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:latest
$ oc create deployment hello-node --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
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 <source_name>
$ kn source apiserver describe <source_name>
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 the ApiServerSource using the Knative CLI (kn) Copiar o linkLink copiado para a área de transferência!
This section describes the steps used to delete the ApiServerSource, trigger, service, service account, cluster role, and cluster binding using 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 event 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. Using the ApiServerSource with the YAML method Copiar o linkLink copiado para a área de transferência!
This guide describes the steps required to create an ApiServerSource using YAML files.
Prerequisites
- You will need to have a Knative Serving and Eventing installation.
-
You will need to have created the
default
broker in the same namespace as the one defined in the ApiServerSource YAML file.
Procedure
To create a service account, role, and role binding for the ApiServerSource, 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 event source, create a file named
k8s-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 ApiServerSource 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 ApiServerSource Copiar o linkLink copiado para a área de transferência!
This section describes how to delete the ApiServerSource, 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 event 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 PingSource Copiar o linkLink copiado para a área de transferência!
A PingSource is used to periodically send ping events with a constant payload to an event consumer, and can be used to schedule sending events, similar to a timer.
Example PingSource YAML
- 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. Using a PingSource with the kn CLI Copiar o linkLink copiado para a área de transferência!
The following sections describe how to create, verify and remove a basic PingSource using the kn
CLI.
Prerequisites
- You have Knative Serving and Eventing installed.
-
You have the
kn
CLI installed.
Procedure
To verify that the PingSource 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 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
Verfication steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the sink pod’s logs.
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 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 PingSource Copiar o linkLink copiado para a área de transferência!
Delete the PingSource:
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. Using a PingSource with YAML Copiar o linkLink copiado para a área de transferência!
The following sections describe how to create, verify and remove a basic PingSource 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 PingSource 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 Create the service:
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 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 Create the PingSource:
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:
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
Verfication steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the sink pod’s logs.
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 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 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