Skip to content

Commit

Permalink
refactor: move list of allowed ResourceTemplate types to its own package
Browse files Browse the repository at this point in the history
This will help reduce the number of places we keep track of the list of
types that Triggers can create especially as we add the v1beta1 types.

Partially addresses #494 and part of the work needed for #1067
  • Loading branch information
dibyom authored and tekton-robot committed May 4, 2021
1 parent 7e52b44 commit a23ac4f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
30 changes: 30 additions & 0 deletions pkg/apis/triggers/config/allowed_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
pipelinev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var Decoder runtime.Decoder

// TODO(dibyom): We should have a way of configuring this instead of an init function?
func init() {
scheme := runtime.NewScheme()
utilruntime.Must(pipelinev1alpha1.AddToScheme(scheme))
utilruntime.Must(pipelinev1beta1.AddToScheme(scheme))
codec := serializer.NewCodecFactory(scheme)
Decoder = codec.UniversalDecoder(
pipelinev1alpha1.SchemeGroupVersion,
pipelinev1beta1.SchemeGroupVersion,
)
}

// EnsureAllowedType returns nil if the resourceTemplate has an apiVersion
// and kind field set to one of the allowed ones.
func EnsureAllowedType(rt runtime.RawExtension) error {
_, err := runtime.Decode(Decoder, rt.Raw)
return err
}
38 changes: 38 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

pipelinev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"github.com/tektoncd/triggers/test"
bldr "github.com/tektoncd/triggers/test/builder"
Expand Down Expand Up @@ -274,6 +275,43 @@ func Test_EventListenerValidate(t *testing.T) {
},
},
},
}, {
name: "valid EventListener with embedded TriggerTemplate",
el: &v1alpha1.EventListener{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: v1alpha1.EventListenerSpec{
Triggers: []v1alpha1.EventListenerTrigger{{
Bindings: []*v1alpha1.EventListenerBinding{{
Ref: "tb",
Kind: v1alpha1.NamespacedTriggerBindingKind,
APIVersion: "v1alpha1", // TODO: APIVersions seem wrong?
}},
Template: &v1alpha1.EventListenerTemplate{
Spec: &v1alpha1.TriggerTemplateSpec{
Params: []v1alpha1.ParamSpec{{
Name: "foo",
Description: "desc",
Default: ptr.String("val"),
}},
ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{
RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1alpha1",
Kind: "TaskRun",
},
ObjectMeta: metav1.ObjectMeta{
Name: "$(tt.params.foo)",
},
}),
}},
},
},
}},
},
},
}}

for _, test := range tests {
Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/triggers/v1alpha1/trigger_template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,3 @@ type TriggerTemplateList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []TriggerTemplate `json:"items"`
}

// IsAllowedType returns true if the resourceTemplate has an apiVersion
// and kind field set to one of the allowed ones.
func (trt *TriggerResourceTemplate) IsAllowedType() error {
_, err := runtime.Decode(Decoder, trt.RawExtension.Raw)
return err
}
3 changes: 2 additions & 1 deletion pkg/apis/triggers/v1alpha1/trigger_template_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/tektoncd/pipeline/pkg/apis/validate"
"github.com/tektoncd/triggers/pkg/apis/triggers/config"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (s *TriggerTemplateSpec) validate(ctx context.Context) (errs *apis.FieldErr

func validateResourceTemplates(templates []TriggerResourceTemplate) (errs *apis.FieldError) {
for i, trt := range templates {
if err := trt.IsAllowedType(); err != nil {
if err := config.EnsureAllowedType(trt.RawExtension); err != nil {
if runtime.IsMissingVersion(err) {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("[%d].apiVersion", i)))
}
Expand Down

0 comments on commit a23ac4f

Please sign in to comment.