2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-09 17:04:05 +00:00
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2020-12-18 22:55:05 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2020-08-09 17:04:05 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 19:55:59 +00:00
|
|
|
func NameAndNamespaceExactMatchFilterFactory(name, namespace string) controllerlib.Filter {
|
|
|
|
return SimpleFilter(func(obj metav1.Object) bool {
|
2020-08-09 17:04:05 +00:00
|
|
|
return obj.GetName() == name && obj.GetNamespace() == namespace
|
2020-10-02 17:22:18 +00:00
|
|
|
// 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)
|
2020-09-09 19:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 00:51:40 +00:00
|
|
|
// MatchAnythingFilter returns a controllerlib.Filter that allows all objects.
|
2020-10-02 17:22:18 +00:00
|
|
|
func MatchAnythingFilter(parentFunc controllerlib.ParentFunc) controllerlib.Filter {
|
|
|
|
return SimpleFilter(func(object metav1.Object) bool { return true }, parentFunc)
|
2020-09-09 19:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleFilter takes a single boolean match function on a metav1.Object and wraps it into a proper controllerlib.Filter.
|
2020-10-02 17:22:18 +00:00
|
|
|
func SimpleFilter(match func(metav1.Object) bool, parentFunc controllerlib.ParentFunc) controllerlib.Filter {
|
2020-08-28 15:59:09 +00:00
|
|
|
return controllerlib.FilterFuncs{
|
2020-09-09 19:55:59 +00:00
|
|
|
AddFunc: match,
|
|
|
|
UpdateFunc: func(oldObj, newObj metav1.Object) bool { return match(oldObj) || match(newObj) },
|
|
|
|
DeleteFunc: match,
|
2020-10-02 17:22:18 +00:00
|
|
|
ParentFunc: parentFunc,
|
2020-08-09 17:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:41:07 +00:00
|
|
|
func MatchAnySecretOfTypeFilter(secretType v1.SecretType, parentFunc controllerlib.ParentFunc) controllerlib.Filter {
|
2020-12-18 22:55:05 +00:00
|
|
|
isSecretOfType := func(obj metav1.Object) bool {
|
|
|
|
secret, ok := obj.(*v1.Secret)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return secret.Type == secretType
|
|
|
|
}
|
2020-12-18 23:41:07 +00:00
|
|
|
return SimpleFilter(isSecretOfType, parentFunc)
|
2020-12-18 22:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SecretIsControlledByParentFunc(matchFunc func(obj metav1.Object) bool) func(obj metav1.Object) controllerlib.Key {
|
|
|
|
return func(obj metav1.Object) controllerlib.Key {
|
|
|
|
if matchFunc(obj) {
|
|
|
|
controller := metav1.GetControllerOf(obj)
|
|
|
|
return controllerlib.Key{
|
|
|
|
Name: controller.Name,
|
|
|
|
Namespace: obj.GetNamespace(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return controllerlib.Key{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:22:18 +00:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:59:09 +00:00
|
|
|
// Same signature as controllerlib.WithInformer().
|
2020-08-09 17:04:05 +00:00
|
|
|
type WithInformerOptionFunc func(
|
2020-08-28 15:59:09 +00:00
|
|
|
getter controllerlib.InformerGetter,
|
|
|
|
filter controllerlib.Filter,
|
|
|
|
opt controllerlib.InformerOption) controllerlib.Option
|
2020-08-11 17:14:57 +00:00
|
|
|
|
2020-08-28 15:59:09 +00:00
|
|
|
// Same signature as controllerlib.WithInitialEvent().
|
|
|
|
type WithInitialEventOptionFunc func(key controllerlib.Key) controllerlib.Option
|