Knative Tutorial

Events, Triggers and Brokers

With Knative 0.5.0, Broker and Trigger objects are introduced to make event filtering easier.

Brokers provide selection of events by attributes. They receive events and forwards them to subscribers defined by matching Triggers.

Triggers provide a way of filtering of events by attributes. They are attached to subscribers and make the subscribers receive the events only they are interested in.

Navigate to the tutorial chapter’s knative folder:

cd $TUTORIAL_HOME/05-eventing/knative

Broker

First of all, we need to create a Broker. Knative eventing provides a Broker named default when a special label is added to a namespace.

  • kubectl

  • oc

kubectl label namespace knativetutorial knative-eventing-injection=enabled

oc label namespace knativetutorial knative-eventing-injection=enabled

This will create the default broker. Execute the following command to see it:

  • kubectl

  • oc

kubectl -n knativetutorial get brokers.eventing.knative.dev

oc --namespace knativetutorial get brokers.eventing.knative.dev

Output should be similar to below:

NAME      READY     REASON    HOSTNAME                                           AGE
default   True                default-broker.knativetutorial.svc.cluster.local   2m

Labeling the namespace will also create some pods that are related to the default broker.

  • kubectl

  • oc

kubectl -n knativetutorial get pods

oc --namespace knativetutorial get pods

Running the above command should return the following result:

NAME                                      READY     STATUS    RESTARTS   AGE
default-broker-filter-5bb94d8ddf-r9j4v    2/2       Running   1          2m43s
default-broker-ingress-59b9b4985c-8n6d4   2/2       Running   1          2m43s

Service

Generate Service

Run the following command to create the Knative service that will be used as the subscriber for the events that are going to be published later:

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: event-greeter
spec:
  template:
    metadata:
      name: event-greeter-v1
      annotations:
        # disable istio-proxy injection
        sidecar.istio.io/inject: "false"
    spec:
      containers:
      - image: quay.io/rhdevelopers/knative-tutorial-greeter:quarkus
        livenessProbe:
          httpGet:
            path: /healthz
        readinessProbe:
          httpGet:
            path: /healthz

Deploy Service

Run the following commands to create the service:

  • kubectl

  • oc

kubectl apply -n knativetutorial -f service.yaml

oc apply -n knativetutorial -f service.yaml

Event Source

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: ContainerSource (1)
metadata:
  name: heartbeat-event-source
spec:
  image: quay.io/openshift-knative/knative-eventing-sources-heartbeats:v0.5.0 (2)
  args: (3)
    - '--label="Thanks for doing Knative Tutorial"'
    - '--period=1'
  env:
    - name: POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
    - name: POD_NAMESPACE
      valueFrom:
        fieldRef:
          fieldPath: metadata.namespace
  sink: (4)
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Broker
    name: default
1 The type of event source. ContainerSource type is basically a container sending events.
2 The image to run with this ContainerSource. Source of the image is available here.
3 Arguments for the ContainerSource image. label in this particular case is what the image will be sending as event data and period is how often it will send events.
4 Sink here is the default broker created earlier. So, the default broker will receive the events sent by this event source.

Run the following commands to create the event source resources:

  • kubectl

  • oc

kubectl apply -n knativetutorial -f event-source-broker.yaml

oc apply -n knativetutorial -f event-source-broker.yaml

Trigger

apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: event-greeter-trigger
spec:
  filter:
    sourceAndType:
      type: dev.knative.eventing.samples.heartbeat (1)
  subscriber: (2)
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service (3)
      name: event-greeter (4)
1 The event type to filter on. The event type dev.knative.eventing.samples.heartbeat is the one used by the ContainerSource image we deployed earlier.
2 Definition of the subscriber for the events that are matched by the filter of the trigger.
3 In this case, the subscriber is a service.
4 Subscriber service name. This trigger will make sure the events filtered will be sent to this service.

Run the following commands to create the event source resources:

  • kubectl

  • oc

kubectl apply -n knativetutorial -f trigger.yaml

oc apply -n knativetutorial -f trigger.yaml

Verification

Logs

When you watch logs, you will notice this data being delivered to the service.

See what you have deployed

sources

  • kubectl

  • oc

kubectl --namespace knativetutorial get containersources.sources.eventing.knative.dev heartbeat-event-source

oc --namespace knativetutorial get containersources.sources.eventing.knative.dev heartbeat-event-source

services

  • kubectl

  • oc

kubectl --namespace knativetutorial  get services.serving.knative.dev event-greeter

# get a Knative Service (short name ksvc) called greeter
oc --namespace knativetutorial  get services.serving.knative.dev event-greeter

triggers

  • kubectl

  • oc

kubectl --namespace knativetutorial  get triggers.eventing.knative.dev event-greeter-trigger

oc --namespace knativetutorial  get triggers.eventing.knative.dev event-greeter-trigger

Cleanup

  • kubectl

  • oc

kubectl -n knativetutorial delete services.serving.knative.dev event-greeter
kubectl -n knativetutorial delete \
 containersource.sources.eventing.knative.dev heartbeat-event-source
kubectl -n knativetutorial delete triggers.eventing.knative.dev event-greeter-trigger
kubectl -n knativetutorial delete brokers.eventing.knative.dev default

oc -n knativetutorial delete services.serving.knative.dev event-greeter
oc -n knativetutorial delete \
 containersource.sources.eventing.knative.dev heartbeat-event-source
oc -n knativetutorial delete triggers.eventing.knative.dev event-greeter-trigger
oc -n knativetutorial delete brokers.eventing.knative.dev default

You can also delete knative-tutorial-build:ROOT:build.adoc#build-apply-prereq-resources that were created if you don’t need them anymore.