EventListener
EventListener is a Kubernetes custom resource that allows users a declarativeway to process incoming HTTP based events with JSON payloads. EventListenersexpose an addressable “Sink” to which incoming events are directed. Users candeclare TriggerBindings to extract fields from events,and apply them to TriggerTemplates in order to createTekton resources. In addition, EventListeners allow lightweight event processingusing Event Interceptors.
Multi-Tenant Concerns
The EventListener is effectively an additional form of client into Tekton, versus whatexample usage via kubectl
or tkn
which you have seen elsewhere. In particular, the HTTP basedevents bypass the normal Kubernetes authentication path you get via kubeconfig
filesand the kubectl config
family of commands.
As such, there are set of items to consider when deciding how to
- best expose (each) EventListener in your cluster to the outside world.
- best control how (each) EventListener and the underlying API Objects described below access, create,and update Tekton related API Objects in your cluster.
Minimally, each EventListener has its ServiceAccountName as noted below and allevents coming over the “Sink” result in any Tekton resource interactions being done with the permissionsassigned to that ServiceAccount.
However, if you need differing levels of permissions over a set of Tekton resources across the variousTriggers and Interceptors, where not all Triggers or Interceptors canmanipulate certain Tekton Resources in the same way, a simple, single EventListener will not suffice.
Your options at that point are as follows:
Multiple EventListers (One EventListener Per Namespace)
You can create multiple EventListener objects, where your set of Triggers and Interceptors are spread out across theEventListeners.
If you create each of those EventListeners in their own namespace, it becomes easy to assignvarying permissions to the ServiceAccount of each one to serve your needs. And often times namespacecreation is coupled with a default set of ServiceAccounts and Secrets that are also defined.So conceivably some administration steps are taken care of. You just update the permissionsof the automatically created ServiceAccounts.
Possible drawbacks:- Namespaces with associated Secrets and ServiceAccounts in an aggregate sense prove to be the most expensiveitems in Kubernetes underlying etcd
store. In larger clusters etcd
storage capacity can become a concern.- Multiple EventListeners means multiple HTTP ports that must be exposed to the external entities accessingthe “Sink”. If you happen to have a HTTP Firewall between your Cluster and external entities, that means moreadministrative cost, opening ports in the firewall for each Service, unless you can employ Kubernetes Ingress
toserve as a routing abstraction layer for your set of EventListeners.
Multiple EventListeners (Multiple EventListeners per Namespace)
Multiple EventListeners per namespace will most likely mean more ServiceAccount/Secret/RBAC manipulation forthe administrator, as some of the built in generation of those artifacts as part of namespace creation are notapplicable.
However you will save some on the etcd
storage costs by reducing the number of namespaces.
Multiple EventListeners and potential Firewall concerns still apply (again unless you employ Ingress
).
ServiceAccount per EventListenerTrigger
Being able to set a ServiceAccount on an EventListenerTrigger allows for finer grained permissions as well.
You still have to create the additional ServiceAccounts.
But staying within 1 namespace and minimizing the number of EventListeners with their associated “Sinks” minimizesconcerns around etcd
storage and port considerations with Firewalls if Ingress
is not utilized.
Syntax
To define a configuration file for an EventListener
resource, you can specifythe following fields:
- Required:
apiVersion
- Specifies the API version, for exampletriggers.tekton.dev/v1alpha1
.kind
- Specifies theEventListener
resourceobject.metadata
- Specifies data to uniquely identify theEventListener
resource object, for example aname
.spec
- Specifies the configuration information foryour EventListener resource object. In order for an EventListener to doanything, the spec must include:triggers
- Specifies a list of Triggers to runserviceAccountName
- Specifies the ServiceAccountthat the EventListener uses to create resources
- Optional:
serviceType
- Specifies what type of service the sink podis exposed as
ServiceAccountName
The serviceAccountName
field is required. The ServiceAccount that theEventListener sink uses to create the Tekton resources. The ServiceAccount needsa role with the following rules:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-triggers-example-minimal
rules:
# Permissions for every EventListener deployment to function
- apiGroups: ["triggers.tekton.dev"]
resources: ["eventlisteners", "triggerbindings", "triggertemplates"]
verbs: ["get"]
- apiGroups: [""]
# secrets are only needed for Github/Gitlab interceptors, serviceaccounts only for per trigger authorization
resources: ["configmaps", "secrets", "serviceaccounts"]
verbs: ["get", "list", "watch"]
# Permissions to create resources in associated TriggerTemplates
- apiGroups: ["tekton.dev"]
resources: ["pipelineruns", "pipelineresources", "taskruns"]
verbs: ["create"]
If your EventListener is usingClusterTriggerBindings
, you’ll need aServiceAccount with aClusterRole instead.
Triggers
The triggers
field is required. Each EventListener can consist of one or moretriggers
. A Trigger consists of:
name
- (Optional) a validKubernetes nameinterceptors
- (Optional) list of interceptors to usebindings
- A list of names ofTriggerBindings
to usetemplate
- The name ofTriggerTemplate
to use
triggers:
- name: trigger-1
interceptors:
- github:
eventTypes: ["pull_request"]
bindings:
- name: pipeline-binding
- name: message-binding
template:
name: pipeline-template
Also, to support multi-tenant styled scenarios, where an administrator may not want all triggers to havethe same permissions as the EventListener
, a service account can optionally be set at the trigger leveland used if present in place of the EventListener
service account when creating resources:
triggers:
- name: trigger-1
serviceAccount:
name: trigger-1-sa
namespace: event-listener-namespace
interceptors:
- github:
eventTypes: ["pull_request"]
bindings:
- name: pipeline-binding
- name: message-binding
template:
name: pipeline-template
The default ClusterRole for the EventLister allows for reading ServiceAccounts from any namespace.
ServiceType
The serviceType
field is optional. EventListener sinks are exposed viaKubernetes Services.By default, the serviceType is ClusterIP
which means any pods running in thesame Kubernetes cluster can access services’ via their cluster DNS. Other validvalues are NodePort
and LoadBalancer
. Check theKubernetes Service typesdocumentations for details.
For external services to connect to your cluster (e.g. GitHub sending webhooks),check out the guide on exposing EventListeners.
Logging
EventListener sinks are exposed as Kubernetes services that are backed by a Podrunning the sink logic. The logging configuration can be controlled via theconfig-logging-triggers
ConfigMap present in the namespace that theEventListener was created in. This ConfigMap is automatically created andcontains the default values defined inconfig-logging.yaml.
To access logs for the EventListener sink, you can query for pods with theeventlistener
label set to the name of your EventListener resource:
kubectl get pods --selector eventlistener=my-eventlistener
Labels
By default, EventListeners will attach the following labels automatically to allresources it creates:
Name | Description |
---|---|
triggers.tekton.dev/eventlistener | Name of the EventListener that generated the resource. |
triggers.tekton.dev/trigger | Name of the Trigger that generated the resource. |
triggers.tekton.dev/eventid | UID of the incoming event. |
Since the EventListener name and Trigger name are used as label values, theymust adhere to theKubernetes syntax and character set requirementsfor label values.
Interceptors
Triggers within an EventListener
can optionally specify interceptors, tomodify the behavior or payload of Triggers.
Event Interceptors can take several different forms today:
Webhook Interceptors
Webhook Interceptors allow users to configure an external k8s object whichcontains business logic. These are currently specified under the Webhook
field, which contains anObjectReference
to a Kubernetes Service. If a Webhook Interceptor is specified, theEventListener
sink will forward incoming events to the service referenced bythe Interceptor over HTTP. The service is expected to process the event andreturn a response back. The status code of the response determines if theprocessing is successful - a 200 response means the Interceptor was successfuland that processing should continue, any other status code will halt Triggerprocessing. The returned request (body and headers) is used as the new eventpayload by the EventListener and passed on the TriggerBinding
. An Interceptorhas an optional header field with key-value pairs that will be merged with eventheaders before being sent;canonical namesmust be specified.
When multiple Interceptors are specified, requests are piped through eachInterceptor sequentially for processing - e.g. the headers/body of the firstInterceptor’s response will be sent as the request to the second Interceptor. Itis the responsibility of Interceptors to preserve header/body data if desired.The response body and headers of the last Interceptor is used for resourcebinding/templating.
Event Interceptor Services
To be an Event Interceptor, a Kubernetes object should:
- Be fronted by a regular Kubernetes v1 Service over port 80
- Accept JSON payloads over HTTP
- Accept HTTP POST requests with JSON payloads.
- Return a HTTP 200 OK Status if the EventListener should continue processingthe event
- Return a JSON body back. This will be used by the EventListener as the eventpayload for any further processing. If the Interceptor does not need to modifythe body, it can simply return the body that it received.
- Return any Headers that might be required by other chained Interceptors or anybindings.
Note: It is the responsibility of Interceptors to preserve header/body dataif desired. The response body and headers of the last Interceptor is used forresource binding/templating.
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: listener-interceptor
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: foo-trig
interceptors:
- webhook:
header:
- name: Foo-Trig-Header1
value: string-value
- name: Foo-Trig-Header2
value:
- array-val1
- array-val2
objectRef:
kind: Service
name: gh-validate
apiVersion: v1
namespace: default
bindings:
- name: pipeline-binding
template:
name: pipeline-template
GitHub Interceptors
GitHub Interceptors contain logic to validate and filter webhooks that come fromGitHub. Supported features include validating webhooks actually came from GitHubusing the logic outlined in GitHubdocumentation, as well asfiltering incoming events.
To use this Interceptor as a validator, create a secret string using the methodof your choice, and configure the GitHub webhook to use that secret value.Create a Kubernetes secret containing this value, and pass that as a referenceto the github
Interceptor.
To use this Interceptor as a filter, add the event types you would like toaccept to the eventTypes
field. Valid values can be found in GitHubdocs.
The body/header of the incoming request will be preserved in this Interceptor’sresponse.
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: github-listener-interceptor
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: foo-trig
interceptors:
- github:
secretRef:
secretName: foo
secretKey: bar
eventTypes:
- pull_request
bindings:
- name: pipeline-binding
template:
name: pipeline-template
GitLab Interceptors
GitLab Interceptors contain logic to validate and filter requests that come fromGitLab. Supported features include validating that a webhook actually came fromGitLab, using the logic outlined in GitLabdocumentation,and to filter incoming events based on the event types. Event types can be foundin GitLabdocumentation.
To use this Interceptor as a validator, create a secret string using the methodof your choice, and configure the GitLab webhook to use that secret value.Create a Kubernetes secret containing this value, and pass that as a referenceto the gitlab
Interceptor.
To use this Interceptor as a filter, add the event types you would like toaccept to the eventTypes
field.
The body/header of the incoming request will be preserved in this Interceptor’sresponse.
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: gitlab-listener-interceptor
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: foo-trig
interceptors:
- gitlab:
secretRef:
secretName: foo
secretKey: bar
eventTypes:
- Push Hook
bindings:
- name: pipeline-binding
template:
name: pipeline-template
CEL Interceptors
CEL Interceptors can be used to filter or modify incoming events, using theCEL expression language.
Please read thecel-spec language definitionfor more details on the expression language syntax.
The cel-trig-with-matches
trigger below filters events that don’t have an'X-GitHub-Event'
header matching 'pull_request'
.
It also modifies the incoming request, adding an extra key to the JSON body,with a truncated string coming from the hook body.
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: cel-listener-interceptor
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: cel-trig-with-matches
interceptors:
- cel:
filter: "header.match('X-GitHub-Event', 'pull_request')"
overlays:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
- name: cel-trig-with-canonical
interceptors:
- cel:
filter: "header.canonical('X-GitHub-Event') == 'push'"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
In addition to the standard expressions provided by CEL, Triggers supports someuseful functions for dealing with event dataCEL expressions.
The body/header of the incoming request will be preserved in this Interceptor’sresponse.
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: cel-listener-interceptor
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: cel-trig-with-matches
interceptors:
- cel:
filter: "header.match('X-GitHub-Event', 'pull_request')"
overlays:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
- name: cel-trig-with-canonical
interceptors:
- cel:
filter: "header.canonical('X-GitHub-Event') == 'push'"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
The filter
expression must return a true
value if this trigger is to beprocessed, and the overlays
applied.
Optionally, no filter
expression can be provided, and the overlays
will beapplied to the incoming body.
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: cel-eventlistener-no-filter
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: cel-trig
interceptors:
- cel:
overlays:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
Overlays
The CEL interceptor supports “overlays”, these are CEL expressions that areapplied to the body before it’s returned to the event-listener.
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: example-with-multiple-overlays
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: cel-trig
interceptors:
- cel:
overlays:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
- key: extensions.branch_name
expression: "truncate(body.ref.split, '/')[2]"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
In this example, the bindings will see two additional fields:
Assuming that the input body looked something like this:
{
"ref": "refs/heads/master",
"pull_request": {
"head": {
"sha": "6113728f27ae82c7b1a177c8d03f9e96e0adf246"
}
}
}
The output body would look like this:
{
"ref": "refs/heads/master",
"pull_request": {
"head": {
"sha": "6113728f27ae82c7b1a177c8d03f9e96e0adf246"
}
},
"extensions": {
"truncated_sha": "6113728",
"branch_name": "master"
}
}
The key
element of the overlay can create new elements in a body, or, overlayexisting elements.
For example, this expression:
- key: body.pull_request.head.short_sha
expression: "truncate(body.pull_request.head.sha, 7)"
Would see the short_sha
being inserted into the existing body:
{
"ref": "refs/heads/master",
"pull_request": {
"head": {
"sha": "6113728f27ae82c7b1a177c8d03f9e96e0adf246",
"short_sha": "6113728"
}
}
}
It’s even possible to replace existing fields, by providing a key that matchesthe path to an existing value.
Anything that is applied as an overlay can be extracted using a binding e.g.
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
name: pipeline-binding-with-cel-extensions
spec:
params:
- name: gitrevision
value: $(body.extensions.branch_name)
- name: branch
value: $(body.pull_request.head.short_sha)
Examples
For complete examples, seethe examples folder.