-
Notifications
You must be signed in to change notification settings - Fork 286
/
vsphere.go
212 lines (178 loc) Β· 5.23 KB
/
vsphere.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
package api
import (
"fmt"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/templater"
)
type VSphereConfig struct {
datacenterConfig *anywherev1.VSphereDatacenterConfig
machineConfigs map[string]*anywherev1.VSphereMachineConfig
}
type VSphereFiller func(config VSphereConfig)
func AutoFillVSphereProvider(filename string, fillers ...VSphereFiller) ([]byte, error) {
vsphereDatacenterConfig, err := anywherev1.GetVSphereDatacenterConfig(filename)
if err != nil {
return nil, fmt.Errorf("unable to get vsphere datacenter config from file: %v", err)
}
vsphereMachineConfigs, err := anywherev1.GetVSphereMachineConfigs(filename)
if err != nil {
return nil, fmt.Errorf("unable to get vsphere machine config from file: %v", err)
}
config := VSphereConfig{
datacenterConfig: vsphereDatacenterConfig,
machineConfigs: vsphereMachineConfigs,
}
for _, f := range fillers {
f(config)
}
resources := make([]interface{}, 0, len(config.machineConfigs)+1)
resources = append(resources, config.datacenterConfig)
for _, m := range config.machineConfigs {
resources = append(resources, m)
}
yamlResources := make([][]byte, 0, len(resources))
for _, r := range resources {
yamlContent, err := yaml.Marshal(r)
if err != nil {
return nil, fmt.Errorf("marshalling vsphere resource: %v", err)
}
yamlResources = append(yamlResources, yamlContent)
}
return templater.AppendYamlResources(yamlResources...), nil
}
func WithOsFamilyForAllMachines(value anywherev1.OSFamily) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.OSFamily = value
}
}
}
func WithNumCPUsForAllMachines(value int) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.NumCPUs = value
}
}
}
func WithDiskGiBForAllMachines(value int) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.DiskGiB = value
}
}
}
func WithMemoryMiBForAllMachines(value int) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.MemoryMiB = value
}
}
}
func WithTLSInsecure(value bool) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.Insecure = value
}
}
func WithTLSThumbprint(value string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.Thumbprint = value
}
}
func WithTemplateForAllMachines(value string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.Template = value
}
}
}
func WithStoragePolicyNameForAllMachines(value string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.StoragePolicyName = value
}
}
}
func WithVSphereConfigNamespaceForAllMachinesAndDatacenter(ns string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Namespace = ns
for _, m := range config.machineConfigs {
m.Namespace = ns
}
}
}
func WithSSHAuthorizedKeyForAllMachines(key string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
setSSHKeyForFirstUser(m, key)
}
}
}
func WithServer(value string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.Server = value
}
}
func WithResourcePoolForAllMachines(value string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.ResourcePool = value
}
}
}
func WithNetwork(value string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.Network = value
}
}
func WithFolderForAllMachines(value string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.Folder = value
}
}
}
func WithDatastoreForAllMachines(value string) VSphereFiller {
return func(config VSphereConfig) {
for _, m := range config.machineConfigs {
m.Spec.Datastore = value
}
}
}
func WithDatacenter(value string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.Datacenter = value
}
}
// WithDisableCSI sets the value for DisableCSI in VSphereDatacenterConfig.
func WithDisableCSI(value bool) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Spec.DisableCSI = value
}
}
func WithVSphereStringFromEnvVar(envVar string, opt func(string) VSphereFiller) VSphereFiller {
return opt(os.Getenv(envVar))
}
func WithVSphereBoolFromEnvVar(envVar string, opt func(bool) VSphereFiller) VSphereFiller {
return opt(os.Getenv(envVar) == "true")
}
func WithVSphereMachineConfig(name string, fillers ...VSphereMachineConfigFiller) VSphereFiller {
return func(config VSphereConfig) {
m, ok := config.machineConfigs[name]
if !ok {
m = &anywherev1.VSphereMachineConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.VSphereMachineConfigKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
config.machineConfigs[name] = m
}
FillVSphereMachineConfig(m, fillers...)
}
}