Knative Tutorial

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.

Navigate to the tutorial chapter’s knative folder:

cd $TUTORIAL_HOME/05-eventing/knative

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: serving.knative.dev/v1alpha1
    kind: Service
    name: 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 service(sink) where the event data will be sent

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-svc.yaml

oc apply -n knativetutorial -f event-source-svc.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

Create Sink Service

Generate 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/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 Sink 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.

See what you have deployed

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

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

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

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

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