-
Notifications
You must be signed in to change notification settings - Fork 286
/
validator.go
311 lines (264 loc) Β· 10.3 KB
/
validator.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
package nutanix
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/nutanix-cloud-native/prism-go-client/utils"
v3 "github.com/nutanix-cloud-native/prism-go-client/v3"
"k8s.io/apimachinery/pkg/api/resource"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/crypto"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/networkutils"
)
const (
minNutanixCPUSockets = 1
minNutanixCPUPerSocket = 1
minNutanixMemoryMiB = 2048
minNutanixDiskGiB = 20
)
// Validator is a client to validate nutanix resources.
type Validator struct {
client Client
httpClient *http.Client
certValidator crypto.TlsValidator
}
// NewValidator returns a new validator client.
func NewValidator(client Client, certValidator crypto.TlsValidator, httpClient *http.Client) *Validator {
return &Validator{
client: client,
certValidator: certValidator,
httpClient: httpClient,
}
}
// ValidateDatacenterConfig validates the datacenter config.
func (v *Validator) ValidateDatacenterConfig(ctx context.Context, config *anywherev1.NutanixDatacenterConfig) error {
if config.Spec.Insecure {
logger.Info("Warning: Skipping TLS validation for insecure connection to Nutanix Prism Central; this is not recommended for production use")
}
if err := v.validateEndpointAndPort(config.Spec); err != nil {
return err
}
if err := v.validateCredentials(ctx); err != nil {
return err
}
if err := v.validateTrustBundleConfig(config.Spec); err != nil {
return err
}
return nil
}
func (v *Validator) validateEndpointAndPort(dcConf anywherev1.NutanixDatacenterConfigSpec) error {
if !networkutils.IsPortValid(strconv.Itoa(dcConf.Port)) {
return fmt.Errorf("nutanix prism central port %d out of range", dcConf.Port)
}
if dcConf.Endpoint == "" {
return fmt.Errorf("nutanix prism central endpoint must be provided")
}
server := fmt.Sprintf("%s:%d", dcConf.Endpoint, dcConf.Port)
if !strings.HasPrefix(server, "https://") {
server = fmt.Sprintf("https://%s", server)
}
if _, err := v.httpClient.Get(server); err != nil {
return fmt.Errorf("failed to reach server %s: %v", server, err)
}
return nil
}
func (v *Validator) validateCredentials(ctx context.Context) error {
_, err := v.client.GetCurrentLoggedInUser(ctx)
if err != nil {
return err
}
return nil
}
func (v *Validator) validateTrustBundleConfig(dcConf anywherev1.NutanixDatacenterConfigSpec) error {
if dcConf.AdditionalTrustBundle == "" {
return nil
}
return v.certValidator.ValidateCert(dcConf.Endpoint, fmt.Sprintf("%d", dcConf.Port), dcConf.AdditionalTrustBundle)
}
func (v *Validator) validateMachineSpecs(machineSpec anywherev1.NutanixMachineConfigSpec) error {
if machineSpec.VCPUSockets < minNutanixCPUSockets {
return fmt.Errorf("vCPU sockets %d must be greater than or equal to %d", machineSpec.VCPUSockets, minNutanixCPUSockets)
}
if machineSpec.VCPUsPerSocket < minNutanixCPUPerSocket {
return fmt.Errorf("vCPUs per socket %d must be greater than or equal to %d", machineSpec.VCPUsPerSocket, minNutanixCPUPerSocket)
}
minNutanixMemory, err := resource.ParseQuantity(fmt.Sprintf("%dMi", minNutanixMemoryMiB))
if err != nil {
return err
}
if machineSpec.MemorySize.Cmp(minNutanixMemory) < 0 {
return fmt.Errorf("MemorySize must be greater than or equal to %dMi", minNutanixMemoryMiB)
}
minNutanixDisk, err := resource.ParseQuantity(fmt.Sprintf("%dGi", minNutanixDiskGiB))
if err != nil {
return err
}
if machineSpec.SystemDiskSize.Cmp(minNutanixDisk) < 0 {
return fmt.Errorf("SystemDiskSize must be greater than or equal to %dGi", minNutanixDiskGiB)
}
return nil
}
// ValidateMachineConfig validates the Prism Element cluster, subnet, and image for the machine.
func (v *Validator) ValidateMachineConfig(ctx context.Context, config *anywherev1.NutanixMachineConfig) error {
if err := v.validateMachineSpecs(config.Spec); err != nil {
return err
}
if err := v.validateClusterConfig(ctx, config.Spec.Cluster); err != nil {
return err
}
if err := v.validateSubnetConfig(ctx, config.Spec.Subnet); err != nil {
return err
}
if err := v.validateImageConfig(ctx, config.Spec.Image); err != nil {
return err
}
return nil
}
func (v *Validator) validateClusterConfig(ctx context.Context, identifier anywherev1.NutanixResourceIdentifier) error {
switch identifier.Type {
case anywherev1.NutanixIdentifierName:
if identifier.Name == nil || *identifier.Name == "" {
return fmt.Errorf("missing cluster name")
} else {
clusterName := *identifier.Name
if _, err := findClusterUUIDByName(ctx, v.client, clusterName); err != nil {
return fmt.Errorf("failed to find cluster with name %q: %v", clusterName, err)
}
}
case anywherev1.NutanixIdentifierUUID:
if identifier.UUID == nil || *identifier.UUID == "" {
return fmt.Errorf("missing cluster uuid")
} else {
clusterUUID := *identifier.UUID
if _, err := v.client.GetCluster(ctx, clusterUUID); err != nil {
return fmt.Errorf("failed to find cluster with uuid %v: %v", clusterUUID, err)
}
}
default:
return fmt.Errorf("invalid cluster identifier type: %s; valid types are: %q and %q", identifier.Type, anywherev1.NutanixIdentifierName, anywherev1.NutanixIdentifierUUID)
}
return nil
}
func (v *Validator) validateImageConfig(ctx context.Context, identifier anywherev1.NutanixResourceIdentifier) error {
switch identifier.Type {
case anywherev1.NutanixIdentifierName:
if identifier.Name == nil || *identifier.Name == "" {
return fmt.Errorf("missing image name")
} else {
imageName := *identifier.Name
if _, err := findImageUUIDByName(ctx, v.client, imageName); err != nil {
return fmt.Errorf("failed to find image with name %q: %v", imageName, err)
}
}
case anywherev1.NutanixIdentifierUUID:
if identifier.UUID == nil || *identifier.UUID == "" {
return fmt.Errorf("missing image uuid")
} else {
imageUUID := *identifier.UUID
if _, err := v.client.GetImage(ctx, imageUUID); err != nil {
return fmt.Errorf("failed to find image with uuid %s: %v", imageUUID, err)
}
}
default:
return fmt.Errorf("invalid image identifier type: %s; valid types are: %q and %q", identifier.Type, anywherev1.NutanixIdentifierName, anywherev1.NutanixIdentifierUUID)
}
return nil
}
func (v *Validator) validateSubnetConfig(ctx context.Context, identifier anywherev1.NutanixResourceIdentifier) error {
switch identifier.Type {
case anywherev1.NutanixIdentifierName:
if identifier.Name == nil || *identifier.Name == "" {
return fmt.Errorf("missing subnet name")
} else {
subnetName := *identifier.Name
if _, err := findSubnetUUIDByName(ctx, v.client, subnetName); err != nil {
return fmt.Errorf("failed to find subnet with name %s: %v", subnetName, err)
}
}
case anywherev1.NutanixIdentifierUUID:
if identifier.UUID == nil || *identifier.UUID == "" {
return fmt.Errorf("missing subnet uuid")
} else {
subnetUUID := *identifier.UUID
if _, err := v.client.GetSubnet(ctx, subnetUUID); err != nil {
return fmt.Errorf("failed to find subnet with uuid %s: %v", subnetUUID, err)
}
}
default:
return fmt.Errorf("invalid subnet identifier type: %s; valid types are: %q and %q", identifier.Type, anywherev1.NutanixIdentifierName, anywherev1.NutanixIdentifierUUID)
}
return nil
}
// findSubnetUUIDByName retrieves the subnet uuid by the given subnet name.
func findSubnetUUIDByName(ctx context.Context, v3Client Client, subnetName string) (*string, error) {
res, err := v3Client.ListSubnet(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", subnetName)),
})
if err != nil || len(res.Entities) == 0 {
return nil, fmt.Errorf("failed to find subnet by name %q: %v", subnetName, err)
}
if len(res.Entities) > 1 {
return nil, fmt.Errorf("found more than one (%v) subnet with name %q", len(res.Entities), subnetName)
}
return res.Entities[0].Metadata.UUID, nil
}
// findClusterUUIDByName retrieves the cluster uuid by the given cluster name.
func findClusterUUIDByName(ctx context.Context, v3Client Client, clusterName string) (*string, error) {
res, err := v3Client.ListCluster(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", clusterName)),
})
if err != nil {
return nil, fmt.Errorf("failed to find cluster by name %q: %v", clusterName, err)
}
entities := make([]*v3.ClusterIntentResponse, 0)
for _, entity := range res.Entities {
if entity.Status != nil && entity.Status.Resources != nil && entity.Status.Resources.Config != nil {
serviceList := entity.Status.Resources.Config.ServiceList
isPrismCentral := false
for _, svc := range serviceList {
// Prism Central is also internally a cluster, but we filter that out here as we only care about prism element clusters
if svc != nil && strings.ToUpper(*svc) == "PRISM_CENTRAL" {
isPrismCentral = true
}
}
if !isPrismCentral && *entity.Spec.Name == clusterName {
entities = append(entities, entity)
}
}
}
if len(entities) == 0 {
return nil, fmt.Errorf("failed to find cluster by name %q: %v", clusterName, err)
}
if len(entities) > 1 {
return nil, fmt.Errorf("found more than one (%v) cluster with name %q", len(entities), clusterName)
}
return entities[0].Metadata.UUID, nil
}
// findImageUUIDByName retrieves the image uuid by the given image name.
func findImageUUIDByName(ctx context.Context, v3Client Client, imageName string) (*string, error) {
res, err := v3Client.ListImage(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", imageName)),
})
if err != nil || len(res.Entities) == 0 {
return nil, fmt.Errorf("failed to find image by name %q: %v", imageName, err)
}
if len(res.Entities) > 1 {
return nil, fmt.Errorf("found more than one (%v) image with name %q", len(res.Entities), imageName)
}
return res.Entities[0].Metadata.UUID, nil
}
func (v *Validator) validateUpgradeRolloutStrategy(clusterSpec *cluster.Spec) error {
if clusterSpec.Cluster.Spec.ControlPlaneConfiguration.UpgradeRolloutStrategy != nil {
return fmt.Errorf("Upgrade rollout strategy customization is not supported for nutanix provider")
}
for _, workerNodeGroupConfiguration := range clusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
if workerNodeGroupConfiguration.UpgradeRolloutStrategy != nil {
return fmt.Errorf("Upgrade rollout strategy customization is not supported for nutanix provider")
}
}
return nil
}