Skip to content

Commit

Permalink
Add TriggerCRD object validation and default
Browse files Browse the repository at this point in the history
Defaults and Validation for TriggerCRD object have been added.
  • Loading branch information
khrm committed Sep 8, 2020
1 parent 45daf11 commit a386dea
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 124 deletions.
1 change: 1 addition & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("EventListener"): &v1alpha1.EventListener{},
v1alpha1.SchemeGroupVersion.WithKind("TriggerBinding"): &v1alpha1.TriggerBinding{},
v1alpha1.SchemeGroupVersion.WithKind("TriggerTemplate"): &v1alpha1.TriggerTemplate{},
v1alpha1.SchemeGroupVersion.WithKind("Trigger"): &v1alpha1.Trigger{},
}

func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
Expand Down
14 changes: 2 additions & 12 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,8 @@ func (el *EventListener) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
// set defaults
for i := range el.Spec.Triggers {
defaultBindings(&el.Spec.Triggers[i])
}
}
}

// set default TriggerBinding kind for Bindings
func defaultBindings(t *EventListenerTrigger) {
if len(t.Bindings) > 0 {
for _, b := range t.Bindings {
if b.Kind == "" {
b.Kind = NamespacedTriggerBindingKind
}
triggerSpecBindingArray(el.Spec.Triggers[i].Bindings).
defaultBindings()
}
}
}
118 changes: 6 additions & 112 deletions pkg/apis/triggers/v1alpha1/event_listener_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package v1alpha1
import (
"context"
"fmt"
"net/http"

"github.com/google/cel-go/cel"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"k8s.io/apimachinery/pkg/util/validation"
"knative.dev/pkg/apis"
)
Expand Down Expand Up @@ -55,34 +52,17 @@ func (t *EventListenerTrigger) validate(ctx context.Context) *apis.FieldError {
}

// Validate optional Bindings
for i, b := range t.Bindings {
// 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))
}
if err := triggerSpecBindingArray(t.Bindings).validate(ctx); err != nil {
return err
}
// Validate required TriggerTemplate
// Optional explicit match
if t.Template != nil {
if t.Template.APIVersion != "" {
if t.Template.APIVersion != "v1alpha1" {
return apis.ErrInvalidValue(fmt.Errorf("invalid apiVersion"), "template.apiVersion")
}
}
if t.Template.Name == "" {
return apis.ErrMissingField("template.name")
// Validate required TriggerTemplate
if err := t.Template.validate(ctx); err != nil {
return err
}
}

// Validate optional Interceptors
for i, interceptor := range t.Interceptors {
if err := interceptor.validate(ctx).ViaField(fmt.Sprintf("interceptors[%d]", i)); err != nil {
return err
Expand All @@ -97,89 +77,3 @@ func (t *EventListenerTrigger) validate(ctx context.Context) *apis.FieldError {

return nil
}

func (i *EventInterceptor) validate(ctx context.Context) *apis.FieldError {
if i.Webhook == nil && i.GitHub == nil && i.GitLab == nil && i.CEL == nil && i.Bitbucket == nil {
return apis.ErrMissingField("interceptor")
}

// Enforce oneof
numSet := 0
if i.Webhook != nil {
numSet++
}
if i.GitHub != nil {
numSet++
}
if i.GitLab != nil {
numSet++
}
if i.Bitbucket != nil {
numSet++
}

if numSet > 1 {
return apis.ErrMultipleOneOf("interceptor.webhook", "interceptor.github", "interceptor.gitlab")
}

if i.Webhook != nil {
if i.Webhook.ObjectRef == nil || i.Webhook.ObjectRef.Name == "" {
return apis.ErrMissingField("interceptor.webhook.objectRef")
}
w := i.Webhook
if w.ObjectRef.Kind != "Service" {
return apis.ErrInvalidValue(fmt.Errorf("invalid kind"), "interceptor.webhook.objectRef.kind")
}

// Optional explicit match
if w.ObjectRef.APIVersion != "v1" {
return apis.ErrInvalidValue(fmt.Errorf("invalid apiVersion"), "interceptor.webhook.objectRef.apiVersion")
}

for i, header := range w.Header {
// Enforce non-empty canonical header keys
if len(header.Name) == 0 || http.CanonicalHeaderKey(header.Name) != header.Name {
return apis.ErrInvalidValue(fmt.Errorf("invalid header name"), fmt.Sprintf("interceptor.webhook.header[%d].name", i))
}
// Enforce non-empty header values
if header.Value.Type == pipelinev1.ParamTypeString {
if len(header.Value.StringVal) == 0 {
return apis.ErrInvalidValue(fmt.Errorf("invalid header value"), fmt.Sprintf("interceptor.webhook.header[%d].value", i))
}
} else if len(header.Value.ArrayVal) == 0 {
return apis.ErrInvalidValue(fmt.Errorf("invalid header value"), fmt.Sprintf("interceptor.webhook.header[%d].value", i))
}
}
}

// No github validation required yet.
// if i.GitHub != nil {
//
// }

// No gitlab validation required yet.
// if i.GitLab != nil {
//
// }

if i.CEL != nil {
if i.CEL.Filter == "" && len(i.CEL.Overlays) == 0 {
return apis.ErrMultipleOneOf("cel.filter", "cel.overlays")
}
env, err := cel.NewEnv()
if err != nil {
return apis.ErrInvalidValue(fmt.Errorf("failed to create a CEL env: %s", err), "cel.filter")
}
if i.CEL.Filter != "" {
if _, issues := env.Parse(i.CEL.Filter); issues != nil && issues.Err() != nil {
return apis.ErrInvalidValue(fmt.Errorf("failed to parse the CEL filter: %s", issues.Err()), "cel.filter")
}
}
for _, v := range i.CEL.Overlays {
if _, issues := env.Parse(v.Expression); issues != nil && issues.Err() != nil {
return apis.ErrInvalidValue(fmt.Errorf("failed to parse the CEL overlay: %s", issues.Err()), "cel.overlay")
}
}
}
return nil
}
42 changes: 42 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2020 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
)

type triggerSpecBindingArray []*TriggerSpecBinding

// SetDefaults sets the defaults on the object.
func (t *Trigger) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
// set defaults
triggerSpecBindingArray(t.Spec.Bindings).defaultBindings()
}
}

// set default TriggerBinding kind for Bindings in TriggerSpec
func (t triggerSpecBindingArray) defaultBindings() {
if len(t) > 0 {
for _, b := range t {
if b.Kind == "" {
b.Kind = NamespacedTriggerBindingKind
}
}
}
}
86 changes: 86 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2020 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
)

func TestTriggerSetDefaults(t *testing.T) {
tests := []struct {
name string
in *v1alpha1.Trigger
want *v1alpha1.Trigger
wc func(context.Context) context.Context
}{{
name: "default binding",
in: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Bindings: []*v1alpha1.TriggerSpecBinding{
{
Ref: "binding",
},
{
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "namespace-binding",
},
{
Kind: v1alpha1.ClusterTriggerBindingKind,
Ref: "cluster-binding",
},
},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Bindings: []*v1alpha1.TriggerSpecBinding{
{
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "binding",
},
{
Kind: v1alpha1.NamespacedTriggerBindingKind,
Ref: "namespace-binding",
},
{
Kind: v1alpha1.ClusterTriggerBindingKind,
Ref: "cluster-binding",
},
},
},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.in
ctx := context.Background()
if tc.wc != nil {
ctx = tc.wc(ctx)
}
got.SetDefaults(ctx)

if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("SetDefaults (-want, +got) = %v", diff)
}
})
}
}
Loading

0 comments on commit a386dea

Please sign in to comment.