Skip to content

Commit bb09db8

Browse files
authored
✨ SetErrorWatchHandler on SharedIndexInformer (kubernetes-sigs#2494)
* add SetErrorWatchHandler to informers Signed-off-by: Troy Connor <[email protected]> * pass watchErrorHandler through Options struct Signed-off-by: Troy Connor <[email protected]> --------- Signed-off-by: Troy Connor <[email protected]>
1 parent dcf55bd commit bb09db8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/cache/cache.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ type Options struct {
188188
// unless there is already one set in ByObject or DefaultNamespaces.
189189
DefaultTransform toolscache.TransformFunc
190190

191+
// DefaultWatchErrorHandler will be used to the WatchErrorHandler which is called
192+
// whenever ListAndWatch drops the connection with an error.
193+
//
194+
// After calling this handler, the informer will backoff and retry.
195+
DefaultWatchErrorHandler toolscache.WatchErrorHandler
196+
191197
// DefaultUnsafeDisableDeepCopy is the default for UnsafeDisableDeepCopy
192198
// for everything that doesn't specify this.
193199
//
@@ -353,6 +359,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
353359
Field: config.FieldSelector,
354360
},
355361
Transform: config.Transform,
362+
WatchErrorHandler: opts.DefaultWatchErrorHandler,
356363
UnsafeDisableDeepCopy: pointer.BoolDeref(config.UnsafeDisableDeepCopy, false),
357364
NewInformer: opts.newInformer,
358365
}),

pkg/cache/internal/informers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type InformersOpts struct {
4949
Selector Selector
5050
Transform cache.TransformFunc
5151
UnsafeDisableDeepCopy bool
52+
WatchErrorHandler cache.WatchErrorHandler
5253
}
5354

5455
// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -76,6 +77,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7677
transform: options.Transform,
7778
unsafeDisableDeepCopy: options.UnsafeDisableDeepCopy,
7879
newInformer: newInformer,
80+
watchErrorHandler: options.WatchErrorHandler,
7981
}
8082
}
8183

@@ -159,6 +161,11 @@ type Informers struct {
159161

160162
// NewInformer allows overriding of the shared index informer constructor for testing.
161163
newInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
164+
165+
// WatchErrorHandler allows the shared index informer's
166+
// watchErrorHandler to be set by overriding the options
167+
// or to use the default watchErrorHandler
168+
watchErrorHandler cache.WatchErrorHandler
162169
}
163170

164171
// Start calls Run on each of the informers and sets started to true. Blocks on the context.
@@ -323,6 +330,13 @@ func (ip *Informers) addInformerToMap(gvk schema.GroupVersionKind, obj runtime.O
323330
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
324331
})
325332

333+
// Set WatchErrorHandler on SharedIndexInformer if set
334+
if ip.watchErrorHandler != nil {
335+
if err := sharedIndexInformer.SetWatchErrorHandler(ip.watchErrorHandler); err != nil {
336+
return nil, false, err
337+
}
338+
}
339+
326340
// Check to see if there is a transformer for this gvk
327341
if err := sharedIndexInformer.SetTransform(ip.transform); err != nil {
328342
return nil, false, err

0 commit comments

Comments
 (0)