Creating a PingSource object

API version v1

This topic describes how to create a PingSource object.

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. If you have an existing sink, you can replace the examples with your own values.

Before you begin

To create a PingSource:

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

Create a PingSource object

  1. Optional: Create a namespace for your PingSource by running the command:

    1. kubectl create namespace <namespace>

    Where <namespace> is the namespace that you want your PingSource to use. For example, pingsource-example.

    Note

    Creating a namespace for your PingSource and related components allows you to view changes and events for this workflow more easily, because these are isolated from the other components that might exist in your default namespace.

    It also makes removing the source easier, because you can delete the namespace to remove all of the resources.

  2. Create a sink. If you do not have your own sink, you can use the following example Knative Service that dumps incoming messages to a log:

    1. Copy the YAML below into a file:

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

      1. kubectl apply -f <filename>.yaml

      Where <filename> is the name of the file you created in the previous step.

  3. Create the PingSource object.

    Note

    The data you want to send must be represented as text in the PingSource YAML file. Events that send binary data cannot be directly serialized in YAML. However, you can send binary data that is base64 encoded by using dataBase64 in place of data in the PingSource spec.

    Use one of the following options:

    kn

    • To create a PingSource that sends data that can be represented as plain text, such as text, JSON, or XML, run the command:

      1. kn source ping create <pingsource-name> \
      2. --namespace <namespace> \
      3. --schedule "<cron-schedule>" \
      4. --data '<data>' \
      5. --sink <sink-name>

      Where:

      • <pingsource-name> is the name of the PingSource that you want to create, for example, test-ping-source.
      • <namespace> is the name of the namespace that you created in step 1 above.
      • <cron-schedule> is a cron expression for the schedule for the PingSource to send events, for example, */1 * * * * sends an event every minute.
      • <data> is the data you want to send. This data must be represented as text, not binary. For example, a JSON object such as {"message": "Hello world!"}.
      • <sink-name> is the name of your sink, for example, http://event-display.pingsource-example.svc.cluster.local.

      For a list of available options, see the Knative client documentation.

    YAML

    • To create a PingSource that sends data that can be represented as plain text, such as text, JSON, or XML:

      1. Create a YAML file using the template below:

        1. apiVersion: sources.knative.dev/v1
        2. kind: PingSource
        3. metadata:
        4. name: <pingsource-name>
        5. spec:
        6. schedule: "<cron-schedule>"
        7. contentType: "<content-type>"
        8. data: '<data>'
        9. sink:
        10. ref:
        11. apiVersion: v1
        12. kind: <sink-kind>
        13. name: <sink-name>

        Where:

        • <pingsource-name> is the name of the PingSource that you want to create, for example, test-ping-source.
        • <cron-schedule> is a cron expression for the schedule for the PingSource to send events, for example, */1 * * * * sends an event every minute.
        • <content-type> is the media type of the data you want to send, for example, application/json.
        • <data> is the data you want to send. This data must be represented as text, not binary. For example, a JSON object such as {"message": "Hello world!"}.
        • <sink-kind> is any supported PodSpecable object that you want to use as a sink, for example, Service or Deployment. A PodSpecable is an object that describes a PodSpec.
        • <sink-name> is the name of your sink, for example, event-display.

        For more information about the fields you can configure for the PingSource object, see PingSource reference.

      2. Apply the YAML file by running the command:

        1. kubectl apply -f <filename>.yaml

        Where <filename> is the name of the file you created in the previous step.

    YAML: binary data

    • To create a PingSource that sends binary data:

      1. Create a YAML file using the template below:

        1. apiVersion: sources.knative.dev/v1
        2. kind: PingSource
        3. metadata:
        4. name: <pingsource-name>
        5. spec:
        6. schedule: "<cron-schedule>"
        7. contentType: "<content-type>"
        8. dataBase64: "<base64-data>"
        9. sink:
        10. ref:
        11. apiVersion: v1
        12. kind: <sink-kind>
        13. name: <sink-name>

        Where:

        • <pingsource-name> is the name of the PingSource that you want to create, for example, test-ping-source-binary.
        • <cron-schedule> is a cron expression for the schedule for the PingSource to send events, for example, */1 * * * * sends an event every minute.
        • <content-type> is the media type of the data you want to send, for example, application/json.
        • <base64-data> is the base64 encoded binary data that you want to send, for example, ZGF0YQ==.
        • <sink-kind> is any supported PodSpecable object that you want to use as a sink, for example, Service or Deployment. A PodSpecable is an object that describes a PodSpec.
        • <sink-name> is the name of your sink, for example, event-display.

        For more information about the fields you can configure for the PingSource object, see PingSource reference.

      2. Apply the YAML file by running the command:

        1. kubectl apply -f <filename>.yaml

        Where <filename> is the name of the file you created in the previous step.

Verify the PingSource object

  1. View the logs for the event-display event consumer by running the command:

    kubectl

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

    kail

    1. kail -l serving.knative.dev/service=event-display -c user-container --since=10m
  2. Verify that the output returns the properties of the events that your PingSource sent to your sink. In the example below, the command has returned the Attributes and Data properties 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. }

Delete the PingSource object

You can either delete the PingSource and all related resources, or delete the resources individually:

  • To remove the PingSource object and all of the related resources, delete the namespace by running the command:

    1. kubectl delete namespace <namespace>

    Where <namespace> is the namespace that contains the PingSource object.

  • To delete the PingSource instance only, run the command:

    kn

    1. kn source ping delete <pingsource-name>

    Where <pingsource-name> is the name of the PingSource you want to delete, for example, test-ping-source.

    kubectl

    1. kubectl delete pingsources.sources.knative.dev <pingsource-name>

    Where <pingsource-name> is the name of the PingSource you want to delete, for example, test-ping-source.

  • To delete the sink only, run the command:

    kn

    1. kn service delete <sink-name>

    Where <sink-name> is the name of your sink, for example, event-display.

    kubectl

    1. kubectl delete service.serving.knative.dev <sink-name>

    Where <sink-name> is the name of your sink, for example, event-display.