Cron job source example

Cron Job Source example shows how to configure Cron Job as event source for functions.

Deployment Steps

Prerequisites

  1. Setup Knative Serving.
  2. Setup Knative Eventing.

Create a Knative Service

In order to verify CronJobSource is working, we will create a simple Knative Service that dumps incoming messages to its log.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: event-display
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display

Use following command to create the service from service.yaml:

kubectl apply --filename service.yaml

Create Cron Job Event Source

For each set of cron events you want to request, you need to create an Event Source in the same namespace as the destination. If you need a different ServiceAccount to create the Deployment, modify the entry accordingly in the yaml.

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: test-cronjob-source
spec:
  schedule: "*/2 * * * *"
  data: '{"message": "Hello world!"}'
  sink:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: event-display

Use following command to create the event source from cronjob-source.yaml:

kubectl apply --filename cronjob-source.yaml

Verify

We will verify that the message was sent to the Knative eventing system by looking at message dumper logs.

kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m

You should see log lines showing the request headers and body from the source:

☁️  cloudevents.Event
Validation: valid
Context Attributes,
  specversion: 1.0
  type: dev.knative.cronjob.event
  source: /apis/v1/namespaces/default/cronjobsources/test-cronjob-source
  id: d8e761eb-30c7-49a3-a421-cd5895239f2d
  time: 2019-12-04T14:24:00.000702251Z
  datacontenttype: application/json
Data,
  {
    "message": "Hello world!"
  }

You can also use kail instead of kubectl logs to tail the logs of the subscriber.

kail -l serving.knative.dev/service=event-display -c user-container --since=10m

Cleanup

You can remove the Cron Event Source via:

kubectl delete --filename cronjob-source.yaml

Similarly, you can remove the Service via:

kubectl delete --filename service.yaml