diff --git a/cluster-autoscaler/cloudprovider/gce/gce_manager.go b/cluster-autoscaler/cloudprovider/gce/gce_manager.go index 946bd714b68a..7166bf4937ba 100644 --- a/cluster-autoscaler/cloudprovider/gce/gce_manager.go +++ b/cluster-autoscaler/cloudprovider/gce/gce_manager.go @@ -851,18 +851,16 @@ func (m *gceManagerImpl) forceRefresh() error { return nil } +// Fetch explicitly configured MIGs. These MIGs should never be unregistered +// during refreshes, even if they no longer exist in GCE. func (m *gceManagerImpl) fetchExplicitMigs(specs []string) error { m.assertGCE() changed := false - for _, s := range specs { - spec, err := dynamic.SpecFromString(s, scaleToZeroSupported) + for _, spec := range specs { + mig, err := m.buildMigFromSpec(spec) if err != nil { - return fmt.Errorf("failed to parse node group spec: %v", err) - } - mig := &Mig{gceManager: m, minSize: spec.MinSize, maxSize: spec.MaxSize, exist: true} - if mig.Project, mig.Zone, mig.Name, err = ParseMigUrl(spec.Name); err != nil { - return fmt.Errorf("failed to parse mig url: %s got error: %v", spec.Name, err) + return err } if m.RegisterMig(mig) { changed = true @@ -878,6 +876,20 @@ func (m *gceManagerImpl) fetchExplicitMigs(specs []string) error { return nil } +func (m *gceManagerImpl) buildMigFromSpec(spec string) (*Mig, error) { + s, err := dynamic.SpecFromString(spec, scaleToZeroSupported) + if err != nil { + return nil, fmt.Errorf("failed to parse node group spec: %v", err) + } + mig := &Mig{gceManager: m, minSize: s.MinSize, maxSize: s.MaxSize, exist: true} + if mig.Project, mig.Zone, mig.Name, err = ParseMigUrl(s.Name); err != nil { + return nil, fmt.Errorf("failed to parse mig url: %s got error: %v", s.Name, err) + } + return mig, nil +} + +// Fetch automatically discovered MIGs. These MIGs should be unregistered if +// they no longer exist in GCE. func (m *gceManagerImpl) fetchAutoMigs() error { m.assertGCE() @@ -889,18 +901,9 @@ func (m *gceManagerImpl) fetchAutoMigs() error { return fmt.Errorf("cannot autodiscover managed instance groups: %s", err) } for _, link := range links { - spec := dynamic.NodeGroupSpec{ - Name: link, - MinSize: cfg.MinSize, - MaxSize: cfg.MaxSize, - SupportScaleToZero: scaleToZeroSupported, - } - if verr := spec.Validate(); verr != nil { - return fmt.Errorf("failed to create node group spec: %v", verr) - } - mig := &Mig{gceManager: m, minSize: spec.MinSize, maxSize: spec.MaxSize, exist: true} - if mig.Project, mig.Zone, mig.Name, err = ParseMigUrl(spec.Name); err != nil { - return fmt.Errorf("failed to parse mig url: %s got error: %v", spec.Name, err) + mig, err := m.buildMigFromAutoCfg(link, cfg) + if err != nil { + return err } if m.RegisterMig(mig) { changed = true @@ -925,6 +928,24 @@ func (m *gceManagerImpl) fetchAutoMigs() error { return nil } +func (m *gceManagerImpl) buildMigFromAutoCfg(link string, cfg cloudprovider.MIGAutoDiscoveryConfig) (*Mig, error) { + spec := dynamic.NodeGroupSpec{ + Name: link, + MinSize: cfg.MinSize, + MaxSize: cfg.MaxSize, + SupportScaleToZero: scaleToZeroSupported, + } + if verr := spec.Validate(); verr != nil { + return nil, fmt.Errorf("failed to create node group spec: %v", verr) + } + mig := &Mig{gceManager: m, minSize: spec.MinSize, maxSize: spec.MaxSize, exist: true} + var err error + if mig.Project, mig.Zone, mig.Name, err = ParseMigUrl(spec.Name); err != nil { + return nil, fmt.Errorf("failed to parse mig url: %s got error: %v", spec.Name, err) + } + return mig, nil +} + func (m *gceManagerImpl) fetchResourceLimiter() error { m.assertGKENAP()