Use parent func to indicate when the controller queue is a singleton

This prevents unnecessary sync loop runs when the controller is
running with a single worker.  When the controller is running with
more than one worker, it prevents subtle bugs that can cause the
controller to go "back in time."

Signed-off-by: Monis Khan <mok@vmware.com>
Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Monis Khan
2020-11-04 11:08:10 -06:00
committed by Matt Moyer
parent e7a817e67a
commit 418f4d20ae
11 changed files with 34 additions and 19 deletions
+19 -4
View File
@@ -12,23 +12,38 @@ import (
func NameAndNamespaceExactMatchFilterFactory(name, namespace string) controllerlib.Filter {
return SimpleFilter(func(obj metav1.Object) bool {
return obj.GetName() == name && obj.GetNamespace() == namespace
})
// nil parent func is fine because we only match a key with the given name and namespace
// i.e. it is equivalent to having a SingletonQueue() parent func
}, nil)
}
// MatchAnythingFilter returns a controllerlib.Filter that allows all objects.
func MatchAnythingFilter() controllerlib.Filter {
return SimpleFilter(func(object metav1.Object) bool { return true })
func MatchAnythingFilter(parentFunc controllerlib.ParentFunc) controllerlib.Filter {
return SimpleFilter(func(object metav1.Object) bool { return true }, parentFunc)
}
// SimpleFilter takes a single boolean match function on a metav1.Object and wraps it into a proper controllerlib.Filter.
func SimpleFilter(match func(metav1.Object) bool) controllerlib.Filter {
func SimpleFilter(match func(metav1.Object) bool, parentFunc controllerlib.ParentFunc) controllerlib.Filter {
return controllerlib.FilterFuncs{
AddFunc: match,
UpdateFunc: func(oldObj, newObj metav1.Object) bool { return match(oldObj) || match(newObj) },
DeleteFunc: match,
ParentFunc: parentFunc,
}
}
// SingletonQueue returns a parent func that treats all events as the same key.
func SingletonQueue() controllerlib.ParentFunc {
return func(_ metav1.Object) controllerlib.Key {
return controllerlib.Key{}
}
}
// SimpleFilterWithSingletonQueue returns a Filter based on the given match function that treats all events as the same key.
func SimpleFilterWithSingletonQueue(match func(metav1.Object) bool) controllerlib.Filter {
return SimpleFilter(match, SingletonQueue())
}
// Same signature as controllerlib.WithInformer().
type WithInformerOptionFunc func(
getter controllerlib.InformerGetter,