-
Notifications
You must be signed in to change notification settings - Fork 273
/
nutanix.go
394 lines (342 loc) Β· 16.5 KB
/
nutanix.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
package framework
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/aws/eks-anywhere/internal/pkg/api"
"github.com/aws/eks-anywhere/internal/pkg/nutanix"
"github.com/aws/eks-anywhere/internal/test/cleanup"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/constants"
releasev1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
clusterf "github.com/aws/eks-anywhere/test/framework/cluster"
)
const (
nutanixEndpoint = "T_NUTANIX_ENDPOINT"
nutanixPort = "T_NUTANIX_PORT"
nutanixAdditionalTrustBundle = "T_NUTANIX_ADDITIONAL_TRUST_BUNDLE"
nutanixInsecure = "T_NUTANIX_INSECURE"
nutanixMachineBootType = "T_NUTANIX_MACHINE_BOOT_TYPE"
nutanixMachineMemorySize = "T_NUTANIX_MACHINE_MEMORY_SIZE"
nutanixSystemDiskSize = "T_NUTANIX_SYSTEMDISK_SIZE"
nutanixMachineVCPUsPerSocket = "T_NUTANIX_MACHINE_VCPU_PER_SOCKET"
nutanixMachineVCPUSocket = "T_NUTANIX_MACHINE_VCPU_SOCKET"
nutanixPrismElementClusterName = "T_NUTANIX_PRISM_ELEMENT_CLUSTER_NAME"
nutanixSSHAuthorizedKey = "T_NUTANIX_SSH_AUTHORIZED_KEY"
nutanixSubnetName = "T_NUTANIX_SUBNET_NAME"
nutanixControlPlaneEndpointIP = "T_NUTANIX_CONTROL_PLANE_ENDPOINT_IP"
nutanixControlPlaneCidrVar = "T_NUTANIX_CONTROL_PLANE_CIDR"
nutanixPodCidrVar = "T_NUTANIX_POD_CIDR"
nutanixServiceCidrVar = "T_NUTANIX_SERVICE_CIDR"
nutanixTemplateNameUbuntu123Var = "T_NUTANIX_TEMPLATE_NAME_UBUNTU_1_23"
nutanixTemplateNameUbuntu124Var = "T_NUTANIX_TEMPLATE_NAME_UBUNTU_1_24"
nutanixTemplateNameUbuntu125Var = "T_NUTANIX_TEMPLATE_NAME_UBUNTU_1_25"
nutanixTemplateNameUbuntu126Var = "T_NUTANIX_TEMPLATE_NAME_UBUNTU_1_26"
nutanixTemplateNameUbuntu127Var = "T_NUTANIX_TEMPLATE_NAME_UBUNTU_1_27"
)
var requiredNutanixEnvVars = []string{
constants.EksaNutanixUsernameKey,
constants.EksaNutanixPasswordKey,
nutanixEndpoint,
nutanixPort,
nutanixAdditionalTrustBundle,
nutanixMachineBootType,
nutanixMachineMemorySize,
nutanixSystemDiskSize,
nutanixMachineVCPUsPerSocket,
nutanixMachineVCPUSocket,
nutanixPrismElementClusterName,
nutanixSSHAuthorizedKey,
nutanixSubnetName,
nutanixPodCidrVar,
nutanixServiceCidrVar,
nutanixTemplateNameUbuntu123Var,
nutanixTemplateNameUbuntu124Var,
nutanixTemplateNameUbuntu125Var,
nutanixTemplateNameUbuntu126Var,
nutanixTemplateNameUbuntu127Var,
nutanixInsecure,
}
type Nutanix struct {
t *testing.T
fillers []api.NutanixFiller
clusterFillers []api.ClusterFiller
client nutanix.PrismClient
controlPlaneEndpointIP string
cpCidr string
podCidr string
serviceCidr string
devRelease *releasev1.EksARelease
templatesRegistry *templateRegistry
}
type NutanixOpt func(*Nutanix)
func NewNutanix(t *testing.T, opts ...NutanixOpt) *Nutanix {
checkRequiredEnvVars(t, requiredNutanixEnvVars)
nutanixProvider := &Nutanix{
t: t,
fillers: []api.NutanixFiller{
api.WithNutanixStringFromEnvVar(nutanixEndpoint, api.WithNutanixEndpoint),
api.WithNutanixIntFromEnvVar(nutanixPort, api.WithNutanixPort),
api.WithNutanixStringFromEnvVar(nutanixAdditionalTrustBundle, api.WithNutanixAdditionalTrustBundle),
api.WithNutanixStringFromEnvVar(nutanixMachineMemorySize, api.WithNutanixMachineMemorySize),
api.WithNutanixStringFromEnvVar(nutanixSystemDiskSize, api.WithNutanixMachineSystemDiskSize),
api.WithNutanixInt32FromEnvVar(nutanixMachineVCPUsPerSocket, api.WithNutanixMachineVCPUsPerSocket),
api.WithNutanixInt32FromEnvVar(nutanixMachineVCPUSocket, api.WithNutanixMachineVCPUSocket),
api.WithNutanixStringFromEnvVar(nutanixSSHAuthorizedKey, api.WithNutanixSSHAuthorizedKey),
api.WithNutanixBoolFromEnvVar(nutanixInsecure, api.WithNutanixInsecure),
// Assumption: generated clusterconfig by nutanix provider sets name as id type by default.
// for uuid specific id type, we will set it thru each specific test so that current CI
// works as is with name id type for following resources
api.WithNutanixStringFromEnvVar(nutanixPrismElementClusterName, api.WithNutanixPrismElementClusterName),
api.WithNutanixStringFromEnvVar(nutanixSubnetName, api.WithNutanixSubnetName),
},
}
nutanixProvider.controlPlaneEndpointIP = os.Getenv(nutanixControlPlaneEndpointIP)
nutanixProvider.cpCidr = os.Getenv(nutanixControlPlaneCidrVar)
nutanixProvider.podCidr = os.Getenv(nutanixPodCidrVar)
nutanixProvider.serviceCidr = os.Getenv(nutanixServiceCidrVar)
client, err := nutanix.NewPrismClient(os.Getenv(nutanixEndpoint), os.Getenv(nutanixPort), true)
if err != nil {
t.Fatalf("Failed to initialize Nutanix Prism Client: %v", err)
}
nutanixProvider.client = client
nutanixProvider.templatesRegistry = &templateRegistry{cache: map[string]string{}, generator: nutanixProvider}
for _, opt := range opts {
opt(nutanixProvider)
}
return nutanixProvider
}
// RequiredNutanixEnvVars returns a list of environment variables needed for Nutanix tests.
func RequiredNutanixEnvVars() []string {
return requiredNutanixEnvVars
}
// Name returns the provider name. It satisfies the test framework Provider.
func (n *Nutanix) Name() string {
return "nutanix"
}
// Setup does nothing. It satisfies the test framework Provider.
func (n *Nutanix) Setup() {}
// UpdateKubeConfig customizes generated kubeconfig for the provider.
func (n *Nutanix) UpdateKubeConfig(content *[]byte, clusterName string) error {
return nil
}
// CleanupVMs satisfies the test framework Provider.
func (n *Nutanix) CleanupVMs(clustername string) error {
return cleanup.NutanixTestResourcesCleanup(context.Background(), clustername, os.Getenv(nutanixEndpoint), os.Getenv(nutanixPort), true, true)
}
// ClusterConfigUpdates satisfies the test framework Provider.
func (n *Nutanix) ClusterConfigUpdates() []api.ClusterConfigFiller {
f := make([]api.ClusterFiller, 0, len(n.clusterFillers)+3)
f = append(f, n.clusterFillers...)
if n.controlPlaneEndpointIP != "" {
f = append(f, api.WithControlPlaneEndpointIP(n.controlPlaneEndpointIP))
} else {
clusterIP, err := GetIP(n.cpCidr, ClusterIPPoolEnvVar)
if err != nil {
n.t.Fatalf("failed to get cluster ip for test environment: %v", err)
}
f = append(f, api.WithControlPlaneEndpointIP(clusterIP))
}
if n.podCidr != "" {
f = append(f, api.WithPodCidr(n.podCidr))
}
if n.serviceCidr != "" {
f = append(f, api.WithServiceCidr(n.serviceCidr))
}
return []api.ClusterConfigFiller{api.ClusterToConfigFiller(f...), api.NutanixToConfigFiller(n.fillers...)}
}
// WithProviderUpgrade returns a ClusterE2EOpt that updates the cluster config for provider-specific upgrade.
func (n *Nutanix) WithProviderUpgrade(fillers ...api.NutanixFiller) ClusterE2ETestOpt {
return func(e *ClusterE2ETest) {
e.UpdateClusterConfig(api.NutanixToConfigFiller(fillers...))
}
}
// WithKubeVersionAndOS returns a cluster config filler that sets the cluster kube version and the right template for all
// nutanix machine configs.
func (n *Nutanix) WithKubeVersionAndOS(kubeVersion anywherev1.KubernetesVersion, os OS, release *releasev1.EksARelease) api.ClusterConfigFiller {
// TODO: Update tests to use this
panic("Not implemented for Nutanix yet")
}
// WithNewWorkerNodeGroup returns an api.ClusterFiller that adds a new workerNodeGroupConfiguration and
// a corresponding NutanixMachineConfig to the cluster config.
func (n *Nutanix) WithNewWorkerNodeGroup(name string, workerNodeGroup *WorkerNodeGroup) api.ClusterConfigFiller {
// TODO: Implement for Nutanix provider
panic("Not implemented for Nutanix yet")
}
// withNutanixKubeVersionAndOS returns a NutanixOpt that adds API fillers to use a Nutanix template for
// the specified OS family and version (default if not provided), corresponding to a particular
// Kubernetes version, in addition to configuring all machine configs to use this OS family.
func withNutanixKubeVersionAndOS(kubeVersion anywherev1.KubernetesVersion, os OS, release *releasev1.EksARelease) NutanixOpt {
return func(n *Nutanix) {
n.fillers = append(n.fillers,
n.templateForKubeVersionAndOS(kubeVersion, os, release),
api.WithOsFamilyForAllNutanixMachines(osFamiliesForOS[os]),
)
}
}
// WithUbuntu123Nutanix returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template for k8s 1.23
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu123Nutanix() NutanixOpt {
return withNutanixKubeVersionAndOS(anywherev1.Kube123, Ubuntu2004, nil)
}
// WithUbuntu124Nutanix returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template for k8s 1.24
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu124Nutanix() NutanixOpt {
return withNutanixKubeVersionAndOS(anywherev1.Kube124, Ubuntu2004, nil)
}
// WithUbuntu125Nutanix returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template for k8s 1.25
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu125Nutanix() NutanixOpt {
return withNutanixKubeVersionAndOS(anywherev1.Kube125, Ubuntu2004, nil)
}
// WithUbuntu126Nutanix returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template for k8s 1.26
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu126Nutanix() NutanixOpt {
return withNutanixKubeVersionAndOS(anywherev1.Kube126, Ubuntu2004, nil)
}
// WithUbuntu127Nutanix returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template for k8s 1.27
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu127Nutanix() NutanixOpt {
return withNutanixKubeVersionAndOS(anywherev1.Kube127, Ubuntu2004, nil)
}
// withNutanixKubeVersionAndOSForUUID returns a NutanixOpt that adds API fillers to use a Nutanix template UUID
// corresponding to the provided OS family and Kubernetes version, in addition to configuring all machine configs
// to use this OS family.
func withNutanixKubeVersionAndOSForUUID(kubeVersion anywherev1.KubernetesVersion, os OS, release *releasev1.EksARelease) NutanixOpt {
return func(n *Nutanix) {
name := n.templateForDevRelease(kubeVersion, os)
n.fillers = append(n.fillers, n.withNutanixUUID(name, osFamiliesForOS[os])...)
}
}
// WithUbuntu123NutanixUUID returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template UUID for k8s 1.23
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu123NutanixUUID() NutanixOpt {
return withNutanixKubeVersionAndOSForUUID(anywherev1.Kube123, Ubuntu2004, nil)
}
// WithUbuntu124NutanixUUID returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template UUID for k8s 1.24
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu124NutanixUUID() NutanixOpt {
return withNutanixKubeVersionAndOSForUUID(anywherev1.Kube124, Ubuntu2004, nil)
}
// WithUbuntu125NutanixUUID returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template UUID for k8s 1.25
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu125NutanixUUID() NutanixOpt {
return withNutanixKubeVersionAndOSForUUID(anywherev1.Kube125, Ubuntu2004, nil)
}
// WithUbuntu126NutanixUUID returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template UUID for k8s 1.26
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu126NutanixUUID() NutanixOpt {
return withNutanixKubeVersionAndOSForUUID(anywherev1.Kube126, Ubuntu2004, nil)
}
// WithUbuntu127NutanixUUID returns a NutanixOpt that adds API fillers to use a Ubuntu Nutanix template UUID for k8s 1.27
// and the "ubuntu" osFamily in all machine configs.
func WithUbuntu127NutanixUUID() NutanixOpt {
return withNutanixKubeVersionAndOSForUUID(anywherev1.Kube127, Ubuntu2004, nil)
}
func (n *Nutanix) withNutanixUUID(name string, osFamily anywherev1.OSFamily) []api.NutanixFiller {
uuid, err := n.client.GetImageUUIDFromName(context.Background(), name)
if err != nil {
n.t.Fatalf("Failed to get UUID for image %s: %v", name, err)
}
return append([]api.NutanixFiller{},
api.WithNutanixMachineTemplateImageUUID(*uuid),
api.WithOsFamilyForAllNutanixMachines(osFamily),
)
}
// WithPrismElementClusterUUID returns a NutanixOpt that adds API fillers to use a PE Cluster UUID.
func WithPrismElementClusterUUID() NutanixOpt {
return func(n *Nutanix) {
name := os.Getenv(nutanixPrismElementClusterName)
uuid, err := n.client.GetClusterUUIDFromName(context.Background(), name)
if err != nil {
n.t.Fatalf("Failed to get UUID for image %s: %v", name, err)
}
n.fillers = append(n.fillers, api.WithNutanixPrismElementClusterUUID(*uuid))
}
}
// WithNutanixSubnetUUID returns a NutanixOpt that adds API fillers to use a Subnet UUID.
func WithNutanixSubnetUUID() NutanixOpt {
return func(n *Nutanix) {
name := os.Getenv(nutanixSubnetName)
uuid, err := n.client.GetSubnetUUIDFromName(context.Background(), name)
if err != nil {
n.t.Fatalf("Failed to get UUID for image %s: %v", name, err)
}
n.fillers = append(n.fillers, api.WithNutanixSubnetUUID(*uuid))
}
}
// templateForKubeVersionAndOS returns a Nutanix filler for the given OS and Kubernetes version.
func (n *Nutanix) templateForKubeVersionAndOS(kubeVersion anywherev1.KubernetesVersion, os OS, release *releasev1.EksARelease) api.NutanixFiller {
var template string
if release == nil {
template = n.templateForDevRelease(kubeVersion, os)
} else {
template = n.templatesRegistry.templateForRelease(n.t, release, kubeVersion, os)
}
return api.WithNutanixMachineTemplateImageName(template)
}
// Ubuntu123Template returns a Nutanix filler for 1.23 Ubuntu.
func (n *Nutanix) Ubuntu123Template() api.NutanixFiller {
return n.templateForKubeVersionAndOS(anywherev1.Kube123, Ubuntu2004, nil)
}
// Ubuntu124Template returns NutanixFiller by reading the env var and setting machine config's
// image name parameter in the spec.
func (n *Nutanix) Ubuntu124Template() api.NutanixFiller {
return n.templateForKubeVersionAndOS(anywherev1.Kube124, Ubuntu2004, nil)
}
// Ubuntu125Template returns NutanixFiller by reading the env var and setting machine config's
// image name parameter in the spec.
func (n *Nutanix) Ubuntu125Template() api.NutanixFiller {
return n.templateForKubeVersionAndOS(anywherev1.Kube125, Ubuntu2004, nil)
}
// Ubuntu126Template returns NutanixFiller by reading the env var and setting machine config's
// image name parameter in the spec.
func (n *Nutanix) Ubuntu126Template() api.NutanixFiller {
return n.templateForKubeVersionAndOS(anywherev1.Kube126, Ubuntu2004, nil)
}
// Ubuntu127Template returns NutanixFiller by reading the env var and setting machine config's
// image name parameter in the spec.
func (n *Nutanix) Ubuntu127Template() api.NutanixFiller {
return n.templateForKubeVersionAndOS(anywherev1.Kube127, Ubuntu2004, nil)
}
// ClusterStateValidations returns a list of provider specific ClusterStateValidations.
func (n *Nutanix) ClusterStateValidations() []clusterf.StateValidation {
return []clusterf.StateValidation{}
}
func (n *Nutanix) getDevRelease() *releasev1.EksARelease {
n.t.Helper()
if n.devRelease == nil {
latestRelease, err := getLatestDevRelease()
if err != nil {
n.t.Fatal(err)
}
n.devRelease = latestRelease
}
return n.devRelease
}
func (n *Nutanix) templateForDevRelease(kubeVersion anywherev1.KubernetesVersion, os OS) string {
n.t.Helper()
return n.templatesRegistry.templateForRelease(n.t, n.getDevRelease(), kubeVersion, os)
}
// envVarForTemplate looks for explicit configuration through an env var: "T_NUTANIX_TEMPLATE_{osFamily}_{eks-d version}"
// eg: T_NUTANIX_TEMPLATE_UBUNTU_KUBERNETES_1_23_EKS_22.
func (n *Nutanix) envVarForTemplate(os OS, eksDName string) string {
return fmt.Sprintf("T_NUTANIX_TEMPLATE_%s_%s", strings.ToUpper(strings.ReplaceAll(string(os), "-", "_")), strings.ToUpper(strings.ReplaceAll(eksDName, "-", "_")))
}
// defaultNameForTemplate looks for a template: "{eks-d version}-{osFamily}"
// eg: kubernetes-1-23-eks-22-ubuntu.
func (n *Nutanix) defaultNameForTemplate(os OS, eksDName string) string {
return fmt.Sprintf("%s-%s", strings.ToLower(eksDName), strings.ToLower(string(os)))
}
// defaultEnvVarForTemplate returns the value of the default template env vars: "T_NUTANIX_TEMPLATE_{osFamily}_{kubeVersion}"
// eg. T_NUTANIX_TEMPLATE_UBUNTU_1_23.
func (n *Nutanix) defaultEnvVarForTemplate(os OS, kubeVersion anywherev1.KubernetesVersion) string {
return fmt.Sprintf("T_NUTANIX_TEMPLATE_NAME_%s_%s", strings.ToUpper(strings.ReplaceAll(string(os), "-", "_")), strings.ReplaceAll(string(kubeVersion), ".", "_"))
}
// searchTemplate returns template name if the given template exists in Prism Central.
func (n *Nutanix) searchTemplate(ctx context.Context, template string) (string, error) {
// TODO: implement search functionality for Nutanix templates
return "", nil
}