PingSource

version

A PingSource produces events with a fixed payload on a specified cron schedule.

Installation

The PingSource source type is enabled by default when you install Knative Eventing.

Example

This example shows how to send an event every minute to a Event Display Service.

Creating a namespace

Create a new namespace called pingsource-example by entering the following command:

  1. kubectl create namespace pingsource-example

Creating the Event Display Service

In this step, you create one event consumer, event-display to verify that PingSource is properly working.

To deploy the event-display consumer to your cluster, run the following command:

  1. kubectl -n pingsource-example apply -f - << EOF
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: event-display
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels: &labels
  10. app: event-display
  11. template:
  12. metadata:
  13. labels: *labels
  14. spec:
  15. containers:
  16. - name: event-display
  17. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  18. ---
  19. kind: Service
  20. apiVersion: v1
  21. metadata:
  22. name: event-display
  23. spec:
  24. selector:
  25. app: event-display
  26. ports:
  27. - protocol: TCP
  28. port: 80
  29. targetPort: 8080
  30. EOF

Creating the PingSource

You can now create the PingSource sending an event containing {"message": "Hello world!"} every minute.

  1. kubectl create -n pingsource-example -f - <<EOF
  2. apiVersion: sources.knative.dev/v1beta2
  3. kind: PingSource
  4. metadata:
  5. name: test-ping-source
  6. spec:
  7. schedule: "*/1 * * * *"
  8. contentType: "application/json"
  9. data: '{"message": "Hello world!"}'
  10. sink:
  11. ref:
  12. apiVersion: v1
  13. kind: Service
  14. name: event-display
  15. EOF

Notice that the namespace is specified in two places in the command in --namespace and the --sink hostname

  1. kn source ping create test-ping-source \
  2. --namespace pingsource-example \
  3. --schedule "*/1 * * * *" \
  4. --data '{"message": "Hello world!"}' \
  5. --sink http://event-display.pingsource-example.svc.cluster.local

(Optional) Create a PingSource with binary data

Sometimes you may want to send binary data, which cannot be directly serialized in yaml, to downstream. This can be achieved by using dataBase64 as the payload. As the name suggests, dataBase64 should carry data that is base64 encoded.

Please note that data and dataBase64 cannot co-exist.

  1. kubectl create -n pingsource-example -f - <<EOF
  2. apiVersion: sources.knative.dev/v1beta2
  3. kind: PingSource
  4. metadata:
  5. name: test-ping-source-binary
  6. spec:
  7. schedule: "*/1 * * * *"
  8. contentType: "text/plain"
  9. dataBase64: "ZGF0YQ=="
  10. sink:
  11. ref:
  12. apiVersion: v1
  13. kind: Service
  14. name: event-display
  15. EOF

Verify

View the logs for the event-display event consumer by entering the following command:

  1. kubectl -n pingsource-example logs -l app=event-display --tail=100

This returns the Attributes and Data of the events that the PingSource sent to the event-display Service:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.sources.ping
  6. source: /apis/v1/namespaces/pingsource-example/pingsources/test-ping-source
  7. id: 49f04fe2-7708-453d-ae0a-5fbaca9586a8
  8. time: 2021-03-25T19:41:00.444508332Z
  9. datacontenttype: application/json
  10. Data,
  11. {
  12. "message": "Hello world!"
  13. }

If you created a PingSource with binary data, you should also see the following:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.sources.ping
  6. source: /apis/v1/namespaces/pingsource-example/pingsources/test-ping-source-binary
  7. id: ddd7bad2-9b6a-42a7-8f9b-b64494a6ce43
  8. time: 2021-03-25T19:38:00.455013472Z
  9. datacontenttype: text/plain
  10. Data,
  11. data

Cleanup

Delete the pingsource-example namespace and all of its resources from your cluster by entering the following command:

  1. kubectl delete namespace pingsource-example

Reference Documentation

See the PingSource specification.

Contact

For any inquiries about this source, please reach out on to the Knative users group.