-
Notifications
You must be signed in to change notification settings - Fork 279
/
validator.go
692 lines (591 loc) Β· 22.6 KB
/
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package vsphere
import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"net"
"path/filepath"
"gopkg.in/yaml.v2"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/govmomi"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/types"
)
const (
vsphereRootPath = "/"
)
type PrivAssociation struct {
objectType string
privsContent string
path string
}
type missingPriv struct {
Username string `yaml:"username"`
ObjectType string `yaml:"objectType"`
Path string `yaml:"path"`
Permissions []string `yaml:"permissions"`
}
type VSphereClientBuilder interface {
Build(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (govmomi.VSphereClient, error)
}
type Validator struct {
govc ProviderGovcClient
vSphereClientBuilder VSphereClientBuilder
}
// NewValidator initializes the client for VSphere provider validations.
func NewValidator(govc ProviderGovcClient, vscb VSphereClientBuilder) *Validator {
return &Validator{
govc: govc,
vSphereClientBuilder: vscb,
}
}
func (v *Validator) validateVCenterAccess(ctx context.Context, server string) error {
if err := v.govc.ValidateVCenterConnection(ctx, server); err != nil {
return fmt.Errorf("failed validating connection to vCenter: %v", err)
}
logger.MarkPass("Connected to server")
if err := v.govc.ValidateVCenterAuthentication(ctx); err != nil {
return fmt.Errorf("failed validating credentials for vCenter: %v", err)
}
logger.MarkPass("Authenticated to vSphere")
return nil
}
func (v *Validator) ValidateVCenterConfig(ctx context.Context, datacenterConfig *anywherev1.VSphereDatacenterConfig) error {
if err := v.validateVCenterAccess(ctx, datacenterConfig.Spec.Server); err != nil {
return err
}
if err := v.validateThumbprint(ctx, datacenterConfig); err != nil {
return err
}
if err := v.validateDatacenter(ctx, datacenterConfig.Spec.Datacenter); err != nil {
return err
}
logger.MarkPass("Datacenter validated")
if err := v.validateNetwork(ctx, datacenterConfig.Spec.Network); err != nil {
return err
}
logger.MarkPass("Network validated")
return nil
}
func (v *Validator) validateMachineConfigTagsExist(ctx context.Context, machineConfigs []*anywherev1.VSphereMachineConfig) error {
tags, err := v.govc.ListTags(ctx)
if err != nil {
return fmt.Errorf("failed to check if tags exists in vSphere: %v", err)
}
tagIDs := make([]string, 0, len(tags))
for _, t := range tags {
tagIDs = append(tagIDs, t.Id)
}
idLookup := types.SliceToLookup(tagIDs)
for _, machineConfig := range machineConfigs {
for _, tagID := range machineConfig.Spec.TagIDs {
if !idLookup.IsPresent(tagID) {
return fmt.Errorf("tag (%s) does not exist in vSphere. please provide a valid tag id in the urn format (example: urn:vmomi:InventoryServiceTag:8e0ce079-0677-48d6-8865-19ada4e6dabd:GLOBAL)", tagID)
}
}
}
logger.MarkPass("Machine config tags validated")
return nil
}
// ValidateClusterMachineConfigs validates all the attributes of etcd, control plane, and worker node VSphereMachineConfigs.
func (v *Validator) ValidateClusterMachineConfigs(ctx context.Context, vsphereClusterSpec *Spec) error {
var etcdMachineConfig *anywherev1.VSphereMachineConfig
controlPlaneMachineConfig := vsphereClusterSpec.controlPlaneMachineConfig()
if controlPlaneMachineConfig == nil {
return fmt.Errorf("cannot find VSphereMachineConfig %v for control plane", vsphereClusterSpec.Cluster.Spec.ControlPlaneConfiguration.MachineGroupRef.Name)
}
for _, workerNodeGroupConfiguration := range vsphereClusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
workerNodeGroupMachineConfig := vsphereClusterSpec.workerMachineConfig(workerNodeGroupConfiguration)
if workerNodeGroupMachineConfig == nil {
return fmt.Errorf("cannot find VSphereMachineConfig %v for worker nodes", workerNodeGroupConfiguration.MachineGroupRef.Name)
}
}
if vsphereClusterSpec.Cluster.Spec.ExternalEtcdConfiguration != nil {
etcdMachineConfig = vsphereClusterSpec.etcdMachineConfig()
if etcdMachineConfig == nil {
return fmt.Errorf("cannot find VSphereMachineConfig %v for etcd machines", vsphereClusterSpec.Cluster.Spec.ExternalEtcdConfiguration.MachineGroupRef.Name)
}
if !v.sameOSFamily(vsphereClusterSpec.VSphereMachineConfigs) {
return errors.New("all VSphereMachineConfigs must have the same osFamily specified")
}
if !v.sameTemplate(vsphereClusterSpec.VSphereMachineConfigs) {
return errors.New("all VSphereMachineConfigs must have the same template specified")
}
if etcdMachineConfig.Spec.HostOSConfiguration != nil && etcdMachineConfig.Spec.HostOSConfiguration.BottlerocketConfiguration != nil && etcdMachineConfig.Spec.HostOSConfiguration.BottlerocketConfiguration.Kubernetes != nil {
logger.Info("Bottlerocket Kubernetes settings are not supported for etcd machines. Ignoring Kubernetes settings for etcd machines.", "etcdMachineConfig", etcdMachineConfig.Name)
}
}
// TODO: move this to api Cluster validations
if err := v.validateControlPlaneIp(vsphereClusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host); err != nil {
return err
}
for _, config := range vsphereClusterSpec.VSphereMachineConfigs {
var b bool // Temporary until we remove the need to pass a bool pointer
err := v.govc.ValidateVCenterSetupMachineConfig(ctx, vsphereClusterSpec.VSphereDatacenter, config, &b) // TODO: remove side effects from this implementation or directly move it to set defaults (pointer to bool is not needed)
if err != nil {
return fmt.Errorf("validating vCenter setup for VSphereMachineConfig %v: %v", config.Name, err)
}
}
if err := v.validateTemplate(ctx, vsphereClusterSpec, controlPlaneMachineConfig); err != nil {
logger.V(1).Info("Control plane template validation failed.")
return err
}
if err := v.validateMachineConfigTagsExist(ctx, vsphereClusterSpec.machineConfigs()); err != nil {
return err
}
logger.MarkPass("Control plane and Workload templates validated")
for _, mc := range vsphereClusterSpec.VSphereMachineConfigs {
if mc.OSFamily() == v1alpha1.Bottlerocket {
if err := v.validateBRHardDiskSize(ctx, vsphereClusterSpec, mc); err != nil {
return fmt.Errorf("failed validating BR Hard Disk size: %v", err)
}
}
}
return v.validateDatastoreUsage(ctx, vsphereClusterSpec, controlPlaneMachineConfig, etcdMachineConfig)
}
func (v *Validator) validateControlPlaneIp(ip string) error {
// check if controlPlaneEndpointIp is valid
parsedIp := net.ParseIP(ip)
if parsedIp == nil {
return fmt.Errorf("cluster controlPlaneConfiguration.Endpoint.Host is invalid: %s", ip)
}
return nil
}
func (v *Validator) validateTemplate(ctx context.Context, spec *Spec, machineConfig *anywherev1.VSphereMachineConfig) error {
if err := v.validateTemplatePresence(ctx, spec.VSphereDatacenter.Spec.Datacenter, machineConfig); err != nil {
return err
}
if err := v.validateTemplateTags(ctx, spec, machineConfig); err != nil {
return err
}
return nil
}
func (v *Validator) validateTemplatePresence(ctx context.Context, datacenter string, machineConfig *anywherev1.VSphereMachineConfig) error {
templateFullPath, err := v.govc.SearchTemplate(ctx, datacenter, machineConfig.Spec.Template)
if err != nil {
return fmt.Errorf("validating template: %v", err)
}
if len(templateFullPath) <= 0 {
return fmt.Errorf("template <%s> not found. Has the template been imported?", machineConfig.Spec.Template)
}
return nil
}
func (v *Validator) validateTemplateTags(ctx context.Context, spec *Spec, machineConfig *anywherev1.VSphereMachineConfig) error {
tags, err := v.govc.GetTags(ctx, machineConfig.Spec.Template)
if err != nil {
return fmt.Errorf("validating template tags: %v", err)
}
tagsLookup := types.SliceToLookup(tags)
for _, t := range requiredTemplateTags(spec.Spec, machineConfig) {
if !tagsLookup.IsPresent(t) {
// TODO: maybe add help text about to how to tag a template?
return fmt.Errorf("template %s is missing tag %s", machineConfig.Spec.Template, t)
}
}
return nil
}
func (v *Validator) validateBRHardDiskSize(ctx context.Context, spec *Spec, machineConfigSpec *anywherev1.VSphereMachineConfig) error {
dataCenter := spec.Config.VSphereDatacenter.Spec.Datacenter
template := machineConfigSpec.Spec.Template
hardDiskMap, err := v.govc.GetHardDiskSize(ctx, template, dataCenter)
if err != nil {
return fmt.Errorf("validating hard disk size: %v", err)
}
if len(hardDiskMap) == 0 {
return fmt.Errorf("no hard disks found for template: %v", template)
} else if len(hardDiskMap) > 1 {
if hardDiskMap[disk1] != 2097152 { // 2GB in KB to avoid roundoff errors
return fmt.Errorf("Incorrect disk size for disk1 - expected: 2097152 kB got: %v", hardDiskMap[disk1])
} else if hardDiskMap[disk2] != 20971520 { // 20GB in KB to avoid roundoff errors
return fmt.Errorf("Incorrect disk size for disk2 - expected: 20971520 kB got: %v", hardDiskMap[disk2])
}
} else if hardDiskMap[disk1] != 23068672 { // 22GB in KB to avoid roundoff errors
return fmt.Errorf("Incorrect disk size for disk1 - expected: 23068672 kB got: %v", hardDiskMap[disk1])
}
logger.V(5).Info("Bottlerocket Disk size validated: ", "diskMap", hardDiskMap)
return nil
}
type datastoreUsage struct {
availableSpace float64
needGiBSpace int
}
// TODO: cleanup this method signature
// TODO: dry out implementation.
func (v *Validator) validateDatastoreUsage(ctx context.Context, vsphereClusterSpec *Spec, controlPlaneMachineConfig *anywherev1.VSphereMachineConfig, etcdMachineConfig *anywherev1.VSphereMachineConfig) error {
usage := make(map[string]*datastoreUsage)
controlPlaneAvailableSpace, err := v.govc.GetWorkloadAvailableSpace(ctx, controlPlaneMachineConfig.Spec.Datastore) // TODO: remove dependency on machineConfig
if err != nil {
return fmt.Errorf("getting datastore details: %v", err)
}
controlPlaneNeedGiB := controlPlaneMachineConfig.Spec.DiskGiB * vsphereClusterSpec.Cluster.Spec.ControlPlaneConfiguration.Count
usage[controlPlaneMachineConfig.Spec.Datastore] = &datastoreUsage{
availableSpace: controlPlaneAvailableSpace,
needGiBSpace: controlPlaneNeedGiB,
}
for _, workerNodeGroupConfiguration := range vsphereClusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
workerMachineConfig := vsphereClusterSpec.workerMachineConfig(workerNodeGroupConfiguration)
workerAvailableSpace, err := v.govc.GetWorkloadAvailableSpace(ctx, workerMachineConfig.Spec.Datastore)
if err != nil {
return fmt.Errorf("getting datastore details: %v", err)
}
workerNeedGiB := workerMachineConfig.Spec.DiskGiB * *workerNodeGroupConfiguration.Count
_, ok := usage[workerMachineConfig.Spec.Datastore]
if ok {
usage[workerMachineConfig.Spec.Datastore].needGiBSpace += workerNeedGiB
} else {
usage[workerMachineConfig.Spec.Datastore] = &datastoreUsage{
availableSpace: workerAvailableSpace,
needGiBSpace: workerNeedGiB,
}
}
}
if etcdMachineConfig != nil {
etcdAvailableSpace, err := v.govc.GetWorkloadAvailableSpace(ctx, etcdMachineConfig.Spec.Datastore)
if err != nil {
return fmt.Errorf("getting datastore details: %v", err)
}
etcdNeedGiB := etcdMachineConfig.Spec.DiskGiB * vsphereClusterSpec.Cluster.Spec.ExternalEtcdConfiguration.Count
if _, ok := usage[etcdMachineConfig.Spec.Datastore]; ok {
usage[etcdMachineConfig.Spec.Datastore].needGiBSpace += etcdNeedGiB
} else {
usage[etcdMachineConfig.Spec.Datastore] = &datastoreUsage{
availableSpace: etcdAvailableSpace,
needGiBSpace: etcdNeedGiB,
}
}
}
for datastore, usage := range usage {
if float64(usage.needGiBSpace) > usage.availableSpace {
return fmt.Errorf("not enough space in datastore %v for given diskGiB and count for respective machine groups", datastore)
}
}
return nil
}
func (v *Validator) validateThumbprint(ctx context.Context, datacenterConfig *anywherev1.VSphereDatacenterConfig) error {
// No need to validate thumbprint in insecure mode
if datacenterConfig.Spec.Insecure {
return nil
}
// If cert is not self signed, thumbprint is ignored
if !v.govc.IsCertSelfSigned(ctx) {
return nil
}
if datacenterConfig.Spec.Thumbprint == "" {
return fmt.Errorf("thumbprint is required for secure mode with self-signed certificates")
}
thumbprint, err := v.govc.GetCertThumbprint(ctx)
if err != nil {
return err
}
if thumbprint != datacenterConfig.Spec.Thumbprint {
return fmt.Errorf("thumbprint mismatch detected, expected: %s, actual: %s", datacenterConfig.Spec.Thumbprint, thumbprint)
}
return nil
}
func (v *Validator) validateDatacenter(ctx context.Context, datacenter string) error {
exists, err := v.govc.DatacenterExists(ctx, datacenter)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("datacenter %s not found", datacenter)
}
return nil
}
func (v *Validator) validateNetwork(ctx context.Context, network string) error {
exists, err := v.govc.NetworkExists(ctx, network)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("network %s not found", network)
}
return nil
}
func (v *Validator) collectSpecMachineConfigs(ctx context.Context, spec *Spec) ([]*anywherev1.VSphereMachineConfig, error) {
controlPlaneMachineConfig := spec.controlPlaneMachineConfig()
machineConfigs := []*anywherev1.VSphereMachineConfig{controlPlaneMachineConfig}
for _, workerNodeGroupConfiguration := range spec.Cluster.Spec.WorkerNodeGroupConfigurations {
workerNodeGroupMachineConfig := spec.workerMachineConfig(workerNodeGroupConfiguration)
machineConfigs = append(machineConfigs, workerNodeGroupMachineConfig)
}
if spec.Cluster.Spec.ExternalEtcdConfiguration != nil {
etcdMachineConfig := spec.etcdMachineConfig()
machineConfigs = append(machineConfigs, etcdMachineConfig)
}
return machineConfigs, nil
}
func (v *Validator) validateUserPrivs(ctx context.Context, spec *Spec, vuc *config.VSphereUserConfig) (bool, error) {
machineConfigs, err := v.collectSpecMachineConfigs(ctx, spec)
if err != nil {
return false, err
}
requiredPrivAssociations := []PrivAssociation{
// validate global root priv settings are correct
{
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereGlobalPrivsFile,
path: vsphereRootPath,
},
{
objectType: govmomi.VSphereTypeNetwork,
privsContent: config.VSphereUserPrivsFile,
path: spec.VSphereDatacenter.Spec.Network,
},
}
seen := map[string]interface{}{}
for _, mc := range machineConfigs {
if _, ok := seen[mc.Spec.Datastore]; !ok {
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeDatastore,
privsContent: config.VSphereUserPrivsFile,
path: mc.Spec.Datastore,
},
)
seen[mc.Spec.Datastore] = 1
}
if _, ok := seen[mc.Spec.ResourcePool]; !ok {
// do something here
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeResourcePool,
privsContent: config.VSphereUserPrivsFile,
path: mc.Spec.ResourcePool,
})
seen[mc.Spec.ResourcePool] = 1
}
if _, ok := seen[mc.Spec.Folder]; !ok {
// validate Administrator role (all privs) on VM folder and Template folder
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereAdminPrivsFile,
path: mc.Spec.Folder,
})
seen[mc.Spec.Folder] = 1
}
if _, ok := seen[mc.Spec.Template]; !ok {
// ToDo: add more sophisticated validation around a scenario where someone has uploaded templates
// on their own and does not want to allow EKSA user write access to templates
// Verify privs on the template
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeVirtualMachine,
privsContent: config.VSphereAdminPrivsFile,
path: mc.Spec.Template,
})
seen[mc.Spec.Template] = 1
}
if _, ok := seen[filepath.Dir(mc.Spec.Template)]; !ok {
// Verify privs on the template directory
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereAdminPrivsFile,
path: filepath.Dir(mc.Spec.Template),
})
seen[filepath.Dir(mc.Spec.Template)] = 1
}
}
host := spec.VSphereDatacenter.Spec.Server
datacenter := spec.VSphereDatacenter.Spec.Datacenter
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereUsername,
vuc.EksaVspherePassword,
spec.VSphereDatacenter.Spec.Insecure,
datacenter,
)
if err != nil {
return false, err
}
return v.validatePrivs(ctx, requiredPrivAssociations, vsc)
}
func (v *Validator) validateCSIUserPrivs(ctx context.Context, spec *Spec, vuc *config.VSphereUserConfig) (bool, error) {
requiredPrivAssociations := []PrivAssociation{
{ // CNS-SEARCH-AND-SPBM role
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereCnsSearchAndSpbmPrivsFile,
path: vsphereRootPath,
},
}
machineConfigs, err := v.collectSpecMachineConfigs(ctx, spec)
if err != nil {
return false, err
}
var pas []PrivAssociation
seen := map[string]interface{}{}
for _, mc := range machineConfigs {
if _, ok := seen[mc.Spec.Datastore]; !ok {
requiredPrivAssociations = append(
requiredPrivAssociations,
// CNS-Datastore role
PrivAssociation{
objectType: govmomi.VSphereTypeDatastore,
privsContent: config.VSphereCnsDatastorePrivsFile,
path: mc.Spec.Datastore,
},
// CNS-HOST-CONFIG-STORAGE role
PrivAssociation{
objectType: govmomi.VSphereTypeDatastore,
privsContent: config.VSphereCnsHostConfigStorageFile,
path: mc.Spec.Datastore,
},
)
seen[mc.Spec.Datastore] = 1
}
if _, ok := seen[mc.Spec.Folder]; !ok {
// CNS-VM role
requiredPrivAssociations = append(requiredPrivAssociations, PrivAssociation{
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereCnsVmPrivsFile,
path: mc.Spec.Folder,
})
seen[mc.Spec.Folder] = 1
}
requiredPrivAssociations = append(requiredPrivAssociations, pas...)
}
host := spec.VSphereDatacenter.Spec.Server
datacenter := spec.VSphereDatacenter.Spec.Datacenter
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereCSIUsername,
vuc.EksaVsphereCSIPassword,
spec.VSphereDatacenter.Spec.Insecure,
datacenter,
)
if err != nil {
return false, err
}
return v.validatePrivs(ctx, requiredPrivAssociations, vsc)
}
func (v *Validator) validateCPUserPrivs(ctx context.Context, spec *Spec, vuc *config.VSphereUserConfig) (bool, error) {
// CP role just needs read only
privObjs := []PrivAssociation{
{
objectType: govmomi.VSphereTypeFolder,
privsContent: config.VSphereReadOnlyPrivs,
path: vsphereRootPath,
},
}
host := spec.VSphereDatacenter.Spec.Server
datacenter := spec.VSphereDatacenter.Spec.Datacenter
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereCPUsername,
vuc.EksaVsphereCPPassword,
spec.VSphereDatacenter.Spec.Insecure,
datacenter,
)
if err != nil {
return false, err
}
return v.validatePrivs(ctx, privObjs, vsc)
}
func (v *Validator) validatePrivs(ctx context.Context, privObjs []PrivAssociation, vsc govmomi.VSphereClient) (bool, error) {
var privs []string
var err error
missingPrivs := []missingPriv{}
passed := false
for _, obj := range privObjs {
path := obj.path
privsContent := obj.privsContent
t := obj.objectType
username := vsc.Username()
privs, err = v.getMissingPrivs(ctx, vsc, path, t, privsContent, username)
if err != nil {
return passed, err
} else if len(privs) > 0 {
mp := missingPriv{
Username: username,
ObjectType: t,
Path: path,
Permissions: privs,
}
missingPrivs = append(missingPrivs, mp)
content, err := yaml.Marshal(mp)
if err == nil {
s := fmt.Sprintf(" Warning: User %s missing %d vSphere permissions on %s, cluster creation may fail.\nRe-run create cluster with --verbosity=3 to see specific missing permissions.", username, len(privs), path)
logger.MarkWarning(s)
s = fmt.Sprintf("Missing Permissions:\n%s", string(content))
logger.V(3).Info(s)
} else {
s := fmt.Sprintf(" Warning: failed to list missing privs: %v", err)
logger.MarkWarning(s)
}
}
}
if len(missingPrivs) == 0 {
passed = true
}
return passed, nil
}
func checkRequiredPrivs(requiredPrivs []string, hasPrivs []string) []string {
hp := map[string]interface{}{}
for _, val := range hasPrivs {
hp[val] = 1
}
missingPrivs := []string{}
for _, p := range requiredPrivs {
if _, ok := hp[p]; !ok {
missingPrivs = append(missingPrivs, p)
}
}
return missingPrivs
}
func (v *Validator) getMissingPrivs(ctx context.Context, vsc govmomi.VSphereClient, path string, objType string, requiredPrivsContent string, username string) ([]string, error) {
var requiredPrivs []string
err := json.Unmarshal([]byte(requiredPrivsContent), &requiredPrivs)
if err != nil {
return nil, err
}
hasPrivs, err := vsc.GetPrivsOnEntity(ctx, path, objType, username)
if err != nil {
return nil, err
}
missingPrivs := checkRequiredPrivs(requiredPrivs, hasPrivs)
return missingPrivs, nil
}
func (v *Validator) sameOSFamily(configs map[string]*anywherev1.VSphereMachineConfig) bool {
c := getRandomMachineConfig(configs)
osFamily := c.Spec.OSFamily
for _, machineConfig := range configs {
if machineConfig.Spec.OSFamily != osFamily {
return false
}
}
return true
}
func (v *Validator) sameTemplate(configs map[string]*anywherev1.VSphereMachineConfig) bool {
c := getRandomMachineConfig(configs)
template := c.Spec.Template
for _, machineConfig := range configs {
if machineConfig.Spec.Template != template {
return false
}
}
return true
}
func getRandomMachineConfig(configs map[string]*anywherev1.VSphereMachineConfig) *anywherev1.VSphereMachineConfig {
var machineConfig *anywherev1.VSphereMachineConfig
for _, c := range configs {
machineConfig = c
break
}
return machineConfig
}
func (v *Validator) validateUpgradeRolloutStrategy(clusterSpec *cluster.Spec) error {
if clusterSpec.Cluster.Spec.ControlPlaneConfiguration.UpgradeRolloutStrategy != nil {
return fmt.Errorf("Upgrade rollout strategy customization is not supported for vSphere provider")
}
for _, workerNodeGroupConfiguration := range clusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
if workerNodeGroupConfiguration.UpgradeRolloutStrategy != nil {
return fmt.Errorf("Upgrade rollout strategy customization is not supported for vSphere provider")
}
}
return nil
}