/
azuremachine_validation.go
380 lines (311 loc) · 15.8 KB
/
azuremachine_validation.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
/*
Copyright 2021 The Kubernetes Authors.
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 v1beta1
import (
"encoding/base64"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/google/uuid"
"golang.org/x/crypto/ssh"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// ValidateAzureMachineSpec check for validation errors of azuremachine.spec.
func ValidateAzureMachineSpec(spec AzureMachineSpec) field.ErrorList {
var allErrs field.ErrorList
if errs := ValidateImage(spec.Image, field.NewPath("image")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateOSDisk(spec.OSDisk, field.NewPath("osDisk")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateSSHKey(spec.SSHPublicKey, field.NewPath("sshPublicKey")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateUserAssignedIdentity(spec.Identity, spec.UserAssignedIdentities, field.NewPath("userAssignedIdentities")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateDataDisks(spec.DataDisks, field.NewPath("dataDisks")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateDiagnostics(spec.Diagnostics, field.NewPath("diagnostics")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateNetwork(spec.SubnetName, spec.AcceleratedNetworking, spec.NetworkInterfaces, field.NewPath("networkInterfaces")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if errs := ValidateSystemAssignedIdentityRole(spec.Identity, spec.RoleAssignmentName, spec.SystemAssignedIdentityRole, field.NewPath("systemAssignedIdentityRole")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
return allErrs
}
// ValidateNetwork validates the network configuration.
func ValidateNetwork(subnetName string, acceleratedNetworking *bool, networkInterfaces []NetworkInterface, fldPath *field.Path) field.ErrorList {
if (networkInterfaces != nil) && len(networkInterfaces) > 0 && subnetName != "" {
return field.ErrorList{field.Invalid(fldPath, networkInterfaces, "cannot set both networkInterfaces and machine subnetName")}
}
if (networkInterfaces != nil) && len(networkInterfaces) > 0 && acceleratedNetworking != nil {
return field.ErrorList{field.Invalid(fldPath, networkInterfaces, "cannot set both networkInterfaces and machine acceleratedNetworking")}
}
for _, nic := range networkInterfaces {
if nic.PrivateIPConfigs < 1 {
return field.ErrorList{field.Invalid(fldPath, networkInterfaces, "number of privateIPConfigs per interface must be at least 1")}
}
}
return field.ErrorList{}
}
// ValidateSSHKey validates an SSHKey.
func ValidateSSHKey(sshKey string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
decoded, err := base64.StdEncoding.DecodeString(sshKey)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, sshKey, "the SSH public key is not properly base64 encoded"))
return allErrs
}
if _, _, _, _, err := ssh.ParseAuthorizedKey(decoded); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, sshKey, "the SSH public key is not valid"))
return allErrs
}
return allErrs
}
// ValidateSystemAssignedIdentity validates the system-assigned identities list.
func ValidateSystemAssignedIdentity(identityType VMIdentity, oldIdentity, newIdentity string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if identityType == VMIdentitySystemAssigned {
if _, err := uuid.Parse(newIdentity); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, newIdentity, "Role assignment name must be a valid GUID. It is optional and will be auto-generated when not specified."))
}
if oldIdentity != "" && oldIdentity != newIdentity {
allErrs = append(allErrs, field.Invalid(fldPath, newIdentity, "Role assignment name should not be modified after AzureMachine creation."))
}
} else if newIdentity != "" {
allErrs = append(allErrs, field.Forbidden(fldPath, "Role assignment name should only be set when using system assigned identity."))
}
return allErrs
}
// ValidateUserAssignedIdentity validates the user-assigned identities list.
func ValidateUserAssignedIdentity(identityType VMIdentity, userAssignedIdentities []UserAssignedIdentity, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if identityType == VMIdentityUserAssigned && len(userAssignedIdentities) == 0 {
allErrs = append(allErrs, field.Required(fldPath, "must be specified for the 'UserAssigned' identity type"))
}
return allErrs
}
// ValidateSystemAssignedIdentityRole validates the system-assigned identity role.
func ValidateSystemAssignedIdentityRole(identityType VMIdentity, roleAssignmentName string, role *SystemAssignedIdentityRole, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
if roleAssignmentName != "" && role != nil && role.Name != "" {
allErrs = append(allErrs, field.Invalid(fldPath, role.Name, "cannot set both roleAssignmentName and systemAssignedIdentityRole.name"))
}
if identityType == VMIdentitySystemAssigned {
if role.DefinitionID == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "SystemAssignedIdentityRole", "DefinitionID"), role.DefinitionID, "the roleDefinitionID field cannot be empty"))
}
if role.Scope == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "SystemAssignedIdentityRole", "Scope"), role.Scope, "the scope field cannot be empty"))
}
}
if identityType != VMIdentitySystemAssigned && role != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("Spec", "Role"), "systemAssignedIdentityRole can only be set when identity is set to SystemAssigned"))
}
return allErrs
}
// ValidateDataDisks validates a list of data disks.
func ValidateDataDisks(dataDisks []DataDisk, fieldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
lunSet := make(map[int32]struct{})
nameSet := make(map[string]struct{})
for _, disk := range dataDisks {
// validate that the disk size is between 4 and 32767.
if disk.DiskSizeGB < 4 || disk.DiskSizeGB > 32767 {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("DiskSizeGB"), "", "the disk size should be a value between 4 and 32767"))
}
// validate that all names are unique
if disk.NameSuffix == "" {
allErrs = append(allErrs, field.Required(fieldPath.Child("NameSuffix"), "the name suffix cannot be empty"))
}
if _, ok := nameSet[disk.NameSuffix]; ok {
allErrs = append(allErrs, field.Duplicate(fieldPath, disk.NameSuffix))
} else {
nameSet[disk.NameSuffix] = struct{}{}
}
// validate optional managed disk option
if disk.ManagedDisk != nil {
if errs := validateManagedDisk(disk.ManagedDisk, fieldPath.Child("managedDisk"), false); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
}
// validate that all LUNs are unique and between 0 and 63.
if disk.Lun == nil {
allErrs = append(allErrs, field.Required(fieldPath, "LUN should not be nil"))
} else if *disk.Lun < 0 || *disk.Lun > 63 {
allErrs = append(allErrs, field.Invalid(fieldPath, disk.Lun, "logical unit number must be between 0 and 63"))
} else if _, ok := lunSet[*disk.Lun]; ok {
allErrs = append(allErrs, field.Duplicate(fieldPath, disk.Lun))
} else {
lunSet[*disk.Lun] = struct{}{}
}
// validate cachingType
allErrs = append(allErrs, validateCachingType(disk.CachingType, fieldPath, disk.ManagedDisk)...)
}
return allErrs
}
// ValidateOSDisk validates the OSDisk spec.
func ValidateOSDisk(osDisk OSDisk, fieldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if osDisk.DiskSizeGB != nil {
if *osDisk.DiskSizeGB <= 0 || *osDisk.DiskSizeGB > 2048 {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("DiskSizeGB"), "", "the Disk size should be a value between 1 and 2048"))
}
}
if osDisk.OSType == "" {
allErrs = append(allErrs, field.Required(fieldPath.Child("OSType"), "the OS type cannot be empty"))
}
allErrs = append(allErrs, validateCachingType(osDisk.CachingType, fieldPath, osDisk.ManagedDisk)...)
if osDisk.ManagedDisk != nil {
if errs := validateManagedDisk(osDisk.ManagedDisk, fieldPath.Child("managedDisk"), true); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
}
if osDisk.DiffDiskSettings != nil && osDisk.ManagedDisk != nil && osDisk.ManagedDisk.DiskEncryptionSet != nil {
allErrs = append(allErrs, field.Invalid(
fieldPath.Child("managedDisks").Child("diskEncryptionSet"),
osDisk.ManagedDisk.DiskEncryptionSet.ID,
"diskEncryptionSet is not supported when diffDiskSettings.option is 'Local'",
))
}
return allErrs
}
// validateManagedDisk validates updates to the ManagedDiskParameters field.
func validateManagedDisk(m *ManagedDiskParameters, fieldPath *field.Path, isOSDisk bool) field.ErrorList {
allErrs := field.ErrorList{}
if m != nil {
allErrs = append(allErrs, validateStorageAccountType(m.StorageAccountType, fieldPath.Child("StorageAccountType"), isOSDisk)...)
}
return allErrs
}
// ValidateDataDisksUpdate validates updates to Data disks.
func ValidateDataDisksUpdate(oldDataDisks, newDataDisks []DataDisk, fieldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
diskErrMsg := "adding/removing data disks after machine creation is not allowed"
fieldErrMsg := "modifying data disk's fields after machine creation is not allowed"
if len(oldDataDisks) != len(newDataDisks) {
allErrs = append(allErrs, field.Invalid(fieldPath, newDataDisks, diskErrMsg))
return allErrs
}
oldDisks := make(map[string]DataDisk)
for _, disk := range oldDataDisks {
oldDisks[disk.NameSuffix] = disk
}
for i, newDisk := range newDataDisks {
if oldDisk, ok := oldDisks[newDisk.NameSuffix]; ok {
if newDisk.DiskSizeGB != oldDisk.DiskSizeGB {
allErrs = append(allErrs, field.Invalid(fieldPath.Index(i).Child("diskSizeGB"), newDataDisks, fieldErrMsg))
}
allErrs = append(allErrs, validateManagedDisksUpdate(oldDisk.ManagedDisk, newDisk.ManagedDisk, fieldPath.Index(i).Child("managedDisk"))...)
if (newDisk.Lun != nil && oldDisk.Lun != nil) && (*newDisk.Lun != *oldDisk.Lun) {
allErrs = append(allErrs, field.Invalid(fieldPath.Index(i).Child("lun"), newDataDisks, fieldErrMsg))
} else if (newDisk.Lun != nil && oldDisk.Lun == nil) || (newDisk.Lun == nil && oldDisk.Lun != nil) {
allErrs = append(allErrs, field.Invalid(fieldPath.Index(i).Child("lun"), newDataDisks, fieldErrMsg))
}
if newDisk.CachingType != oldDisk.CachingType {
allErrs = append(allErrs, field.Invalid(fieldPath.Index(i).Child("cachingType"), newDataDisks, fieldErrMsg))
}
} else {
allErrs = append(allErrs, field.Invalid(fieldPath.Index(i).Child("nameSuffix"), newDataDisks, diskErrMsg))
}
}
return allErrs
}
func validateManagedDisksUpdate(oldDiskParams, newDiskParams *ManagedDiskParameters, fieldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
fieldErrMsg := "changing managed disk options after machine creation is not allowed"
if newDiskParams != nil && oldDiskParams != nil {
if newDiskParams.StorageAccountType != oldDiskParams.StorageAccountType {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("storageAccountType"), newDiskParams, fieldErrMsg))
}
if newDiskParams.DiskEncryptionSet != nil && oldDiskParams.DiskEncryptionSet != nil {
if newDiskParams.DiskEncryptionSet.ID != oldDiskParams.DiskEncryptionSet.ID {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("diskEncryptionSet").Child("ID"), newDiskParams, fieldErrMsg))
}
} else if (newDiskParams.DiskEncryptionSet != nil && oldDiskParams.DiskEncryptionSet == nil) || (newDiskParams.DiskEncryptionSet == nil && oldDiskParams.DiskEncryptionSet != nil) {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("diskEncryptionSet"), newDiskParams, fieldErrMsg))
}
} else if (newDiskParams != nil && oldDiskParams == nil) || (newDiskParams == nil && oldDiskParams != nil) {
allErrs = append(allErrs, field.Invalid(fieldPath, newDiskParams, fieldErrMsg))
}
return allErrs
}
func validateStorageAccountType(storageAccountType string, fieldPath *field.Path, isOSDisk bool) field.ErrorList {
allErrs := field.ErrorList{}
if isOSDisk && storageAccountType == string(compute.StorageAccountTypesUltraSSDLRS) {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("managedDisks").Child("storageAccountType"), storageAccountType, "UltraSSD_LRS can only be used with data disks, it cannot be used with OS Disks"))
}
if storageAccountType == "" {
allErrs = append(allErrs, field.Required(fieldPath, "the Storage Account Type for Managed Disk cannot be empty"))
return allErrs
}
for _, possibleStorageAccountType := range compute.PossibleDiskStorageAccountTypesValues() {
if string(possibleStorageAccountType) == storageAccountType {
return allErrs
}
}
allErrs = append(allErrs, field.Invalid(fieldPath, "", fmt.Sprintf("allowed values are %v", compute.PossibleDiskStorageAccountTypesValues())))
return allErrs
}
func validateCachingType(cachingType string, fieldPath *field.Path, managedDisk *ManagedDiskParameters) field.ErrorList {
allErrs := field.ErrorList{}
cachingTypeChildPath := fieldPath.Child("CachingType")
if managedDisk != nil && managedDisk.StorageAccountType == string(compute.StorageAccountTypesUltraSSDLRS) {
if cachingType != string(compute.CachingTypesNone) {
allErrs = append(allErrs, field.Invalid(cachingTypeChildPath, cachingType, fmt.Sprintf("cachingType '%s' is not supported when storageAccountType is '%s'. Allowed values are: '%s'", cachingType, compute.StorageAccountTypesUltraSSDLRS, compute.CachingTypesNone)))
}
}
for _, possibleCachingType := range compute.PossibleCachingTypesValues() {
if string(possibleCachingType) == cachingType {
return allErrs
}
}
allErrs = append(allErrs, field.Invalid(cachingTypeChildPath, cachingType, fmt.Sprintf("allowed values are %v", compute.PossibleCachingTypesValues())))
return allErrs
}
// ValidateDiagnostics validates the Diagnostic spec.
func ValidateDiagnostics(diagnostics *Diagnostics, fieldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
if diagnostics != nil && diagnostics.Boot != nil {
switch diagnostics.Boot.StorageAccountType {
case UserManagedDiagnosticsStorage:
if diagnostics.Boot.UserManaged == nil {
allErrs = append(allErrs, field.Required(fieldPath.Child("UserManaged"),
fmt.Sprintf("userManaged must be specified when storageAccountType is '%s'", UserManagedDiagnosticsStorage)))
} else if diagnostics.Boot.UserManaged.StorageAccountURI == "" {
allErrs = append(allErrs, field.Required(fieldPath.Child("StorageAccountURI"),
fmt.Sprintf("StorageAccountURI cannot be empty when storageAccountType is '%s'", UserManagedDiagnosticsStorage)))
}
case ManagedDiagnosticsStorage:
if diagnostics.Boot.UserManaged != nil &&
diagnostics.Boot.UserManaged.StorageAccountURI != "" {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("StorageAccountURI"), diagnostics.Boot.UserManaged.StorageAccountURI,
fmt.Sprintf("StorageAccountURI cannot be set when storageAccountType is '%s'",
ManagedDiagnosticsStorage)))
}
case DisabledDiagnosticsStorage:
if diagnostics.Boot.UserManaged != nil &&
diagnostics.Boot.UserManaged.StorageAccountURI != "" {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("StorageAccountURI"), diagnostics.Boot.UserManaged.StorageAccountURI,
fmt.Sprintf("StorageAccountURI cannot be set when storageAccountType is '%s'",
ManagedDiagnosticsStorage)))
}
}
}
return allErrs
}