Skip to content

Commit 6699bce

Browse files
committed
cache: Allow fine-granular configuration of SyncPeriod
Signed-off-by: Stefan Büringer [email protected]
1 parent 100354a commit 6699bce

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

pkg/cache/cache.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ type ByObject struct {
307307
//
308308
// Defaults to true.
309309
EnableWatchBookmarks *bool
310+
311+
// SyncPeriod determines the minimum frequency at which watched resources are
312+
// reconciled.
313+
SyncPeriod *time.Duration
310314
}
311315

312316
// Config describes all potential options for a given watch.
@@ -342,6 +346,10 @@ type Config struct {
342346
//
343347
// Defaults to true.
344348
EnableWatchBookmarks *bool
349+
350+
// SyncPeriod determines the minimum frequency at which watched resources are
351+
// reconciled.
352+
SyncPeriod *time.Duration
345353
}
346354

347355
// NewCacheFunc - Function for creating a new cache from the options and a rest config.
@@ -412,6 +420,7 @@ func optionDefaultsToConfig(opts *Options) Config {
412420
Transform: opts.DefaultTransform,
413421
UnsafeDisableDeepCopy: opts.DefaultUnsafeDisableDeepCopy,
414422
EnableWatchBookmarks: opts.DefaultEnableWatchBookmarks,
423+
SyncPeriod: opts.SyncPeriod,
415424
}
416425
}
417426

@@ -422,6 +431,7 @@ func byObjectToConfig(byObject ByObject) Config {
422431
Transform: byObject.Transform,
423432
UnsafeDisableDeepCopy: byObject.UnsafeDisableDeepCopy,
424433
EnableWatchBookmarks: byObject.EnableWatchBookmarks,
434+
SyncPeriod: byObject.SyncPeriod,
425435
}
426436
}
427437

@@ -435,7 +445,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
435445
HTTPClient: opts.HTTPClient,
436446
Scheme: opts.Scheme,
437447
Mapper: opts.Mapper,
438-
ResyncPeriod: *opts.SyncPeriod,
448+
ResyncPeriod: ptr.Deref(config.SyncPeriod, defaultSyncPeriod),
439449
Namespace: namespace,
440450
Selector: internal.Selector{
441451
Label: config.LabelSelector,
@@ -533,6 +543,7 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
533543
byObject.Transform = defaultedConfig.Transform
534544
byObject.UnsafeDisableDeepCopy = defaultedConfig.UnsafeDisableDeepCopy
535545
byObject.EnableWatchBookmarks = defaultedConfig.EnableWatchBookmarks
546+
byObject.SyncPeriod = defaultedConfig.SyncPeriod
536547
}
537548

538549
opts.ByObject[obj] = byObject
@@ -554,10 +565,6 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
554565
opts.DefaultNamespaces[namespace] = cfg
555566
}
556567

557-
// Default the resync period to 10 hours if unset
558-
if opts.SyncPeriod == nil {
559-
opts.SyncPeriod = &defaultSyncPeriod
560-
}
561568
return opts, nil
562569
}
563570

@@ -577,6 +584,9 @@ func defaultConfig(toDefault, defaultFrom Config) Config {
577584
if toDefault.EnableWatchBookmarks == nil {
578585
toDefault.EnableWatchBookmarks = defaultFrom.EnableWatchBookmarks
579586
}
587+
if toDefault.SyncPeriod == nil {
588+
toDefault.SyncPeriod = defaultFrom.SyncPeriod
589+
}
580590
return toDefault
581591
}
582592

pkg/cache/defaulting_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ func TestDefaultOpts(t *testing.T) {
249249
return cmp.Diff(expected, o.ByObject[pod].EnableWatchBookmarks)
250250
},
251251
},
252+
{
253+
name: "ByObject.SyncPeriod gets defaulted from SyncPeriod",
254+
in: Options{
255+
ByObject: map[client.Object]ByObject{pod: {}},
256+
SyncPeriod: ptr.To(5 * time.Minute),
257+
},
258+
259+
verification: func(o Options) string {
260+
expected := ptr.To(5 * time.Minute)
261+
return cmp.Diff(expected, o.ByObject[pod].SyncPeriod)
262+
},
263+
},
264+
{
265+
name: "ByObject.SyncPeriod doesn't get defaulted when set",
266+
in: Options{
267+
ByObject: map[client.Object]ByObject{pod: {SyncPeriod: ptr.To(1 * time.Minute)}},
268+
SyncPeriod: ptr.To(5 * time.Minute),
269+
},
270+
271+
verification: func(o Options) string {
272+
expected := ptr.To(1 * time.Minute)
273+
return cmp.Diff(expected, o.ByObject[pod].SyncPeriod)
274+
},
275+
},
252276
{
253277
name: "DefaultNamespace label selector gets defaulted from DefaultLabelSelector",
254278
in: Options{

0 commit comments

Comments
 (0)