PingSource

version

A PingSource is an event source that produces events with a fixed payload on a specified cron schedule.

The following example shows how you can configure a PingSource as an event source that sends events every minute to a Knative service named event-display that is used as a sink.

Before you begin

  1. To create a PingSource, you must install Knative Eventing. The PingSource event source type is enabled by default when you install Knative Eventing.
  2. Optional: You can use either kubectl or kn commands to create components such as a sink and PingSource.
  3. Optional: You can use either kubectl or kail for logging during the verification step in this procedure.

Procedure

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

    1. kubectl create namespace pingsource-example

    Creating a namespace for the PingSource example allows you to isolate the components created by this demo, so that it is easier for you to view changes and remove components when you are finished.

  2. To verify that the PingSource is working correctly, create an example sink in the pingsource-example namespace that dumps incoming messages to a log, by entering the 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
  3. Create a PingSource that sends an event containing {"message": "Hello world!"} every minute, by entering the command:

    1. kubectl create -n pingsource-example -f - <<EOF
    2. apiVersion: sources.knative.dev/v1
    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
    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
  4. Optional: Create a PingSource that sends binary data.

    If you want to send binary data in an event, this cannot be directly serialized in YAML. However, you can use dataBase64 in place of data in the PingSource spec to carry a data payload that is base64 encoded.

    To create a PingSource that uses base64 encoded data, enter the command:

    1. kubectl -n pingsource-example apply -f - <<EOF
    2. apiVersion: sources.knative.dev/v1
    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
  5. 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
    1. kail -l serving.knative.dev/service=event-display -c user-container --since=10m

    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 that sends binary data, you will also see output similar to 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
  6. Optional: You can delete the pingsource-example namespace and all related resources from your cluster by entering the following command:

    1. kubectl delete namespace pingsource-example
  7. Optional: You can also delete the PingSource instance only by entering the following command:

    1. kubectl delete pingsources.sources.knative.dev test-ping-source
    1. kn source ping delete test-ping-source
    1. kubectl delete pingsources.sources.knative.dev test-ping-source-binary
    1. kn source ping delete test-ping-source-binary
  8. Optional: Delete the event-display service:

    1. kubectl delete service.serving.knative.dev event-display
    1. kn service delete event-display