PingSource example

This example shows how to configure PingSource as an event source targeting a Knative Service.

Before you begin

  1. Set up Knative Serving.
  2. Set up Knative Eventing.

Create a Knative Service

To verify that PingSource is working, create a simple Knative Service that dumps incoming messages to its log.

Use following command to create the service from STDIN:

  1. cat <<EOF | kubectl create -f -
  2. apiVersion: serving.knative.dev/v1
  3. kind: Service
  4. metadata:
  5. name: event-display
  6. spec:
  7. template:
  8. spec:
  9. containers:
  10. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  11. EOF

Use following command to create the service from the service.yaml file:

  1. kubectl apply --filename service.yaml

Create a PingSource

For each set of ping events that you want to request, create an Event Source in the same namespace as the destination.

Use following command to create the event source from STDIN:

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

Use following command to create the event source from the ping-source.yaml file:

  1. kubectl apply --filename ping-source.yaml

(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.

Use the following command to create the event source with binary data from STDIN:

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

Use the following command to create the event source from the ping-source-binary.yaml file:

  1. kubectl apply --filename ping-source-binary.yaml

Verify

Verify that the message was sent to the Knative eventing system by looking at message dumper logs.

Use following command to view the logs of the event-display service:

  1. kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m

You can also use kail instead of kubectl logs to tail the logs of the subscriber.

  1. kail -l serving.knative.dev/service=event-display -c user-container --since=10m

You should see log lines showing the request headers and body from the source:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.sources.ping
  6. source: /apis/v1/namespaces/default/pingsources/test-ping-source
  7. id: d8e761eb-30c7-49a3-a421-cd5895239f2d
  8. time: 2019-12-04T14:24:00.000702251Z
  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/default/pingsources/test-ping-source-binary
  7. id: a195be33-ff65-49af-9045-0e0711d05e94
  8. time: 2020-11-17T19:48:00.48334181Z
  9. datacontenttype: text/plain
  10. Data,
  11. ZGF0YQ==

Cleanup

You can delete the PingSource instance by entering the following command:

  1. kubectl delete pingsources.sources.knative.dev test-ping-source
  2. kubectl delete pingsources.sources.knative.dev test-ping-source-binary
  1. kubectl delete --filename ping-source.yaml
  2. kubectl delete --filename ping-source-binary.yaml

Similarly, you can delete the Service instance via:

  1. kubectl delete service.serving.knative.dev event-display
  1. kubectl delete --filename service.yaml

Feedback

Was this page helpful?

Glad to hear it! Please tell us how we can improve.

Sorry to hear that. Please tell us how we can improve.