/
machines.go
477 lines (412 loc) · 16.8 KB
/
machines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package worker
import (
"context"
"fmt"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/gardener/gardener/extensions/pkg/controller/csimigration"
"github.com/gardener/gardener/extensions/pkg/controller/worker"
genericworkeractuator "github.com/gardener/gardener/extensions/pkg/controller/worker/genericactuator"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener-extension-provider-azure/charts"
azureapi "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
azureapihelper "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure/helper"
)
const azureCSIDiskDriverTopologyKey = "topology.disk.csi.azure.com/zone"
var tagRegex = regexp.MustCompile(`[<>%\\&?/ ]`)
// MachineClassKind yields the name of machine class kind used by Azure provider.
func (w *workerDelegate) MachineClassKind() string {
return "MachineClass"
}
// MachineClass yields a newly initialized machine class object.
func (w *workerDelegate) MachineClass() client.Object {
return &machinev1alpha1.MachineClass{}
}
// MachineClassList yields a newly initialized MachineClassList object.
func (w *workerDelegate) MachineClassList() client.ObjectList {
return &machinev1alpha1.MachineClassList{}
}
// DeployMachineClasses generates and creates the Azure specific machine classes.
func (w *workerDelegate) DeployMachineClasses(ctx context.Context) error {
if w.machineClasses == nil {
if err := w.generateMachineConfig(ctx); err != nil {
return err
}
}
return w.seedChartApplier.ApplyFromEmbeddedFS(ctx, charts.InternalChart, filepath.Join("internal", "machineclass"), w.worker.Namespace, "machineclass", kubernetes.Values(map[string]interface{}{"machineClasses": w.machineClasses}))
}
// GenerateMachineDeployments generates the configuration for the desired machine deployments.
func (w *workerDelegate) GenerateMachineDeployments(ctx context.Context) (worker.MachineDeployments, error) {
if w.machineDeployments == nil {
if err := w.generateMachineConfig(ctx); err != nil {
return nil, err
}
}
return w.machineDeployments, nil
}
type zoneInfo struct {
name string
index int32
count int32
}
type machineSetInfo struct {
id string
kind string
}
func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
var (
acceleratedNetworkAllowed = true
machineDeployments = worker.MachineDeployments{}
machineClasses []map[string]interface{}
machineImages []azureapi.MachineImage
)
const (
csiMigrationVersion = "1.21"
)
infrastructureStatus, err := w.decodeAzureInfrastructureStatus()
if err != nil {
return err
}
workerStatus, err := w.decodeWorkerProviderStatus()
if err != nil {
return err
}
_, nodesSubnet, err := azureapihelper.FindSubnetByPurposeAndZone(infrastructureStatus.Networks.Subnets, azureapi.PurposeNodes, nil)
if err != nil {
return err
}
csiEnabled, _, err := csimigration.CheckCSIConditions(w.cluster, csiMigrationVersion)
if err != nil {
return err
}
for _, pool := range w.worker.Spec.Pools {
// Get the vmo dependency from the worker status if exists.
vmoDependency, err := w.determineWorkerPoolVmoDependency(ctx, infrastructureStatus, workerStatus, pool.Name)
if err != nil {
return err
}
arch := pointer.StringDeref(pool.Architecture, v1beta1constants.ArchitectureAMD64)
urn, id, communityGalleryImageID, sharedGalleryImageID, imageSupportAcceleratedNetworking, err := w.findMachineImage(pool.MachineImage.Name, pool.MachineImage.Version, &arch)
if err != nil {
return err
}
machineImages = appendMachineImage(machineImages, azureapi.MachineImage{
Name: pool.MachineImage.Name,
Version: pool.MachineImage.Version,
URN: urn,
ID: id,
CommunityGalleryImageID: communityGalleryImageID,
SharedGalleryImageID: sharedGalleryImageID,
AcceleratedNetworking: imageSupportAcceleratedNetworking,
Architecture: &arch,
})
image := map[string]interface{}{}
if urn != nil {
image["urn"] = *urn
} else if communityGalleryImageID != nil {
image["communityGalleryImageID"] = *communityGalleryImageID
} else if sharedGalleryImageID != nil {
image["sharedGalleryImageID"] = *sharedGalleryImageID
} else {
image["id"] = *id
}
disks, err := computeDisks(pool)
if err != nil {
return err
}
workerConfig := &azureapi.WorkerConfig{}
if pool.ProviderConfig != nil && pool.ProviderConfig.Raw != nil {
if _, _, err := w.Decoder().Decode(pool.ProviderConfig.Raw, nil, workerConfig); err != nil {
return fmt.Errorf("could not decode provider config: %+v", err)
}
}
generateMachineClassAndDeployment := func(zone *zoneInfo, machineSet *machineSetInfo, subnetName, workerPoolHash string, workerConfig *azureapi.WorkerConfig) (worker.MachineDeployment, map[string]interface{}) {
var (
machineDeployment = worker.MachineDeployment{
Minimum: pool.Minimum,
Maximum: pool.Maximum,
MaxSurge: pool.MaxSurge,
MaxUnavailable: pool.MaxUnavailable,
Labels: addTopologyLabel(pool.Labels, csiEnabled, w.worker.Spec.Region, zone),
Annotations: pool.Annotations,
Taints: pool.Taints,
MachineConfiguration: genericworkeractuator.ReadMachineConfiguration(pool),
}
machineClassSpec = utils.MergeMaps(map[string]interface{}{
"region": w.worker.Spec.Region,
"resourceGroup": infrastructureStatus.ResourceGroup.Name,
"tags": w.getVMTags(pool),
"secret": map[string]interface{}{
"cloudConfig": string(pool.UserData),
},
"credentialsSecretRef": map[string]interface{}{
"name": w.worker.Spec.SecretRef.Name,
"namespace": w.worker.Spec.SecretRef.Namespace,
},
"machineType": pool.MachineType,
"image": image,
"sshPublicKey": string(w.worker.Spec.SSHPublicKey),
}, disks)
)
networkConfig := map[string]interface{}{
"vnet": infrastructureStatus.Networks.VNet.Name,
"subnet": subnetName,
}
if infrastructureStatus.Networks.VNet.ResourceGroup != nil {
networkConfig["vnetResourceGroup"] = *infrastructureStatus.Networks.VNet.ResourceGroup
}
if imageSupportAcceleratedNetworking != nil && *imageSupportAcceleratedNetworking && w.isMachineTypeSupportingAcceleratedNetworking(pool.MachineType) && acceleratedNetworkAllowed {
networkConfig["acceleratedNetworking"] = true
}
machineClassSpec["network"] = networkConfig
if zone != nil {
machineDeployment.Minimum = worker.DistributeOverZones(zone.index, pool.Minimum, zone.count)
machineDeployment.Maximum = worker.DistributeOverZones(zone.index, pool.Maximum, zone.count)
machineDeployment.MaxSurge = worker.DistributePositiveIntOrPercent(zone.index, pool.MaxSurge, zone.count, pool.Maximum)
machineDeployment.MaxUnavailable = worker.DistributePositiveIntOrPercent(zone.index, pool.MaxUnavailable, zone.count, pool.Minimum)
machineClassSpec["zone"] = zone.name
}
if pool.NodeTemplate != nil {
// Currently Zone field is mandatory, and passing it an
// empty string turns it to `null` string during marshalling which fails CRD validation
// so setting it to a dummy value `no-zone`
// TODO: Zone field in nodeTemplate optional and not to pass it in case of non-zonal setup
zoneName := "no-zone"
if zone != nil {
zoneName = w.worker.Spec.Region + "-" + zone.name
}
if workerConfig.NodeTemplate != nil {
machineClassSpec["nodeTemplate"] = machinev1alpha1.NodeTemplate{
Capacity: workerConfig.NodeTemplate.Capacity,
InstanceType: pool.MachineType,
Region: w.worker.Spec.Region,
Zone: zoneName,
}
} else if pool.NodeTemplate != nil {
machineClassSpec["nodeTemplate"] = machinev1alpha1.NodeTemplate{
Capacity: pool.NodeTemplate.Capacity,
InstanceType: pool.MachineType,
Region: w.worker.Spec.Region,
Zone: zoneName,
}
}
}
if machineSet != nil {
machineClassSpec["machineSet"] = map[string]interface{}{
"kind": machineSet.kind,
"id": machineSet.id,
}
}
if infrastructureStatus.Identity != nil {
machineClassSpec["identityID"] = infrastructureStatus.Identity.ID
}
var (
deploymentName = fmt.Sprintf("%s-%s", w.worker.Namespace, pool.Name)
className = fmt.Sprintf("%s-%s", deploymentName, workerPoolHash)
)
if zone != nil {
deploymentName = fmt.Sprintf("%s-z%s", deploymentName, zone.name)
className = fmt.Sprintf("%s-z%s", className, zone.name)
}
machineDeployment.Name = deploymentName
machineDeployment.ClassName = className
machineDeployment.SecretName = className
machineClassSpec["name"] = className
machineClassSpec["labels"] = map[string]string{v1beta1constants.GardenerPurpose: genericworkeractuator.GardenPurposeMachineClass}
return machineDeployment, machineClassSpec
}
workerPoolHash, err := w.generateWorkerPoolHash(pool, infrastructureStatus, vmoDependency, nil)
if err != nil {
return err
}
// VMO
if vmoDependency != nil {
machineDeployment, machineClassSpec := generateMachineClassAndDeployment(nil, &machineSetInfo{
id: vmoDependency.ID,
kind: "vmo",
}, nodesSubnet.Name, workerPoolHash, workerConfig)
machineDeployments = append(machineDeployments, machineDeployment)
machineClasses = append(machineClasses, machineClassSpec)
continue
}
// AvailabilitySet
if !infrastructureStatus.Zoned && vmoDependency == nil {
nodesAvailabilitySet, err := azureapihelper.FindAvailabilitySetByPurpose(infrastructureStatus.AvailabilitySets, azureapi.PurposeNodes)
if err != nil {
return err
}
// Do not enable accelerated networking for AvSet cluster.
// This is necessary to avoid `ExistingAvailabilitySetWasNotDeployedOnAcceleratedNetworkingEnabledCluster` error.
acceleratedNetworkAllowed = false
machineDeployment, machineClassSpec := generateMachineClassAndDeployment(nil, &machineSetInfo{
id: nodesAvailabilitySet.ID,
kind: "availabilityset",
}, nodesSubnet.Name, workerPoolHash, workerConfig)
machineDeployments = append(machineDeployments, machineDeployment)
machineClasses = append(machineClasses, machineClassSpec)
continue
}
// Availability Zones
zoneCount := len(pool.Zones)
for zoneIndex, zone := range pool.Zones {
if infrastructureStatus.Networks.Layout == azureapi.NetworkLayoutMultipleSubnet {
_, nodesSubnet, err = azureapihelper.FindSubnetByPurposeAndZone(infrastructureStatus.Networks.Subnets, azureapi.PurposeNodes, &zone)
if err != nil {
return err
}
if nodesSubnet.Migrated {
workerPoolHash, err = w.generateWorkerPoolHash(pool, infrastructureStatus, vmoDependency, nil)
if err != nil {
return err
}
} else {
workerPoolHash, err = w.generateWorkerPoolHash(pool, infrastructureStatus, vmoDependency, &nodesSubnet.Name)
if err != nil {
return err
}
}
}
machineDeployment, machineClassSpec := generateMachineClassAndDeployment(&zoneInfo{
name: zone,
index: int32(zoneIndex),
count: int32(zoneCount),
}, nil, nodesSubnet.Name, workerPoolHash, workerConfig)
machineDeployments = append(machineDeployments, machineDeployment)
machineClasses = append(machineClasses, machineClassSpec)
}
}
w.machineDeployments = machineDeployments
w.machineClasses = machineClasses
w.machineImages = machineImages
return nil
}
// isMachineTypeSupportingAcceleratedNetworking checks if the passed machine type is supporting Azure accelerated networking.
func (w *workerDelegate) isMachineTypeSupportingAcceleratedNetworking(machineTypeName string) bool {
for _, machType := range w.cloudProfileConfig.MachineTypes {
if machType.Name == machineTypeName && machType.AcceleratedNetworking != nil && *machType.AcceleratedNetworking {
return true
}
}
return false
}
// getVMTags returns a map of vm tags
func (w *workerDelegate) getVMTags(pool extensionsv1alpha1.WorkerPool) map[string]string {
vmTags := map[string]string{
"Name": w.worker.Namespace,
SanitizeAzureVMTag(fmt.Sprintf("kubernetes.io-cluster-%s", w.worker.Namespace)): "1",
SanitizeAzureVMTag("kubernetes.io-role-node"): "1",
}
for k, v := range pool.Labels {
vmTags[SanitizeAzureVMTag(k)] = v
}
return vmTags
}
func computeDisks(pool extensionsv1alpha1.WorkerPool) (map[string]interface{}, error) {
// handle root disk
volumeSize, err := worker.DiskSize(pool.Volume.Size)
if err != nil {
return nil, err
}
osDisk := map[string]interface{}{
"size": volumeSize,
}
// In the past the volume type information was not passed to the machineclass.
// In consequence the Machine controller manager has created machines always
// with the default volume type of the requested machine type. Existing clusters
// respectively their worker pools could have an invalid volume configuration
// which was not applied. To do not damage existing cluster we will set for
// now the volume type only if it's a valid Azure volume type.
// Otherwise we will still use the default volume of the machine type.
if pool.Volume.Type != nil && (*pool.Volume.Type == "Standard_LRS" || *pool.Volume.Type == "StandardSSD_LRS" || *pool.Volume.Type == "Premium_LRS") {
osDisk["type"] = *pool.Volume.Type
}
disks := map[string]interface{}{
"osDisk": osDisk,
}
// handle data disks
var dataDisks []map[string]interface{}
if dataVolumes := pool.DataVolumes; len(dataVolumes) > 0 {
// sort data volumes for consistent device naming
sort.Slice(dataVolumes, func(i, j int) bool {
return dataVolumes[i].Name < dataVolumes[j].Name
})
for i, volume := range dataVolumes {
volumeSize, err := worker.DiskSize(volume.Size)
if err != nil {
return nil, err
}
disk := map[string]interface{}{
"name": volume.Name,
"lun": int32(i),
"diskSizeGB": volumeSize,
"caching": "None",
}
if volume.Type != nil {
disk["storageAccountType"] = *volume.Type
}
dataDisks = append(dataDisks, disk)
}
disks["dataDisks"] = dataDisks
}
return disks, nil
}
// SanitizeAzureVMTag will sanitize the tag base on the azure tag Restrictions
// refer: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources#limitations
func SanitizeAzureVMTag(label string) string {
return tagRegex.ReplaceAllString(strings.ToLower(label), "_")
}
func addTopologyLabel(labels map[string]string, csiEnabled bool, region string, zone *zoneInfo) map[string]string {
if csiEnabled && zone != nil {
return utils.MergeStringMaps(labels, map[string]string{azureCSIDiskDriverTopologyKey: region + "-" + zone.name})
}
return labels
}
func (w *workerDelegate) generateWorkerPoolHash(pool extensionsv1alpha1.WorkerPool, infrastructureStatus *azureapi.InfrastructureStatus, vmoDependency *azureapi.VmoDependency, subnetName *string) (string, error) {
additionalHashData := []string{}
// Integrate data disks/volumes in the hash.
for _, dv := range pool.DataVolumes {
additionalHashData = append(additionalHashData, dv.Size)
if dv.Type != nil {
additionalHashData = append(additionalHashData, *dv.Type)
}
}
// Incorporate the identity ID in the workerpool hash.
// Machines need to be rolled when the identity has been exchanged.
if infrastructureStatus.Identity != nil {
additionalHashData = append(additionalHashData, infrastructureStatus.Identity.ID)
}
// Include the vmo dependency name into the workerpool hash.
if vmoDependency != nil {
additionalHashData = append(additionalHashData, vmoDependency.Name)
}
if subnetName != nil {
additionalHashData = append(additionalHashData, *subnetName)
}
// Generate the worker pool hash.
workerPoolHash, err := worker.WorkerPoolHash(pool, w.cluster, additionalHashData...)
if err != nil {
return "", err
}
return workerPoolHash, nil
}