Subscriptions

After you have created a channel and a sink, you can create a subscription to enable event delivery.

Creating a subscription

Create a subscription between a channel and a sink:

  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 will be 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 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 will be sent to, and where to send the cloud event in case of a failure, respectively. Both use the same naming conventions for specifying the sink as the --sink flag.

Example command:

  1. kn subscription create mysubscription --channel mychannel --sink ksvc:myservice

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

NOTE: The sink prefix is optional. It is also possible to specify the service for --sink as just --sink <service_name> and omit the ksvc prefix.

  1. Create a Subscription object in a YAML file:

    1. apiVersion: messaging.knative.dev/v1beta1
    2. kind: Subscription
    3. metadata:
    4. name: <subscription_name> # Name of the subscription.
    5. namespace: default
    6. spec:
    7. channel:
    8. apiVersion: messaging.knative.dev/v1
    9. kind: Channel
    10. name: <channel_name> # Configuration settings for the channel that the subscription connects to.
    11. delivery:
    12. deadLetterSink:
    13. ref:
    14. apiVersion: serving.knative.dev/v1
    15. kind: Service
    16. name: <service_name>
    17. # Configuration settings for event delivery.
    18. # This tells the subscription what happens to events that cannot be delivered to the subscriber.
    19. # When this is configured, events that failed to be consumed are sent to the deadLetterSink.
    20. # The event is dropped, no re-delivery of the event is attempted, and an error is logged in the system.
    21. # The deadLetterSink value must be a Destination.
    22. subscriber:
    23. ref:
    24. apiVersion: serving.knative.dev/v1
    25. kind: Service
    26. name: <service_name> # Configuration settings for the subscriber. This is the event sink that events are delivered to from the channel.
  2. Apply the YAML file:

    1. kubectl apply -f <filename>

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.

  1. kn subscription delete <subscription_name>
  1. kubectl subscription delete <subscription_name>