Skip to content

Commit

Permalink
use ResyncPeriod func in case of multi controllers list simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmzju committed Oct 16, 2019
1 parent 16c93b0 commit 7062a1e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
12 changes: 6 additions & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ type Options struct {
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
Mapper meta.RESTMapper

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

// Namespace restricts the cache's ListWatch to the desired namespace
// Default watches all namespaces
Namespace string
}

var defaultResyncTime = 10 * time.Hour
var defaultMinResyncTime = 5 * time.Hour

// New initializes and returns a new Cache.
func New(config *rest.Config, opts Options) (Cache, error) {
opts, err := defaultOpts(config, opts)
if err != nil {
return nil, err
}
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace)
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.MinResyncPeriod, opts.Namespace)
return &informerCache{InformersMap: im}, nil
}

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

// Default the resync period to 10 hours if unset
if opts.Resync == nil {
opts.Resync = &defaultResyncTime
if opts.MinResyncPeriod == nil {
opts.MinResyncPeriod = &defaultMinResyncTime
}
return opts, nil
}
21 changes: 15 additions & 6 deletions pkg/cache/internal/deleg_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package internal

import (
"math/rand"
"time"

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

// resyncPeriod returns a function which generates a duration each time it is
// invoked; this is so that multiple controllers don't get into lock-step and all
// hammer the apiserver with list requests simultaneously.
func resyncPeriod(resync time.Duration) func() time.Duration {
return func() time.Duration {
factor := rand.Float64() + 1
return time.Duration(float64(resync.Nanoseconds()) * factor)
}
}

// NewInformersMap creates a new InformersMap that can create informers for
// both structured and unstructured objects.
func NewInformersMap(config *rest.Config,
scheme *runtime.Scheme,
mapper meta.RESTMapper,
resync time.Duration,
minResyncPeriod time.Duration,
namespace string) *InformersMap {

return &InformersMap{
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace),
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace),

Scheme: scheme,
structured: newStructuredInformersMap(config, scheme, mapper, resyncPeriod(minResyncPeriod)(), namespace),
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resyncPeriod(minResyncPeriod)(), namespace),
Scheme: scheme,
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ type Options struct {
// MapperProvider provides the rest mapper used to map go types to Kubernetes APIs
MapperProvider func(c *rest.Config) (meta.RESTMapper, error)

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

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

// Create the cache for the cached read client and registering informers
cache, err := options.NewCache(config, cache.Options{Scheme: options.Scheme, Mapper: mapper, Resync: options.SyncPeriod, Namespace: options.Namespace})
cache, err := options.NewCache(config, cache.Options{Scheme: options.Scheme, Mapper: mapper, MinResyncPeriod: options.MinResyncPeriod, Namespace: options.Namespace})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7062a1e

Please sign in to comment.