-
Notifications
You must be signed in to change notification settings - Fork 286
/
vspheremachineconfig.go
106 lines (89 loc) · 3.43 KB
/
vspheremachineconfig.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
package v1alpha1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/aws/eks-anywhere/pkg/logger"
)
const (
VSphereMachineConfigKind = "VSphereMachineConfig"
DefaultVSphereDiskGiB = 25
DefaultVSphereNumCPUs = 2
DefaultVSphereMemoryMiB = 8192
DefaultVSphereOSFamily = Bottlerocket
)
// Used for generating yaml for generate clusterconfig command.
func NewVSphereMachineConfigGenerate(name string) *VSphereMachineConfigGenerate {
return &VSphereMachineConfigGenerate{
TypeMeta: metav1.TypeMeta{
Kind: VSphereMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: ObjectMeta{
Name: name,
},
Spec: VSphereMachineConfigSpec{
DiskGiB: DefaultVSphereDiskGiB,
NumCPUs: DefaultVSphereNumCPUs,
MemoryMiB: DefaultVSphereMemoryMiB,
OSFamily: DefaultVSphereOSFamily,
Users: []UserConfiguration{{
Name: "ec2-user",
SshAuthorizedKeys: []string{"ssh-rsa AAAA..."},
}},
},
}
}
func (c *VSphereMachineConfigGenerate) APIVersion() string {
return c.TypeMeta.APIVersion
}
func (c *VSphereMachineConfigGenerate) Kind() string {
return c.TypeMeta.Kind
}
func (c *VSphereMachineConfigGenerate) Name() string {
return c.ObjectMeta.Name
}
func setVSphereMachineConfigDefaults(machineConfig *VSphereMachineConfig) {
if len(machineConfig.Spec.Folder) <= 0 {
logger.Info("VSphereMachineConfig Folder is not set or is empty. Defaulting to root vSphere folder.")
}
if machineConfig.Spec.MemoryMiB <= 0 {
logger.V(1).Info("VSphereMachineConfig MemoryMiB is not set or is empty. Defaulting to 8192.", "machineConfig", machineConfig.Name)
machineConfig.Spec.MemoryMiB = 8192
}
if machineConfig.Spec.MemoryMiB < 2048 {
logger.Info("Warning: VSphereMachineConfig MemoryMiB should not be less than 2048. Defaulting to 2048. Recommended memory is 8192.", "machineConfig", machineConfig.Name)
machineConfig.Spec.MemoryMiB = 2048
}
if machineConfig.Spec.NumCPUs <= 0 {
logger.V(1).Info("VSphereMachineConfig NumCPUs is not set or is empty. Defaulting to 2.", "machineConfig", machineConfig.Name)
machineConfig.Spec.NumCPUs = 2
}
if machineConfig.Spec.OSFamily == "" {
logger.Info("Warning: OS family not specified in machine config specification. Defaulting to Bottlerocket.")
machineConfig.Spec.OSFamily = Bottlerocket
}
}
func validateVSphereMachineConfig(config *VSphereMachineConfig) error {
if len(config.Spec.Datastore) <= 0 {
return fmt.Errorf("VSphereMachineConfig %s datastore is not set or is empty", config.Name)
}
if len(config.Spec.ResourcePool) <= 0 {
return fmt.Errorf("VSphereMachineConfig %s VM resourcePool is not set or is empty", config.Name)
}
if config.Spec.OSFamily != Bottlerocket && config.Spec.OSFamily != Ubuntu && config.Spec.OSFamily != RedHat {
return fmt.Errorf("VSphereMachineConfig %s osFamily: %s is not supported, please use one of the following: %s, %s, %s", config.Name, config.Spec.OSFamily, Bottlerocket, Ubuntu, RedHat)
}
if err := validateVSphereMachineConfigOSFamilyUser(config); err != nil {
return err
}
if err := validateHostOSConfig(config.Spec.HostOSConfiguration, config.Spec.OSFamily); err != nil {
return fmt.Errorf("HostOSConfiguration is invalid for VSphereMachineConfig %s: %v", config.Name, err)
}
return nil
}
func validateVSphereMachineConfigHasTemplate(config *VSphereMachineConfig) error {
if config.Spec.Template == "" {
return fmt.Errorf("template field is required")
}
return nil
}