Creating a PingSource object

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

    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 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.
    1. Optional: Create a namespace for your PingSource by running the command:

      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 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. namespace: <namespace>
        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/cmd/event_display
        18. ---
        19. kind: Service
        20. apiVersion: v1
        21. metadata:
        22. name: event-display
        23. namespace: <namespace>
        24. spec:
        25. selector:
        26. app: event-display
        27. - protocol: TCP
        28. port: 80
        29. targetPort: 8080

        Where <namespace> is the name of the namespace that you created in step 1 above.

      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.

      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:

      knYAMLYAML: binary data

      • 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. --schedule "<cron-schedule>" \
        3. --data '<data>' \
        4. --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.

      • 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. namespace: <namespace>
          6. spec:
          7. schedule: "<cron-schedule>"
          8. contentType: "<content-type>"
          9. data: '<data>'
          10. sink:
          11. ref:
          12. apiVersion: v1
          13. kind: <sink-kind>
          14. name: <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.
          • <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-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 .

        2. Apply the YAML file by running the command:

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

      • 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. name: <pingsource-name>
          4. namespace: <namespace>
          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.
          • <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.
          • <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 Addressable object that you want to use as a sink, for example, a Kubernetes Service.
          • <sink-name> is the name of your sink, for example, event-display.
        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.

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

      kubectlkail

      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
    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:

    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:

      knkubectl

      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.

      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:

      knkubectl

      1. kn service delete <sink-name>

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