Skip to content

Commit a9ad079

Browse files
authored
Merge pull request kubernetes#2921 from nilo19/fix-vm-cache-invalidate
Provider/Azure: Invalidating vm cache when the size is incorrect.
2 parents 9440f37 + 5b30a92 commit a9ad079

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

cluster-autoscaler/cloudprovider/azure/azure_agent_pool.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,24 @@ func (as *AgentPool) getVirtualMachinesFromCache() ([]compute.VirtualMachine, er
164164
virtualMachinesStatusCache.virtualMachines[as.Id()] = vms
165165
virtualMachinesStatusCache.lastRefresh[as.Id()] = time.Now()
166166

167-
return vms, err
167+
return vms, nil
168+
}
169+
170+
func invalidateVMCache(agentpoolName string) {
171+
virtualMachinesStatusCache.mutex.Lock()
172+
virtualMachinesStatusCache.lastRefresh[agentpoolName] = time.Now().Add(-1 * vmInstancesRefreshPeriod)
173+
virtualMachinesStatusCache.mutex.Unlock()
168174
}
169175

170176
// GetVMIndexes gets indexes of all virtual machines belonging to the agent pool.
171177
func (as *AgentPool) GetVMIndexes() ([]int, map[int]string, error) {
178+
klog.V(6).Infof("GetVMIndexes: starts for as %v", as)
179+
172180
instances, err := as.getVirtualMachinesFromCache()
173181
if err != nil {
174182
return nil, nil, err
175183
}
184+
klog.V(6).Infof("GetVMIndexes: got instances, length = %d", len(instances))
176185

177186
indexes := make([]int, 0)
178187
indexToVM := make(map[int]string)
@@ -210,6 +219,11 @@ func (as *AgentPool) getCurSize() (int64, error) {
210219
}
211220
klog.V(5).Infof("Returning agent pool (%q) size: %d\n", as.Name, len(indexes))
212221

222+
if as.curSize != int64(len(indexes)) {
223+
klog.V(6).Infof("getCurSize:as.curSize(%d) != real size (%d), invalidating vm cache", as.curSize, len(indexes))
224+
invalidateVMCache(as.Id())
225+
}
226+
213227
as.curSize = int64(len(indexes))
214228
as.lastRefresh = time.Now()
215229
return as.curSize, nil
@@ -296,6 +310,9 @@ func (as *AgentPool) IncreaseSize(delta int) error {
296310
klog.Warningf("IncreaseSize: failed to cleanup outdated deployments with err: %v.", err)
297311
}
298312

313+
klog.V(6).Infof("IncreaseSize: invalidating vm cache")
314+
invalidateVMCache(as.Id())
315+
299316
indexes, _, err := as.GetVMIndexes()
300317
if err != nil {
301318
return err
@@ -334,6 +351,8 @@ func (as *AgentPool) IncreaseSize(delta int) error {
334351
// Update cache after scale success.
335352
as.curSize = int64(expectedSize)
336353
as.lastRefresh = time.Now()
354+
klog.V(6).Info("IncreaseSize: invalidating vm cache")
355+
invalidateVMCache(as.Id())
337356
return nil
338357
}
339358

@@ -450,12 +469,14 @@ func (as *AgentPool) DeleteInstances(instances []*azureRef) error {
450469
}
451470
}
452471

472+
klog.V(6).Infof("DeleteInstances: invalidating vm cache")
473+
invalidateVMCache(as.Id())
453474
return nil
454475
}
455476

456477
// DeleteNodes deletes the nodes from the group.
457478
func (as *AgentPool) DeleteNodes(nodes []*apiv1.Node) error {
458-
klog.V(8).Infof("Delete nodes requested: %v\n", nodes)
479+
klog.V(6).Infof("Delete nodes requested: %v\n", nodes)
459480
indexes, _, err := as.GetVMIndexes()
460481
if err != nil {
461482
return err

0 commit comments

Comments
 (0)