Skip to content

Commit 467b011

Browse files
mattmoorknative-prow-robot
authored andcommitted
Add a SinkBinding sample. (#2008)
* Add a SinkBinding sample. This is largely derived from the ContainerSource docs, but using SinkBinding and a CronJob. * Fix the eventing-sources reference. * Add licenses
1 parent 3138186 commit 467b011

File tree

4 files changed

+318
-0
lines changed

4 files changed

+318
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
title: "Sink Binding Example"
3+
linkTitle: "Sink Binding"
4+
weight: 10
5+
type: "docs"
6+
---
7+
8+
A SinkBinding is responsible for linking together "addressable" Kubernetes
9+
resources that may receive events (aka the event "sink") with Kubernetes
10+
resources that embed a PodSpec (as `spec.template.spec`) and want to produce
11+
events.
12+
13+
The SinkBinding can be used to author new event sources using any of the
14+
familiar compute abstractions that Kubernetes makes available (e.g. Deployment,
15+
Job, DaemonSet, StatefulSet), or Knative abstractions (e.g. Service,
16+
Configuration).
17+
18+
19+
## Create a CronJob that uses SinkBinding
20+
21+
### Prerequisites
22+
23+
1. Setup [Knative Serving](../../../serving).
24+
1. Setup [Knative Eventing and Sources](../../../eventing).
25+
26+
### Prepare the heartbeats image
27+
28+
Knative [event-contrib](https://github.com/knative/eventing-contrib) has a
29+
sample of heartbeats event source. You could clone the source codes by
30+
31+
```
32+
git clone -b "{{< branch >}}" https://github.com/knative/eventing-contrib.git
33+
```
34+
35+
And then build a heartbeats image and publish to your image repo with
36+
37+
```
38+
ko publish knative.dev/eventing-contrib/cmd/heartbeats
39+
```
40+
41+
**Note**: `ko publish` requires:
42+
43+
- [`KO_DOCKER_REPO`](https://github.com/knative/serving/blob/master/DEVELOPMENT.md#environment-setup)
44+
to be set. (e.g. `gcr.io/[gcloud-project]` or `docker.io/<username>`)
45+
- you to be authenticated with your `KO_DOCKER_REPO`
46+
47+
### Creating our event sink
48+
49+
In order to verify our `SinkBinding` is working, we will create an Event Display
50+
Service that dumps incoming messages to its log.
51+
52+
```yaml
53+
apiVersion: serving.knative.dev/v1
54+
kind: Service
55+
metadata:
56+
name: event-display
57+
spec:
58+
template:
59+
spec:
60+
containers:
61+
- image: gcr.io/knative-releases/github.com/knative/eventing-contrib/cmd/event_display
62+
```
63+
64+
Use following command to create the service from `service.yaml`:
65+
66+
```shell
67+
kubectl apply --filename service.yaml
68+
```
69+
70+
The status of the created service can be seen using:
71+
72+
```shell
73+
kubectl get ksvc
74+
75+
NAME URL LATESTCREATED LATESTREADY READY REASON
76+
event-display http://event-display.default.1.2.3.4.xip.io event-display-gqjbw event-display-gqjbw True
77+
```
78+
79+
### Create our SinkBinding
80+
81+
In order to direct events to our Event Display, we will first create a
82+
SinkBinding that will inject `$K_SINK` into select `Jobs`:
83+
84+
```yaml
85+
apiVersion: sources.eventing.knative.dev/v1alpha1
86+
kind: SinkBinding
87+
metadata:
88+
name: bind-heartbeat
89+
spec:
90+
subject:
91+
apiVersion: batch/v1
92+
kind: Job
93+
selector:
94+
matchLabels:
95+
app: heartbeat-cron
96+
97+
sink:
98+
ref:
99+
apiVersion: serving.knative.dev/v1
100+
kind: Service
101+
name: event-display
102+
```
103+
104+
In this case, we will bind any `Job` with the labels `app: heartbeat-cron`.
105+
106+
Use the following command to create the event source from `sinkbinding.yaml`:
107+
108+
```shell
109+
kubectl apply --filename sinkbinding.yaml
110+
```
111+
112+
### Create our CronJob
113+
114+
Now we will use the heartbeats container to send events to `$K_SINK` every time
115+
the CronJob runs:
116+
117+
```yaml
118+
apiVersion: batch/v1beta1
119+
kind: CronJob
120+
metadata:
121+
name: heartbeat-cron
122+
spec:
123+
spec:
124+
# Run every minute
125+
schedule: "* * * * *"
126+
jobTemplate:
127+
metadata:
128+
labels:
129+
app: heartbeat-cron
130+
spec:
131+
template:
132+
spec:
133+
restartPolicy: Never
134+
containers:
135+
- name: single-heartbeat
136+
image: <FILL IN YOUR IMAGE HERE>
137+
args:
138+
- --period=1
139+
env:
140+
- name: ONE_SHOT
141+
value: "true"
142+
- name: POD_NAME
143+
valueFrom:
144+
fieldRef:
145+
fieldPath: metadata.name
146+
- name: POD_NAMESPACE
147+
valueFrom:
148+
fieldRef:
149+
fieldPath: metadata.namespace
150+
```
151+
152+
First, edit `heartbeats-source.yaml` to include the image name from the
153+
`ko publish` command above, then run the following to apply it:
154+
155+
```shell
156+
kubectl apply --filename heartbeats-source.yaml
157+
```
158+
159+
### Verify
160+
161+
We will verify that the message was sent to the Knative eventing system by
162+
looking at event-display service logs.
163+
164+
```shell
165+
kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m
166+
```
167+
168+
You should see log lines showing the request headers and body of the event
169+
message sent by the heartbeats source to the display function:
170+
171+
```
172+
☁️ cloudevents.Event
173+
Validation: valid
174+
Context Attributes,
175+
specversion: 0.3
176+
type: dev.knative.eventing.samples.heartbeat
177+
source: https://knative.dev/eventing-contrib/cmd/heartbeats/#event-test/mypod
178+
id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596
179+
time: 2019-10-18T15:23:20.809775386Z
180+
contenttype: application/json
181+
Extensions,
182+
beats: true
183+
heart: yes
184+
the: 42
185+
Data,
186+
{
187+
"id": 2,
188+
"label": ""
189+
}
190+
```
191+
192+
## Using the SinkBinding with a Knative Service
193+
194+
SinkBinding is also compatible with our Knative Serving Cloud Events
195+
[samples](../../../serving/samples/cloudevents); as a next step try using those
196+
together. For example, the [`cloudevents-go`
197+
sample](../../../serving/samples/cloudevents/cloudevents-go) may be bound with:
198+
199+
```yaml
200+
apiVersion: sources.eventing.knative.dev/v1alpha1
201+
kind: SinkBinding
202+
metadata:
203+
name: bind-heartbeat
204+
spec:
205+
subject:
206+
apiVersion: serving.knative.dev/v1
207+
kind: Service
208+
name: cloudevents-go
209+
210+
sink:
211+
ref:
212+
apiVersion: serving.knative.dev/v1
213+
kind: Service
214+
name: event-display
215+
```
216+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2019 The Knative Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: batch/v1beta1
16+
kind: CronJob
17+
metadata:
18+
name: heartbeat-cron
19+
spec:
20+
spec:
21+
# Run every minute
22+
schedule: "* * * * *"
23+
jobTemplate:
24+
metadata:
25+
labels:
26+
app: heartbeat-cron
27+
spec:
28+
template:
29+
spec:
30+
restartPolicy: Never
31+
containers:
32+
# This corresponds to a heartbeats image uri you build and publish,
33+
# e.g. gcr.io/[gcloud-project]/github.com/knative/eventing-contrib/cmd/heartbeats
34+
- name: single-heartbeat
35+
image: knative.dev/eventing-contrib/cmd/heartbeats
36+
args:
37+
- --period=1
38+
env:
39+
- name: ONE_SHOT
40+
value: "true"
41+
- name: POD_NAME
42+
valueFrom:
43+
fieldRef:
44+
fieldPath: metadata.name
45+
- name: POD_NAMESPACE
46+
valueFrom:
47+
fieldRef:
48+
fieldPath: metadata.namespace
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 The Knative Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: serving.knative.dev/v1
16+
kind: Service
17+
metadata:
18+
name: event-display
19+
spec:
20+
template:
21+
spec:
22+
containers:
23+
- image: gcr.io/knative-releases/github.com/knative/eventing-contrib/cmd/event_display
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 The Knative Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: sources.eventing.knative.dev/v1alpha1
16+
kind: SinkBinding
17+
metadata:
18+
name: bind-heartbeat
19+
spec:
20+
subject:
21+
apiVersion: batch/v1
22+
kind: Job
23+
selector:
24+
matchLabels:
25+
app: heartbeat-cron
26+
27+
sink:
28+
ref:
29+
apiVersion: serving.knative.dev/v1
30+
kind: Service
31+
name: event-display

0 commit comments

Comments
 (0)