Skip to content

Commit 5584da3

Browse files
whynowyknative-prow-robot
authored andcommitted
Added cronjob sample (#647)
* Added cronjob sample Give a sample about how to configure CronJobSource event. * default namespace * Refine the sample
1 parent 921aa48 commit 5584da3

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

eventing/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,19 @@ FTP server for new files or generate events at a set time interval.
226226
[ObjectReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#objectreference-v1-core)
227227
A reference to the object that should receive events.
228228

229+
### CronJobSource
230+
231+
The CronJobSource fires events based on given [Cron](https://en.wikipedia.org/wiki/Cron) schedule.
232+
233+
**Spec fields**:
234+
235+
- `schedule` (**required**): `string` A [Cron](https://en.wikipedia.org/wiki/Cron) format string, such as `0 * * * *` or `@hourly`.
236+
- `data`: `string` Optional data sent to downstream receiver.
237+
- `serviceAccountName`: `string` The name of the ServiceAccount to run the container as.
238+
- `sink`:
239+
[ObjectReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#objectreference-v1-core)
240+
A reference to the object that should receive events.
241+
229242
## Getting Started
230243

231244
- [Setup Knative Serving](../install/README.md)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Cron Job Source example
2+
3+
Cron Job Source example shows how to configure Cron Job as event source for
4+
functions.
5+
6+
## Deployment Steps
7+
8+
### Prerequisites
9+
10+
1. Setup [Knative Serving](https://github.com/knative/docs/tree/master/serving).
11+
1. Setup
12+
[Knative Eventing](https://github.com/knative/docs/tree/master/eventing).
13+
14+
### Create a Knative Service
15+
16+
In order to verify `CronJobSource` is working, we will create a simple Knative
17+
Service that dumps incoming messages to its log.
18+
19+
```yaml
20+
apiVersion: serving.knative.dev/v1alpha1
21+
kind: Service
22+
metadata:
23+
name: message-dumper
24+
spec:
25+
runLatest:
26+
configuration:
27+
revisionTemplate:
28+
spec:
29+
container:
30+
image: github.com/knative/eventing-sources/cmd/message_dumper
31+
```
32+
33+
Use following command to create the service from `service.yaml`:
34+
35+
```shell
36+
kubectl apply -f service.yaml
37+
```
38+
39+
### Create Cron Job Event Source
40+
41+
For each set of cron events you want to request, you need to create an Event
42+
Source in the same namespace as the destiantion. If you need a different
43+
ServiceAccount to create the Deployment, modify the entry accordingly in the
44+
yaml.
45+
46+
```yaml
47+
apiVersion: sources.eventing.knative.dev/v1alpha1
48+
kind: CronJobSource
49+
metadata:
50+
name: test-cronjob-source
51+
spec:
52+
schedule: "*/2 * * * *"
53+
data: '{"message": "Hello world!"}'
54+
sink:
55+
apiVersion: serving.knative.dev/v1alpha1
56+
kind: Service
57+
name: message-dumper
58+
```
59+
60+
Use following command to create the event source from `cronjob-source.yaml`:
61+
62+
```shell
63+
kubectl apply -f cronjob-source.yaml
64+
```
65+
66+
### Verify
67+
68+
We will verify that the message was sent to the Knative eventing system by
69+
looking at message dumper logs.
70+
71+
1. Use [`kail`](https://github.com/boz/kail) to tail the logs of the subscriber.
72+
73+
```shell
74+
kail -d message-dumper -c user-container --since=10m
75+
```
76+
77+
You should see log lines similar to:
78+
79+
```json
80+
{
81+
"ID": "1543616460000180552-203",
82+
"EventTime": "2018-11-30T22:21:00.000186721Z",
83+
"Body": "{\"message\": \"Hello world!\"}"
84+
}
85+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: sources.eventing.knative.dev/v1alpha1
2+
kind: CronJobSource
3+
metadata:
4+
name: test-cronjob-source
5+
spec:
6+
schedule: "*/2 * * * *"
7+
data: '{"message": "Hello world!"}'
8+
sink:
9+
apiVersion: serving.knative.dev/v1alpha1
10+
kind: Service
11+
name: message-dumper
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This is a very simple Knative Service that writes the input request to its log.
2+
3+
apiVersion: serving.knative.dev/v1alpha1
4+
kind: Service
5+
metadata:
6+
name: message-dumper
7+
spec:
8+
runLatest:
9+
configuration:
10+
revisionTemplate:
11+
spec:
12+
container:
13+
image: github.com/knative/eventing-sources/cmd/message_dumper

0 commit comments

Comments
 (0)