This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Cluster-autoscaler: fix for multi-mig autoscaling #1282
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package gce | |
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -40,9 +41,14 @@ const ( | |
operationPollInterval = 100 * time.Millisecond | ||
) | ||
|
||
type migInformation struct { | ||
config *config.MigConfig | ||
basename string | ||
} | ||
|
||
// GceManager is handles gce communication and data caching. | ||
type GceManager struct { | ||
migs []*config.MigConfig | ||
migs []*migInformation | ||
service *gce.Service | ||
migCache map[config.InstanceConfig]*config.MigConfig | ||
cacheMutex sync.Mutex | ||
|
@@ -74,8 +80,15 @@ func CreateGceManager(migs []*config.MigConfig, configReader io.Reader) (*GceMan | |
return nil, err | ||
} | ||
|
||
migInfos := make([]*migInformation, 0, len(migs)) | ||
for _, mig := range migs { | ||
migInfos = append(migInfos, &migInformation{ | ||
config: mig, | ||
}) | ||
} | ||
|
||
manager := &GceManager{ | ||
migs: migs, | ||
migs: migInfos, | ||
service: gceService, | ||
migCache: map[config.InstanceConfig]*config.MigConfig{}, | ||
} | ||
|
@@ -164,13 +177,22 @@ func (m *GceManager) GetMigForInstance(instance *config.InstanceConfig) (*config | |
if mig, found := m.migCache[*instance]; found { | ||
return mig, nil | ||
} | ||
if err := m.regenerateCache(); err != nil { | ||
return nil, fmt.Errorf("Error while looking for MIG for instance %+v, error: %v", *instance, err) | ||
} | ||
if mig, found := m.migCache[*instance]; found { | ||
return mig, nil | ||
|
||
for _, mig := range m.migs { | ||
if mig.config.Project == instance.Project && | ||
mig.config.Zone == instance.Zone && | ||
strings.HasPrefix(instance.Name, mig.basename) { | ||
if err := m.regenerateCache(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be readable if you restructure the code such that:
|
||
return nil, fmt.Errorf("Error while looking for MIG for instance %+v, error: %v", *instance, err) | ||
} | ||
if mig, found := m.migCache[*instance]; found { | ||
return mig, nil | ||
} | ||
return nil, fmt.Errorf("Instance %+v does not belong to any configured MIG", *instance) | ||
} | ||
} | ||
return nil, fmt.Errorf("Instance %+v does not belong to any known MIG", *instance) | ||
// Instance doesn't belong to any configured mig. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain why it's ok. |
||
return nil, nil | ||
} | ||
|
||
func (m *GceManager) regenerateCacheIgnoreError() { | ||
|
@@ -184,8 +206,16 @@ func (m *GceManager) regenerateCacheIgnoreError() { | |
func (m *GceManager) regenerateCache() error { | ||
newMigCache := map[config.InstanceConfig]*config.MigConfig{} | ||
|
||
for _, mig := range m.migs { | ||
for _, migInfo := range m.migs { | ||
mig := migInfo.config | ||
glog.V(4).Infof("Regenerating MIG information for %s %s %s", mig.Project, mig.Zone, mig.Name) | ||
|
||
instanceGroupManager, err := m.service.InstanceGroupManagers.Get(mig.Project, mig.Zone, mig.Name).Do() | ||
if err != nil { | ||
return err | ||
} | ||
migInfo.basename = instanceGroupManager.BaseInstanceName | ||
|
||
instances, err := m.service.InstanceGroupManagers.ListManagedInstances(mig.Project, mig.Zone, mig.Name).Do() | ||
if err != nil { | ||
glog.V(4).Infof("Failed MIG info request for %s %s %s: %v", mig.Project, mig.Zone, mig.Name, err) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During the first run of this loop basename will be empty and it will always be a prefix and we'll return a prefix. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache is 'regenerated' in the beginning, so this could happen only in case of race condition (with very low probability).