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.11.4. Using a ping source
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 复制链接链接已复制到粘贴板!
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 ksvc:event-display
$ kn source ping create test-ping-source \ --schedule "*/2 * * * *" \ --data '{"message": "Hello world!"}' \ --sink ksvc: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
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 复制链接链接已复制到粘贴板!
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 复制链接链接已复制到粘贴板!
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 log of the service.
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
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 复制链接链接已复制到粘贴板!
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