Kubernetes event using the API Server Source
You must have a Knative cluster running both the Serving and Eventing components. To learn how to install the required components, see Installing Knative.
You can follow the steps below to create new files, or you clone a copy from the repo by running:
Deployment Steps
These instructions assume the namespace , which you can change to your preferred namespace. If you use a different namespace, you will need to modify all the YAML files deployed in this sample to point at that namespace.
Create the
default
Broker in your namespace:kubectl create -f - <<EOF
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: default
EOF
Service Account
Create a Service Account that the
ApiServerSource
runs as. TheApiServerSource
watches for Kubernetes events and forwards them to the Knative Eventing Broker. Create a file namedserviceaccount.yaml
and copy the code block below into it.apiVersion: v1
kind: ServiceAccount
metadata:
name: events-sa
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: event-watcher
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: k8s-ra-event-watcher
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
subjects:
- kind: ServiceAccount
name: events-sa
namespace: default
Enter the following command to create the service account from
serviceaccount.yaml
:kubectl apply --filename serviceaccount.yaml
In order to receive events, you have to create a concrete Event Source for a specific namespace. Create a file named
k8s-events.yaml
and copy the code block below into it.If you want to consume events from a different namespace or use a different
Service Account
, you need to modifyk8s-events.yaml
accordingly.Enter the following command to create the event source:
Trigger
In order to check the ApiServerSource
is fully working, we will create a simple Knative Service that dumps incoming messages to its log and creates a Trigger
from the Broker
to that Knative Service.
-
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: testevents-trigger
namespace: default
spec:
broker: default
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display
---
# This is a very simple Knative Service that writes the input request to its log.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
namespace: default
spec:
template:
spec:
containers:
- # This corresponds to
# https://github.com/knative/eventing-contrib/tree/main/cmd/event_display/main.go
image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
If the deployed
ApiServerSource
is pointing at aBroker
other thandefault
, modifytrigger.yaml
by addingspec.broker
with theBroker
’s name.Deploy
trigger.yaml
:kubectl apply --filename trigger.yaml
Create events by launching a pod in the default namespace. Create a busybox
container and immediately delete it:
Verify
We will verify that the Kubernetes events were sent into the Knative eventing system by looking at our message dumper function logs. If you deployed the , continue using this section. If not, you will need to look downstream yourself:
kubectl get pods
kubectl logs -l serving.knative.dev/service=event-display -c user-container
You should see log lines similar to:
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 1.0
type: dev.knative.apiserver.resource.update
subject: /apis/v1/namespaces/default/events/testevents.15dd3050eb1e6f50
time: 2020-07-28T16:35:14.172979816Z
datacontenttype: application/json
Extensions,
kind: Event
knativearrivaltime: 2020-07-28T16:35:14.173381505Z
knativehistory: default-kne-trigger-kn-channel.default.svc.cluster.local
name: busybox.1625f7cfa4cd12f8
namespace: default
Data,
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2020-07-28T16:35:14Z",
"involvedObject": {
"apiVersion": "v1",
"fieldPath": "spec.containers{busybox}",
"kind": "Pod",
"name": "busybox",
"namespace": "default",
"resourceVersion": "28987493",
"uid": "1efb342a-737b-11e9-a6c5-42010a8a00ed"
},
"kind": "Event",
"lastTimestamp": "2020-07-28T16:35:14Z",
"message": "Started container",
"metadata": {
"creationTimestamp": "2020-07-28T16:35:14Z",
"name": "busybox.1625f7cfa4cd12f8",
"namespace": "default",
"resourceVersion": "506088",
"selfLink": "/api/v1/namespaces/default/events/busybox.1625f7cfa4cd12f8",
"uid": "7f841049-7979-48db-9cbc-93ed2346a1b5",
},
"reason": "Started",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "kubelet",
"host": "gke-knative-auto-cluster-default-pool-23c23c4f-xdj0"
},
"type": "Normal"
}
kubectl --namespace default delete --filename serviceaccount.yaml
kubectl --namespace default delete --filename trigger.yaml