diff --git a/CHANGELOG.md b/CHANGELOG.md index 883530d6..a9a77ca7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ NOTE: Breaking release in controllers. - Refactor metrics recorder implementation including the prometheus backend. - Refactor internal controller queue into a decorator implementation approach. - Remove `Delete` method from `controller.Handler` and simplify to only `Handle` method +- Add `DisableResync` flag on controller configuration to disable the resync of all resources. ## [0.8.0] - 2019-12-11 diff --git a/README.md b/README.md index eb576bbd..b5557ca9 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,8 @@ The solution to this problems embraces simplicity once again, and mainly is to c - One of the type retrieval fails, the other type controller continues working (running in degradation mode). - Flexibility, e.g leader election for the primary type, no leader election for the secondary type. +Controller config has a handy flag to disable resync (`DisableResync`), sometimes this can be useful on secondary resources. + ## Compatibility matrix | | Kubernetes <=1.9 | Kubernetes 1.10 | Kubernetes 1.11 | Kubernetes 1.12 | Kubernetes 1.13 | Kubernetes 1.14 | diff --git a/controller/controller.go b/controller/controller.go index a28eb708..f6270954 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -53,6 +53,10 @@ type Config struct { ResyncInterval time.Duration // ProcessingJobRetries is the number of times the job will try to reprocess the event before returning a real error. ProcessingJobRetries int + // DisableResync will disable resyncing, if disabled the controller only will react on event updates and resync + // all when it runs for the first time. + // This is useful for secondary resource controllers (e.g pod controller of a primary controller based on deployments). + DisableResync bool } func (c *Config) setDefaults() error { @@ -90,6 +94,10 @@ func (c *Config) setDefaults() error { c.ResyncInterval = 3 * time.Minute } + if c.DisableResync { + c.ResyncInterval = 0 // 0 == resync disabled. + } + if c.ProcessingJobRetries < 0 { c.ProcessingJobRetries = 0 } @@ -149,7 +157,7 @@ func New(cfg *Config) (Controller, error) { informer := cache.NewSharedIndexInformer(lw, nil, cfg.ResyncInterval, store) // Set up our informer event handler. - // Objects are already in our local store. Add only keys/jobs on the queue so they can bre processed + // Objects are already in our local store. Add only keys/jobs on the queue so they can re processed // afterwards. informer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { diff --git a/examples/controller-concurrency-handling/main.go b/examples/controller-concurrency-handling/main.go index 4abbdaeb..46c2b7dc 100644 --- a/examples/controller-concurrency-handling/main.go +++ b/examples/controller-concurrency-handling/main.go @@ -30,6 +30,7 @@ var ( sleepMS int intervalS int retries int + disableResync bool ) func initFlags() error { @@ -38,27 +39,13 @@ func initFlags() error { fg.IntVar(&sleepMS, "sleep-ms", 25, "The number of milliseconds to sleep on each event handling") fg.IntVar(&intervalS, "interval-s", 45, "The number of seconds to for reconciliation loop intervals") fg.IntVar(&retries, "retries", 3, "The number of retries in case of error") + fg.BoolVar(&disableResync, "disable-resync", false, "Disables the resync") err := fg.Parse(os.Args[1:]) if err != nil { return err } - if concurrentWorkers < 1 { - concurrentWorkers = 1 - } - - if sleepMS < 1 { - sleepMS = 25 - } - - if intervalS < 1 { - intervalS = 300 - } - if retries < 0 { - retries = 0 - } - return nil } @@ -119,6 +106,7 @@ func run() error { ProcessingJobRetries: retries, ResyncInterval: time.Duration(intervalS) * time.Second, ConcurrentWorkers: concurrentWorkers, + DisableResync: disableResync, } ctrl, err := controller.New(cfg) if err != nil {