79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
/*
|
|
Copyright 2020 The Karmada Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package fedinformer
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/tools/cache"
|
|
)
|
|
|
|
// NewHandlerOnAllEvents builds a ResourceEventHandler that the function 'fn' will be called on all events(add/update/delete).
|
|
func NewHandlerOnAllEvents(fn func(interface{})) cache.ResourceEventHandler {
|
|
return &cache.ResourceEventHandlerFuncs{
|
|
AddFunc: func(cur interface{}) {
|
|
curObj := cur.(runtime.Object)
|
|
fn(curObj)
|
|
},
|
|
UpdateFunc: func(old, cur interface{}) {
|
|
curObj := cur.(runtime.Object)
|
|
if !reflect.DeepEqual(old, cur) {
|
|
fn(curObj)
|
|
}
|
|
},
|
|
DeleteFunc: func(old interface{}) {
|
|
if deleted, ok := old.(cache.DeletedFinalStateUnknown); ok {
|
|
// This object might be stale but ok for our current usage.
|
|
old = deleted.Obj
|
|
if old == nil {
|
|
return
|
|
}
|
|
}
|
|
oldObj := old.(runtime.Object)
|
|
fn(oldObj)
|
|
},
|
|
}
|
|
}
|
|
|
|
// NewHandlerOnEvents builds a ResourceEventHandler.
|
|
func NewHandlerOnEvents(addFunc func(obj interface{}), updateFunc func(oldObj, newObj interface{}), deleteFunc func(obj interface{})) cache.ResourceEventHandler {
|
|
return &cache.ResourceEventHandlerFuncs{
|
|
AddFunc: addFunc,
|
|
UpdateFunc: updateFunc,
|
|
DeleteFunc: deleteFunc,
|
|
}
|
|
}
|
|
|
|
// NewFilteringHandlerOnAllEvents builds a FilteringResourceEventHandler applies the provided filter to all events
|
|
// coming in, ensuring the appropriate nested handler method is invoked.
|
|
//
|
|
// Note: 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.
|
|
// Like the handlers, the filter MUST NOT modify the objects it is given.
|
|
func NewFilteringHandlerOnAllEvents(filterFunc func(obj interface{}) bool, addFunc func(obj interface{}),
|
|
updateFunc func(oldObj, newObj interface{}), deleteFunc func(obj interface{})) cache.ResourceEventHandler {
|
|
return &cache.FilteringResourceEventHandler{
|
|
FilterFunc: filterFunc,
|
|
Handler: cache.ResourceEventHandlerFuncs{
|
|
AddFunc: addFunc,
|
|
UpdateFunc: updateFunc,
|
|
DeleteFunc: deleteFunc,
|
|
},
|
|
}
|
|
}
|