Skip to content

Commit ed1c5d9

Browse files
author
Phillip Wittrock
authored
Merge pull request #43 from pwittrock/master
Change watching events to use interface instead of struct
2 parents f634b54 + 98f35f3 commit ed1c5d9

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

pkg/controller/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ func (gc *GenericController) WatchChannel(source <-chan string) error {
176176

177177
// fnToInterfaceAdapter adapts a function to an interface
178178
type fnToInterfaceAdapter struct {
179-
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
179+
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandler
180180
}
181181

182-
func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
182+
func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
183183
return f.val(q)
184184
}
185185

pkg/controller/controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ var _ = Describe("GenericController", func() {
291291
It("should call the event handling add function", func() {
292292
// Listen for Pod changes
293293
Expect(instance.WatchEvents(&corev1.Pod{},
294-
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
294+
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
295295
return cache.ResourceEventHandlerFuncs{
296296
AddFunc: func(obj interface{}) { w.AddRateLimited("key/value") },
297297
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
@@ -312,7 +312,7 @@ var _ = Describe("GenericController", func() {
312312
It("should call the event handling update function", func() {
313313
// Listen for Pod changes
314314
Expect(instance.WatchEvents(&corev1.Pod{},
315-
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
315+
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
316316
return cache.ResourceEventHandlerFuncs{
317317
AddFunc: func(obj interface{}) { Fail("Add function called") },
318318
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
@@ -343,7 +343,7 @@ var _ = Describe("GenericController", func() {
343343
It("should call the event handling delete function", func() {
344344
// Listen for Pod changes
345345
Expect(instance.WatchEvents(&corev1.Pod{},
346-
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
346+
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
347347
return cache.ResourceEventHandlerFuncs{
348348
AddFunc: func(obj interface{}) { Fail("Add function called") },
349349
DeleteFunc: func(obj interface{}) { w.AddRateLimited("key/value") },

pkg/controller/eventhandlers/eventhandlers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import (
2828
"k8s.io/client-go/util/workqueue"
2929
)
3030

31-
// EventHandler accepts a workqueue and returns ResourceEventHandlerFuncs that enqueue messages to it
31+
// EventHandler accepts a workqueue and returns ResourceEventHandler that enqueue messages to it
3232
// for add / update / delete events
3333
type EventHandler interface {
34-
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
34+
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler
3535
}
3636

3737
// MapAndEnqueue provides Fns to map objects to name/namespace keys and enqueue them as messages
@@ -43,8 +43,8 @@ type MapAndEnqueue struct {
4343
MultiMap func(interface{}) []types.ReconcileKey
4444
}
4545

46-
// Get returns ResourceEventHandlerFuncs that Map an object to a Key and enqueue the key if it is non-empty
47-
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
46+
// Get returns ResourceEventHandler that Map an object to a Key and enqueue the key if it is non-empty
47+
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler {
4848
// Enqueue the mapped key for updates to the object
4949
return cache.ResourceEventHandlerFuncs{
5050
AddFunc: func(obj interface{}) {

pkg/controller/eventhandlers/eventhandlers_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ var _ = Describe("Eventhandlers", func() {
5252
Context("Where there are no Predicates", func() {
5353
It("should set the Add function", func() {
5454
fns := mae.Get(q)
55-
fns.AddFunc("add")
55+
fns.OnAdd("add")
5656
Eventually(q.Len).Should(Equal(1))
5757
Expect(q.Get()).Should(Equal("p-add"))
5858
})
5959

6060
It("should set the Delete function", func() {
6161
fns := mae.Get(q)
62-
fns.DeleteFunc("delete")
62+
fns.OnDelete("delete")
6363
Eventually(q.Len()).Should(Equal(1))
6464
Expect(q.Get()).Should(Equal("p-delete"))
6565
})
6666

6767
It("should set the Update function", func() {
6868
fns := mae.Get(q)
69-
fns.UpdateFunc("old", "update")
69+
fns.OnUpdate("old", "update")
7070
Eventually(q.Len()).Should(Equal(1))
7171
Expect(q.Get()).Should(Equal("p-update"))
7272
})
@@ -76,36 +76,36 @@ var _ = Describe("Eventhandlers", func() {
7676
It("should set the Add function", func() {
7777
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}}
7878
fns := mae.Get(q)
79-
fns.AddFunc("add")
79+
fns.OnAdd("add")
8080
Eventually(q.Len()).Should(Equal(1))
8181
Expect(q.Get()).Should(Equal("p-add"))
8282

83-
fns.DeleteFunc("delete")
84-
fns.UpdateFunc("old", "update")
83+
fns.OnDelete("delete")
84+
fns.OnUpdate("old", "update")
8585
Consistently(q.Len).Should(Equal(0))
8686
})
8787

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

95-
fns.AddFunc("add")
96-
fns.UpdateFunc("old", "add")
95+
fns.OnAdd("add")
96+
fns.OnUpdate("old", "add")
9797
Consistently(q.Len).Should(Equal(0))
9898
})
9999

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

107-
fns.AddFunc("add")
108-
fns.DeleteFunc("delete")
107+
fns.OnAdd("add")
108+
fns.OnDelete("delete")
109109
Consistently(q.Len).Should(Equal(0))
110110
})
111111
})
@@ -115,42 +115,42 @@ var _ = Describe("Eventhandlers", func() {
115115
It("should not Add", func() {
116116
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}, FakePredicates{}}
117117
fns := mae.Get(q)
118-
fns.AddFunc("add")
118+
fns.OnAdd("add")
119119
Consistently(q.Len).Should(Equal(0))
120120
})
121121

122122
It("should not Delete", func() {
123123
mae.Predicates = []predicates.Predicate{FakePredicates{delete: true}, FakePredicates{}}
124124
fns := mae.Get(q)
125-
fns.DeleteFunc("delete")
125+
fns.OnDelete("delete")
126126
Consistently(q.Len).Should(Equal(0))
127127
})
128128

129129
It("should not Update", func() {
130130
mae.Predicates = []predicates.Predicate{FakePredicates{update: true}, FakePredicates{}}
131131
fns := mae.Get(q)
132-
fns.UpdateFunc("old", "update")
132+
fns.OnUpdate("old", "update")
133133
Consistently(q.Len).Should(Equal(0))
134134
})
135135

136136
It("should not Add", func() {
137137
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{create: true}}
138138
fns := mae.Get(q)
139-
fns.AddFunc("add")
139+
fns.OnAdd("add")
140140
Consistently(q.Len).Should(Equal(0))
141141
})
142142

143143
It("should not Delete", func() {
144144
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{delete: true}}
145145
fns := mae.Get(q)
146-
fns.DeleteFunc("delete")
146+
fns.OnDelete("delete")
147147
Consistently(q.Len).Should(Equal(0))
148148
})
149149

150150
It("should not Update", func() {
151151
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{update: true}}
152152
fns := mae.Get(q)
153-
fns.UpdateFunc("old", "update")
153+
fns.OnUpdate("old", "update")
154154
Consistently(q.Len).Should(Equal(0))
155155
})
156156
})

pkg/controller/example_watchandhandleevents_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ExampleGenericController_WatchEvents() {
4949
}
5050
err := c.WatchEvents(&corev1.Pod{},
5151
// This function returns the callbacks that will be invoked for events
52-
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
52+
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
5353
// This function implements the same functionality as GenericController.Watch
5454
return cache.ResourceEventHandlerFuncs{
5555
AddFunc: func(obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },

pkg/controller/types/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
// ReconcileFn takes the key of an object and reconciles its desired and observed state.
2525
type ReconcileFn func(ReconcileKey) error
2626

27-
// HandleFnProvider returns cache.ResourceEventHandlerFuncs that may enqueue messages
28-
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
27+
// HandleFnProvider returns cache.ResourceEventHandler that may enqueue messages
28+
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandler
2929

3030
// ReconcileKey provides a lookup key for a Kubernetes object.
3131
type ReconcileKey struct {

0 commit comments

Comments
 (0)