Skip to content

Commit 177ce3c

Browse files
committed
Add predicates for handling objects
1 parent 4850c2d commit 177ce3c

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

pkg/controller/controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ func (gc *GenericController) WatchAndMapToController(obj metav1.Object, gvks ...
9999
return gc.queue.watchForAndMapToController(obj, gvks...)
100100
}
101101

102+
func (gc *GenericController) WatchAndMapToControllerIf(obj metav1.Object,
103+
p handlefunctions.Predicate, gvks ...metav1.GroupVersionKind) error {
104+
gc.once.Do(gc.init)
105+
return gc.queue.watchForAndMapToControllerIf(obj, p, gvks...)
106+
}
107+
102108
// WatchAndMap watches objects matching obj's type and enqueues the key returned by mapFn.
103109
func (gc *GenericController) WatchAndMap(obj metav1.Object, mapFn handlefunctions.ObjToKey) error {
104110
gc.once.Do(gc.init)

pkg/controller/handlefunctions/handlerfuncs.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ import (
2626
"k8s.io/client-go/util/workqueue"
2727
)
2828

29+
var (
30+
ResourceVersionChanged = ResourceVersionChangedPredicate{}
31+
)
32+
2933
// MappingEnqueuingFnProvider provides Fns to map objects to name/namespace keys and enqueue them as messages
3034
type MappingEnqueuingFnProvider struct {
35+
Predicate Predicate
3136
// Map maps an object to a key that can be enqueued
3237
Map func(interface{}) string
3338
}
@@ -109,3 +114,64 @@ func MapToSelf(obj interface{}) string {
109114

110115
// ObjToKey returns a string namespace/name key for an object
111116
type ObjToKey func(interface{}) string
117+
118+
type Predicate interface {
119+
HandleUpdate(old, new interface{}) bool
120+
HandleDelete(obj interface{}) bool
121+
HandleCreate(obj interface{}) bool
122+
}
123+
124+
type TrueMixin struct{}
125+
126+
func (TrueMixin) HandleUpdate(old, new interface{}) bool {
127+
return true
128+
}
129+
130+
func (TrueMixin) HandleDelete(obj interface{}) bool {
131+
return true
132+
}
133+
134+
func (TrueMixin) HandleCreate(obj interface{}) bool {
135+
return true
136+
}
137+
138+
type FalseMixin struct{}
139+
140+
func (FalseMixin) HandleUpdate(old, new interface{}) bool {
141+
return false
142+
}
143+
144+
func (FalseMixin) HandleDelete(obj interface{}) bool {
145+
return false
146+
}
147+
148+
func (FalseMixin) HandleCreate(obj interface{}) bool {
149+
return false
150+
}
151+
152+
type ConditionalMappingHandler struct {
153+
MappingEnqueuingFnProvider
154+
}
155+
156+
type ResourceVersionChangedPredicate struct {
157+
TrueMixin
158+
}
159+
160+
func (ResourceVersionChangedPredicate) HandleUpdate(old, new interface{}) bool {
161+
oldObject, ok := old.(metav1.Object)
162+
if !ok {
163+
fmt.Errorf("Cannot handle %T because old is not an Object: %v\n", oldObject, oldObject)
164+
return false
165+
}
166+
newObject, ok := new.(metav1.Object)
167+
if !ok {
168+
fmt.Errorf("Cannot handle %T because new is not an Object: %v\n", newObject, newObject)
169+
return false
170+
}
171+
172+
if oldObject.GetResourceVersion() == newObject.GetResourceVersion() {
173+
// Periodic resync will send update events for all resources Deployments.
174+
return false
175+
}
176+
return true
177+
}

pkg/controller/listeningqueue.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,30 @@ type listeningQueue struct {
4343

4444
// watchFor watches objects matching obj's type and enqueues their keys.
4545
func (q *listeningQueue) watchFor(obj metav1.Object) error {
46-
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{handlefunctions.MapToSelf})
46+
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{Map: handlefunctions.MapToSelf})
4747
}
4848

4949
// watchForAndMapToController watches objects matching obj's type and enqueues the keys of their controllers.
5050
func (q *listeningQueue) watchForAndMapToController(obj metav1.Object, gvks ...metav1.GroupVersionKind) error {
5151
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{
52-
handlefunctions.MapToController{GVK: gvks}.Map,
52+
Map: handlefunctions.MapToController{GVK: gvks}.Map,
53+
})
54+
}
55+
56+
// watchForAndMapToControllerIf watches objects matching obj's type and enqueues the keys of their controllers.
57+
func (q *listeningQueue) watchForAndMapToControllerIf(obj metav1.Object, predicate handlefunctions.Predicate,
58+
gvks ...metav1.GroupVersionKind) error {
59+
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{
60+
Map: handlefunctions.MapToController{GVK: gvks}.Map,
61+
Predicate: predicate,
5362
})
5463
}
5564

5665
// WatchAndMap watches objects matching obj's type and maps them to keys that it then enqueues.
5766
func (q *listeningQueue) watchForAndMapToNewObjectKey(
5867
obj metav1.Object, mappingFn handlefunctions.ObjToKey) error {
5968

60-
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{mappingFn})
69+
return q.addEventHandler(obj, handlefunctions.MappingEnqueuingFnProvider{Map: mappingFn})
6170
}
6271

6372
// watchForAndHandleEvent watches objects matching obj's type and uses the functions from provider to handle events.

0 commit comments

Comments
 (0)