Skip to content

Commit

Permalink
Change watching events to use interface instead of struct for more fl…
Browse files Browse the repository at this point in the history
…exibility
  • Loading branch information
pwittrock committed Apr 2, 2018
1 parent 8a85d7c commit 4d976f5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ func (gc *GenericController) WatchChannel(source <-chan string) error {

// fnToInterfaceAdapter adapts a function to an interface
type fnToInterfaceAdapter struct {
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandler
}

func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return f.val(q)
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"time"

"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/test"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
Expand All @@ -29,7 +31,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"time"
)

var _ = Describe("GenericController", func() {
Expand Down Expand Up @@ -236,7 +237,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling add function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { w.AddRateLimited("key/value") },
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
Expand All @@ -257,7 +258,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling update function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { Fail("Add function called") },
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
Expand Down Expand Up @@ -288,7 +289,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling delete function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { Fail("Add function called") },
DeleteFunc: func(obj interface{}) { w.AddRateLimited("key/value") },
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/eventhandlers/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// EventHandler accepts a workqueue and returns ResourceEventHandlerFuncs that enqueue messages to it
// for add / update / delete events
type EventHandler interface {
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler
}

// MapAndEnqueue provides Fns to map objects to name/namespace keys and enqueue them as messages
Expand All @@ -42,7 +42,7 @@ type MapAndEnqueue struct {
}

// Get returns ResourceEventHandlerFuncs that Map an object to a Key and enqueue the key if it is non-empty
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler {
// Enqueue the mapped key for updates to the object
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
Expand Down
36 changes: 18 additions & 18 deletions pkg/controller/eventhandlers/eventhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ var _ = Describe("Eventhandlers", func() {
Context("Where there are no Predicates", func() {
It("should set the Add function", func() {
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Eventually(q.Len).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-add"))
})

It("should set the Delete function", func() {
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-delete"))
})

It("should set the Update function", func() {
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-update"))
})
Expand All @@ -76,36 +76,36 @@ var _ = Describe("Eventhandlers", func() {
It("should set the Add function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-add"))

fns.DeleteFunc("delete")
fns.UpdateFunc("old", "update")
fns.OnDelete("delete")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})

It("should set the Delete function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{delete: true}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-delete"))

fns.AddFunc("add")
fns.UpdateFunc("old", "add")
fns.OnAdd("add")
fns.OnUpdate("old", "add")
Consistently(q.Len).Should(Equal(0))
})

It("should set the Update function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{update: true}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-update"))

fns.AddFunc("add")
fns.DeleteFunc("delete")
fns.OnAdd("add")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})
})
Expand All @@ -115,42 +115,42 @@ var _ = Describe("Eventhandlers", func() {
It("should not Add", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}, FakePredicates{}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Consistently(q.Len).Should(Equal(0))
})

It("should not Delete", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{delete: true}, FakePredicates{}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})

It("should not Update", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{update: true}, FakePredicates{}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})

It("should not Add", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{create: true}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Consistently(q.Len).Should(Equal(0))
})

It("should not Delete", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{delete: true}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})

It("should not Update", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{update: true}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/example_watchandhandleevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExampleGenericController_WatchEvents() {
}
err := c.WatchEvents(&corev1.Pod{},
// This function returns the callbacks that will be invoked for events
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
// This function implements the same functionality as GenericController.Watch
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type ReconcileFn func(ReconcileKey) error

// HandleFnProvider returns cache.ResourceEventHandlerFuncs that may enqueue messages
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandler

// ReconcileKey provides a lookup key for a Kubernetes object.
type ReconcileKey struct {
Expand Down

0 comments on commit 4d976f5

Please sign in to comment.