diff --git a/pkg/plugin/group/quorum.go b/pkg/plugin/group/quorum.go index 1ec77005d..f714e77f3 100644 --- a/pkg/plugin/group/quorum.go +++ b/pkg/plugin/group/quorum.go @@ -70,7 +70,7 @@ func (q *quorum) Size() uint { } func (q *quorum) converge() { - descriptions, err := q.scaled.List() + descriptions, err := labelAndList(q.scaled) if err != nil { log.Errorf("Failed to check group: %s", err) return diff --git a/pkg/plugin/group/rollingupdate.go b/pkg/plugin/group/rollingupdate.go index 45863874c..28469c3a6 100644 --- a/pkg/plugin/group/rollingupdate.go +++ b/pkg/plugin/group/rollingupdate.go @@ -61,7 +61,7 @@ func (r *rollingupdate) waitUntilQuiesced(pollInterval time.Duration, expectedNe // - instances with the desired config are healthy // TODO(wfarner): Get this information from the scaler to reduce redundant network calls. - instances, err := r.scaled.List() + instances, err := labelAndList(r.scaled) if err != nil { return err } @@ -116,7 +116,7 @@ func (r *rollingupdate) waitUntilQuiesced(pollInterval time.Duration, expectedNe // TODO(wfarner): Make this routine more resilient to transient errors. func (r *rollingupdate) Run(pollInterval time.Duration) error { - instances, err := r.scaled.List() + instances, err := labelAndList(r.scaled) if err != nil { return err } @@ -133,7 +133,7 @@ func (r *rollingupdate) Run(pollInterval time.Duration) error { } log.Info("Scaler has quiesced") - instances, err := r.scaled.List() + instances, err := labelAndList(r.scaled) if err != nil { return err } diff --git a/pkg/plugin/group/scaled.go b/pkg/plugin/group/scaled.go index 1898f397a..5c9125cd3 100644 --- a/pkg/plugin/group/scaled.go +++ b/pkg/plugin/group/scaled.go @@ -153,6 +153,23 @@ func (s *scaledGroup) Label() error { return nil } +func labelAndList(scaled Scaled) ([]instance.Description, error) { + descriptions, err := scaled.List() + if err != nil { + return nil, err + } + + if !needsLabel(descriptions) { + return descriptions, nil + } + + if err := scaled.Label(); err != nil { + return nil, err + } + + return scaled.List() +} + func needsLabel(instances []instance.Description) bool { for _, inst := range instances { if instanceNeedsLabel(inst) { diff --git a/pkg/plugin/group/scaler.go b/pkg/plugin/group/scaler.go index 6ab9ddb71..42ed51ca4 100644 --- a/pkg/plugin/group/scaler.go +++ b/pkg/plugin/group/scaler.go @@ -35,7 +35,7 @@ func (s *scaler) PlanUpdate(scaled Scaled, settings groupSettings, newSettings g sizeChange := int(newSettings.config.Allocation.Size) - int(settings.config.Allocation.Size) - instances, err := scaled.List() + instances, err := labelAndList(s.scaled) if err != nil { return nil, err } @@ -220,26 +220,12 @@ func (s *scaler) waitIfReachParallelLimit(current int, batch *sync.WaitGroup) { } func (s *scaler) converge() { - descriptions, err := s.scaled.List() + descriptions, err := labelAndList(s.scaled) if err != nil { log.Errorf("Failed to list group instances: %s", err) return } - if needsLabel(descriptions) { - err := s.scaled.Label() - if err != nil { - log.Errorf("Failed to label the group instances: %s", err) - return - } - - descriptions, err = s.scaled.List() - if err != nil { - log.Errorf("Failed to list group instances: %s", err) - return - } - } - log.Debugf("Found existing instances: %v", descriptions) grp := sync.WaitGroup{}