You can use an event source or a SinkBinding, the default broker, and a trigger together to decouple applications that produce events from the destination configuration.

Events are sent from an event source to the broker as an HTTP POST request. After events have entered the broker, they can be filtered and sent to an event sink by using triggers. You can use a trigger to consume events from a broker based on event attributes. Your application will receive events as an HTTP POST request.

You can create the default broker by using the knative-eventing-injection annotation.

Although both developers and cluster administrators can add the knative-eventing-injection annotation, only cluster administrators can remove brokers created using this annotation.

Creating a broker as a developer

Procedure
  1. If you do not have an existing event sink created to consume events from the broker, create a simple Knative Service to use as an event sink.

    1. Save the following YAML in a .yaml file:

      apiVersion: serving.knative.dev/v1
      kind: Service
      metadata:
        name: event-display
        namespace: default
      spec:
        template:
          spec:
            containers:
              - image: quay.io/openshift-knative/knative-eventing-sources-event-display
    2. Create the Service by applying the file:

      $ oc apply -f <filename>
  2. If you do not have an existing event source, create an event source that emits events to the default broker. The following example shows how you can create a basic PingSource.

    1. Save the following YAML in a .yaml file:

      apiVersion: sources.knative.dev/v1alpha2
      kind: PingSource
      metadata:
        name: ping-source
      spec:
        schedule: "*/1 * * * *"
        jsonData: '{"message": "Hello serverless!"}'
        sink:
          ref:
            apiVersion: eventing.knative.dev/v1beta1
            kind: Broker
            name: default
    2. Create the event source by applying the file:

    $ oc apply -f <FILENAME>
  3. Create the broker by creating a Trigger object that uses the knative-eventing-injection: enabled annotation.

    If you create a broker that uses the knative-eventing-injection: enabled annotation, you cannot delete this broker without cluster administrator permissions. If you delete the broker without having a cluster administrator remove this annotation first, the broker will be created again after deletion.

    1. Save the following YAML in a .yaml file:

      apiVersion: eventing.knative.dev/v1beta1
      kind: Trigger
      metadata:
        annotations:
          knative-eventing-injection: enabled
        name: example-trigger
      spec:
        broker: default
        subscriber: (1)
          ref:
            apiVersion: serving.knative.dev/v1
            kind: Service
            name: event-display (1)
      1 Specify the name of the event sink to use with the broker.
    2. Create the broker by applying the file:

      $ oc apply -f <filename>
Verification steps

You can verify that events were sent to the event sink by looking at the sink Pod’s logs.

By default, Knative Services terminate their pods if no traffic is received within a 60 second period. The example shown in this procedure creates a PingSource that sends a message every 2 minutes, so each message should be observed in a newly created pod.

  1. Watch for a new Pod:

    $ watch oc get pods
  2. Cancel watching the pods by pressing Ctrl+C.

  3. Review the logs of the new Pod:

    $ oc logs $(oc get pod -o name | grep event-display) -c user-container

    The logs should contain lines similar to the following:

    Example output
    ☁️  cloudevents.Event
    Validation: valid
    Context Attributes,
      specversion: 1.0
      type: dev.knative.sources.ping
      source: /apis/v1/namespaces/default/pingsources/test-ping-source
      id: 042ff529-240e-45ee-b40c-3a908129853e
      time: 2020-04-07T16:22:00.000791674Z
      datacontenttype: application/json
    Data,
      {
        "message": "Hello serverless!"
      }

Creating a broker as a cluster administrator using namespace annotation

If you have cluster administrator permissions, you can create the default broker automatically by using namespace annotation.

Brokers created due to annotation will not be removed if you remove the annotation. You must manually delete them.

Prerequisites
  • Knative Eventing installed.

  • Cluster administrator permissions for OpenShift Container Platform.

Procedure
  • Annotate your namespace by entering the following command:

    $ oc label namespace <namespace> knative-eventing-injection=enabled
Verification steps
  • You can verify that the broker has been created successfully by entering the following command:

    $ oc -n <namespace> get broker <broker-name>

Deleting a broker that was created by a cluster administrator using namespace annotation

Brokers created due to annotation will not be removed if you remove the annotation. You must manually delete them.

Procedure
  1. Remove the knative-eventing-injection=enabled annotation from the namespace:

    $ oc label namespace <NAMESPACE> knative-eventing-injection-

    Removing the annotation prevents you from recreating the broker after you delete it.

  2. Delete the injected broker from the selected namespace:

    $ oc -n <NAMESPACE> delete broker <BROKER-NAME>