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 7, 2020
1 parent e55f86f commit ff1227e
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 0 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
40 changes: 40 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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"
)

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

// set default TriggerBinding kind for Bindings
func (t *Trigger) defaultBindings() {
if len(t.Spec.Bindings) > 0 {
for _, b := range t.Spec.Bindings {
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)
}
})
}
}
71 changes: 71 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
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"
"fmt"

"github.com/tektoncd/pipeline/pkg/apis/validate"

"knative.dev/pkg/apis"
)

// Validate validates a Trigger
func (t *Trigger) Validate(ctx context.Context) *apis.FieldError {
if err := validate.ObjectMetadata(t.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return t.Spec.validate(ctx).ViaField("spec")
}

func (t *TriggerSpec) 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))
}
}
// Validate required TriggerTemplate
// Optional explicit match
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")
}

for i, interceptor := range t.Interceptors {
if err := interceptor.validate(ctx).ViaField(fmt.Sprintf("interceptors[%d]", i)); err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit ff1227e

Please sign in to comment.