Skip to content

Commit

Permalink
Define a helper method pkg/controller.FilterRunRef
Browse files Browse the repository at this point in the history
This will be useful to external Custom Task controllers to filter out
update notifications for Runs of irrelevant types.
  • Loading branch information
imjasonh authored and tekton-robot committed Jul 9, 2020
1 parent 732d236 commit fe77b31
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/controller/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 controller provides helper methods for external controllers for
// Custom Task types.
package controller

import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)

// FilterRunRef returns a filter that can be passed to a Run Informer, which
// filters out Runs for apiVersion and kinds that a controller doesn't care
// about.
//
// For example, a controller impl that wants to be notified of updates to Runs
// which reference a Task with apiVersion "example.dev/v0" and kind "Example":
//
// runinformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
// FilterFunc: FilterRunRef("example.dev/v0", "Example"),
// Handler: controller.HandleAll(impl.Enqueue),
// })
func FilterRunRef(apiVersion, kind string) func(interface{}) bool {
return func(obj interface{}) bool {
r, ok := obj.(*v1alpha1.Run)
if !ok {
// Somehow got informed of a non-Run object.
// Ignore.
return false
}
if r == nil || r.Spec.Ref == nil {
// These are invalid, but just in case they get
// created somehow, don't panic.
return false
}

return r.Spec.Ref.APIVersion == apiVersion && r.Spec.Ref.Kind == v1alpha1.TaskKind(kind)
}
}
105 changes: 105 additions & 0 deletions pkg/controller/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 controller_test

import (
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/controller"
)

const (
apiVersion = "example.dev/v0"
kind = "Example"
)

func TestFilterRunRef(t *testing.T) {
for _, c := range []struct {
desc string
in interface{}
want bool
}{{
desc: "not a Run",
in: struct{}{},
want: false,
}, {
desc: "nil Run",
in: (*v1alpha1.Run)(nil),
want: false,
}, {
desc: "nil ref",
in: &v1alpha1.Run{
Spec: v1alpha1.RunSpec{
Ref: nil,
},
},
want: false,
}, {
desc: "Run without matching apiVersion",
in: &v1alpha1.Run{
Spec: v1alpha1.RunSpec{
Ref: &v1alpha1.TaskRef{
APIVersion: "not-matching",
Kind: kind,
},
},
},
want: false,
}, {
desc: "Run without matching kind",
in: &v1alpha1.Run{
Spec: v1alpha1.RunSpec{
Ref: &v1alpha1.TaskRef{
APIVersion: apiVersion,
Kind: "not-matching",
},
},
},
want: false,
}, {
desc: "Run with matching apiVersion and kind",
in: &v1alpha1.Run{
Spec: v1alpha1.RunSpec{
Ref: &v1alpha1.TaskRef{
APIVersion: apiVersion,
Kind: kind,
},
},
},
want: true,
}, {
desc: "Run with matching apiVersion and kind and name",
in: &v1alpha1.Run{
Spec: v1alpha1.RunSpec{
Ref: &v1alpha1.TaskRef{
APIVersion: apiVersion,
Kind: kind,
Name: "some-name",
},
},
},
want: true,
}} {
t.Run(c.desc, func(t *testing.T) {
got := controller.FilterRunRef(apiVersion, kind)(c.in)
if got != c.want {
t.Fatalf("FilterRunRef(%q, %q) got %t, want %t", apiVersion, kind, got, c.want)
}
})
}
}

0 comments on commit fe77b31

Please sign in to comment.