Subscriptions

After you have created a Channel and a Sink, you can create a Subscription to enable event delivery.

The Subscription consists of a Subscription object, which specifies the Channel and the Sink (also known as the Subscriber) to deliver events to. You can also specify some Sink-specific options, such as how to handle failures.

For more information about Subscription objects, see Subscription.

Creating a Subscription

knYAML

Create a Subscription between a Channel and a Sink by running:

  1. kn subscription create <subscription-name> \
  2. --channel <Group:Version:Kind>:<channel-name> \
  3. --sink <sink-prefix>:<sink-name> \
  4. --sink-reply <sink-prefix>:<sink-name> \
  5. --sink-dead-letter <sink-prefix>:<sink-name>
  • --channel specifies the source for cloud events that should be processed. You must provide the Channel name. If you are not using the default Channel that is backed by the Channel resource, you must prefix the Channel name with the <Group:Version:Kind> for the specified Channel type. For example, this is messaging.knative.dev:v1beta1:KafkaChannel for a Kafka-backed Channel.

  • --sink specifies the target destination to which the event should be delivered. By default, the <sink-name> is interpreted as a Knative service of this name, in the same namespace as the Subscription. You can specify the type of the Sink by using one of the following prefixes:

    • ksvc: A Knative service.
    • svc: A Kubernetes Service.
    • channel: A Channel that should be used as the destination. You can only reference default Channel types here.
    • broker: An Eventing Broker.
    • --sink-reply is an optional argument you can use to specify where the Sink reply is sent. It uses the same naming conventions for specifying the Sink as the --sink flag.
    • --sink-dead-letter is an optional argument you can use to specify where to send the CloudEvent in case of a failure. It uses the same naming conventions for specifying the Sink as the --sink flag.

      • ksvc: A Knative service.
      • svc: A Kubernetes Service.
      • channel: A Channel that should be used as destination. Only default Channel types can be referenced here.
      • broker: An Eventing Broker.
    • --sink-reply and --sink-dead-letter are optional arguments. They can be used to specify where the Sink reply is sent, and where to send the CloudEvent in case of a failure, respectively. Both use the same naming conventions for specifying the Sink as the --sink flag.

This example command creates a Subscription named mysubscription that routes events from a Channel named mychannel to a Knative service named myservice.

Note

The Sink prefix is optional. You can also specify the service for --sink as just --sink <service-name> and omit the ksvc prefix.

  1. Create a YAML file for the Subscription object using the following example:

    1. apiVersion: messaging.knative.dev/v1
    2. kind: Subscription
    3. metadata:
    4. name: <subscription-name>
    5. # Name of the Subscription.
    6. namespace: default
    7. spec:
    8. channel:
    9. apiVersion: messaging.knative.dev/v1
    10. kind: Channel
    11. name: <channel-name>
    12. # Name of the Channel that the Subscription connects to.
    13. delivery:
    14. # Optional delivery configuration settings for events.
    15. deadLetterSink:
    16. # When this is configured, events that failed to be consumed are sent to the deadLetterSink.
    17. # The event is dropped, no re-delivery of the event is attempted, and an error is logged in the system.
    18. # The deadLetterSink value must be a Destination.
    19. ref:
    20. apiVersion: serving.knative.dev/v1
    21. kind: Service
    22. name: <service-name>
    23. reply:
    24. # Optional configuration settings for the reply event.
    25. # This is the event Sink that events replied from the subscriber are delivered to.
    26. ref:
    27. apiVersion: messaging.knative.dev/v1
    28. kind: InMemoryChannel
    29. name: <service-name>
    30. subscriber:
    31. # Required configuration settings for the Subscriber. This is the event Sink that events are delivered to from the Channel.
    32. ref:
    33. apiVersion: serving.knative.dev/v1
    34. kind: Service
    35. name: <service-name>
  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.

Listing Subscriptions

You can list all existing Subscriptions by using the kn CLI tool.

  • List all Subscriptions:

    1. kn subscription list
  • List Subscriptions in YAML format:

    1. kn subscription list -o yaml

Describing a Subscription

You can print details about a Subscription by using the kn CLI tool:

  1. kn subscription describe <subscription-name>

Deleting Subscriptions

You can delete a Subscription by using the kn or kubectl CLI tools.

knkubectl

  1. kn subscription delete <subscription-name>
  1. kubectl subscription delete <subscription-name>

Next steps