ContainerSource

version

ContainerSource will start a container image which will generate events under certain situations and send messages to a sink URI. It also can be an easy way to support your own event sources in Knative. This guide shows how to configure ContainerSource as an event source for functions and summarizes guidelines for creating your own event source as a ContainerSource.

Prerequisites

  • Install ko
  • Set KO_DOCKER_REPO (e.g. gcr.io/[gcloud-project] or docker.io/<username>)
  • Authenticated with your KO_DOCKER_REPO
  • Install docker

Installation

The ContainerSource source type is enabled by default when you install Knative Eventing.

Example

This example shows how the heartbeats container sends events to the Event Display Service.

Preparing the heartbeats image

Knative event-sources has a sample of heartbeats event source. You could clone the source code by

  1. git clone -b "release-0.21" https://github.com/knative/eventing-contrib.git

And then build a heartbeats image and publish to your image repo with

  1. ko publish knative.dev/eventing-contrib/cmd/heartbeats

Creating a namespace

Create a new namespace called containersource-example by entering the following command:

  1. kubectl create namespace containersource-example

Creating the Event Display Service

In order to verify ContainerSource is working, we will create a Event Display Service that dumps incoming messages to its log.

  1. kubectl -n containersource-example apply -f - << EOF
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: event-display
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels: &labels
  10. app: event-display
  11. template:
  12. metadata:
  13. labels: *labels
  14. spec:
  15. containers:
  16. - name: event-display
  17. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  18. ---
  19. kind: Service
  20. apiVersion: v1
  21. metadata:
  22. name: event-display
  23. spec:
  24. selector:
  25. app: event-display
  26. ports:
  27. - protocol: TCP
  28. port: 80
  29. targetPort: 8080
  30. EOF

Creating the ContainerSource using the heartbeats image

In order to run the heartbeats container as an event source, you have to create a concrete ContainerSource with specific arguments and environment settings. Be sure to replace heartbeats_image_uri with a valid uri for your heartbeats image you published in the previous step. Note that arguments and environment variables are set and will be passed to the container.

  1. kubectl -n containersource-example apply -f - << EOF
  2. apiVersion: sources.knative.dev/v1
  3. kind: ContainerSource
  4. metadata:
  5. name: test-heartbeats
  6. spec:
  7. template:
  8. spec:
  9. containers:
  10. # This corresponds to a heartbeats image uri you build and publish in the previous step
  11. # e.g. gcr.io/[gcloud-project]/knative.dev/eventing-contrib/cmd/heartbeats
  12. - image: <heartbeats_image_uri>
  13. name: heartbeats
  14. args:
  15. - --period=1
  16. env:
  17. - name: POD_NAME
  18. value: "mypod"
  19. - name: POD_NAMESPACE
  20. value: "event-test"
  21. sink:
  22. ref:
  23. apiVersion: v1
  24. kind: Service
  25. name: event-display
  26. EOF

Verify

View the logs for the event-display event consumer by entering the following command:

  1. kubectl -n containersource-example logs -l app=event-display --tail=200

This returns the Attributes and Data of the events that the ContainerSource sent to the event-display Service:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.eventing.samples.heartbeat
  6. source: https://knative.dev/eventing-contrib/cmd/heartbeats/#event-test/mypod
  7. id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596
  8. time: 2019-10-18T15:23:20.809775386Z
  9. contenttype: application/json
  10. Extensions,
  11. beats: true
  12. heart: yes
  13. the: 42
  14. Data,
  15. {
  16. "id": 2,
  17. "label": ""
  18. }

Cleanup

Delete the containersource-example namespace and all of its resources from your cluster by entering the following command:

  1. kubectl delete namespace containersource-example

Reference Documentation

See the ContainerSource specification.

Contact

For any inquiries about this source, please reach out on to the Knative users group.