Container Source Example

    1. Setup Knative Serving.
    2. Setup .

    Prepare the heartbeats image

    Knative has a sample of heartbeats event source. You could clone the source codes by

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

    Note: ko publish requires:

    • KO_DOCKER_REPO to be set. (e.g. gcr.io/[gcloud-project] or docker.io/<username>)
    • you to be authenticated with your KO_DOCKER_REPO
    • to be installed

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

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: event-display
    5. spec:
    6. template:
    7. spec:
    8. containers:

    Use following command to create the service from service.yaml:

    1. kubectl get ksvc
    2. NAME URL LATESTCREATED LATESTREADY READY REASON
    3. event-display http://event-display.default.1.2.3.4.xip.io event-display-gqjbw event-display-gqjbw True

    Create a 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 in your image repo in file. Note that arguments and environment variables are set and will be passed to the container.

    1. apiVersion: sources.knative.dev/v1
    2. metadata:
    3. name: test-heartbeats
    4. spec:
    5. template:
    6. spec:
    7. containers:
    8. - image: <heartbeats_image_uri>
    9. name: heartbeats
    10. args:
    11. - --period=1
    12. env:
    13. - name: POD_NAME
    14. value: "mypod"
    15. - name: POD_NAMESPACE
    16. value: "event-test"
    17. sink:
    18. ref:
    19. apiVersion: serving.knative.dev/v1
    20. name: event-display

    Use the following command to create the event source from heartbeats-source.yaml:

    We will verify that the message was sent to the Knative eventing system by looking at event-display service logs.

    1. kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m

    You should see log lines showing the request headers and body of the event message sent by the heartbeats source to the display function:

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

    In order to create a new event source using ContainerSource, you will create a container image at first, and then create a ContainerSource with the image uri and specify the values of parameters.

    Develop, build and publish a container image

    • The container image must have a main method to start with.
    • The main method will accept parameters from arguments and environment variables.
    • Two environments variables will be injected by the ContainerSource controller, K_SINK and K_CE_OVERRIDES, resolved from spec.sink and spec.ceOverrides respectively.
    • The event messages shall be sent to the sink URI specified in . The message can be any format. format is recommended.

    heartbeats event source is a sample for your reference.

    When the container image is ready, a YAML file will be used to create a concrete ContainerSource. Use as a sample for reference. Learn more about the ContainerSource specification.

    Was this page helpful?

    Glad to hear it! Please .

    Sorry to hear that. Please tell us how we can improve.