Skip to content

Instantly share code, notes, and snippets.

@matzew
Created March 5, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matzew/5b0b28b8462e574d9f38796f6037c795 to your computer and use it in GitHub Desktop.
Save matzew/5b0b28b8462e574d9f38796f6037c795 to your computer and use it in GitHub Desktop.

Migration from ContainerSource to SinkBinding

With the Knative 0.13 release the ContainerSource is deprecated, and it is highly to use the SinkBinding instead.

From ContainerSource ...

With older releases (up to 0.12) the ContainerSource usage was like:

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: ContainerSource
metadata:
  name: urbanobservatory-event-source
spec:
  image: quay.io/openshift-knative/knative-eventing-sources-websocketsource:latest 
  args: 
    - '--source=wss://api.usb.urbanobservatory.ac.uk/stream'
    - '--eventType=my.custom.event'
  sink:
    apiVersion: serving.knative.dev/v1
    kind: Service
    name: wss-event-display

The referenced image of the ContainerSource needed an "sink" argument, see for details.

... to SinkBinding

With the new release things are slightly different, but not much

Instead of a ContainerSource, you need to create a SinkBinding, like:

apiVersion: sources.knative.dev/v1alpha2
kind: SinkBinding
metadata:
  name: bind-wss
spec:
  subject:
    apiVersion: apps/v1
    kind: Deployment
    selector:
      matchLabels:
        app: wss
  sink:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: wss-event-display

In this example it reference a Kubernetes Deployment like you might have used before. Below is the referenced Deployment of our SinkBinding example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wss
  labels:
    app: wss
spec:
  selector:
    matchLabels:
      app: wss
  template:
    metadata:
      labels:
        app: wss
    spec:
      containers:
        - image: quay.io/openshift-knative/knative-eventing-sources-websocketsource:latest
          name: wss
          args: 
            - '--source=wss://api.usb.urbanobservatory.ac.uk/stream'
            - '--eventType=my.custom.event'

The deployment looks like any other deployment, but what's important here is that the label app: wss is needed, for binding the two together. In side of the used image, it is required that it understands the semantics of the K_SINK environment variable, which is used to resolve the DNS name for the sink, that should receive the events.

Make sure you apply the SinkBinding before you apply the Deployment. The SinkBinding injects the K_SINK to the image of the deployment. So the code can read it to use it as its sink references for sending out events.

Running the above example will give a log like:

☁️  cloudevents.Event
Validation: valid
Context Attributes,
  specversion: 1.0
  type: my.custom.event
  source: wss://api.usb.urbanobservatory.ac.uk/stream
  id: 3029a2f2-3ce1-48c0-9ed3-37d7ad88d0ef
  time: 2020-03-05T13:46:15.329595422Z
Data,
  {"signal":2,"data":{"brokerage":{"broker":{"id":"BMS-USB-5-JACE","meta":{"protocol":"BACNET","building":"Urban Sciences Building","buildingFloor":"5"}},"id":"Drivers.L6_C3_Electric_Meters.C3_Mechcanical_Plant.points.C3_HP_Current_L1","meta":{}},"entity":{"name":"Urban Sciences Building: Floor 5","meta":{"building":"Urban Sciences Building","buildingFloor":"5"}},"feed":{"metric":"C3 HP Current L1","meta":{}},"timeseries":{"unit":"no units","value":{"time":"2020-03-05T13:45:51.468Z","timeAccuracy":8.754,"data":0.47110211849212646,"type":"Real"}}},"recipients":0}

Code change on the image

Converting an image that used to work with ContainerSource, to be ready to go with the SinkBinding, requires a little change like:

https://github.com/knative/eventing-contrib/commit/746aa562f6389e80f71b8ba91539fd7348504a23#diff-32df043ca9a0c7fb0730e4d31e1dd3f0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment