1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-06 06:46:26 +01:00
Files
openshift-docs/modules/apiserversource-yaml.adoc
2020-11-10 21:02:28 +00:00

252 lines
5.5 KiB
Plaintext

// Module included in the following assemblies:
//
// serverless/event_workflows/serverless-listing-event-sources.adoc
[id="apiserversource-yaml_context"]
= Using the ApiServerSource with the YAML method
This guide describes the steps required to create an ApiServerSource using YAML files.
.Prerequisites
* You will need to have a Knative Serving and Eventing installation.
* You will need to have created the `default` broker in the same namespace as the one defined in the ApiServerSource YAML file.
.Procedure
. To create a service account, role, and role binding for the ApiServerSource, create a file named `authentication.yaml` and copy the following sample code into it:
+
[source,yaml]
----
apiVersion: v1
kind: ServiceAccount
metadata:
name: events-sa
namespace: default <1>
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: event-watcher
namespace: default <1>
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: k8s-ra-event-watcher
namespace: default <1>
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: event-watcher
subjects:
- kind: ServiceAccount
name: events-sa
namespace: default <1>
----
+
<1> Change this namespace to the namespace that you have selected for installing ApiServerSource.
+
[NOTE]
====
If you want to re-use an existing service account with the appropriate permissions, you must modify the `authentication.yaml` for that service account.
====
+
After you have created the `authentication.yaml` file, apply it:
+
[source,terminal]
----
$ oc apply -f authentication.yaml
----
. To create an ApiServerSource event source, create a file named `k8s-events.yaml` and copy the following sample code into it:
+
[source,yaml]
----
apiVersion: sources.knative.dev/v1alpha1
kind: ApiServerSource
metadata:
name: testevents
spec:
serviceAccountName: events-sa
mode: Resource
resources:
- apiVersion: v1
kind: Event
sink:
ref:
apiVersion: eventing.knative.dev/v1
kind: Broker
name: default
----
+
After you have created the `k8s-events.yaml` file, apply it:
+
[source,terminal]
----
$ oc apply -f k8s-events.yaml
----
. To check that the ApiServerSource is set up correctly, create a Knative service that dumps incoming messages to its log.
+
Copy the following sample YAML into a file named `service.yaml`:
+
[source,yaml]
----
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
namespace: default
spec:
template:
spec:
containers:
- image: quay.io/openshift-knative/knative-eventing-sources-event-display:latest
----
+
After you have created the `service.yaml` file, apply it:
+
[source,terminal]
----
$ oc apply -f service.yaml
----
. To create a trigger from the `default` broker that filters events to the service created in the previous step, create a file named `trigger.yaml` and copy the following sample code into it:
+
[source,yaml]
----
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: event-display-trigger
namespace: default
spec:
broker: default
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display
----
+
After you have created the `trigger.yaml` file, apply it:
+
[source,terminal]
----
$ oc apply -f trigger.yaml
----
. To create events, launch a Pod in the default namespace:
+
[source,terminal]
----
$ oc create deployment hello-node --image=quay.io/openshift-knative/knative-eventing-sources-event-display
----
. To check that the controller is mapped correctly, enter the following command and inspect the output:
+
[source,terminal]
----
$ oc get apiserversource.sources.knative.dev testevents -o yaml
----
+
.Example output
+
[source,yaml]
----
apiVersion: sources.knative.dev/v1alpha1
kind: ApiServerSource
metadata:
annotations:
creationTimestamp: "2020-04-07T17:24:54Z"
generation: 1
name: testevents
namespace: default
resourceVersion: "62868"
selfLink: /apis/sources.knative.dev/v1alpha1/namespaces/default/apiserversources/testevents2
uid: 1603d863-bb06-4d1c-b371-f580b4db99fa
spec:
mode: Resource
resources:
- apiVersion: v1
controller: false
controllerSelector:
apiVersion: ""
kind: ""
name: ""
uid: ""
kind: Event
labelSelector: {}
serviceAccountName: events-sa
sink:
ref:
apiVersion: eventing.knative.dev/v1
kind: Broker
name: default
----
.Verification steps
To verify that the Kubernetes events were sent to Knative, you can look at the message dumper function logs.
. Get the pods:
+
[source,terminal]
----
$ oc get pods
----
. View the message dumper function logs for the pods:
+
[source,terminal]
----
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
----
+
.Example output
+
[source,terminal]
----
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 1.0
type: dev.knative.apiserver.resource.update
datacontenttype: application/json
...
Data,
{
"apiVersion": "v1",
"involvedObject": {
"apiVersion": "v1",
"fieldPath": "spec.containers{hello-node}",
"kind": "Pod",
"name": "hello-node",
"namespace": "default",
.....
},
"kind": "Event",
"message": "Started container",
"metadata": {
"name": "hello-node.159d7608e3a3572c",
"namespace": "default",
....
},
"reason": "Started",
...
}
----