@@ -25,6 +25,7 @@ import (
25
25
"sync"
26
26
"time"
27
27
28
+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v5"
28
29
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-08-01/compute"
29
30
"github.com/Azure/go-autorest/autorest/to"
30
31
"github.com/Azure/skewer"
@@ -67,13 +68,18 @@ type azureCache struct {
67
68
68
69
// Cache content.
69
70
70
- // resourceGroup specifies the name of the resource group that this cache tracks
71
- resourceGroup string
71
+ // resourceGroup specifies the name of the node resource group that this cache tracks
72
+ resourceGroup string
73
+ clusterResourceGroup string
74
+ clusterName string
75
+
76
+ // enableVMsAgentPool specifies whether VMs agent pool type is supported.
77
+ enableVMsAgentPool bool
72
78
73
79
// vmType can be one of vmTypeVMSS (default), vmTypeStandard
74
80
vmType string
75
81
76
- vmsPoolSet map [string ]struct {} // track the nodepools that're vms pool
82
+ vmsPoolMap map [string ]armcontainerservice. AgentPool // track the nodepools that're vms pool
77
83
78
84
// scaleSets keeps the set of all known scalesets in the resource group, populated/refreshed via VMSS.List() call.
79
85
// It is only used/populated if vmType is vmTypeVMSS (default).
@@ -106,8 +112,11 @@ func newAzureCache(client *azClient, cacheTTL time.Duration, config Config) (*az
106
112
azClient : client ,
107
113
refreshInterval : cacheTTL ,
108
114
resourceGroup : config .ResourceGroup ,
115
+ clusterResourceGroup : config .ClusterResourceGroup ,
116
+ clusterName : config .ClusterName ,
117
+ enableVMsAgentPool : config .EnableVMsAgentPool ,
109
118
vmType : config .VMType ,
110
- vmsPoolSet : make (map [string ]struct {} ),
119
+ vmsPoolMap : make (map [string ]armcontainerservice. AgentPool ),
111
120
scaleSets : make (map [string ]compute.VirtualMachineScaleSet ),
112
121
virtualMachines : make (map [string ][]compute.VirtualMachine ),
113
122
registeredNodeGroups : make ([]cloudprovider.NodeGroup , 0 ),
@@ -130,11 +139,11 @@ func newAzureCache(client *azClient, cacheTTL time.Duration, config Config) (*az
130
139
return cache , nil
131
140
}
132
141
133
- func (m * azureCache ) getVMsPoolSet () map [string ]struct {} {
142
+ func (m * azureCache ) getVMsPoolMap () map [string ]armcontainerservice. AgentPool {
134
143
m .mutex .Lock ()
135
144
defer m .mutex .Unlock ()
136
145
137
- return m .vmsPoolSet
146
+ return m .vmsPoolMap
138
147
}
139
148
140
149
func (m * azureCache ) getVirtualMachines () map [string ][]compute.VirtualMachine {
@@ -232,13 +241,20 @@ func (m *azureCache) fetchAzureResources() error {
232
241
return err
233
242
}
234
243
m .scaleSets = vmssResult
235
- vmResult , vmsPoolSet , err := m .fetchVirtualMachines ()
244
+ vmResult , err := m .fetchVirtualMachines ()
236
245
if err != nil {
237
246
return err
238
247
}
239
248
// we fetch both sets of resources since CAS may operate on mixed nodepools
240
249
m .virtualMachines = vmResult
241
- m .vmsPoolSet = vmsPoolSet
250
+ // fetch VMs pools if enabled
251
+ if m .enableVMsAgentPool {
252
+ vmsPoolMap , err := m .fetchVMsPools ()
253
+ if err != nil {
254
+ return err
255
+ }
256
+ m .vmsPoolMap = vmsPoolMap
257
+ }
242
258
243
259
return nil
244
260
}
@@ -251,19 +267,17 @@ const (
251
267
)
252
268
253
269
// fetchVirtualMachines returns the updated list of virtual machines in the config resource group using the Azure API.
254
- func (m * azureCache ) fetchVirtualMachines () (map [string ][]compute.VirtualMachine , map [ string ] struct {}, error ) {
270
+ func (m * azureCache ) fetchVirtualMachines () (map [string ][]compute.VirtualMachine , error ) {
255
271
ctx , cancel := getContextWithCancel ()
256
272
defer cancel ()
257
273
258
274
result , err := m .azClient .virtualMachinesClient .List (ctx , m .resourceGroup )
259
275
if err != nil {
260
276
klog .Errorf ("VirtualMachinesClient.List in resource group %q failed: %v" , m .resourceGroup , err )
261
- return nil , nil , err .Error ()
277
+ return nil , err .Error ()
262
278
}
263
279
264
280
instances := make (map [string ][]compute.VirtualMachine )
265
- // track the nodepools that're vms pools
266
- vmsPoolSet := make (map [string ]struct {})
267
281
for _ , instance := range result {
268
282
if instance .Tags == nil {
269
283
continue
@@ -280,20 +294,43 @@ func (m *azureCache) fetchVirtualMachines() (map[string][]compute.VirtualMachine
280
294
}
281
295
282
296
instances [to .String (vmPoolName )] = append (instances [to .String (vmPoolName )], instance )
297
+ }
298
+ return instances , nil
299
+ }
283
300
284
- // if the nodepool is already in the map, skip it
285
- if _ , ok := vmsPoolSet [to .String (vmPoolName )]; ok {
286
- continue
301
+ // fetchVMsPools returns a name to agentpool map of all the VMs pools in the cluster
302
+ func (m * azureCache ) fetchVMsPools () (map [string ]armcontainerservice.AgentPool , error ) {
303
+ ctx , cancel := getContextWithTimeout (vmsContextTimeout )
304
+ defer cancel ()
305
+
306
+ // defensive check, should never happen when enableVMsAgentPool toggle is on
307
+ if m .azClient .agentPoolClient == nil {
308
+ return nil , errors .New ("agentPoolClient is nil" )
309
+ }
310
+
311
+ vmsPoolMap := make (map [string ]armcontainerservice.AgentPool )
312
+ pager := m .azClient .agentPoolClient .NewListPager (m .clusterResourceGroup , m .clusterName , nil )
313
+ var aps []* armcontainerservice.AgentPool
314
+ for pager .More () {
315
+ resp , err := pager .NextPage (ctx )
316
+ if err != nil {
317
+ klog .Errorf ("agentPoolClient.pager.NextPage in cluster %s resource group %s failed: %v" ,
318
+ m .clusterName , m .clusterResourceGroup , err )
319
+ return nil , err
287
320
}
321
+ aps = append (aps , resp .Value ... )
322
+ }
288
323
289
- // nodes from vms pool will have tag "aks-managed-agentpool-type" set to "VirtualMachines"
290
- if agentpoolType := tags [agentpoolTypeTag ]; agentpoolType != nil {
291
- if strings .EqualFold (to .String (agentpoolType ), vmsPoolType ) {
292
- vmsPoolSet [to .String (vmPoolName )] = struct {}{}
293
- }
324
+ for _ , ap := range aps {
325
+ if ap != nil && ap .Name != nil && ap .Properties != nil && ap .Properties .Type != nil &&
326
+ * ap .Properties .Type == armcontainerservice .AgentPoolTypeVirtualMachines {
327
+ // we only care about VMs pools, skip other types
328
+ klog .V (6 ).Infof ("Found VMs pool %q" , * ap .Name )
329
+ vmsPoolMap [* ap .Name ] = * ap
294
330
}
295
331
}
296
- return instances , vmsPoolSet , nil
332
+
333
+ return vmsPoolMap , nil
297
334
}
298
335
299
336
// fetchScaleSets returns the updated list of scale sets in the config resource group using the Azure API.
@@ -422,7 +459,7 @@ func (m *azureCache) HasInstance(providerID string) (bool, error) {
422
459
423
460
// FindForInstance returns node group of the given Instance
424
461
func (m * azureCache ) FindForInstance (instance * azureRef , vmType string ) (cloudprovider.NodeGroup , error ) {
425
- vmsPoolSet := m .getVMsPoolSet ()
462
+ vmsPoolMap := m .getVMsPoolMap ()
426
463
m .mutex .Lock ()
427
464
defer m .mutex .Unlock ()
428
465
@@ -441,7 +478,7 @@ func (m *azureCache) FindForInstance(instance *azureRef, vmType string) (cloudpr
441
478
}
442
479
443
480
// cluster with vmss pool only
444
- if vmType == providerazureconsts .VMTypeVMSS && len (vmsPoolSet ) == 0 {
481
+ if vmType == providerazureconsts .VMTypeVMSS && len (vmsPoolMap ) == 0 {
445
482
if m .areAllScaleSetsUniform () {
446
483
// Omit virtual machines not managed by vmss only in case of uniform scale set.
447
484
if ok := virtualMachineRE .Match ([]byte (inst .Name )); ok {
0 commit comments