From 22f860a3a38e32e5edf37a7147e57b8cd29abf56 Mon Sep 17 00:00:00 2001 From: mathetake Date: Sun, 8 Mar 2020 16:06:53 +0900 Subject: [PATCH] refactor pkg/controller --- cmd/flagger/main.go | 1 - pkg/controller/controller.go | 3 -- pkg/controller/scheduler.go | 32 +++++++++---------- .../scheduler_daemonset_fixture_test.go | 1 - .../scheduler_deployment_fixture_test.go | 1 - 5 files changed, 15 insertions(+), 23 deletions(-) diff --git a/cmd/flagger/main.go b/cmd/flagger/main.go index f12fa83c..708ab504 100644 --- a/cmd/flagger/main.go +++ b/cmd/flagger/main.go @@ -183,7 +183,6 @@ func main() { c := controller.NewController( kubeClient, - meshClient, flaggerClient, infos, controlLoopInterval, diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 673dc5d1..0b92c458 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -35,7 +35,6 @@ const controllerAgentName = "flagger" // Controller is managing the canary objects and schedules canary deployments type Controller struct { kubeClient kubernetes.Interface - istioClient clientset.Interface flaggerClient clientset.Interface flaggerInformers Informers flaggerSynced cache.InformerSynced @@ -62,7 +61,6 @@ type Informers struct { func NewController( kubeClient kubernetes.Interface, - istioClient clientset.Interface, flaggerClient clientset.Interface, flaggerInformers Informers, flaggerWindow time.Duration, @@ -89,7 +87,6 @@ func NewController( ctrl := &Controller{ kubeClient: kubeClient, - istioClient: istioClient, flaggerClient: flaggerClient, flaggerInformers: flaggerInformers, flaggerSynced: flaggerInformers.CanaryInformer.Informer().HasSynced, diff --git a/pkg/controller/scheduler.go b/pkg/controller/scheduler.go index 0a200491..1dd9a101 100644 --- a/pkg/controller/scheduler.go +++ b/pkg/controller/scheduler.go @@ -27,26 +27,26 @@ func (c *Controller) scheduleCanaries() { stats := make(map[string]int) c.canaries.Range(func(key interface{}, value interface{}) bool { - canary := value.(*flaggerv1.Canary) + cn := value.(*flaggerv1.Canary) // format: . name := key.(string) - current[name] = fmt.Sprintf("%s.%s", canary.Spec.TargetRef.Name, canary.Namespace) + current[name] = fmt.Sprintf("%s.%s", cn.Spec.TargetRef.Name, cn.Namespace) job, exists := c.jobs[name] // schedule new job for existing job with different analysis interval or non-existing job - if (exists && job.GetCanaryAnalysisInterval() != canary.GetAnalysisInterval()) || !exists { + if (exists && job.GetCanaryAnalysisInterval() != cn.GetAnalysisInterval()) || !exists { if exists { job.Stop() } newJob := CanaryJob{ - Name: canary.Name, - Namespace: canary.Namespace, + Name: cn.Name, + Namespace: cn.Namespace, function: c.advanceCanary, done: make(chan bool), - ticker: time.NewTicker(canary.GetAnalysisInterval()), - analysisInterval: canary.GetAnalysisInterval(), + ticker: time.NewTicker(cn.GetAnalysisInterval()), + analysisInterval: cn.GetAnalysisInterval(), } c.jobs[name] = newJob @@ -54,11 +54,11 @@ func (c *Controller) scheduleCanaries() { } // compute canaries per namespace total - t, ok := stats[canary.Namespace] + t, ok := stats[cn.Namespace] if !ok { - stats[canary.Namespace] = 1 + stats[cn.Namespace] = 1 } else { - stats[canary.Namespace] = t + 1 + stats[cn.Namespace] = t + 1 } return true }) @@ -75,7 +75,8 @@ func (c *Controller) scheduleCanaries() { for canaryName, targetName := range current { for name, target := range current { if name != canaryName && target == targetName { - c.logger.With("canary", canaryName).Errorf("Bad things will happen! Found more than one canary with the same target %s", targetName) + c.logger.With("canary", canaryName). + Errorf("Bad things will happen! Found more than one canary with the same target %s", targetName) } } } @@ -111,8 +112,8 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh } // init Kubernetes router - router := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.Kind, labelSelector, map[string]string{}, ports) - if err := router.Initialize(cd); err != nil { + kubeRouter := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.Kind, labelSelector, map[string]string{}, ports) + if err := kubeRouter.Initialize(cd); err != nil { c.recordEventWarningf(cd, "%v", err) return } @@ -128,7 +129,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh meshRouter := c.routerFactory.MeshRouter(provider) // create or update svc - if err := router.Reconcile(cd); err != nil { + if err := kubeRouter.Reconcile(cd); err != nil { c.recordEventWarningf(cd, "%v", err) return } @@ -206,7 +207,6 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh } if err := canaryController.SyncStatus(cd, status); err != nil { c.recordEventWarningf(cd, "%v", err) - return } return } @@ -305,7 +305,6 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh if ok := c.runPreRolloutHooks(cd); !ok { if err := canaryController.SetStatusFailedChecks(cd, cd.Status.FailedChecks+1); err != nil { c.recordEventWarningf(cd, "%v", err) - return } return } @@ -313,7 +312,6 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh if ok := c.runAnalysis(cd); !ok { if err := canaryController.SetStatusFailedChecks(cd, cd.Status.FailedChecks+1); err != nil { c.recordEventWarningf(cd, "%v", err) - return } return } diff --git a/pkg/controller/scheduler_daemonset_fixture_test.go b/pkg/controller/scheduler_daemonset_fixture_test.go index 8d2cdd6d..65570ac8 100644 --- a/pkg/controller/scheduler_daemonset_fixture_test.go +++ b/pkg/controller/scheduler_daemonset_fixture_test.go @@ -90,7 +90,6 @@ func newDaemonSetFixture(c *flaggerv1.Canary) daemonSetFixture { ctrl := &Controller{ kubeClient: kubeClient, - istioClient: flaggerClient, flaggerClient: flaggerClient, flaggerInformers: fi, flaggerSynced: fi.CanaryInformer.Informer().HasSynced, diff --git a/pkg/controller/scheduler_deployment_fixture_test.go b/pkg/controller/scheduler_deployment_fixture_test.go index 74a7321e..be7c10dd 100644 --- a/pkg/controller/scheduler_deployment_fixture_test.go +++ b/pkg/controller/scheduler_deployment_fixture_test.go @@ -92,7 +92,6 @@ func newDeploymentFixture(c *flaggerv1.Canary) fixture { ctrl := &Controller{ kubeClient: kubeClient, - istioClient: flaggerClient, flaggerClient: flaggerClient, flaggerInformers: fi, flaggerSynced: fi.CanaryInformer.Informer().HasSynced,