Eventing Source to Sink
Before beginning to run the exercises, navigate to the tutorial chapter’s eventing
folder:
Event Source
Knative Eventing Sources are software components that emit events. The job of a Source is to connect to, drain, capture and potentially buffer events; often from an external system and then relay those events to the Sink.
Knative Eventing Sources installs the following four sources out-of-the-box:
kubectl api-resources --api-group='sources.knative.dev'
NAME SHORTNAMES APIVERSION NAMESPACED KIND
apiserversources sources.knative.dev/v1 true ApiServerSource
containersources sources.knative.dev/v1 true ContainerSource
pingsources sources.knative.dev/v1 true PingSource
sinkbindings sources.knative.dev/v1 true SinkBinding
Let’s see how a simple source, the PingSource looks like :
apiVersion: sources.knative.dev/v1
kind: PingSource (1)
metadata:
name: eventinghello-ping-source
spec: (2)
schedule: "*/2 * * * *"
data: '{"key": "every 2 mins"}'
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: eventinghello
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 | spec will be unique per source, per kind |
Referencing the Event Sink
Knative Eventing Sink is how you specify the event receiver — that is the consumer of the event--. Sinks can be invoked directly in a point-to-point fashion by referencing them via the Event Source’s sink
as shown below:
apiVersion: sources.knative.dev/v1
kind: PingSource
metadata:
name: eventinghello-ping-source
spec:
schedule: "*/2 * * * *"
data: '{"message": "Thanks for doing Knative Tutorial"}'
sink: (1)
ref:
apiVersion: serving.knative.dev/v1 (2)
kind: Service
name: eventinghello (3)
1 | sink can target any Kubernetes Service or |
2 | a Knative Service |
3 | Deployed as "eventinghello" |
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 jsonData that will be sent as part of the event. When you watch logs, you will notice this data being delivered to the service. |
Create 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/v1
kind: Service
metadata:
name: eventinghello
spec:
template:
metadata:
name: eventinghello-v1
annotations:
autoscaling.knative.dev/target: "1"
spec:
containers:
- image: quay.io/rhdevelopers/eventinghello:0.0.2
Deploy Sink Service
Run the following commands to create the service:
kn service create eventinghello \
--concurrency-target=1 \
--image=quay.io/rhdevelopers/eventinghello:0.0.2
kubectl apply -f ${TUTORIAL_HOME}/eventing/eventing-hello-sink.yaml
Create Event Source
Run the following commands to create the event source resources:
kn source ping create eventinghello-ping-source \
--schedule "*/2 * * * *" \
--data '{"message": "Thanks for doing Knative Tutorial"}' \
--sink ksvc:eventinghello
kubectl apply -f ${TUTORIAL_HOME}/eventing/eventinghello-source.yaml
Verification
kn source ping list
Running the above command should return the following result:
NAME SCHEDULE SINK AGE CONDITIONS READY REASON
eventinghello-ping-source */2 * * * * ksvc:eventinghello 9s 3 OK / 3 True
kubectl get pingsources.sources.knative.dev \
eventinghello-ping-source
Running the above command should return the following result:
NAME SINK AGE READY REASON
eventinghello-ping-source http://eventinghello.knativetutorial.svc.cluster.local 47s True
If you haven’t already, you can watch logs to see the ping job source sending an event every 2 minutes:
stern eventinghello -c user-container -n knativetutorial
...
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,427 INFO [eventing-hello] (executor-thread-1) ce-id=4b59c201-56a3-4189-b042-4246d3f33504
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,501 INFO [eventing-hello] (executor-thread-1) ce-source=/apis/v1/namespaces/user2-knativetutorial/pingsources/eventinghello-ping-source
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,501 INFO [eventing-hello] (executor-thread-1) ce-specversion=1.0
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,501 INFO [eventing-hello] (executor-thread-1) ce-time=2021-01-28T08:14:00.000224506Z
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,502 INFO [eventing-hello] (executor-thread-1) ce-type=dev.knative.sources.ping
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,502 INFO [eventing-hello] (executor-thread-1) content-type=application/json
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,502 INFO [eventing-hello] (executor-thread-1) content-length=47
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container 2021-01-28 08:14:05,502 INFO [eventing-hello] (executor-thread-1) POST:{"message":"Thanks for doing Knative Tutorial"}
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container I> No access restrictor found, access to any MBean is allowed
eventinghello-00001-deployment-c8f4f6758-qrjm6 user-container Jolokia: Agent started with URL https://10.129.4.97:8778/jolokia/