Creating a ContainerSource object¶
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 following examples, 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 following example heartbeats event source, you must also:
Create a ContainerSource object¶
-
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 inK_SINK
. If you do not already have an image, you can use the following example heartbeats event source by running the commands:git clone -b "release-0.26" https://github.com/knative/eventing.git
ko publish ko://knative.dev/eventing/cmd/heartbeats
-
Create a namespace for your ContainerSource by running the command:
kubectl create namespace <namespace>
Where
<namespace>
is the namespace that you want your ContainerSource to use. For example,containersource-example
. -
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.
-
To create a sink, run the command:
kn service create event-display --port 8080 --image gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
-
Create a YAML file using the following example:
apiVersion: apps/v1 kind: Deployment metadata: name: event-display spec: replicas: 1 selector: matchLabels: &labels app: event-display template: metadata: labels: *labels spec: containers: - name: event-display image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display --- kind: Service apiVersion: v1 metadata: name: event-display spec: selector: app: event-display ports: - protocol: TCP port: 80 targetPort: 8080
-
Apply the YAML file by running the command:
Wherekubectl apply -f <filename>.yaml
<filename>
is the name of the file you created in the previous step.
-
-
Create a concrete ContainerSource with specific arguments and environment settings:
-
To create the ContainerSource, run the command:
Where:kn source container create <name> --image <image-uri> --sink <sink>
<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.
-
Create a YAML file using the following template:
Where:apiVersion: sources.knative.dev/v1 kind: ContainerSource metadata: name: <containersource-name> spec: template: spec: containers: - image: <event-source-image-uri> name: <container-name> env: - name: POD_NAME value: "<pod-name>" - name: POD_NAMESPACE value: "<pod-namespace>" sink: ref: apiVersion: v1 kind: Service name: <sink>
<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.
-
Apply the YAML file by running the command:
Wherekubectl apply -f <filename>.yaml
<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¶
-
View the logs for your event consumer by running the command:
Where:kubectl -n <namespace> logs -l <pod-name> --tail=200
<namespace>
is the namespace that contains the ContainerSource object.<pod-name>
is the name of the Pod that the container runs in.
For example:
$ kubectl -n containersource-example logs -l app=event-display --tail=200
-
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
andData
properties of the events that the ContainerSource sent to theevent-display
Service:☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: dev.knative.eventing.samples.heartbeat source: https://knative.dev/eventing/cmd/heartbeats/#event-test/mypod id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596 time: 2019-10-18T15:23:20.809775386Z contenttype: application/json Extensions, beats: true heart: yes the: 42 Data, { "id": 2, "label": "" }
Cleanup¶
To delete the ContainerSource object and all of the related resources in the namespace:
-
Delete the namespace by running the command:
kubectl delete namespace <namespace>
Where
<namespace>
is the namespace that contains the ContainerSource object.
Reference Documentation¶
See the ContainerSource reference.