Skip to content

Commit

Permalink
Add Support for Embedding TriggerBindingSpec in EL
Browse files Browse the repository at this point in the history
TriggerBindingSpec can be added to EventListener Trigger
  • Loading branch information
khrm committed Apr 30, 2020
1 parent 4060450 commit 70113c1
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 62 deletions.
2 changes: 2 additions & 0 deletions docs/clustertriggerbindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ spec:
bindings:
- name: pipeline-clusterbinding
kind: ClusterTriggerBinding
ref: pipeline-clusterbinding
- name: message-clusterbinding
kind: ClusterTriggerBinding
ref: message-clusterbinding
template:
name: pipeline-template
```
Expand Down
18 changes: 17 additions & 1 deletion docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The `triggers` field is required. Each EventListener can consist of one or more
- `name` - (Optional) a valid
[Kubernetes name](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set)
- [`interceptors`](#interceptors) - (Optional) list of interceptors to use
- `bindings` - A list of names of `TriggerBindings` to use
- `bindings` - A list of names of `TriggerBindings` to use or embedded TriggerBindingsSpecs to use.
- `template` - The name of `TriggerTemplate` to use

```yaml
Expand All @@ -160,7 +160,14 @@ triggers:
eventTypes: ["pull_request"]
bindings:
- name: pipeline-binding
ref: pipeline-binding
- name: message-binding
ref: message-binding
- name: embed-binding
spec:
params:
- name: gitrevision
value: $(body.head_commit.id)
template:
name: pipeline-template
```
Expand All @@ -180,7 +187,9 @@ triggers:
eventTypes: ["pull_request"]
bindings:
- name: pipeline-binding
ref: pipeline-binding
- name: message-binding
ref: message-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -316,6 +325,7 @@ spec:
namespace: default
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -361,6 +371,7 @@ spec:
- pull_request
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -405,6 +416,7 @@ spec:
- Push Hook
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -450,6 +462,7 @@ spec:
filter: "header.canonical('X-GitHub-Event') == 'push'"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -488,6 +501,7 @@ spec:
filter: "header.canonical('X-GitHub-Event') == 'push'"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -515,6 +529,7 @@ spec:
expression: "truncate(body.pull_request.head.sha, 7)"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down Expand Up @@ -544,6 +559,7 @@ spec:
expression: "split(body.ref, '/')[2]"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ spec:
filter: "'test-secret'.compareSecret('token', 'mysecret')"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
1 change: 1 addition & 0 deletions examples/eventlisteners/cel-eventlistener-interceptor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ spec:
filter: "header.canonical('X-GitHub-Event') == 'push'"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ spec:
expression: "split(body.ref, '/')[2]"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
1 change: 1 addition & 0 deletions examples/eventlisteners/cel-eventlistener-no-filter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ spec:
expression: "truncate(body.pull_request.head.sha, 7)"
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ spec:
bindings:
- name: pipeline-clusterbinding
kind: ClusterTriggerBinding
ref: pipeline-clusterbinding
- name: message-clusterbinding
kind: ClusterTriggerBinding
ref: message-clusterbinding
template:
name: pipeline-template
19 changes: 19 additions & 0 deletions examples/eventlisteners/eventlistener-embedded-bindings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: listener-embed-binding
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: foo-trig
bindings:
- name: pipeline-binding
ref: pipeline-binding
- name: message-binding
spec:
params:
- name: message
value: Hello from the Triggers EventListener(listener-embed-binding)!
template:
name: pipeline-template
1 change: 1 addition & 0 deletions examples/eventlisteners/eventlistener-interceptor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ spec:
namespace: default
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ spec:
namespace: default
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
2 changes: 2 additions & 0 deletions examples/eventlisteners/eventlistener.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
- name: foo-trig
bindings:
- name: pipeline-binding
ref: pipeline-binding
- name: message-binding
ref: message-binding
template:
name: pipeline-template
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ spec:
- pull_request
bindings:
- name: pipeline-binding
ref: pipeline-binding
template:
name: pipeline-template
13 changes: 13 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (el *EventListener) SetDefaults(ctx context.Context) {
// set defaults
for i := range el.Spec.Triggers {
defaultBindings(&el.Spec.Triggers[i])
// TODO: Remove this before v0.6 release.
defaultMandatoryRef((&el.Spec.Triggers[i]))
}
}
}
Expand All @@ -40,3 +42,14 @@ func defaultBindings(t *EventListenerTrigger) {
}
}
}

// set default TriggerBinding kind for Bindings
func defaultMandatoryRef(t *EventListenerTrigger) {
if len(t.Bindings) > 0 {
for _, b := range t.Bindings {
if b.Ref == "" && b.Spec == nil {
b.Ref = b.Name
}
}
}
}
6 changes: 6 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ func TestEventListenerSetDefaults(t *testing.T) {
Bindings: []*v1alpha1.EventListenerBinding{
{
Name: "binding",
Ref: "binding",
},
{
Name: "namespace-binding",
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "namespace-binding",
},
{
Name: "cluster-binding",
Kind: v1alpha1.ClusterTriggerBindingKind,
Ref: "cluster-binding",
},
},
}},
Expand All @@ -59,14 +62,17 @@ func TestEventListenerSetDefaults(t *testing.T) {
{
Name: "binding",
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "binding",
},
{
Name: "namespace-binding",
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "namespace-binding",
},
{
Name: "cluster-binding",
Kind: v1alpha1.ClusterTriggerBindingKind,
Ref: "cluster-binding",
},
},
}},
Expand Down
9 changes: 6 additions & 3 deletions pkg/apis/triggers/v1alpha1/event_listener_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ type SecretRef struct {

// EventListenerBinding refers to a particular TriggerBinding or ClusterTriggerBindingresource.
type EventListenerBinding struct {
Name string `json:"name"`
Kind TriggerBindingKind `json:"kind"`
APIVersion string `json:"apiversion,omitempty"`
Name string `json:"name"`
Kind TriggerBindingKind `json:"kind"`
// +optional
Ref string `json:"ref"`
Spec *TriggerBindingSpec `json:"spec"`
APIVersion string `json:"apiversion,omitempty"`
}

// EventListenerTemplate refers to a particular TriggerTemplate resource.
Expand Down
12 changes: 11 additions & 1 deletion pkg/apis/triggers/v1alpha1/event_listener_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ func (t *EventListenerTrigger) validate(ctx context.Context) *apis.FieldError {
return apis.ErrMissingField(fmt.Sprintf("bindings[%d].name", i))
}

if b.Kind != NamespacedTriggerBindingKind && b.Kind != ClusterTriggerBindingKind {
// Either Ref or Spec should be present
if b.Ref == "" && b.Spec == nil {
return apis.ErrMissingOneOf(fmt.Sprintf("bindings[%d].Ref", i), fmt.Sprintf("bindings[%d].Spec", i))
}

// Both Ref and Spec can't be present at the same time
if b.Ref != "" && b.Spec != nil {
return apis.ErrMultipleOneOf(fmt.Sprintf("bindings[%d].Ref", i), fmt.Sprintf("bindings[%d].Spec", i))
}

if b.Ref != "" && b.Kind != NamespacedTriggerBindingKind && b.Kind != ClusterTriggerBindingKind {
return apis.ErrInvalidValue(fmt.Errorf("invalid kind"), fmt.Sprintf("bindings[%d].kind", i))
}
}
Expand Down
Loading

0 comments on commit 70113c1

Please sign in to comment.