Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#43 from pwittrock/master
Browse files Browse the repository at this point in the history
Change watching events to use interface instead of struct
  • Loading branch information
Phillip Wittrock authored Apr 4, 2018
2 parents f634b54 + 98f35f3 commit ed1c5d9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,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
6 changes: 3 additions & 3 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,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 @@ -312,7 +312,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 @@ -343,7 +343,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
8 changes: 4 additions & 4 deletions pkg/controller/eventhandlers/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"k8s.io/client-go/util/workqueue"
)

// EventHandler accepts a workqueue and returns ResourceEventHandlerFuncs that enqueue messages to it
// EventHandler accepts a workqueue and returns ResourceEventHandler 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 @@ -43,8 +43,8 @@ type MapAndEnqueue struct {
MultiMap func(interface{}) []types.ReconcileKey
}

// 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 {
// Get returns ResourceEventHandler that Map an object to a Key and enqueue the key if it is non-empty
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
4 changes: 2 additions & 2 deletions pkg/controller/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
// ReconcileFn takes the key of an object and reconciles its desired and observed state.
type ReconcileFn func(ReconcileKey) error

// HandleFnProvider returns cache.ResourceEventHandlerFuncs that may enqueue messages
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
// HandleFnProvider returns cache.ResourceEventHandler that may enqueue messages
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 ed1c5d9

Please sign in to comment.