From c533f45ccd65e9da6643c0ef6699047f2ada9528 Mon Sep 17 00:00:00 2001 From: whynowy Date: Sun, 9 Dec 2018 00:47:59 -0800 Subject: [PATCH 1/3] Added cronjob sample Give a sample about how to configure CronJobSource event. --- eventing/README.md | 13 +++++ eventing/samples/cronjob-source/README.md | 58 +++++++++++++++++++ eventing/samples/cronjob-source/channel.yaml | 10 ++++ .../cronjob-source/cronjob-source.yaml | 11 ++++ .../samples/cronjob-source/subscriber.yaml | 35 +++++++++++ 5 files changed, 127 insertions(+) create mode 100644 eventing/samples/cronjob-source/README.md create mode 100644 eventing/samples/cronjob-source/channel.yaml create mode 100644 eventing/samples/cronjob-source/cronjob-source.yaml create mode 100644 eventing/samples/cronjob-source/subscriber.yaml diff --git a/eventing/README.md b/eventing/README.md index bdb3343999c..6bbdfa47e55 100644 --- a/eventing/README.md +++ b/eventing/README.md @@ -209,6 +209,19 @@ FTP server for new files or generate events at a set time interval. [ObjectReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#objectreference-v1-core) A reference to the object that should receive events. +### CronJobSource + +The CronJobSource fires events based on given [Cron](https://en.wikipedia.org/wiki/Cron) schedule. + +**Spec fields**: + +- `schedule` (**required**): `string` A [Cron](https://en.wikipedia.org/wiki/Cron) format string, such as `0 * * * *` or `@hourly`. +- `data`: `string` Optional data sent to downstream receiver. +- `serviceAccountName`: `string` The name of the ServiceAccount to run the container as. +- `sink`: + [ObjectReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#objectreference-v1-core) + A reference to the object that should receive events. + ## Getting Started - [Setup Knative Serving](../install/README.md) diff --git a/eventing/samples/cronjob-source/README.md b/eventing/samples/cronjob-source/README.md new file mode 100644 index 00000000000..1015f32981c --- /dev/null +++ b/eventing/samples/cronjob-source/README.md @@ -0,0 +1,58 @@ +# 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](https://github.com/knative/docs/tree/master/serving). +1. Setup + [Knative Eventing](https://github.com/knative/docs/tree/master/eventing). + +### Channel + +1. Create a `Channel`. You can use your own `Channel` or use the provided sample, which creates a channel called `cronjob-test`. If you use your own `Channel` with a different name, then you will need to alter other commands later. + +```shell +kubectl -n default apply -f channel.yaml +``` + +### Create Cron Job Event Source + +1. In order to receive events, you need to create a concrete Event Source for a specific namespace. If you want to consume events from a differenet namespace or using a different `Service Account`, you need to modify the yaml accordingly. + +```shell +kubectl -n default apply -f cronjob-source.yaml +``` + +### Subscriber + +In order to check the `CronJobSource` is fully working, we will create a simple Knative Service that dumps incoming messages to its log and create a `Subscription` from the `Channel` to that Knative Service. + +1. If the deployed `CronJobSource` is pointing at a `Channel` other than `cronjob-test`, modify `subscriber.yaml` by replacing `cronjob-test` with that `Channel`'s name. +1. Deploy `subscriber.yaml`. + +```shell +kubectl -n default apply -f subscriber.yaml +``` + +### Verify + +We will verify that the message was sent to the Knative eventing system by looking at message dumper logs. + +1. Use [`kail`](https://github.com/boz/kail) to tail the logs of the subscriber. + + ```shell + kail -d message-dumper -c user-container --since=10m + ``` + +You should see log lines similar to: + +```json +{ + "ID": "1543616460000180552-203", + "EventTime": "2018-11-30T22:21:00.000186721Z", + "Body": "{\"message\": \"Hello world!\"}" +} +``` diff --git a/eventing/samples/cronjob-source/channel.yaml b/eventing/samples/cronjob-source/channel.yaml new file mode 100644 index 00000000000..6264a946141 --- /dev/null +++ b/eventing/samples/cronjob-source/channel.yaml @@ -0,0 +1,10 @@ +apiVersion: eventing.knative.dev/v1alpha1 +kind: Channel +metadata: + name: cronjob-test + namespace: default +spec: + provisioner: + apiVersion: eventing.knative.dev/v1alpha1 + kind: ClusterChannelProvisioner + name: in-memory-channel diff --git a/eventing/samples/cronjob-source/cronjob-source.yaml b/eventing/samples/cronjob-source/cronjob-source.yaml new file mode 100644 index 00000000000..bfe79ecbf05 --- /dev/null +++ b/eventing/samples/cronjob-source/cronjob-source.yaml @@ -0,0 +1,11 @@ +apiVersion: sources.eventing.knative.dev/v1alpha1 +kind: CronJobSource +metadata: + name: test-cronjob-source +spec: + schedule: '*/2 * * * *' + data: '{"message": "Hello world!"}' + sink: + apiVersion: eventing.knative.dev/v1alpha1 + kind: Channel + name: cronjob-test diff --git a/eventing/samples/cronjob-source/subscriber.yaml b/eventing/samples/cronjob-source/subscriber.yaml new file mode 100644 index 00000000000..8421002848d --- /dev/null +++ b/eventing/samples/cronjob-source/subscriber.yaml @@ -0,0 +1,35 @@ +# This is a very simple Knative Service that writes the input request to its log. + +apiVersion: serving.knative.dev/v1alpha1 +kind: Service +metadata: + name: message-dumper + namespace: default +spec: + runLatest: + configuration: + revisionTemplate: + spec: + container: + image: github.com/knative/eventing-sources/cmd/message_dumper + +--- +# Subscription from the CronJobSource's output Channel to the Knative Service below. + +apiVersion: eventing.knative.dev/v1alpha1 +kind: Subscription +metadata: + name: cronjob-source-sample + namespace: default +spec: + channel: + apiVersion: eventing.knative.dev/v1alpha1 + kind: Channel + name: cronjob-test + subscriber: + ref: + apiVersion: serving.knative.dev/v1alpha1 + kind: Service + name: message-dumper + + From 18bc523dad32b2dfdaba318af31de388d4131984 Mon Sep 17 00:00:00 2001 From: whynowy Date: Wed, 12 Dec 2018 21:27:22 -0800 Subject: [PATCH 2/3] default namespace --- eventing/samples/cronjob-source/README.md | 6 +++--- eventing/samples/cronjob-source/subscriber.yaml | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/eventing/samples/cronjob-source/README.md b/eventing/samples/cronjob-source/README.md index 1015f32981c..4a22313149b 100644 --- a/eventing/samples/cronjob-source/README.md +++ b/eventing/samples/cronjob-source/README.md @@ -15,7 +15,7 @@ Cron Job Source example shows how to configure Cron Job as event source for func 1. Create a `Channel`. You can use your own `Channel` or use the provided sample, which creates a channel called `cronjob-test`. If you use your own `Channel` with a different name, then you will need to alter other commands later. ```shell -kubectl -n default apply -f channel.yaml +kubectl apply -f channel.yaml ``` ### Create Cron Job Event Source @@ -23,7 +23,7 @@ kubectl -n default apply -f channel.yaml 1. In order to receive events, you need to create a concrete Event Source for a specific namespace. If you want to consume events from a differenet namespace or using a different `Service Account`, you need to modify the yaml accordingly. ```shell -kubectl -n default apply -f cronjob-source.yaml +kubectl apply -f cronjob-source.yaml ``` ### Subscriber @@ -34,7 +34,7 @@ In order to check the `CronJobSource` is fully working, we will create a simple 1. Deploy `subscriber.yaml`. ```shell -kubectl -n default apply -f subscriber.yaml +kubectl apply -f subscriber.yaml ``` ### Verify diff --git a/eventing/samples/cronjob-source/subscriber.yaml b/eventing/samples/cronjob-source/subscriber.yaml index 8421002848d..5e69f638283 100644 --- a/eventing/samples/cronjob-source/subscriber.yaml +++ b/eventing/samples/cronjob-source/subscriber.yaml @@ -31,5 +31,3 @@ spec: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: message-dumper - - From 5172070d788b903ab03504e6faefa4e16de461b3 Mon Sep 17 00:00:00 2001 From: Derek Wang Date: Tue, 1 Jan 2019 22:01:31 -0800 Subject: [PATCH 3/3] Refine the sample --- eventing/samples/cronjob-source/README.md | 59 ++++++++++++++----- eventing/samples/cronjob-source/channel.yaml | 10 ---- .../cronjob-source/cronjob-source.yaml | 8 +-- eventing/samples/cronjob-source/service.yaml | 13 ++++ .../samples/cronjob-source/subscriber.yaml | 33 ----------- 5 files changed, 60 insertions(+), 63 deletions(-) delete mode 100644 eventing/samples/cronjob-source/channel.yaml create mode 100644 eventing/samples/cronjob-source/service.yaml delete mode 100644 eventing/samples/cronjob-source/subscriber.yaml diff --git a/eventing/samples/cronjob-source/README.md b/eventing/samples/cronjob-source/README.md index 4a22313149b..1e5c4a8d697 100644 --- a/eventing/samples/cronjob-source/README.md +++ b/eventing/samples/cronjob-source/README.md @@ -1,6 +1,7 @@ # Cron Job Source example -Cron Job Source example shows how to configure Cron Job as event source for functions. +Cron Job Source example shows how to configure Cron Job as event source for +functions. ## Deployment Steps @@ -10,36 +11,62 @@ Cron Job Source example shows how to configure Cron Job as event source for func 1. Setup [Knative Eventing](https://github.com/knative/docs/tree/master/eventing). -### Channel +### 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. + +```yaml +apiVersion: serving.knative.dev/v1alpha1 +kind: Service +metadata: + name: message-dumper +spec: + runLatest: + configuration: + revisionTemplate: + spec: + container: + image: github.com/knative/eventing-sources/cmd/message_dumper +``` -1. Create a `Channel`. You can use your own `Channel` or use the provided sample, which creates a channel called `cronjob-test`. If you use your own `Channel` with a different name, then you will need to alter other commands later. +Use following command to create the service from `service.yaml`: ```shell -kubectl apply -f channel.yaml +kubectl apply -f service.yaml ``` ### Create Cron Job Event Source -1. In order to receive events, you need to create a concrete Event Source for a specific namespace. If you want to consume events from a differenet namespace or using a different `Service Account`, you need to modify the yaml accordingly. - -```shell -kubectl apply -f cronjob-source.yaml +For each set of cron events you want to request, you need to create an Event +Source in the same namespace as the destiantion. If you need a different +ServiceAccount to create the Deployment, modify the entry accordingly in the +yaml. + +```yaml +apiVersion: sources.eventing.knative.dev/v1alpha1 +kind: CronJobSource +metadata: + name: test-cronjob-source +spec: + schedule: "*/2 * * * *" + data: '{"message": "Hello world!"}' + sink: + apiVersion: serving.knative.dev/v1alpha1 + kind: Service + name: message-dumper ``` -### Subscriber - -In order to check the `CronJobSource` is fully working, we will create a simple Knative Service that dumps incoming messages to its log and create a `Subscription` from the `Channel` to that Knative Service. - -1. If the deployed `CronJobSource` is pointing at a `Channel` other than `cronjob-test`, modify `subscriber.yaml` by replacing `cronjob-test` with that `Channel`'s name. -1. Deploy `subscriber.yaml`. +Use following command to create the event source from `cronjob-source.yaml`: ```shell -kubectl apply -f subscriber.yaml +kubectl apply -f cronjob-source.yaml ``` ### Verify -We will verify that the message was sent to the Knative eventing system by looking at message dumper logs. +We will verify that the message was sent to the Knative eventing system by +looking at message dumper logs. 1. Use [`kail`](https://github.com/boz/kail) to tail the logs of the subscriber. diff --git a/eventing/samples/cronjob-source/channel.yaml b/eventing/samples/cronjob-source/channel.yaml deleted file mode 100644 index 6264a946141..00000000000 --- a/eventing/samples/cronjob-source/channel.yaml +++ /dev/null @@ -1,10 +0,0 @@ -apiVersion: eventing.knative.dev/v1alpha1 -kind: Channel -metadata: - name: cronjob-test - namespace: default -spec: - provisioner: - apiVersion: eventing.knative.dev/v1alpha1 - kind: ClusterChannelProvisioner - name: in-memory-channel diff --git a/eventing/samples/cronjob-source/cronjob-source.yaml b/eventing/samples/cronjob-source/cronjob-source.yaml index bfe79ecbf05..67c6911d0c7 100644 --- a/eventing/samples/cronjob-source/cronjob-source.yaml +++ b/eventing/samples/cronjob-source/cronjob-source.yaml @@ -3,9 +3,9 @@ kind: CronJobSource metadata: name: test-cronjob-source spec: - schedule: '*/2 * * * *' + schedule: "*/2 * * * *" data: '{"message": "Hello world!"}' sink: - apiVersion: eventing.knative.dev/v1alpha1 - kind: Channel - name: cronjob-test + apiVersion: serving.knative.dev/v1alpha1 + kind: Service + name: message-dumper diff --git a/eventing/samples/cronjob-source/service.yaml b/eventing/samples/cronjob-source/service.yaml new file mode 100644 index 00000000000..172a25fad14 --- /dev/null +++ b/eventing/samples/cronjob-source/service.yaml @@ -0,0 +1,13 @@ +# This is a very simple Knative Service that writes the input request to its log. + +apiVersion: serving.knative.dev/v1alpha1 +kind: Service +metadata: + name: message-dumper +spec: + runLatest: + configuration: + revisionTemplate: + spec: + container: + image: github.com/knative/eventing-sources/cmd/message_dumper diff --git a/eventing/samples/cronjob-source/subscriber.yaml b/eventing/samples/cronjob-source/subscriber.yaml deleted file mode 100644 index 5e69f638283..00000000000 --- a/eventing/samples/cronjob-source/subscriber.yaml +++ /dev/null @@ -1,33 +0,0 @@ -# This is a very simple Knative Service that writes the input request to its log. - -apiVersion: serving.knative.dev/v1alpha1 -kind: Service -metadata: - name: message-dumper - namespace: default -spec: - runLatest: - configuration: - revisionTemplate: - spec: - container: - image: github.com/knative/eventing-sources/cmd/message_dumper - ---- -# Subscription from the CronJobSource's output Channel to the Knative Service below. - -apiVersion: eventing.knative.dev/v1alpha1 -kind: Subscription -metadata: - name: cronjob-source-sample - namespace: default -spec: - channel: - apiVersion: eventing.knative.dev/v1alpha1 - kind: Channel - name: cronjob-test - subscriber: - ref: - apiVersion: serving.knative.dev/v1alpha1 - kind: Service - name: message-dumper