mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 02:09:56 +00:00
Add a filtering resource handler for informers
Allows an informer consumer to easily filter a set of changes out, possibly to maintain a smaller cache or to only operate on a known set of objects.
This commit is contained in:
parent
3e095d12b4
commit
5439cfd245
@ -207,6 +207,47 @@ func (r ResourceEventHandlerFuncs) OnDelete(obj interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FilteringResourceEventHandler applies the provided filter to all events coming
|
||||||
|
// in, ensuring the appropriate nested handler method is invoked. An object
|
||||||
|
// that starts passing the filter after an update is considered an add, and an
|
||||||
|
// object that stops passing the filter after an update is considered a delete.
|
||||||
|
type FilteringResourceEventHandler struct {
|
||||||
|
FilterFunc func(obj interface{}) bool
|
||||||
|
Handler ResourceEventHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnAdd calls the nested handler only if the filter succeeds
|
||||||
|
func (r FilteringResourceEventHandler) OnAdd(obj interface{}) {
|
||||||
|
if !r.FilterFunc(obj) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Handler.OnAdd(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnUpdate ensures the proper handler is called depending on whether the filter matches
|
||||||
|
func (r FilteringResourceEventHandler) OnUpdate(oldObj, newObj interface{}) {
|
||||||
|
newer := r.FilterFunc(newObj)
|
||||||
|
older := r.FilterFunc(oldObj)
|
||||||
|
switch {
|
||||||
|
case newer && older:
|
||||||
|
r.Handler.OnUpdate(oldObj, newObj)
|
||||||
|
case newer && !older:
|
||||||
|
r.Handler.OnAdd(newObj)
|
||||||
|
case !newer && older:
|
||||||
|
r.Handler.OnDelete(oldObj)
|
||||||
|
default:
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnDelete calls the nested handler only if the filter succeeds
|
||||||
|
func (r FilteringResourceEventHandler) OnDelete(obj interface{}) {
|
||||||
|
if !r.FilterFunc(obj) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Handler.OnDelete(obj)
|
||||||
|
}
|
||||||
|
|
||||||
// DeletionHandlingMetaNamespaceKeyFunc checks for
|
// DeletionHandlingMetaNamespaceKeyFunc checks for
|
||||||
// DeletedFinalStateUnknown objects before calling
|
// DeletedFinalStateUnknown objects before calling
|
||||||
// MetaNamespaceKeyFunc.
|
// MetaNamespaceKeyFunc.
|
||||||
|
Loading…
Reference in New Issue
Block a user