Knative Tutorial

Channel(Sink)

The channel or sink is an interface between the event source and the subscriber. The channels are built in to store the incoming events and distribute the event data to the subscribers. When forwarding event to subscribers the channel transforms the event data as per CloudEvent specification.

Create Event Channel

apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
metadata:
  name: ch-event-greeter (1)
spec:
  provisioner: (2)
    apiVersion: eventing.knative.dev/v1alpha1
    kind: ClusterChannelProvisioner
    name: in-memory
1 The name of the channel. Knative makes it addressable, i.e. resolveable to a target (a consumer service)
2 The channel provisioner which is responsible for provisioning this channel. Various messaging implementations provide their own channel(s) via ClusterChannelProvisioner.

Navigate to the tutorial chapter’s knative folder:

cd $TUTORIAL_HOME/05-eventing/knative

Run the following commands to create the channel:

  • kubectl

  • oc

kubectl apply -n knativetutorial -f channel.yaml

oc apply -n knativetutorial -f channel.yaml

Verification

  • kubectl

  • oc

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

oc -n knativetutorial get channels.eventing.knative.dev

Running the above command should return the following result:

NAME                       AGE
ch-event-greeter   39s

Event Source

The event source listens to external events e.g. a kafka topic or for a file on a FTP server. It is responsible to drain the received event(s) along with its data to a configured sink.

Create Event Source

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource (1)
metadata:
  name: event-greeter-cronjob-source
spec:
  schedule: "* * * * *"
  data: '{"message": "Thanks for doing Knative Tutorial"}'
  sink: (2)
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Channel
    name: ch-event-greeter
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 The channel(sink) where the event data will be drained

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 data that will be sent as part of the event.

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

Run the following commands to create the event source resources:

  • kubectl

  • oc

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

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

Verification

  • kubectl

  • oc

kubectl -n knativetutorial get cronjobsources.sources.eventing.knative.dev

oc -n knativetutorial get cronjobsources.sources.eventing.knative.dev

Running the above command should return the following result:

NAME                       AGE
event-greeter-cronjob-source  39s

The cronjob source also creates a service pod,

  • kubectl

  • oc

kubectl -n knativetutorial get pods

oc -n knativetutorial get pods

The above command will return an output like,

NAME                                                          READY     STATUS    RESTARTS   AGE
cronjob-event-greeter-cronjob-source-4v9vq-6bff96b58f-tgrhj   2/2       Running   0          6m

Event Subscriber

The event subscription is responsible of connecting the channel(sink) with the service. Once a service connected to a channel it starts receiving the events (cloud events).

Create Channel Subscriber

apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: event-greeter-subscriber
spec:
  channel:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Channel
    name: ch-event-greeter
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: event-greeter

Run the following commands to create the channel subscriber:

  • kubectl

  • oc

kubectl apply -n knativetutorial -f channel-subscriber.yaml

oc apply -n knativetutorial -f channel-subscriber.yaml

Verification

  • kubectl

  • oc

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

oc -n knativetutorial get subscriptions.eventing.knative.dev

Running the above command should return the following result:

NAME                       AGE
event-greeter-subscriber  39s

See what you have deployed

channel

  • kubectl

  • oc

kubectl --namespace knativetutorial get channels.eventing.knative.dev ch-event-greeter

oc --namespace knativetutorial get channels.eventing.knative.dev ch-event-greeter

sources

  • kubectl

  • oc

kubectl --namespace knativetutorial get cronjobsources.sources.eventing.knative.dev  event-greeter-cronjob-source

oc --namespace knativetutorial get cronjobsources.sources.eventing.knative.dev event-greeter-cronjob-source

subscription

  • kubectl

  • oc

kubectl --namespace knativetutorial get subscriptions.eventing.knative.dev event-greeter-subscriber

oc --namespace knativetutorial get subscriptions.eventing.knative.dev event-greeter-subscriber

Add -oyaml to the above commands to get more details about each object that were queried for.

Create subscriber Service

Generate Subscriber 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/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 Subscriber 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

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

Cleanup

  • kubectl

  • oc

kubectl -n knativetutorial delete services.serving.knative.dev event-greeter
kubectl -n knativetutorial delete \
 cronjobsources.sources.eventing.knative.dev  event-greeter-cronjob-source
kubectl -n knativetutorial delete channels.eventing.knative.dev ch-event-greeter
kubectl -n knativetutorial delete subscriptions.eventing.knative.dev event-greeter-subscriber

oc -n knativetutorial delete services.serving.knative.dev event-greeter
oc -n knativetutorial delete \
 cronjobsources.sources.eventing.knative.dev  event-greeter-cronjob-source
oc -n knativetutorial delete channels.eventing.knative.dev ch-event-greeter
oc -n knativetutorial delete subscriptions.eventing.knative.dev event-greeter-subscriber

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