I am writing a controller to capture the events and do some stateless processing. When I am using ListWatch
in cache.NewSharedIndexInformer
, how can I say it not to list all the objects but start from current state (basically I need just Watch and not List)?
informer := cache.NewSharedIndexInformer(
// This lists entire cache every time we restart the controller
&cache.ListWatch{
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
return kubeClient.CoreV1().Events(core_v1.NamespaceAll).List(options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
return kubeClient.CoreV1().Events(core_v1.NamespaceAll).Watch(options)
},
},
&core_v1.Event{},
0, // Skip resync
cache.Indexers{},
)
Currently whenever my controller is restarted I get a slew of old events due to the ListFunc
(it lists from the beginning). Since the processing is stateless controller do not know whether it has seen the events earlier. Since ListFunc
is sending everything we are seeing a lot of duplicates.
One dirty workaround is to replace core_v1.NamespaceAll
in ListFunc
with some namespace name that do not exist (eg, “NOT_EXISTS”).
What is the cleaner way to solve this?