Skip to content

Commit 6bb1e7f

Browse files
committed
Ensure all WatchFunc enable watch and boomarks
AllowWatchBookmarks is generally pretty safe to enable as it has been available in Kuberentes for a long while, and the server ignores the flag if it doesn't implement it (per docs). Signed-off-by: Vince Prignano <[email protected]>
1 parent e3347b5 commit 6bb1e7f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pkg/cache/cache.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ type Options struct {
223223
// DefaultNamespaces.
224224
DefaultUnsafeDisableDeepCopy *bool
225225

226+
// DefaultEnableWatchBookmarks requests watch events with type "BOOKMARK".
227+
// Servers that do not implement bookmarks may ignore this flag and
228+
// bookmarks are sent at the server's discretion. Clients should not
229+
// assume bookmarks are returned at any specific interval, nor may they
230+
// assume the server will send any BOOKMARK event during a session.
231+
//
232+
// This will be used for all object types, unless it is set in ByObject or
233+
// DefaultNamespaces.
234+
DefaultEnableWatchBookmarks *bool
235+
226236
// ByObject restricts the cache's ListWatch to the desired fields per GVK at the specified object.
227237
// If unset, this will fall through to the Default* settings.
228238
ByObject map[client.Object]ByObject
@@ -273,6 +283,13 @@ type ByObject struct {
273283
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
274284
// otherwise you will mutate the object in the cache.
275285
UnsafeDisableDeepCopy *bool
286+
287+
// EnableWatchBookmarks requests watch events with type "BOOKMARK".
288+
// Servers that do not implement bookmarks may ignore this flag and
289+
// bookmarks are sent at the server's discretion. Clients should not
290+
// assume bookmarks are returned at any specific interval, nor may they
291+
// assume the server will send any BOOKMARK event during a session.
292+
EnableWatchBookmarks *bool
276293
}
277294

278295
// Config describes all potential options for a given watch.
@@ -299,6 +316,13 @@ type Config struct {
299316
// UnsafeDisableDeepCopy specifies if List and Get requests against the
300317
// cache should not DeepCopy. A nil value allows to default this.
301318
UnsafeDisableDeepCopy *bool
319+
320+
// EnableWatchBookmarks requests watch events with type "BOOKMARK".
321+
// Servers that do not implement bookmarks may ignore this flag and
322+
// bookmarks are sent at the server's discretion. Clients should not
323+
// assume bookmarks are returned at any specific interval, nor may they
324+
// assume the server will send any BOOKMARK event during a session.
325+
EnableWatchBookmarks *bool
302326
}
303327

304328
// NewCacheFunc - Function for creating a new cache from the options and a rest config.
@@ -368,6 +392,7 @@ func optionDefaultsToConfig(opts *Options) Config {
368392
FieldSelector: opts.DefaultFieldSelector,
369393
Transform: opts.DefaultTransform,
370394
UnsafeDisableDeepCopy: opts.DefaultUnsafeDisableDeepCopy,
395+
EnableWatchBookmarks: opts.DefaultEnableWatchBookmarks,
371396
}
372397
}
373398

@@ -377,6 +402,7 @@ func byObjectToConfig(byObject ByObject) Config {
377402
FieldSelector: byObject.Field,
378403
Transform: byObject.Transform,
379404
UnsafeDisableDeepCopy: byObject.UnsafeDisableDeepCopy,
405+
EnableWatchBookmarks: byObject.EnableWatchBookmarks,
380406
}
381407
}
382408

@@ -399,6 +425,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
399425
Transform: config.Transform,
400426
WatchErrorHandler: opts.DefaultWatchErrorHandler,
401427
UnsafeDisableDeepCopy: ptr.Deref(config.UnsafeDisableDeepCopy, false),
428+
EnableWatchBookmarks: ptr.Deref(config.EnableWatchBookmarks, true),
402429
NewInformer: opts.newInformer,
403430
}),
404431
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
@@ -508,6 +535,11 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
508535
if opts.SyncPeriod == nil {
509536
opts.SyncPeriod = &defaultSyncPeriod
510537
}
538+
539+
// Default enable watch bookmarks, unless set.
540+
if opts.DefaultEnableWatchBookmarks == nil {
541+
opts.DefaultEnableWatchBookmarks = ptr.To(true)
542+
}
511543
return opts, nil
512544
}
513545

pkg/cache/internal/informers.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type InformersOpts struct {
5151
Selector Selector
5252
Transform cache.TransformFunc
5353
UnsafeDisableDeepCopy bool
54+
EnableWatchBookmarks bool
5455
WatchErrorHandler cache.WatchErrorHandler
5556
}
5657

@@ -78,6 +79,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7879
selector: options.Selector,
7980
transform: options.Transform,
8081
unsafeDisableDeepCopy: options.UnsafeDisableDeepCopy,
82+
enableWatchBookmarks: options.EnableWatchBookmarks,
8183
newInformer: newInformer,
8284
watchErrorHandler: options.WatchErrorHandler,
8385
}
@@ -174,6 +176,7 @@ type Informers struct {
174176
selector Selector
175177
transform cache.TransformFunc
176178
unsafeDisableDeepCopy bool
179+
enableWatchBookmarks bool
177180

178181
// NewInformer allows overriding of the shared index informer constructor for testing.
179182
newInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
@@ -361,8 +364,10 @@ func (ip *Informers) addInformerToMap(gvk schema.GroupVersionKind, obj runtime.O
361364
return listWatcher.ListFunc(opts)
362365
},
363366
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
364-
ip.selector.ApplyToList(&opts)
365367
opts.Watch = true // Watch needs to be set to true separately
368+
opts.AllowWatchBookmarks = ip.enableWatchBookmarks
369+
370+
ip.selector.ApplyToList(&opts)
366371
return listWatcher.WatchFunc(opts)
367372
},
368373
}, obj, calculateResyncPeriod(ip.resync), cache.Indexers{
@@ -444,6 +449,9 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
444449
},
445450
// Setup the watch function
446451
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
452+
opts.Watch = true // Watch needs to be set to true separately
453+
opts.AllowWatchBookmarks = ip.enableWatchBookmarks
454+
447455
if namespace != "" {
448456
return resources.Namespace(namespace).Watch(ip.ctx, opts)
449457
}
@@ -486,6 +494,9 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
486494
},
487495
// Setup the watch function
488496
WatchFunc: func(opts metav1.ListOptions) (watcher watch.Interface, err error) {
497+
opts.Watch = true // Watch needs to be set to true separately
498+
opts.AllowWatchBookmarks = ip.enableWatchBookmarks
499+
489500
if namespace != "" {
490501
watcher, err = resources.Namespace(namespace).Watch(ip.ctx, opts)
491502
} else {
@@ -527,6 +538,9 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
527538
},
528539
// Setup the watch function
529540
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
541+
opts.Watch = true // Watch needs to be set to true separately
542+
opts.AllowWatchBookmarks = ip.enableWatchBookmarks
543+
530544
// Build the request.
531545
req := client.Get().Resource(mapping.Resource.Resource).VersionedParams(&opts, ip.paramCodec)
532546
if namespace != "" {

0 commit comments

Comments
 (0)