Knative Tutorial

Before beginning to run the exercises, navigate to the tutorial chapter’s eventing folder:

cd $TUTORIAL_HOME/eventing

Event Source

Knative Eventing Sources are software components that emit events. The job of a Source is to connect to, drain, capture and potentially buffer events; often from an external system and then relay those events to the Sink.

Knative Eventing Sources installs the following four sources out-of-the-box:

kubectl api-resources --api-group='sources.knative.dev'
NAME              APIGROUP                      NAMESPACED   KIND
apiserversources  sources.knative.dev           true         ApiServerSource
containersources  sources.knative.dev           true         ContainerSource
pingsources       sources.knative.dev           true         PingSource
sinkbindings      sources.knative.dev           true         SinkBinding

Let’s see how a simple source, the PingSource looks like :

apiVersion: sources.knative.dev/v1alpha2
kind: PingSource (1)
metadata:
  name: eventinghello-ping-source
spec: (2)
  schedule: "*/2 * * * *"
  jsonData: '{"key": "every 2 mins"}'
  sink:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghello
1 The type of event source, the eventing system deploys a bunch of sources out of the box and it also provides way to deploy custom resources
2 spec will be unique per source, per kind

Referencing the Event Sink

Knative Eventing Sink is how you specify the event receiver — that is the consumer of the event--. Sinks can be invoked directly in a point-to-point fashion by referencing them via the Event Source’s sink as shown below:

apiVersion: sources.knative.dev/v1alpha2
kind: PingSource
metadata:
  name: eventinghello-ping-source
spec:
  schedule: "*/2 * * * *"
  jsonData: '{"message": "Thanks for doing Knative Tutorial"}'
  sink:  (1)
    ref:
      apiVersion: serving.knative.dev/v1 (2)
      kind: Service
      name: eventinghello (3)
1 sink can target any Kubernetes Service or
2 a Knative Service
3 Deployed as "eventinghello"

Event Source can define the attributes that it wishes to receive via the spec. In the above example it defines schedule(the cron expression) and jsonData that will be sent as part of the event.

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

Create Sink Service

Run the following command to create the Knative service that will be used as the subscriber for the cron events:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghello
spec:
  template:
    metadata:
      name: eventinghello-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/rhdevelopers/eventinghello:0.0.2

Deploy Sink Service

Run the following commands to create the service:

kubectl apply -n knativetutorial -f eventing-hello-sink.yaml

Create Event Source

Run the following commands to create the event source resources:

kubectl apply -n knativetutorial -f eventinghello-source.yaml

Verification

kubectl -n knativetutorial get \
  pingsources.sources.knative.dev \
  eventinghello-ping-source

Running the above command should return the following result:

NAME                       AGE
eventinghello-ping-source  39s

You can watch logs to see the ping job source sending an event every 1 minute.

See what you have deployed

sources

kubectl --namespace knativetutorial get pingsource.sources.knative.dev  eventinghello-ping-source

services

kubectl --namespace knativetutorial get service.serving.knative.dev eventinghello

Cleanup

kubectl -n knativetutorial delete -f eventinghello-source.yaml
kubectl -n knativetutorial delete -f eventing-hello-sink.yaml