Skip to content

Commit

Permalink
Break out MIG building code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Cope committed Nov 30, 2017
1 parent 47d8d85 commit 89eb3ba
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions cluster-autoscaler/cloudprovider/gce/gce_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand Down

0 comments on commit 89eb3ba

Please sign in to comment.