Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak while fetching GCE instance templates. #7694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ func (gc *GceCache) InvalidateAllMigInstanceTemplates() {
gc.instanceTemplatesCache = map[GceRef]*gce.InstanceTemplate{}
}

// DropMigInstanceTemplatesNotFor clears the instance template
// cache intended MIGs which are no longer present in the cluster
func (gc *GceCache) DropInstanceTemplatesForMissingMigs(currentMigs []Mig) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()

requiredKeys := make(map[GceRef]struct{}, len(currentMigs))
for _, mig := range currentMigs {
requiredKeys[mig.GceRef()] = struct{}{}
}

klog.V(5).Infof("Instance template cache partially invalidated")
for key := range gc.instanceTemplatesCache {
if _, exists := requiredKeys[key]; !exists {
delete(gc.instanceTemplatesCache, key)
}
}
}

// GetMigKubeEnv returns the cached KubeEnv for a mig GceRef
func (gc *GceCache) GetMigKubeEnv(ref GceRef) (KubeEnv, bool) {
gc.cacheMutex.Lock()
Expand Down
9 changes: 8 additions & 1 deletion cluster-autoscaler/cloudprovider/gce/gce_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,14 @@ func (m *gceManagerImpl) Refresh() error {
if m.lastRefresh.Add(refreshInterval).After(time.Now()) {
return nil
}
return m.forceRefresh()

if err := m.forceRefresh(); err != nil {
return err
}

migs := m.migLister.GetMigs()
m.cache.DropInstanceTemplatesForMissingMigs(migs)
return nil
}

func (m *gceManagerImpl) CreateInstances(mig Mig, delta int64) error {
Expand Down
Loading