From 6196f69f4db3d421e6e5f15ba24192c0c3d9303c Mon Sep 17 00:00:00 2001 From: Huy Le Date: Fri, 8 Mar 2019 10:27:34 -0800 Subject: [PATCH] Create New Job when Canary's Interval changes - Currently whenever the Canary analysis interval changes, flagger does not reflect this into canary's job. - This change will make sure the canary analysis interval got updated whenever the Canary object's interval changes --- pkg/controller/job.go | 17 +++++++++++------ pkg/controller/scheduler.go | 26 ++++++++++++++++---------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/pkg/controller/job.go b/pkg/controller/job.go index ead2415e4..edd1c846c 100644 --- a/pkg/controller/job.go +++ b/pkg/controller/job.go @@ -4,12 +4,13 @@ import "time" // CanaryJob holds the reference to a canary deployment schedule type CanaryJob struct { - Name string - Namespace string - SkipTests bool - function func(name string, namespace string, skipTests bool) - done chan bool - ticker *time.Ticker + Name string + Namespace string + SkipTests bool + function func(name string, namespace string, skipTests bool) + done chan bool + ticker *time.Ticker + analysisInterval time.Duration } // Start runs the canary analysis on a schedule @@ -33,3 +34,7 @@ func (j CanaryJob) Stop() { close(j.done) j.ticker.Stop() } + +func (j CanaryJob) GetCanaryAnalysisInterval() time.Duration { + return j.analysisInterval +} diff --git a/pkg/controller/scheduler.go b/pkg/controller/scheduler.go index d60a7e953..ed140af00 100644 --- a/pkg/controller/scheduler.go +++ b/pkg/controller/scheduler.go @@ -24,18 +24,24 @@ func (c *Controller) scheduleCanaries() { name := key.(string) current[name] = fmt.Sprintf("%s.%s", canary.Spec.TargetRef.Name, canary.Namespace) - // schedule new jobs - if _, exists := c.jobs[name]; !exists { - job := CanaryJob{ - Name: canary.Name, - Namespace: canary.Namespace, - function: c.advanceCanary, - done: make(chan bool), - ticker: time.NewTicker(canary.GetAnalysisInterval()), + job, exists := c.jobs[name] + // schedule new job for exsiting job with different analysisInterval or non-existing job + if (exists && job.GetCanaryAnalysisInterval() != canary.GetAnalysisInterval()) || !exists { + if exists { + job.Stop() } - c.jobs[name] = job - job.Start() + newJob := CanaryJob{ + Name: canary.Name, + Namespace: canary.Namespace, + function: c.advanceCanary, + done: make(chan bool), + ticker: time.NewTicker(canary.GetAnalysisInterval()), + analysisInterval: canary.GetAnalysisInterval(), + } + + c.jobs[name] = newJob + newJob.Start() } // compute canaries per namespace total