-
Notifications
You must be signed in to change notification settings - Fork 277
/
defaults.go
137 lines (111 loc) Β· 4.78 KB
/
defaults.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
package vsphere
import (
"context"
"fmt"
"path/filepath"
"strings"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers/vsphere/internal/templates"
releasev1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
)
type Defaulter struct {
govc ProviderGovcClient
}
func NewDefaulter(govc ProviderGovcClient) *Defaulter {
return &Defaulter{
govc: govc,
}
}
func (d *Defaulter) setDefaultsForMachineConfig(ctx context.Context, spec *Spec) error {
setDefaultsForEtcdMachineConfig(spec.etcdMachineConfig())
for _, m := range spec.machineConfigs() {
m.SetDefaults()
if err := d.setDefaultTemplateIfMissing(ctx, spec, m); err != nil {
return err
}
if err := d.setTemplateFullPath(ctx, spec.datacenterConfig, m); err != nil {
return err
}
if err := d.setDiskDefaults(ctx, m); err != nil {
return err
}
}
return nil
}
func (d *Defaulter) SetDefaultsForDatacenterConfig(ctx context.Context, datacenterConfig *anywherev1.VSphereDatacenterConfig) error {
datacenterConfig.SetDefaults()
if datacenterConfig.Spec.Thumbprint != "" {
if err := d.govc.ConfigureCertThumbprint(ctx, datacenterConfig.Spec.Server, datacenterConfig.Spec.Thumbprint); err != nil {
return fmt.Errorf("failed configuring govc cert thumbprint: %v", err)
}
}
return nil
}
func setDefaultsForEtcdMachineConfig(machineConfig *anywherev1.VSphereMachineConfig) {
if machineConfig != nil && machineConfig.Spec.MemoryMiB < 8192 {
logger.Info("Warning: VSphereMachineConfig MemoryMiB for etcd machines should not be less than 8192. Defaulting to 8192")
machineConfig.Spec.MemoryMiB = 8192
}
}
func (d *Defaulter) setDefaultTemplateIfMissing(ctx context.Context, spec *Spec, machineConfig *anywherev1.VSphereMachineConfig) error {
if machineConfig.Spec.Template == "" {
logger.V(1).Info("Control plane VSphereMachineConfig template is not set. Using default template.")
if err := d.setupDefaultTemplate(ctx, spec, machineConfig); err != nil {
return err
}
}
return nil
}
func (d *Defaulter) setupDefaultTemplate(ctx context.Context, spec *Spec, machineConfig *anywherev1.VSphereMachineConfig) error {
osFamily := machineConfig.Spec.OSFamily
eksd := spec.VersionsBundle.EksD
var ova releasev1.Archive
switch osFamily {
case anywherev1.Bottlerocket:
ova = eksd.Ova.Bottlerocket
default:
return fmt.Errorf("can not import ova for osFamily: %s, please use %s as osFamily for auto-importing or provide a valid template", osFamily, anywherev1.Bottlerocket)
}
templateName := fmt.Sprintf("%s-%s-%s-%s-%s", osFamily, eksd.KubeVersion, eksd.Name, strings.Join(ova.Arch, "-"), ova.SHA256[:7])
machineConfig.Spec.Template = filepath.Join("/", spec.datacenterConfig.Spec.Datacenter, defaultTemplatesFolder, templateName)
tags := requiredTemplateTagsByCategory(spec.Spec, machineConfig)
// TODO: figure out if it's worth refactoring the factory to be able to reuse across machine configs.
templateFactory := templates.NewFactory(d.govc, spec.datacenterConfig.Spec.Datacenter, machineConfig.Spec.Datastore, spec.datacenterConfig.Spec.Network, machineConfig.Spec.ResourcePool, defaultTemplateLibrary)
// TODO: remove the factory's dependency on a machineConfig
if err := templateFactory.CreateIfMissing(ctx, spec.datacenterConfig.Spec.Datacenter, machineConfig, ova.URI, tags); err != nil {
return err
}
return nil
}
func (d *Defaulter) setDiskDefaults(ctx context.Context, machineConfig *anywherev1.VSphereMachineConfig) error {
templateHasSnapshot, err := d.govc.TemplateHasSnapshot(ctx, machineConfig.Spec.Template)
if err != nil {
return fmt.Errorf("getting template details: %v", err)
}
if !templateHasSnapshot {
logger.Info("Warning: Your VM template has no snapshots. Defaulting to FullClone mode. VM provisioning might take longer.")
if machineConfig.Spec.DiskGiB < 20 {
logger.Info("Warning: VSphereMachineConfig DiskGiB cannot be less than 20. Defaulting to 20.")
machineConfig.Spec.DiskGiB = 20
}
} else if machineConfig.Spec.DiskGiB != 25 {
logger.Info("Warning: Your VM template includes snapshot(s). LinkedClone mode will be used. DiskGiB cannot be customizable as disks cannot be expanded when using LinkedClone mode. Using default of 25 for DiskGiBs.")
machineConfig.Spec.DiskGiB = 25
}
return nil
}
func (d *Defaulter) setTemplateFullPath(ctx context.Context,
datacenterConfig *anywherev1.VSphereDatacenterConfig,
machine *anywherev1.VSphereMachineConfig,
) error {
templateFullPath, err := d.govc.SearchTemplate(ctx, datacenterConfig.Spec.Datacenter, machine.Spec.Template)
if err != nil {
return fmt.Errorf("setting template full path: %v", err)
}
if len(templateFullPath) <= 0 {
return fmt.Errorf("template <%s> not found", machine.Spec.Template)
}
machine.Spec.Template = templateFullPath
return nil
}