Skip to content

Commit 7062a1e

Browse files
committed
use ResyncPeriod func in case of multi controllers list simultaneously
1 parent 16c93b0 commit 7062a1e

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

pkg/cache/cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,23 @@ type Options struct {
9393
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
9494
Mapper meta.RESTMapper
9595

96-
// Resync is the resync period. Defaults to defaultResyncTime.
97-
Resync *time.Duration
96+
// The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.
97+
MinResyncPeriod *time.Duration
9898

9999
// Namespace restricts the cache's ListWatch to the desired namespace
100100
// Default watches all namespaces
101101
Namespace string
102102
}
103103

104-
var defaultResyncTime = 10 * time.Hour
104+
var defaultMinResyncTime = 5 * time.Hour
105105

106106
// New initializes and returns a new Cache.
107107
func New(config *rest.Config, opts Options) (Cache, error) {
108108
opts, err := defaultOpts(config, opts)
109109
if err != nil {
110110
return nil, err
111111
}
112-
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace)
112+
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.MinResyncPeriod, opts.Namespace)
113113
return &informerCache{InformersMap: im}, nil
114114
}
115115

@@ -130,8 +130,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
130130
}
131131

132132
// Default the resync period to 10 hours if unset
133-
if opts.Resync == nil {
134-
opts.Resync = &defaultResyncTime
133+
if opts.MinResyncPeriod == nil {
134+
opts.MinResyncPeriod = &defaultMinResyncTime
135135
}
136136
return opts, nil
137137
}

pkg/cache/internal/deleg_map.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package internal
1818

1919
import (
20+
"math/rand"
2021
"time"
2122

2223
"k8s.io/apimachinery/pkg/api/meta"
@@ -39,19 +40,27 @@ type InformersMap struct {
3940
Scheme *runtime.Scheme
4041
}
4142

43+
// resyncPeriod returns a function which generates a duration each time it is
44+
// invoked; this is so that multiple controllers don't get into lock-step and all
45+
// hammer the apiserver with list requests simultaneously.
46+
func resyncPeriod(resync time.Duration) func() time.Duration {
47+
return func() time.Duration {
48+
factor := rand.Float64() + 1
49+
return time.Duration(float64(resync.Nanoseconds()) * factor)
50+
}
51+
}
52+
4253
// NewInformersMap creates a new InformersMap that can create informers for
4354
// both structured and unstructured objects.
4455
func NewInformersMap(config *rest.Config,
4556
scheme *runtime.Scheme,
4657
mapper meta.RESTMapper,
47-
resync time.Duration,
58+
minResyncPeriod time.Duration,
4859
namespace string) *InformersMap {
49-
5060
return &InformersMap{
51-
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace),
52-
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace),
53-
54-
Scheme: scheme,
61+
structured: newStructuredInformersMap(config, scheme, mapper, resyncPeriod(minResyncPeriod)(), namespace),
62+
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resyncPeriod(minResyncPeriod)(), namespace),
63+
Scheme: scheme,
5564
}
5665
}
5766

pkg/manager/manager.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ type Options struct {
107107
// MapperProvider provides the rest mapper used to map go types to Kubernetes APIs
108108
MapperProvider func(c *rest.Config) (meta.RESTMapper, error)
109109

110-
// SyncPeriod determines the minimum frequency at which watched resources are
110+
// MinResyncPeriod determines the minimum frequency at which watched resources are
111111
// reconciled. A lower period will correct entropy more quickly, but reduce
112112
// responsiveness to change if there are many watched resources. Change this
113-
// value only if you know what you are doing. Defaults to 10 hours if unset.
114-
SyncPeriod *time.Duration
113+
// value only if you know what you are doing. Defaults to 5 hours if unset.
114+
// the resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod
115+
MinResyncPeriod *time.Duration
115116

116117
// LeaderElection determines whether or not to use leader election when
117118
// starting the manager.
@@ -240,7 +241,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
240241
}
241242

242243
// Create the cache for the cached read client and registering informers
243-
cache, err := options.NewCache(config, cache.Options{Scheme: options.Scheme, Mapper: mapper, Resync: options.SyncPeriod, Namespace: options.Namespace})
244+
cache, err := options.NewCache(config, cache.Options{Scheme: options.Scheme, Mapper: mapper, MinResyncPeriod: options.MinResyncPeriod, Namespace: options.Namespace})
244245
if err != nil {
245246
return nil, err
246247
}

0 commit comments

Comments
 (0)