-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TriggerCRD object validation and default
Defaults and Validation for TriggerCRD object have been added.
- Loading branch information
Showing
7 changed files
with
598 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
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" | ||
"net/http" | ||
|
||
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
"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 | ||
} | ||
|
||
func (i *TriggerInterceptor) 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") | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.