Skip to content

Commit

Permalink
Add support for custom object to triggers eventlistener
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed Mar 2, 2021
1 parent 2a28864 commit 39fb9d7
Show file tree
Hide file tree
Showing 29 changed files with 2,099 additions and 172 deletions.
4 changes: 3 additions & 1 deletion config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ rules:
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]

- apiGroups: ["serving.knative.dev"]
resources: ["*", "*/status", "*/finalizers"]
verbs: ["get", "list", "create", "update", "delete", "deletecollection", "patch", "watch"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
8 changes: 7 additions & 1 deletion config/300-eventlistener.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ spec:
jsonPath: ".status.conditions[?(@.type=='Available')].status"
- name: Reason
type: string
jsonPath: ".status.conditions[?(@.type=='Available')].reason"
jsonPath: ".status.conditions[?(@.type=='Available')].reason"
- name: Ready
type: string
jsonPath: ".status.conditions[?(@.type=='Ready')].status"
- name: Reason
type: string
jsonPath: ".status.conditions[?(@.type=='Ready')].reason"
84 changes: 81 additions & 3 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ using [Event Interceptors](#Interceptors).
- [Replicas](#replicas)
- [PodTemplate](#podtemplate)
- [Resources](#resources)
- [kubernetesResource](#kubernetesresource)
- [CustomResource](#customresource)
- [Contract](#contract)
- [Logging](#logging)
- [NamespaceSelector](#namespaceSelector)
- [Labels](#labels)
Expand All @@ -45,6 +48,7 @@ using [Event Interceptors](#Interceptors).
- [ServiceAccount per EventListenerTrigger](#serviceaccount-per-eventlistenertrigger)
- [EventListener Secure Connection](#eventlistener-secure-connection)
- [Prerequisites](#prerequisites)
- [EventListener Response Format](#eventlistener-response-format)

## Syntax

Expand All @@ -70,7 +74,7 @@ the following fields:
- [`replicas`](#replicas) - Specifies the number of EventListener pods
- [`podTemplate`](#podTemplate) - Specifies the PodTemplate
for your EventListener pod
- [`resources`](#resources) - Specifies the Kubernetes Resource information
- [`resources`](#resources) - Specifies the Kubernetes/Custom Resource shape for the EventListener sink
for your EventListener pod
- [`namespaceSelector`](#namespaceSelector) - Specifies the namespaces where
EventListener can fetch triggers from and create Tekton resources.
Expand Down Expand Up @@ -225,8 +229,11 @@ For more info on the design refer [TEP-0008](https://github.com/tektoncd/communi
Right now the `resources` field is optional in order to support backward compatibility with original behavior of `podTemplate`, `serviceType` and `serviceAccountName` fieds.
In the future, we plan to remove `serviceType` and `podTemplate` from the EventListener spec in favor of the `resources` field.

For now `resources` has support for `kubernetesResource` but later it will have a support for Custom CRD`(ex: Knative Service)` as `customResource`
`resources` has support for
* **kubernetesResource**
* **CustomResource** for Custom CRD`(ex: Knative Service)`

#### kubernetesResource
`kubernetesResource` have two fields
* ServiceType
* Spec(PodTemplateSpec)
Expand Down Expand Up @@ -257,7 +264,60 @@ spec:

With the help of `kubernetesResource` user can specify [PodTemplateSpec](https://github.com/kubernetes/api/blob/master/core/v1/types.go#L3704).

Right now the allowed values as part of `podSpec` are
#### CustomResource
A `CustomResource` object has one field that supports dynamic objects.
* runtime.RawExtension

Here we will use a [Knative Service](https://knative.dev/docs/) as an example to demonstrate usage of `CustomResource`

**Note:** `Knative Should be installed on the cluster` [ref](https://github.com/tektoncd/community/blob/main/teps/0008-support-knative-service-for-triggers-eventlistener-pod.md#note)
```yaml
spec:
resources:
customResource:
apiVersion: serving.knative.dev/v1
kind: Service
# metadata:
# name: knativeservice # name field is optional if not provided Triggers will use el name with the el- prefix ex: el-github-knative-listener
spec:
template:
spec:
serviceAccountName: tekton-triggers-example-sa
containers:
- resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
```

With the help of `CustomResource` user can specify any dynamic object which adheres to the Contract described below.

##### Contract
For Knative or any new CRD should satisfy [WithPod{}](https://github.com/knative/pkg/blob/master/apis/duck/v1/podspec_types.go#L41)

**Spec**
```spec
spec:
template:
metadata:
spec:
```
**Status**
```status
type EventListenerStatus struct {
duckv1beta1.Status `json:",inline"`

// EventListener is Addressable. It currently exposes the service DNS
// address of the the EventListener sink
duckv1alpha1.AddressStatus `json:",inline"`
}
```

##### Note
For both `CustomResource` and `kubernetesResource` the allowed values for `PodSpec` and `Containers` are
```text
ServiceAccountName
NodeSelector
Expand Down Expand Up @@ -853,3 +913,21 @@ To setup TLS connection add two set of reserved environment variables `TLS_CERT`
where we need to specify the `secret` which contains `cert` and `key` files. See the full [example](../examples/eventlistener-tls-connection/README.md) for more details.

Refer [TEP-0027](https://github.com/tektoncd/community/blob/master/teps/0027-https-connection-to-triggers-eventlistener.md) for more information on design and user stories.

## EventListener Response Format

```
kubectl get el
NAME ADDRESS AVAILABLE REASON READY REASON
tls-listener-interceptor http://el-tls-listener-interceptor.default.svc.cluster.local True MinimumReplicasAvailable
```
Where
* **NAME:** Name of the created eventlistener
* **ADDRESS:** Address of the eventlistener
* **AVAILABLE** This state indicates readiness of Kubernetes Deployment and Service
* **REASON** Shows the failure reason for Kubernetes Deployment and Service
* **READY** This state indicates readiness of Custom Resource ex: Knative Service
* **REASON** Shows the failure reason for Custom Resource
**Note:** The response format will be refactored as part of [issue-932](https://github.com/tektoncd/triggers/issues/932)
35 changes: 35 additions & 0 deletions examples/custom-resource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## GitHub Knative EventListener

Creates an EventListener that listens for GitHub webhook events.

### Try it out locally:

1. To create the custom resource trigger and all related resources, run:

```bash
kubectl apply -f examples/custom-resource/
```

1. Test by sending the sample payload:

```bash
curl -v \
-H 'X-GitHub-Event: pull_request' \
-H 'X-Hub-Signature: sha1=ba0cdc263b3492a74b601d240c27efe81c4720cb' \
-H 'Content-Type: application/json' \
-d '{"action": "opened", "pull_request":{"head":{"sha": "28911bbb5a3e2ea034daf1f6be0a822d50e31e73"}},"repository":{"clone_url": "https://github.com/tektoncd/triggers.git"}}' \
http://localhost:8080
```

The response status code is `201 Created`

[`HMAC`](https://www.freeformatter.com/hmac-generator.html) tool used to create X-Hub-Signature.

In [`HMAC`](https://www.freeformatter.com/hmac-generator.html) `string` is the *body payload ex:* `{"action": "opened", "pull_request":{"head":{"sha": "28911bbb5a3e2ea034daf1f6be0a822d50e31e73"}},"repository":{"clone_url": "https://github.com/tektoncd/triggers.git"}}`
and `secretKey` is the *given secretToken ex:* `1234567`.

1. You will see the newly created TaskRun:

```bash
kubectl get taskruns | grep github-run-
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: github-knative-listener
spec:
triggers:
- name: github-listener
interceptors:
- github:
secretRef:
secretName: github-secret
secretKey: secretToken
eventTypes:
- pull_request
- cel:
filter: "body.action in ['opened', 'synchronize', 'reopened']"
bindings:
- ref: github-pr-binding
template:
ref: github-template
resources:
customResource:
apiVersion: serving.knative.dev/v1
kind: Service
spec:
template:
spec:
serviceAccountName: tekton-triggers-example-sa
containers:
- resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
name: github-pr-binding
spec:
params:
- name: gitrevision
value: $(body.pull_request.head.sha)
- name: gitrepositoryurl
value: $(body.repository.clone_url)

---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
name: github-template
spec:
params:
- name: gitrevision
- name: gitrepositoryurl
resourcetemplates:
- apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
generateName: github-run-
spec:
taskSpec:
inputs:
resources:
- name: source
type: git
steps:
- image: ubuntu
script: |
#! /bin/bash
ls -al $(inputs.resources.source.path)
inputs:
resources:
- name: source
resourceSpec:
type: git
params:
- name: revision
value: $(tt.params.gitrevision)
- name: url
value: $(tt.params.gitrepositoryurl)
1 change: 1 addition & 0 deletions examples/custom-resource/rbac.yaml
7 changes: 7 additions & 0 deletions examples/custom-resource/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: github-secret
type: Opaque
stringData:
secretToken: "1234567"
6 changes: 3 additions & 3 deletions examples/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ rules:
resources: ["eventlisteners", "triggerbindings", "triggertemplates", "triggers"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
# secrets are only needed for GitHub/GitLab interceptors
# configmaps is needed for updating logging config
# secrets are only needed for GitHub/GitLab interceptors
# configmaps is needed for updating logging config
resources: ["configmaps", "secrets"]
verbs: ["get", "list", "watch"]
# Permissions to create resources in associated TriggerTemplates
# Permissions to create resources in associated TriggerTemplates
- apiGroups: ["tekton.dev"]
resources: ["pipelineruns", "pipelineresources", "taskruns"]
verbs: ["create"]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/google/go-github/v31 v31.0.0
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.7.4
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.0.0
github.com/tektoncd/pipeline v0.20.1-0.20210203144343-1b7a37f0d21d
github.com/tektoncd/plumbing v0.0.0-20201021153918-6b7e894737b5
Expand Down
Loading

0 comments on commit 39fb9d7

Please sign in to comment.