Writing Custom Knative Event Sources

Event Producer

Examples of traditional event producers are kafka, github webhook, nats, redis events etc. Knative currently provides/maintains a list of commonly used event sources, which can be found here.

Event Sink

Event sink is any addressable component, i.e. kubernetes resources that could be resolved to an URI. For example knative service, kubernetes service, knative eventing channel etc. List of the same can be retrieved using the discovery api.

Building an Event Source

Now that we have defined the producer, source and sink, let us talk about building a custom event source. As defined earlier and shown below, source is the link between producer and sink and it comes down to building, distributing and deploying this link aka adapter on demand.

Options

  1. Controller
  2. Sink Binding
  3. Container Source

Controller Approach

This approach involves building a kubernetes controller. Building a kubernetes controller can be cumbersome for many, fortunately knative provides a template project to get it up and running quickly. Below are the components you will be building,

  1. Adapter

Sink Binding Approach

The controller approach is a well rounded approach, apart from having a dedicated adapter for event generation, it brings in all the benefits of the kubernetes operator like versioning, seamless upgrades, validation webhooks. But it might not be suitable for all use cases, for example there can be scenarios where a knative service or a kubernetes service or a kubernetes job would like to send an event or you may want to chain sources, this is where sink binding comes in.

  1. K_SINK — URI of the addressable service (knative service, kubernetes service etc)
  2. K_CE_OVERRIDES — Overrides to control the output format and modifications of the event sent to the sink.
selector:
matchLabels:
app: sample-source

Container Source Approach

The third option is kind of a hybrid between first and second, wherein you create a custom resource to get the adapter scheduled in the desired namespace, but in this approach the user just needs to build the adapter, knative eventing controller would operate this adapter.

Final Thoughts

Based on your needs you can choose one of the sources, below is my observation.

+-------------------------------+------------+--------------+------+
| Feature | Controller | Sink Binding | CS |
+-------------------------------+------------+--------------+------+
| Dedicated Adapter | Yes | Depends | Yes | |
| CRD | Yes | No | Yes | |
| Language | Go | Any | Any | |
| knative controller dependency | No | Yes | Yes | |
| Maintained by cluster admin | Yes | No | No | |
| Versioning | Yes | No | No | |
| Resource Validation | Yes | No | No | +-------------------------------+------------+--------------+------+

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Murugappan Sevugan Chetty

Murugappan Sevugan Chetty

Software Engineer, Open Source Contributor