* Convert to runtime.Object to smaller structs This adds conversion for all the objects we want to keep in the cache. It keeps the minimum for CoreDNS to function and throws away the rest. The conversion: api.Endpoints -> object.Endpoints api.Pod -> object.Pod api.Serivce -> object.Service We needed to copy some client-go stuff to insert a conversion function into NewIndexInformers. Some unrelated cleanups in the watch functionality as that needed to be touched because of the above translation of objects. Signed-off-by: Miek Gieben <miek@miek.nl> * Reduce test line-count Signed-off-by: Miek Gieben <miek@miek.nl> * ....and fix test Signed-off-by: Miek Gieben <miek@miek.nl> * Drop use of append Signed-off-by: Miek Gieben <miek@miek.nl> * cosmetic changes Signed-off-by: Miek Gieben <miek@miek.nl> * that was a typo Signed-off-by: Miek Gieben <miek@miek.nl> * re-introduce append here We can't really use len() here because we don't know the number before hand. Signed-off-by: Miek Gieben <miek@miek.nl> * comment in better place Signed-off-by: Miek Gieben <miek@miek.nl> * Make the timestamp a bool; thats where it is used for Signed-off-by: Miek Gieben <miek@miek.nl> * Set incoming object to nil Explicataliy discard the converted object; we did a deep copy it's not needed anymore. Signed-off-by: Miek Gieben <miek@miek.nl> * Per Chris's comment Signed-off-by: Miek Gieben <miek@miek.nl>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package object
|
|
|
|
import (
|
|
"time"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/tools/cache"
|
|
)
|
|
|
|
// NewIndexerInformer is a copy of the cache.NewIndexInformer function, but allows Process to have a conversion function (ToFunc).
|
|
func NewIndexerInformer(lw cache.ListerWatcher, objType runtime.Object, resyncPeriod time.Duration, h cache.ResourceEventHandler, indexers cache.Indexers, convert ToFunc) (cache.Indexer, cache.Controller) {
|
|
clientState := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, indexers)
|
|
|
|
fifo := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, clientState)
|
|
|
|
cfg := &cache.Config{
|
|
Queue: fifo,
|
|
ListerWatcher: lw,
|
|
ObjectType: objType,
|
|
FullResyncPeriod: resyncPeriod,
|
|
RetryOnError: false,
|
|
Process: func(obj interface{}) error {
|
|
for _, d := range obj.(cache.Deltas) {
|
|
|
|
obj := convert(d.Object)
|
|
|
|
switch d.Type {
|
|
case cache.Sync, cache.Added, cache.Updated:
|
|
if old, exists, err := clientState.Get(obj); err == nil && exists {
|
|
if err := clientState.Update(obj); err != nil {
|
|
return err
|
|
}
|
|
h.OnUpdate(old, obj)
|
|
} else {
|
|
if err := clientState.Add(obj); err != nil {
|
|
return err
|
|
}
|
|
h.OnAdd(obj)
|
|
}
|
|
case cache.Deleted:
|
|
if err := clientState.Delete(obj); err != nil {
|
|
return err
|
|
}
|
|
h.OnDelete(obj)
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
return clientState, cache.New(cfg)
|
|
}
|