-
Notifications
You must be signed in to change notification settings - Fork 124
/
distro_validator.go
539 lines (490 loc) · 19.3 KB
/
distro_validator.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package validator
import (
"context"
"fmt"
"strings"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/utility"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
const (
unauthorizedDistroCharacters = "|"
)
type distroValidator func(context.Context, *distro.Distro, *evergreen.Settings) ValidationErrors
// Functions used to validate the syntax of a distro object.
var distroSyntaxValidators = []distroValidator{
ensureHasNonZeroID,
ensureHasRequiredFields,
ensureValidSSHOptions,
ensureStaticHasAuthorizedKeysFile,
ensureValidExpansions,
ensureStaticHostsAreNotSpawnable,
ensureValidContainerPool,
ensureValidArch,
ensureValidBootstrapSettings,
ensureValidStaticBootstrapSettings,
ensureHasNoUnauthorizedCharacters,
ensureHasValidHostAllocatorSettings,
ensureHasValidPlannerSettings,
ensureHasValidFinderSettings,
ensureHasValidDispatcherSettings,
ensureHasValidVirtualWorkstationSettings,
}
// CheckDistro checks if the distro configuration syntax is valid. Returns
// a slice of any validation errors found.
func CheckDistro(ctx context.Context, d *distro.Distro, s *evergreen.Settings, newDistro bool) (ValidationErrors, error) {
validationErrs := ValidationErrors{}
var allDistroIDs, allDistroAliases []string
var err error
if newDistro || len(d.Aliases) > 0 {
allDistroIDs, allDistroAliases, err = getDistros(ctx)
if err != nil {
return nil, err
}
}
if newDistro {
validationErrs = append(validationErrs, ensureUniqueId(d, allDistroIDs)...)
}
validationErrs = append(validationErrs, validateAliases(d, allDistroAliases)...)
for _, v := range distroSyntaxValidators {
validationErrs = append(validationErrs, v(ctx, d, s)...)
}
return validationErrs, nil
}
// ensureStaticHostsAreNotSpawnable makes sure that any static distro cannot also be spawnable.
func ensureStaticHostsAreNotSpawnable(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if d.SpawnAllowed && d.Provider == evergreen.ProviderNameStatic {
return ValidationErrors{
{
Message: fmt.Sprintf("static distro %s cannot be spawnable", d.Id),
Level: Error,
},
}
}
return nil
}
// ensureHasRequiredFields check that the distro configuration has all the required fields
func ensureHasRequiredFields(ctx context.Context, d *distro.Distro, _ *evergreen.Settings) ValidationErrors {
errs := ValidationErrors{}
if d.Id == "" {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("distro '%v' cannot be blank", distro.IdKey),
Level: Error,
})
}
if d.Arch == "" {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("distro '%v' cannot be blank", distro.ArchKey),
Level: Error,
})
}
if d.User == "" {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("distro '%v' cannot be blank", distro.UserKey),
Level: Error,
})
}
if d.WorkDir == "" {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("distro '%v' cannot be blank", distro.WorkDirKey),
Level: Error,
})
}
if d.Provider == "" {
return append(errs, ValidationError{
Message: fmt.Sprintf("distro '%v' cannot be blank", distro.ProviderKey),
Level: Error,
})
}
if evergreen.IsEc2Provider(d.Provider) && len(d.ProviderSettingsList) > 1 {
return append(errs, validateMultipleProviderSettings(d)...)
} else if err := validateSingleProviderSettings(d); err != nil {
errs = append(errs, ValidationError{
Message: err.Error(),
Level: Error,
})
}
return errs
}
func validateMultipleProviderSettings(d *distro.Distro) ValidationErrors {
errs := ValidationErrors{}
definedRegions := map[string]bool{}
for _, doc := range d.ProviderSettingsList {
region, ok := doc.Lookup("region").StringValueOK()
if !ok {
region = evergreen.DefaultEC2Region
}
if definedRegions[region] {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("defined region %s more than once", region),
Level: Error,
})
continue
}
definedRegions[region] = true
bytes, err := doc.MarshalBSON()
if err != nil {
errs = append(errs, ValidationError{
Message: errors.Wrap(err, "error marshalling provider setting into bson").Error(),
Level: Error,
})
continue
}
settings := &cloud.EC2ProviderSettings{}
if err := bson.Unmarshal(bytes, settings); err != nil {
errs = append(errs, ValidationError{
Message: errors.Wrap(err, "error unmarshalling bson into provider settings").Error(),
Level: Error,
})
continue
}
if err := settings.Validate(); err != nil {
errs = append(errs, ValidationError{
Message: errors.Wrapf(err, "error validating settings for region '%s'", region).Error(),
Level: Error,
})
}
}
return errs
}
func validateSingleProviderSettings(d *distro.Distro) error {
settings, err := cloud.GetSettings(d.Provider)
if err != nil {
return errors.WithStack(err)
}
if err = settings.FromDistroSettings(*d, ""); err != nil {
return errors.Wrapf(err, "distro '%v' decode error", distro.ProviderSettingsListKey)
}
if err := settings.Validate(); err != nil {
return errors.Wrap(err, "error validating settings")
}
return nil
}
// ensureUniqueId checks that the distro's id does not collide with an existing id.
func ensureUniqueId(d *distro.Distro, distroIds []string) ValidationErrors {
if utility.StringSliceContains(distroIds, d.Id) {
return ValidationErrors{{Error, fmt.Sprintf("distro '%v' uses an existing identifier", d.Id)}}
}
return nil
}
func ensureValidAliases(d *distro.Distro) ValidationErrors {
var errs ValidationErrors
for _, a := range d.Aliases {
if d.Id == a {
errs = append(errs, ValidationError{
Level: Error,
Message: fmt.Sprintf("'%s' cannot be an distro alias of itself", a),
})
}
}
return errs
}
func ensureNoAliases(d *distro.Distro, distroAliases []string) ValidationErrors {
var errs ValidationErrors
if len(d.Aliases) != 0 {
errs = append(errs, ValidationError{
Level: Error,
Message: fmt.Sprintf("'%s' cannot have aliases", d.Id),
})
}
if utility.StringSliceContains(distroAliases, d.Id) {
errs = append(errs, ValidationError{
Level: Error,
Message: fmt.Sprintf("cannot have alias that resolves to '%s'", d.Id),
})
}
return errs
}
// ensureValidExpansions checks that no expansion option key is blank.
func ensureValidExpansions(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
for _, e := range d.Expansions {
if e.Key == "" {
return ValidationErrors{{Error, "distro cannot be blank expansion key"}}
}
}
return nil
}
// ensureValidSSHOptions checks that no SSH option key is blank.
func ensureValidSSHOptions(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
for _, o := range d.SSHOptions {
if o == "" {
return ValidationErrors{{Error, "distro cannot be blank SSH option"}}
}
}
return nil
}
// ensureStaticHasAuthorizedKeysFile checks that the SSH key name corresponds to an actual
// SSH key.
func ensureStaticHasAuthorizedKeysFile(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if len(s.SSHKeyPairs) != 0 && d.Provider == evergreen.ProviderNameStatic && d.AuthorizedKeysFile == "" {
return ValidationErrors{
{
Message: "authorized keys file was not specified",
Level: Error,
},
}
}
return nil
}
// ensureValidArch checks that the architecture is one of the supported
// architectures.
func ensureValidArch(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if err := distro.ValidateArch(d.Arch); err != nil {
return ValidationErrors{{Level: Error, Message: errors.Wrap(err, "error validating arch").Error()}}
}
return nil
}
// ensureValidBootstrapSettings checks that the bootstrap method
// is one of the supported methods, the communication method is one of the
// supported methods, and the two together form a valid combination.
func ensureValidBootstrapSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if err := d.ValidateBootstrapSettings(); err != nil {
return ValidationErrors{{Level: Error, Message: err.Error()}}
}
return nil
}
// ensureValidBootstrapSettingsForStaticDistro checks that static hosts are
// bootstrapped with one of the allowed methods.
func ensureValidStaticBootstrapSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if d.Provider == evergreen.ProviderNameStatic && d.BootstrapSettings.Method == distro.BootstrapMethodUserData {
return ValidationErrors{
{
Message: fmt.Sprintf("static distro %s cannot be bootstrapped with user data", d.Id),
Level: Error,
},
}
}
return nil
}
func ensureHasNonZeroID(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if d == nil {
return ValidationErrors{{Error, "distro cannot be nil"}}
}
if d.Id == "" {
return ValidationErrors{{Error, "distro must specify id"}}
}
return nil
}
// ensureHasNoUnauthorizedCharacters checks that the distro name does not contain any unauthorized character.
func ensureHasNoUnauthorizedCharacters(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if strings.ContainsAny(d.Id, unauthorizedDistroCharacters) {
message := fmt.Sprintf("distro '%v' contains unauthorized characters (%v)", d.Id, unauthorizedDistroCharacters)
return ValidationErrors{{Error, message}}
}
return nil
}
// ensureValidContainerPool checks that a distro's container pool exists and
// has a valid distro capable of hosting containers
func ensureValidContainerPool(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if d.ContainerPool != "" {
// check if container pool exists
pool := s.ContainerPools.GetContainerPool(d.ContainerPool)
if pool == nil {
return ValidationErrors{{Error, "distro container pool does not exist"}}
}
// warn if container pool exists without valid distro
err := distro.ValidateContainerPoolDistros(ctx, s)
if err != nil {
return ValidationErrors{{Error, "error in container pool settings: " + err.Error()}}
}
}
return nil
}
// ensureHasValidHostAllocatorSettings checks that the distro's HostAllocatorSettings are valid
func ensureHasValidHostAllocatorSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
errs := ValidationErrors{}
settings := d.HostAllocatorSettings
if !utility.StringSliceContains(evergreen.ValidHostAllocators, settings.Version) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.version '%s' for distro '%s'", settings.Version, d.Id),
Level: Error,
})
}
if settings.MinimumHosts < 0 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.minimum_hosts value of %d for distro '%s' - its value must be a non-negative integer", settings.MinimumHosts, d.Id),
Level: Error,
})
}
if settings.MaximumHosts < 0 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.maximum_hosts value of %d for distro '%s' - its value must be a non-negative integer", settings.MaximumHosts, d.Id),
Level: Error,
})
}
if !utility.StringSliceContains(evergreen.ValidHostAllocatorRoundingRules, settings.RoundingRule) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.rounding_rule value of %s for distro '%s' - its value must be %s", settings.RoundingRule, d.Id, evergreen.ValidHostAllocatorRoundingRules),
Level: Error,
})
}
if !utility.StringSliceContains(evergreen.ValidHostAllocatorFeedbackRules, settings.FeedbackRule) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.feedback_rule value of %s for distro '%s' - its value must be %s", settings.FeedbackRule, d.Id, evergreen.ValidHostAllocatorFeedbackRules),
Level: Error,
})
}
if !utility.StringSliceContains(evergreen.ValidHostsOverallocatedRules, settings.HostsOverallocatedRule) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.hosts_overallocated_rule value of %s for distro '%s' - its value must be %s", settings.FeedbackRule, d.Id, evergreen.ValidHostsOverallocatedRules),
Level: Error,
})
}
if settings.AcceptableHostIdleTime < 0 {
ms := settings.AcceptableHostIdleTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.acceptable_host_idle_time value of %dms for distro '%s' - its value must be a non-negative integer", ms, d.Id),
Level: Error,
})
} else if settings.AcceptableHostIdleTime != 0 && (settings.AcceptableHostIdleTime < time.Second) {
ms := settings.AcceptableHostIdleTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.acceptable_host_idle_time value of %dms for distro '%s' - its millisecond value must convert directly to units of seconds", ms, d.Id),
Level: Error,
})
} else if settings.AcceptableHostIdleTime%time.Second != 0 {
ms := settings.AcceptableHostIdleTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.acceptable_host_idle_time value of %dms for distro '%s' - its millisecond value must convert directly to units of seconds", ms, d.Id),
Level: Error,
})
}
if settings.FutureHostFraction < 0 || settings.FutureHostFraction > 1 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid host_allocator_settings.future_host_fraction value of %f for distro '%s' - its value must be a fraction between 0 and 1, inclusive", settings.FutureHostFraction, d.Id),
Level: Error,
})
}
return errs
}
// ensureHasValidPlannerSettings checks that the distro's PlannerSettings are valid
func ensureHasValidPlannerSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
errs := ValidationErrors{}
settings := d.PlannerSettings
if !utility.StringSliceContains(evergreen.ValidTaskPlannerVersions, settings.Version) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.version '%s' for distro '%s'", settings.Version, d.Id),
Level: Error,
})
}
if settings.TargetTime < 0 {
ms := settings.TargetTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.target_time value of %dms for distro '%s' - its value must be a non-negative integer", ms, d.Id),
Level: Error,
})
} else if settings.TargetTime != 0 && (settings.TargetTime < time.Second) {
ms := settings.TargetTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.target_time value of %dms for distro '%s' - its millisecond value must convert directly to units of seconds", ms, d.Id),
Level: Error,
})
} else if settings.TargetTime%time.Second != 0 {
ms := settings.TargetTime / time.Millisecond
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.target_time value of %dms for distro '%s' - its value must convert directly to units of seconds", ms, d.Id),
Level: Error,
})
}
if settings.PatchFactor < 0 || settings.PatchFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.patch_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.PatchFactor, d.Id),
Level: Error,
})
}
if settings.PatchTimeInQueueFactor < 0 || settings.PatchTimeInQueueFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.patch_time_in_queue_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.PatchTimeInQueueFactor, d.Id),
Level: Error,
})
}
if settings.CommitQueueFactor < 0 || settings.CommitQueueFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.commit_queue_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.CommitQueueFactor, d.Id),
Level: Error,
})
}
if settings.MainlineTimeInQueueFactor < 0 || settings.MainlineTimeInQueueFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.mainline_time_in_queue_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.MainlineTimeInQueueFactor, d.Id),
Level: Error,
})
}
if settings.ExpectedRuntimeFactor < 0 || settings.ExpectedRuntimeFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.expected_runtime_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.ExpectedRuntimeFactor, d.Id),
Level: Error,
})
}
if settings.GenerateTaskFactor < 0 || settings.GenerateTaskFactor > 100 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("invalid planner_settings.generate_task_factor value of %d for distro '%s' - its value must be a non-negative integer between 0 and 100, inclusive", settings.GenerateTaskFactor, d.Id),
Level: Error,
})
}
return errs
}
// ensureHasValidFinderSettings checks that the distro's FinderSettings are valid
func ensureHasValidFinderSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if !utility.StringSliceContains(evergreen.ValidTaskFinderVersions, d.FinderSettings.Version) {
return ValidationErrors{
{
Message: fmt.Sprintf("invalid finder_settings.version '%s' for distro '%s'", d.FinderSettings.Version, d.Id),
Level: Error,
},
}
}
return nil
}
// ensureHasValidDispatcherSettings checks that the distro's DispatcherSettings are valid
func ensureHasValidDispatcherSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if !utility.StringSliceContains(evergreen.ValidTaskDispatcherVersions, d.DispatcherSettings.Version) {
return ValidationErrors{
{
Message: fmt.Sprintf("invalid dispatcher_settings.version '%s' for distro '%s'", d.DispatcherSettings.Version, d.Id),
Level: Error,
},
}
}
return nil
}
func ensureHasValidVirtualWorkstationSettings(ctx context.Context, d *distro.Distro, s *evergreen.Settings) ValidationErrors {
if !d.IsVirtualWorkstation {
return nil
}
var errs ValidationErrors
if d.HomeVolumeSettings.FormatCommand == "" {
errs = append(errs, ValidationError{
Message: "missing format command",
Level: Error,
})
}
linuxArchs := []string{
evergreen.ArchLinuxPpc64le,
evergreen.ArchLinuxS390x,
evergreen.ArchLinuxArm64,
evergreen.ArchLinuxAmd64,
}
if !utility.StringSliceContains(linuxArchs, d.Arch) {
errs = append(errs, ValidationError{
Message: "workstation distros must use Linux with a supported CPU architecture",
Level: Error,
})
}
return errs
}
func validateAliases(d *distro.Distro, allDistroAliases []string) ValidationErrors {
var validationErrs ValidationErrors
// Parent and container distros do not support aliases.
if d.ContainerPool != "" || d.Provider == evergreen.ProviderNameDocker {
validationErrs = append(validationErrs, ensureNoAliases(d, allDistroAliases)...)
}
if len(d.Aliases) > 0 {
validationErrs = append(validationErrs, ensureValidAliases(d)...)
}
return validationErrs
}