Creating a ContainerSource object

API version v1

This topic describes how to configure ContainerSource as an event source for functions.

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.

In the examples below, the event source is a heartbeats container and the sink is a Knative Service. If you have an existing event source and sink, you can replace the examples with your own values.

Before you begin

Before you can create a ContainerSource object:

  • You must have Knative Eventing installed on your cluster.
  • If you want to use the example heartbeats event source below, you must also:
    • Install ko
    • Set KO_DOCKER_REPO. For example, gcr.io/[gcloud-project] or docker.io/<username>
    • Authenticate with your KO_DOCKER_REPO
    • Install docker

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. If you do not already have an image, you can use the following example heartbeats event source by running the commands:

    1. git clone -b "release-0.24" https://github.com/knative/eventing.git
    1. ko publish ko://knative.dev/eventing/cmd/heartbeats
  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, containersource-example.

  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, by running the command:

    Note

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

    kn

    1. kn service create event-display --port 8080 --image gcr.io/knative-releases/knative.dev/eventing/cmd/event_display

    YAML

    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/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
  4. Create a concrete ContainerSource with specific arguments and environment settings by running the command:

    kn

    1. kn source container create <name> --image <image-uri> --sink <sink>

    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/[gcloud-project]/knative.dev/eventing/cmd/heartbeats.
    • <sink> is the name of your sink, for example <event-display>.

    For a list of available options, see the Knative client documentation.

    YAML

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

    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/[gcloud-project]/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.

    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 example below, 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. }

Cleanup

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 specification.