Create a ContainerSource

API version v1

The ContainerSource object starts a container image that generates events and sends messages to a sink URI. You can also use ContainerSource to support your own event sources in Knative.

To create a custom event source using ContainerSource, you must create a container image, and a ContainerSource that uses your image URI.

Before you begin

Before you can create a ContainerSource object, you must have Knative Eventing installed on your cluster.

Develop, build and publish a container image

You can develop a container image by using any language, and can build and publish your image by using any tools you like. The following are some basic guidelines:

  • Two environments variables are injected by the ContainerSource controller; K_SINK and K_CE_OVERRIDES, resolved from spec.sink and spec.ceOverrides respectively.
  • The event messages are sent to the sink URI specified in K_SINK. The message must be sent as a POST in CloudEvents HTTP format.

Create a ContainerSource object

  1. Build an image of your event source and publish it to your image repository. Your image must read the environment variable K_SINK and post messages to the URL specified in K_SINK.

    You can use the following YAML to deploy a demo heartbeats event source:

    1. apiVersion: sources.knative.dev/v1
    2. kind: ContainerSource
    3. metadata:
    4. name: heartbeat-source
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats:latest
    10. name: heartbeats
    11. sink:
    12. ref:
    13. apiVersion: serving.knative.dev/v1
    14. kind: Service
    15. name: event-display
  2. Create a namespace for your ContainerSource by running the command:

    1. kubectl create namespace <namespace>

    Where <namespace> is the namespace that you want your ContainerSource to use. For example, heartbeat-source.

  3. Create a sink. If you do not already have a sink, you can use the following Knative Service, which dumps incoming messages into its log:

    Note

    To create a Knative service you must have Knative Serving installed on your cluster.

    knYAML

    • To create a sink, run the command:

      1. kn service create event-display --port 8080 --image gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
    1. Create a YAML file using the following example:

      1. apiVersion: apps/v1
      2. kind: Deployment
      3. metadata:
      4. name: event-display
      5. spec:
      6. replicas: 1
      7. selector:
      8. matchLabels: &labels
      9. app: event-display
      10. template:
      11. metadata:
      12. labels: *labels
      13. spec:
      14. containers:
      15. - name: event-display
      16. image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
      17. ---
      18. kind: Service
      19. apiVersion: v1
      20. metadata:
      21. name: event-display
      22. spec:
      23. selector:
      24. app: event-display
      25. ports:
      26. - protocol: TCP
      27. port: 80
      28. targetPort: 8080
    2. Apply the YAML file by running the command:

      1. kubectl apply -f <filename>.yaml

      Where <filename> is the name of the file you created in the previous step.

  4. Create a concrete ContainerSource with specific arguments and environment settings:

    knYAML

    • To create the ContainerSource, run the command:

      1. kn source container create <name> --image <image-uri> --sink <sink> -e POD_NAME=<pod-name> -e POD_NAMESPACE=<pod-namespace>

      Where:

      • <name> is the name you want for your ContainerSource object, for example, test-heartbeats.
      • <image-uri> corresponds to the image URI you built and published in step 1, for example, gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats.
      • <pod-name> is the name of the Pod that the container runs in, for example, mypod.
      • <pod-namespace> is the namespace that the Pod runs in, for example, event-test.
      • <sink> is the name of your sink, for example, event-display. For a list of available options, see the Knative client documentation.
    1. Create a YAML file using the following template:

      1. apiVersion: sources.knative.dev/v1
      2. kind: ContainerSource
      3. metadata:
      4. name: <containersource-name>
      5. spec:
      6. template:
      7. spec:
      8. containers:
      9. - image: <event-source-image-uri>
      10. name: <container-name>
      11. env:
      12. - name: POD_NAME
      13. value: "<pod-name>"
      14. - name: POD_NAMESPACE
      15. value: "<pod-namespace>"
      16. sink:
      17. ref:
      18. apiVersion: v1
      19. kind: Service
      20. name: <sink>

      Where:

      • <namespace> is the namespace you created for your ContainerSource, for example, containersource-example.
      • <containersource-name> is the name you want for your ContainerSource, for example, test-heartbeats.
      • <event-source-image-uri> corresponds to the image URI you built and published in step 1, for example, gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats.
      • <container-name> is the name of your event source, for example, heartbeats.
      • <pod-name> is the name of the Pod that the container runs in, for example, mypod.
      • <pod-namespace> is the namespace that the Pod runs in, for example, event-test.
      • <sink> is the name of your sink, for example, event-display.

      For more information about the fields you can configure for the ContainerSource object, see ContainerSource Reference.

    2. Apply the YAML file by running the command:

      1. kubectl apply -f <filename>.yaml

      Where <filename> is the name of the file you created in the previous step.

    Note

    Arguments and environment variables are set and are passed to the container.

Verify the ContainerSource object

  1. View the logs for your event consumer by running the command:

    1. kubectl -n <namespace> logs -l <pod-name> --tail=200

    Where:

    • <namespace> is the namespace that contains the ContainerSource object.
    • <pod-name> is the name of the Pod that the container runs in.

    For example:

    1. $ kubectl -n containersource-example logs -l app=event-display --tail=200
  2. Verify that the output returns the properties of the events that your ContainerSource sent to your sink. In the following example, the command has returned the Attributes and Data properties 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/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. }

Delete the ContainerSource object

To delete the ContainerSource object and all of the related resources in the namespace:

  • Delete the namespace by running the command:

    1. kubectl delete namespace <namespace>

    Where <namespace> is the namespace that contains the ContainerSource object.

Reference Documentation

See the ContainerSource reference.