-
Notifications
You must be signed in to change notification settings - Fork 273
/
govc.go
1145 lines (973 loc) Β· 33.3 KB
/
govc.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package executables
import (
"bufio"
"bytes"
"context"
"crypto/tls"
_ "embed"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"sigs.k8s.io/yaml"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
)
const (
govcPath = "govc"
govcUsernameKey = "GOVC_USERNAME"
govcPasswordKey = "GOVC_PASSWORD"
govcURLKey = "GOVC_URL"
govcInsecure = "GOVC_INSECURE"
govcDatacenterKey = "GOVC_DATACENTER"
govcTlsHostsFile = "govc_known_hosts"
govcTlsKnownHostsKey = "GOVC_TLS_KNOWN_HOSTS"
vSphereServerKey = "VSPHERE_SERVER"
byteToGiB = 1073741824.0
DeployOptsFile = "deploy-opts.json"
disk1 = "Hard disk 1"
disk2 = "Hard disk 2"
)
var requiredEnvs = []string{govcUsernameKey, govcPasswordKey, govcURLKey, govcInsecure, govcDatacenterKey}
type networkMapping struct {
Name string `json:"Name,omitempty"`
Network string `json:"Network,omitempty"`
}
type deployOption struct {
DiskProvisioning string `json:"DiskProvisioning,omitempty"`
NetworkMapping []networkMapping `json:"NetworkMapping,omitempty"`
}
type FolderType string
const (
datastore FolderType = "datastore"
vm FolderType = "vm"
maxRetries = 5
backOffPeriod = 5 * time.Second
)
type Govc struct {
writer filewriter.FileWriter
Executable
*retrier.Retrier
requiredEnvs *syncSlice
envMap map[string]string
}
type GovcOpt func(*Govc)
func NewGovc(executable Executable, writer filewriter.FileWriter, opts ...GovcOpt) *Govc {
envVars := newSyncSlice()
envVars.append(requiredEnvs...)
g := &Govc{
writer: writer,
Executable: executable,
Retrier: retrier.NewWithMaxRetries(maxRetries, backOffPeriod),
requiredEnvs: envVars,
}
for _, opt := range opts {
opt(g)
}
return g
}
func WithGovcEnvMap(envMap map[string]string) GovcOpt {
return func(g *Govc) {
g.envMap = envMap
}
}
func (g *Govc) exec(ctx context.Context, args ...string) (stdout bytes.Buffer, err error) {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return bytes.Buffer{}, fmt.Errorf("failed govc validations: %v", err)
}
return g.ExecuteWithEnv(ctx, envMap, args...)
}
func (g *Govc) Close(ctx context.Context) error {
if g == nil {
return nil
}
return g.Logout(ctx)
}
func (g *Govc) Logout(ctx context.Context) error {
logger.V(3).Info("Logging out from current govc session")
if _, err := g.exec(ctx, "session.logout"); err != nil {
return fmt.Errorf("govc returned error when logging out: %v", err)
}
// Commands that skip cert verification will have a different session.
// So we try to destroy it as well here to avoid leaving it orphaned
if _, err := g.exec(ctx, "session.logout", "-k"); err != nil {
return fmt.Errorf("govc returned error when logging out from session without cert verification: %v", err)
}
return nil
}
// SearchTemplate looks for a vm template with the same base name as the provided template path.
// If found, it returns the full qualified path to the template.
// If multiple matching templates are found, it returns an error.
func (g *Govc) SearchTemplate(ctx context.Context, datacenter, template string) (string, error) {
params := []string{"find", "-json", "/" + datacenter, "-type", "VirtualMachine", "-name", filepath.Base(template)}
var templateResponse bytes.Buffer
var err error
err = g.Retry(func() error {
templateResponse, err = g.exec(ctx, params...)
return err
})
if err != nil {
return "", fmt.Errorf("getting template: %v", err)
}
templateJson := templateResponse.String()
templateJson = strings.TrimSuffix(templateJson, "\n")
if templateJson == "null" || templateJson == "" {
logger.V(2).Info(fmt.Sprintf("Template not found: %s", template))
return "", nil
}
templateInfo := make([]string, 0)
if err = json.Unmarshal([]byte(templateJson), &templateInfo); err != nil {
logger.V(2).Info(fmt.Sprintf("Failed unmarshalling govc response: %s, %v", templateJson, err))
return "", nil
}
bTemplateFound := false
var foundTemplate string
for _, t := range templateInfo {
if strings.HasSuffix(t, template) {
if bTemplateFound {
return "", fmt.Errorf("specified template '%s' maps to multiple paths within the datacenter '%s'", template, datacenter)
}
bTemplateFound = true
foundTemplate = t
}
}
if !bTemplateFound {
logger.V(2).Info(fmt.Sprintf("Template '%s' not found", template))
return "", nil
}
return foundTemplate, nil
}
func (g *Govc) LibraryElementExists(ctx context.Context, library string) (bool, error) {
response, err := g.exec(ctx, "library.ls", library)
if err != nil {
return false, fmt.Errorf("govc failed getting library to check if it exists: %v", err)
}
return response.Len() > 0, nil
}
type libElement struct {
ContentVersion string `json:"content_version"`
}
func (g *Govc) GetLibraryElementContentVersion(ctx context.Context, element string) (string, error) {
response, err := g.exec(ctx, "library.info", "-json", element)
if err != nil {
return "", fmt.Errorf("govc failed getting library element info: %v", err)
}
elementInfoJson := response.String()
elementInfoJson = strings.TrimSuffix(elementInfoJson, "\n")
if elementInfoJson == "null" {
return "-1", nil
}
elementInfo := make([]libElement, 0)
err = yaml.Unmarshal([]byte(elementInfoJson), &elementInfo)
if err != nil {
return "", fmt.Errorf("unmarshalling library element info: %v", err)
}
if len(elementInfo) == 0 {
return "", fmt.Errorf("govc failed to return element info for library element %v", element)
}
return elementInfo[0].ContentVersion, nil
}
func (g *Govc) DeleteLibraryElement(ctx context.Context, element string) error {
_, err := g.exec(ctx, "library.rm", element)
if err != nil {
return fmt.Errorf("govc failed deleting library item: %v", err)
}
return nil
}
func (g *Govc) ResizeDisk(ctx context.Context, datacenter, template, diskName string, diskSizeInGB int) error {
_, err := g.exec(ctx, "vm.disk.change", "-dc", datacenter, "-vm", template, "-disk.name", diskName, "-size", strconv.Itoa(diskSizeInGB)+"G")
if err != nil {
return fmt.Errorf("failed to resize disk %s: %v", diskName, err)
}
return nil
}
type deviceInfoResponse struct {
Devices []VirtualDevice
}
// VirtualDevice describes a virtual device for a VM.
type VirtualDevice struct {
Name string
DeviceInfo deviceInfo
CapacityInKB float64
}
type deviceInfo struct {
Label string
}
// DevicesInfo returns the device info for te provided virtual machine.
func (g *Govc) DevicesInfo(ctx context.Context, datacenter, template string, args ...string) ([]VirtualDevice, error) {
params := []string{"device.info", "-dc", datacenter, "-vm", template, "-json"}
params = append(params, args...)
response, err := g.exec(ctx, params...)
if err != nil {
return nil, fmt.Errorf("getting template device information: %v", err)
}
var devicesInfo deviceInfoResponse
err = yaml.Unmarshal(response.Bytes(), &devicesInfo)
if err != nil {
return nil, fmt.Errorf("unmarshalling devices info: %v", err)
}
return devicesInfo.Devices, nil
}
// GetVMDiskSizeInGB returns the size of the first disk on the VM in GB.
func (g *Govc) GetVMDiskSizeInGB(ctx context.Context, vm, datacenter string) (int, error) {
devicesInfo, err := g.DevicesInfo(ctx, datacenter, vm, "disk-*")
if err != nil {
return 0, fmt.Errorf("getting disk size for vm %s: %v", vm, err)
}
if len(devicesInfo) == 0 {
return 0, fmt.Errorf("no disks found for vm %s", vm)
}
return int(devicesInfo[0].CapacityInKB / 1024 / 1024), nil
}
// GetHardDiskSize returns the size of all the hard disks for given VM.
func (g *Govc) GetHardDiskSize(ctx context.Context, vm, datacenter string) (map[string]float64, error) {
devicesInfo, err := g.DevicesInfo(ctx, datacenter, vm, "disk-*")
if err != nil {
return nil, fmt.Errorf("getting hard disk sizes for vm %s: %v", vm, err)
}
if len(devicesInfo) == 0 {
return nil, fmt.Errorf("no hard disks found for vm %s", vm)
}
hardDiskMap := make(map[string]float64)
for _, deviceInfo := range devicesInfo {
if strings.EqualFold(deviceInfo.DeviceInfo.Label, disk1) {
hardDiskMap[disk1] = deviceInfo.CapacityInKB
} else if strings.EqualFold(deviceInfo.DeviceInfo.Label, disk2) {
hardDiskMap[disk2] = deviceInfo.CapacityInKB
}
}
return hardDiskMap, nil
}
func (g *Govc) TemplateHasSnapshot(ctx context.Context, template string) (bool, error) {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return false, fmt.Errorf("failed govc validations: %v", err)
}
params := []string{"snapshot.tree", "-vm", template}
snap, err := g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
return false, fmt.Errorf("failed to get snapshot details: %v", err)
}
if snap.String() == "" {
return false, nil
}
return true, nil
}
type datastoreResponse struct {
Datastores []types.Datastores `json:"Datastores"`
}
func (g *Govc) GetWorkloadAvailableSpace(ctx context.Context, datastore string) (float64, error) {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return 0, fmt.Errorf("failed govc validations: %v", err)
}
params := []string{"datastore.info", "-json=true", datastore}
result, err := g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
return 0, fmt.Errorf("getting datastore info: %v", err)
}
response := &datastoreResponse{}
err = json.Unmarshal(result.Bytes(), response)
if err != nil {
return -1, nil
}
if len(response.Datastores) > 0 {
freeSpace := response.Datastores[0].Info.FreeSpace
spaceGiB := freeSpace / byteToGiB
return spaceGiB, nil
}
return 0, fmt.Errorf("getting datastore available space response: %v", err)
}
func (g *Govc) CreateLibrary(ctx context.Context, datastore, library string) error {
if _, err := g.exec(ctx, "library.create", "-ds", datastore, library); err != nil {
return fmt.Errorf("creating library %s: %v", library, err)
}
return nil
}
func (g *Govc) DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, network, resourcePool string, resizeBRDisk bool) error {
logger.V(4).Info("Deploying template", "dir", templateDir, "templateName", templateName)
if err := g.DeployTemplate(ctx, library, templateName, templateName, templateDir, datacenter, datastore, network, resourcePool, nil); err != nil {
return err
}
if resizeBRDisk {
// Get devices information template to identify second disk properly
logger.V(4).Info("Getting devices info for template")
devicesInfo, err := g.DevicesInfo(ctx, datacenter, templateName)
if err != nil {
return err
}
// For 1.22 we switched to using one disk for BR, so it for now as long as the boolean is set, and we only see
// one disk, we can assume this is for 1.22. This loop would need to change if that assumption changes
// in the future, but 1.20 and 1.21 are still using dual disks which is why we need to check for the second
// disk first. Since this loop will get all kinds of devices and not just hard disks, we need to do these
// checks based on the label.
disk1 := ""
disk2 := ""
for _, device := range devicesInfo {
deviceLabel := device.DeviceInfo.Label
// Get the name of the hard disk and resize the disk to 20G
if strings.EqualFold(deviceLabel, "Hard disk 1") {
disk1 = device.Name
} else if strings.EqualFold(deviceLabel, "Hard disk 2") {
disk2 = device.Name
break
}
}
diskName := ""
var diskSizeInGB int
if disk2 != "" {
logger.V(4).Info("Resizing disk 2 of template to 20G")
diskName = disk2
diskSizeInGB = 20
} else if disk1 != "" {
logger.V(4).Info("Resizing disk 1 of template to 22G")
diskName = disk1
diskSizeInGB = 22
} else {
return fmt.Errorf("template %v is not valid as there are no associated disks", templateName)
}
err = g.ResizeDisk(ctx, datacenter, templateName, diskName, diskSizeInGB)
if err != nil {
return fmt.Errorf("resizing disk %v to %dG: %v", diskName, diskSizeInGB, err)
}
}
templateFullPath := filepath.Join(templateDir, templateName)
logger.V(4).Info("Taking template snapshot", "templateName", templateFullPath)
if err := g.createVMSnapshot(ctx, datacenter, templateFullPath); err != nil {
return err
}
logger.V(4).Info("Marking vm as template", "templateName", templateFullPath)
if err := g.markVMAsTemplate(ctx, datacenter, templateFullPath); err != nil {
return err
}
return nil
}
func (g *Govc) ImportTemplate(ctx context.Context, library, ovaURL, name string) error {
logger.V(4).Info("Importing template", "ova", ovaURL, "templateName", name)
if _, err := g.exec(ctx, "library.import", "-k", "-pull", "-n", name, library, ovaURL); err != nil {
return fmt.Errorf("importing template: %v", err)
}
return nil
}
func (g *Govc) DeployTemplate(ctx context.Context, library, templateName, vmName, deployFolder, datacenter, datastore, network, resourcePool string, deployOptionsOverride []byte) error {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return fmt.Errorf("failed govc validations: %v", err)
}
templateInLibraryPath := filepath.Join(library, templateName)
if !filepath.IsAbs(templateInLibraryPath) {
templateInLibraryPath = fmt.Sprintf("/%s", templateInLibraryPath)
}
deployOpts, err := getDeployOptions(network)
if err != nil {
return err
}
if len(deployOptionsOverride) > 0 {
deployOpts = deployOptionsOverride
}
deployOptsPath, err := g.writer.Write(DeployOptsFile, deployOpts, filewriter.PersistentFile)
if err != nil {
return fmt.Errorf("failed writing deploy options file to disk: %v", err)
}
bFolderNotFound := false
params := []string{"folder.info", deployFolder}
err = g.Retry(func() error {
errBuffer, err := g.ExecuteWithEnv(ctx, envMap, params...)
errString := strings.ToLower(errBuffer.String())
if err != nil {
if !strings.Contains(errString, "not found") {
return fmt.Errorf("obtaining folder information: %v", err)
} else {
bFolderNotFound = true
}
}
return nil
})
if err != nil || bFolderNotFound {
params = []string{"folder.create", deployFolder}
err = g.Retry(func() error {
errBuffer, err := g.ExecuteWithEnv(ctx, envMap, params...)
errString := strings.ToLower(errBuffer.String())
if err != nil && !strings.Contains(errString, "already exists") {
return fmt.Errorf("creating folder: %v", err)
}
return nil
})
if err != nil {
return fmt.Errorf("creating folder: %v", err)
}
}
params = []string{
"library.deploy",
"-dc", datacenter,
"-ds", datastore,
"-pool", resourcePool,
"-folder", deployFolder,
"-options", deployOptsPath,
templateInLibraryPath, vmName,
}
if _, err := g.exec(ctx, params...); err != nil {
return fmt.Errorf("deploying template: %v", err)
}
return nil
}
func (g *Govc) DeleteTemplate(ctx context.Context, resourcePool, templatePath string) error {
if err := g.markAsVM(ctx, resourcePool, templatePath); err != nil {
return err
}
if err := g.removeSnapshotsFromVM(ctx, templatePath); err != nil {
return err
}
if err := g.deleteVM(ctx, templatePath); err != nil {
return err
}
return nil
}
func (g *Govc) markAsVM(ctx context.Context, resourcePool, path string) error {
if _, err := g.exec(ctx, "vm.markasvm", "-pool", resourcePool, path); err != nil {
return fmt.Errorf("failed marking as vm: %v", err)
}
return nil
}
func (g *Govc) removeSnapshotsFromVM(ctx context.Context, path string) error {
if _, err := g.exec(ctx, "snapshot.remove", "-vm", path, "*"); err != nil {
return fmt.Errorf("removing snapshots from vm: %v", err)
}
return nil
}
func (g *Govc) deleteVM(ctx context.Context, path string) error {
if _, err := g.exec(ctx, "vm.destroy", path); err != nil {
return fmt.Errorf("deleting vm: %v", err)
}
return nil
}
func (g *Govc) createVMSnapshot(ctx context.Context, datacenter, name string) error {
if _, err := g.exec(ctx, "snapshot.create", "-dc", datacenter, "-m=false", "-vm", name, "root"); err != nil {
return fmt.Errorf("govc failed taking vm snapshot: %v", err)
}
return nil
}
func (g *Govc) markVMAsTemplate(ctx context.Context, datacenter, vmName string) error {
if _, err := g.exec(ctx, "vm.markastemplate", "-dc", datacenter, vmName); err != nil {
return fmt.Errorf("marking VM as template: %v", err)
}
return nil
}
func (g *Govc) getEnvMap() (map[string]string, error) {
if g.envMap != nil {
return g.envMap, nil
}
envMap := make(map[string]string)
for key := range g.requiredEnvs.iterate() {
if env, ok := os.LookupEnv(key); ok && len(env) > 0 {
envMap[key] = env
} else {
if key != govcInsecure {
return nil, fmt.Errorf("warning required env not set %s", key)
}
err := os.Setenv(govcInsecure, "false")
if err != nil {
logger.Info("Warning: Unable to set <%s>", govcInsecure)
}
}
}
return envMap, nil
}
func (g *Govc) validateAndSetupCreds() (map[string]string, error) {
if g.envMap != nil {
return g.envMap, nil
}
var vSphereUsername, vSpherePassword, vSphereURL string
var ok bool
var envMap map[string]string
if vSphereUsername, ok = os.LookupEnv(config.EksavSphereUsernameKey); ok && len(vSphereUsername) > 0 {
if err := os.Setenv(govcUsernameKey, vSphereUsername); err != nil {
return nil, fmt.Errorf("unable to set %s: %v", govcUsernameKey, err)
}
} else if govcUsername, ok := os.LookupEnv(govcUsernameKey); !ok || len(govcUsername) <= 0 {
return nil, fmt.Errorf("%s is not set or is empty: %t", govcUsernameKey, ok)
}
if vSpherePassword, ok = os.LookupEnv(config.EksavSpherePasswordKey); ok && len(vSpherePassword) > 0 {
if err := os.Setenv(govcPasswordKey, vSpherePassword); err != nil {
return nil, fmt.Errorf("unable to set %s: %v", govcPasswordKey, err)
}
} else if govcPassword, ok := os.LookupEnv(govcPasswordKey); !ok || len(govcPassword) <= 0 {
return nil, fmt.Errorf("%s is not set or is empty: %t", govcPasswordKey, ok)
}
if vSphereURL, ok = os.LookupEnv(vSphereServerKey); ok && len(vSphereURL) > 0 {
if err := os.Setenv(govcURLKey, vSphereURL); err != nil {
return nil, fmt.Errorf("unable to set %s: %v", govcURLKey, err)
}
} else if govcURL, ok := os.LookupEnv(govcURLKey); !ok || len(govcURL) <= 0 {
return nil, fmt.Errorf("%s is not set or is empty: %t", govcURLKey, ok)
}
if govcDatacenter, ok := os.LookupEnv(govcDatacenterKey); !ok || len(govcDatacenter) <= 0 {
return nil, fmt.Errorf("%s is not set or is empty: %t", govcDatacenterKey, ok)
}
envMap, err := g.getEnvMap()
if err != nil {
return nil, fmt.Errorf("%v", err)
}
return envMap, nil
}
func (g *Govc) CleanupVms(ctx context.Context, clusterName string, dryRun bool) error {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return fmt.Errorf("failed govc validations: %v", err)
}
var params []string
var result bytes.Buffer
params = strings.Fields("find /" + envMap[govcDatacenterKey] + " -type VirtualMachine -name " + clusterName + "*")
result, err = g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
return fmt.Errorf("getting vm list: %v", err)
}
scanner := bufio.NewScanner(strings.NewReader(result.String()))
for scanner.Scan() {
vmName := scanner.Text()
if dryRun {
logger.Info("Found ", "vm_name", vmName)
continue
}
params = strings.Fields("vm.power -off -force " + vmName)
result, err = g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
logger.Info("WARN: Failed to power off vm ", "vm_name", vmName, "error", err)
}
params = strings.Fields("object.destroy " + vmName)
result, err = g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
logger.Info("WARN: Failed to delete vm ", "vm_name", vmName, "error", err)
} else {
logger.Info("Deleted ", "vm_name", vmName)
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failure reading output of vm list")
}
return nil
}
func (g *Govc) ValidateVCenterConnection(ctx context.Context, server string) error {
skipVerifyTransport := http.DefaultTransport.(*http.Transport).Clone()
skipVerifyTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{Transport: skipVerifyTransport}
if _, err := client.Get("https://" + server); err != nil {
return fmt.Errorf("failed to reach server %s: %v", server, err)
}
return nil
}
func (g *Govc) ValidateVCenterAuthentication(ctx context.Context) error {
err := g.Retry(func() error {
_, err := g.exec(ctx, "about", "-k")
return err
})
if err != nil {
return fmt.Errorf("vSphere authentication failed: %v", err)
}
return nil
}
func (g *Govc) IsCertSelfSigned(ctx context.Context) bool {
_, err := g.exec(ctx, "about")
return err != nil
}
func (g *Govc) GetCertThumbprint(ctx context.Context) (string, error) {
bufferResponse, err := g.exec(ctx, "about.cert", "-thumbprint", "-k")
if err != nil {
return "", fmt.Errorf("unable to retrieve thumbprint: %v", err)
}
data := strings.Split(strings.Trim(bufferResponse.String(), "\n"), " ")
if len(data) != 2 {
return "", fmt.Errorf("invalid thumbprint format")
}
return data[1], nil
}
func (g *Govc) ConfigureCertThumbprint(ctx context.Context, server, thumbprint string) error {
path, err := g.writer.Write(filepath.Base(govcTlsHostsFile), []byte(fmt.Sprintf("%s %s", server, thumbprint)))
if err != nil {
return fmt.Errorf("writing to file %s: %v", govcTlsHostsFile, err)
}
if err = os.Setenv(govcTlsKnownHostsKey, path); err != nil {
return fmt.Errorf("unable to set %s: %v", govcTlsKnownHostsKey, err)
}
g.requiredEnvs.append(govcTlsKnownHostsKey)
return nil
}
func (g *Govc) DatacenterExists(ctx context.Context, datacenter string) (bool, error) {
exists := false
err := g.Retry(func() error {
result, err := g.exec(ctx, "datacenter.info", datacenter)
if err == nil {
exists = true
return nil
}
if strings.HasSuffix(result.String(), "not found") {
exists = false
return nil
}
return err
})
if err != nil {
return false, fmt.Errorf("failed to get datacenter: %v", err)
}
return exists, nil
}
func (g *Govc) NetworkExists(ctx context.Context, network string) (bool, error) {
exists := false
err := g.Retry(func() error {
networkResponse, err := g.exec(ctx, "find", "-maxdepth=1", filepath.Dir(network), "-type", "n", "-name", filepath.Base(network))
if err != nil {
return err
}
if networkResponse.String() == "" {
exists = false
return nil
}
exists = true
return nil
})
if err != nil {
return false, fmt.Errorf("failed checking if network '%s' exists: %v", network, err)
}
return exists, nil
}
func (g *Govc) ValidateVCenterSetupMachineConfig(ctx context.Context, datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfig *v1alpha1.VSphereMachineConfig, _ *bool) error {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return fmt.Errorf("failed govc validations: %v", err)
}
machineConfig.Spec.Datastore, err = prependPath(datastore, machineConfig.Spec.Datastore, datacenterConfig.Spec.Datacenter)
if err != nil {
return err
}
params := []string{"datastore.info", machineConfig.Spec.Datastore}
err = g.Retry(func() error {
_, err = g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
datastorePath := filepath.Dir(machineConfig.Spec.Datastore)
isValidDatastorePath := g.isValidPath(ctx, envMap, datastorePath)
if isValidDatastorePath {
leafDir := filepath.Base(machineConfig.Spec.Datastore)
return fmt.Errorf("valid path, but '%s' is not a datastore", leafDir)
} else {
return fmt.Errorf("failed to get datastore: %v", err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to get datastore: %v", err)
}
logger.MarkPass("Datastore validated")
if len(machineConfig.Spec.Folder) > 0 {
machineConfig.Spec.Folder, err = prependPath(vm, machineConfig.Spec.Folder, datacenterConfig.Spec.Datacenter)
if err != nil {
return err
}
params = []string{"folder.info", machineConfig.Spec.Folder}
err = g.Retry(func() error {
_, err := g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
err = g.createFolder(ctx, envMap, machineConfig)
if err != nil {
currPath := "/" + datacenterConfig.Spec.Datacenter + "/"
dirs := strings.Split(machineConfig.Spec.Folder, "/")
for _, dir := range dirs[2:] {
currPath += dir + "/"
if !g.isValidPath(ctx, envMap, currPath) {
return fmt.Errorf("%s is an invalid intermediate directory", currPath)
}
}
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to get folder: %v", err)
}
logger.MarkPass("Folder validated")
}
var poolInfoResponse bytes.Buffer
params = []string{"find", "-json", "/" + datacenterConfig.Spec.Datacenter, "-type", "p", "-name", filepath.Base(machineConfig.Spec.ResourcePool)}
err = g.Retry(func() error {
poolInfoResponse, err = g.ExecuteWithEnv(ctx, envMap, params...)
return err
})
if err != nil {
return fmt.Errorf("getting resource pool: %v", err)
}
poolInfoJson := poolInfoResponse.String()
poolInfoJson = strings.TrimSuffix(poolInfoJson, "\n")
if poolInfoJson == "null" || poolInfoJson == "" {
return fmt.Errorf("resource pool '%s' not found", machineConfig.Spec.ResourcePool)
}
poolInfo := make([]string, 0)
if err = json.Unmarshal([]byte(poolInfoJson), &poolInfo); err != nil {
return fmt.Errorf("failed unmarshalling govc response: %v", err)
}
machineConfig.Spec.ResourcePool = strings.TrimPrefix(machineConfig.Spec.ResourcePool, "*/")
bPoolFound := false
var foundPool string
for _, p := range poolInfo {
if strings.HasSuffix(p, machineConfig.Spec.ResourcePool) {
if bPoolFound {
return fmt.Errorf("specified resource pool '%s' maps to multiple paths within the datacenter '%s'", machineConfig.Spec.ResourcePool, datacenterConfig.Spec.Datacenter)
}
bPoolFound = true
foundPool = p
}
}
if !bPoolFound {
return fmt.Errorf("resource pool '%s' not found", machineConfig.Spec.ResourcePool)
}
machineConfig.Spec.ResourcePool = foundPool
logger.MarkPass("Resource pool validated")
return nil
}
func prependPath(folderType FolderType, folderPath string, datacenter string) (string, error) {
prefix := fmt.Sprintf("/%s", datacenter)
modPath := folderPath
if !strings.HasPrefix(folderPath, prefix) {
modPath = fmt.Sprintf("%s/%s/%s", prefix, folderType, folderPath)
logger.V(4).Info(fmt.Sprintf("Relative %s path specified, using path %s", folderType, modPath))
return modPath, nil
}
prefix += fmt.Sprintf("/%s", folderType)
if !strings.HasPrefix(folderPath, prefix) {
return folderPath, fmt.Errorf("invalid folder type, expected path under %s", prefix)
}
return modPath, nil
}
func (g *Govc) createFolder(ctx context.Context, envMap map[string]string, machineConfig *v1alpha1.VSphereMachineConfig) error {
params := []string{"folder.create", machineConfig.Spec.Folder}
err := g.Retry(func() error {
_, err := g.ExecuteWithEnv(ctx, envMap, params...)
if err != nil {
return fmt.Errorf("creating folder: %v", err)
}
return nil
})
return err
}
func (g *Govc) isValidPath(ctx context.Context, envMap map[string]string, path string) bool {
params := []string{"folder.info", path}
_, err := g.ExecuteWithEnv(ctx, envMap, params...)
return err == nil
}
func (g *Govc) GetTags(ctx context.Context, path string) ([]string, error) {
var tagsResponse bytes.Buffer
var err error
err = g.Retry(func() error {
tagsResponse, err = g.exec(ctx, "tags.attached.ls", "-json", "-r", path)
return err
})
if err != nil {
return nil, fmt.Errorf("govc returned error when listing tags for %s: %v", path, err)
}
tagsJson := tagsResponse.String()
if tagsJson == "null" {
return nil, nil
}
tags := make([]string, 0)
if err = json.Unmarshal([]byte(tagsJson), &tags); err != nil {
return nil, fmt.Errorf("failed unmarshalling govc response to get tags for %s: %v", path, err)
}
return tags, nil
}
// Tag struct to represent a vSphere Tag.
type Tag struct {
Id string
Name string
CategoryId string `json:"category_id,omitempty"`
}
// ListTags list all vSphere tags in vCenter.
func (g *Govc) ListTags(ctx context.Context) ([]Tag, error) {
tagsResponse, err := g.exec(ctx, "tags.ls", "-json")
if err != nil {
return nil, fmt.Errorf("govc returned error when listing tags: %v", err)
}
tagsJson := tagsResponse.String()
if tagsJson == "null" {
return nil, nil
}
tags := make([]Tag, 0)
if err = json.Unmarshal([]byte(tagsJson), &tags); err != nil {
return nil, fmt.Errorf("failed unmarshalling govc response from list tags: %v", err)
}
return tags, nil
}
func (g *Govc) AddTag(ctx context.Context, path, tag string) error {
if _, err := g.exec(ctx, "tags.attach", tag, path); err != nil {
return fmt.Errorf("govc returned error when attaching tag to %s: %v", path, err)
}
return nil
}
func (g *Govc) CreateTag(ctx context.Context, tag, category string) error {
if _, err := g.exec(ctx, "tags.create", "-c", category, tag); err != nil {
return fmt.Errorf("govc returned error when creating tag %s: %v", tag, err)
}
return nil
}
type category struct {
Id string
Name string
Cardinality string
AssociableTypes []string `json:"associable_types,omitempty"`
}
func (g *Govc) ListCategories(ctx context.Context) ([]string, error) {
categoriesResponse, err := g.exec(ctx, "tags.category.ls", "-json")
if err != nil {
return nil, fmt.Errorf("govc returned error when listing categories: %v", err)
}
categoriesJson := categoriesResponse.String()
if categoriesJson == "null" {
return nil, nil
}
categories := make([]category, 0)
if err = json.Unmarshal([]byte(categoriesJson), &categories); err != nil {
return nil, fmt.Errorf("failed unmarshalling govc response from list categories: %v", err)
}
categoryNames := make([]string, 0, len(categories))
for _, c := range categories {
categoryNames = append(categoryNames, c.Name)
}
return categoryNames, nil
}
type objectType string
const virtualMachine objectType = "VirtualMachine"
func (g *Govc) CreateCategoryForVM(ctx context.Context, name string) error {
return g.createCategory(ctx, name, []objectType{virtualMachine})
}
func (g *Govc) createCategory(ctx context.Context, name string, objectTypes []objectType) error {
params := []string{"tags.category.create"}
for _, t := range objectTypes {
params = append(params, "-t", string(t))
}
params = append(params, name)