-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathnutanix.go
More file actions
351 lines (308 loc) · 9.9 KB
/
Copy pathnutanix.go
File metadata and controls
351 lines (308 loc) · 9.9 KB
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
// Package nutanix collects Nutanix-specific configuration.
package nutanix
import (
"context"
"fmt"
"sort"
"strconv"
"time"
"github.com/AlecAivazis/survey/v2"
nutanixclient "github.com/nutanix-cloud-native/prism-go-client"
nutanixclientv3 "github.com/nutanix-cloud-native/prism-go-client/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/openshift/installer/pkg/types/nutanix"
nutanixtypes "github.com/openshift/installer/pkg/types/nutanix"
"github.com/openshift/installer/pkg/validate"
)
// PrismCentralClient wraps a Nutanix V3 client
type PrismCentralClient struct {
PrismCentral string
Username string
Password string
Port string
V3Client *nutanixclientv3.Client
}
// Platform collects Nutanix-specific configuration.
func Platform() (*nutanix.Platform, error) {
nutanixClient, err := getClients()
if err != nil {
return nil, err
}
portNum, err := strconv.Atoi(nutanixClient.Port)
if err != nil {
return nil, err
}
pc := nutanixtypes.PrismCentral{
Endpoint: nutanix.PrismEndpoint{
Address: nutanixClient.PrismCentral,
Port: int32(portNum),
},
Username: nutanixClient.Username,
Password: nutanixClient.Password,
}
ctx := context.TODO()
v3Client := nutanixClient.V3Client
pe, err := getPrismElement(ctx, v3Client)
if err != nil {
return nil, err
}
pe.Endpoint.Port = int32(portNum)
subnetUUID, err := getSubnet(ctx, v3Client, pe.UUID)
if err != nil {
return nil, err
}
apiVIP, ingressVIP, err := getVIPs()
if err != nil {
return nil, errors.Wrap(err, "failed to get VIPs")
}
platform := &nutanix.Platform{
PrismCentral: pc,
PrismElements: []nutanixtypes.PrismElement{*pe},
SubnetUUIDs: []string{subnetUUID},
APIVIPs: []string{apiVIP},
IngressVIPs: []string{ingressVIP},
}
return platform, nil
}
// getClients() surveys the user for username, password, port & prism central.
// Validation on the three fields is performed by creating a client.
// If creating the client fails, an error is returned.
func getClients() (*PrismCentralClient, error) {
var prismCentral, port, username, password string
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Prism Central",
Help: "The domain name or IP address of the Prism Central to be used for installation.",
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
return validate.Host(ans.(string))
}),
},
}, &prismCentral); err != nil {
return nil, errors.Wrap(err, "failed UserInput")
}
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Port",
Help: "The port used to login to Prism Central.",
Default: "9440",
},
Validate: survey.Required,
},
}, &port); err != nil {
return nil, errors.Wrap(err, "failed UserInput")
}
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Username",
Help: "The username to login to the Prism Central.",
},
Validate: survey.Required,
},
}, &username); err != nil {
return nil, errors.Wrap(err, "failed UserInput")
}
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Password{
Message: "Password",
Help: "The password to login to Prism Central.",
},
Validate: survey.Required,
},
}, &password); err != nil {
return nil, errors.Wrap(err, "failed UserInput")
}
// There is a noticeable delay when creating the client, so let the user know what's going on.
logrus.Infof("Connecting to Prism Central %s", prismCentral)
clientV3, err := nutanixtypes.CreateNutanixClient(context.TODO(),
prismCentral,
port,
username,
password,
)
if err != nil {
return nil, errors.Wrapf(err, "unable to connect to Prism Central %s. Ensure provided information is correct", prismCentral)
}
return &PrismCentralClient{
PrismCentral: prismCentral,
Username: username,
Password: password,
Port: port,
V3Client: clientV3,
}, nil
}
func getPrismElement(ctx context.Context, client *nutanixclientv3.Client) (*nutanixtypes.PrismElement, error) {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
pe := &nutanixtypes.PrismElement{}
emptyFilter := ""
pesAll, err := client.V3.ListAllCluster(ctx, emptyFilter)
if err != nil {
return nil, errors.Wrap(err, "unable to list prism element clusters")
}
pes := pesAll.Entities
if len(pes) == 0 {
return nil, errors.New("did not find any prism element clusters")
}
// Filter out Prism Central clusters - we only want Prism Elements
filteredPes := make([]*nutanixclientv3.ClusterIntentResponse, 0, len(pes))
for _, p := range pes {
// Skip Prism Central clusters by checking the service list
if p.Status != nil && p.Status.Resources != nil && p.Status.Resources.Config != nil &&
p.Status.Resources.Config.ServiceList != nil {
isPrismCentral := false
for _, service := range p.Status.Resources.Config.ServiceList {
if service != nil && *service == "PRISM_CENTRAL" {
isPrismCentral = true
break
}
}
if isPrismCentral {
continue
}
}
filteredPes = append(filteredPes, p)
}
pes = filteredPes
if len(pes) == 0 {
return nil, errors.New("did not find any prism element clusters")
}
if len(pes) == 1 {
p := pes[0]
switch {
case p.Metadata == nil || p.Metadata.UUID == nil:
return nil, errors.New("missing UUID in Prism Element metadata")
case p.Spec == nil || p.Spec.Resources == nil:
return nil, errors.New("missing Resources in Prism Element spec")
case p.Spec.Resources.Network == nil || p.Spec.Resources.Network.ExternalIP == "":
return nil, errors.New("missing ExternalIP in Prism Element network spec")
}
pe.UUID = *p.Metadata.UUID
pe.Endpoint.Address = p.Spec.Resources.Network.ExternalIP
logrus.Infof("Defaulting to only available prism element (cluster): %s", p.Spec.Name)
return pe, nil
}
pesMap := make(map[string]*nutanixclientv3.ClusterIntentResponse)
var peChoices []string
for _, p := range pes {
n := p.Spec.Name
pesMap[n] = p
peChoices = append(peChoices, n)
}
var selectedPe string
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Prism Element",
Options: peChoices,
Help: "The Prism Element to be used for installation.",
},
Validate: survey.Required,
},
}, &selectedPe); err != nil {
return nil, errors.Wrap(err, "failed UserInput")
}
peEntry, ok := pesMap[selectedPe]
if !ok {
return nil, fmt.Errorf("prism Element %q not found", selectedPe)
}
switch {
case peEntry.Metadata == nil || peEntry.Metadata.UUID == nil:
return nil, fmt.Errorf("missing UUID in Prism Element metadata for %q", selectedPe)
case peEntry.Spec == nil || peEntry.Spec.Resources == nil:
return nil, fmt.Errorf("missing Resources in Prism Element spec for %q", selectedPe)
case peEntry.Spec.Resources.Network == nil || peEntry.Spec.Resources.Network.ExternalIP == "":
return nil, fmt.Errorf("missing ExternalIP in Prism Element network spec for %q", selectedPe)
}
pe.UUID = *peEntry.Metadata.UUID
pe.Endpoint.Address = peEntry.Spec.Resources.Network.ExternalIP
return pe, nil
}
func getSubnet(ctx context.Context, client *nutanixclientv3.Client, peUUID string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
emptyFilter := ""
emptyClientFilters := make([]*nutanixclient.AdditionalFilter, 0)
subnetsAll, err := client.V3.ListAllSubnet(ctx, emptyFilter, emptyClientFilters)
if err != nil {
return "", errors.Wrap(err, "unable to list subnets")
}
subnets := subnetsAll.Entities
// API returns an error when no results, but let's leave this in to be defensive.
if len(subnets) == 0 {
return "", errors.New("did not find any subnets")
}
if len(subnets) == 1 {
n := *subnets[0].Spec.Name
u := *subnets[0].Metadata.UUID
logrus.Infof("Defaulting to only available network: %s", n)
return u, nil
}
subnetUUIDs := make(map[string]string)
var subnetChoices []string
for _, subnet := range subnets {
// some subnet types (e.g. VPC overlays) do not come with a cluster reference; we don't need to check them
if subnet.Spec.ClusterReference == nil || (subnet.Spec.ClusterReference.UUID != nil && *subnet.Spec.ClusterReference.UUID == peUUID) {
n := *subnet.Spec.Name
subnetUUIDs[n] = *subnet.Metadata.UUID
subnetChoices = append(subnetChoices, n)
}
}
if len(subnetChoices) == 0 {
return "", errors.New(fmt.Sprintf("could not find any subnets linked to Prism Element with UUID %s", peUUID))
}
sort.Strings(subnetChoices)
var selectedSubnet string
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Subnet",
Options: subnetChoices,
Help: "The subnet to be used for installation.",
},
Validate: survey.Required,
},
}, &selectedSubnet); err != nil {
return "", errors.Wrap(err, "failed UserInput")
}
return subnetUUIDs[selectedSubnet], nil
}
func getVIPs() (string, string, error) {
var apiVIP, ingressVIP string
//TODO: Add support to specify multiple VIPs (-> dual-stack)
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Virtual IP Address for API",
Help: "The VIP to be used for the OpenShift API.",
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
return validate.IP((ans).(string))
}),
},
}, &apiVIP); err != nil {
return "", "", errors.Wrap(err, "failed UserInput")
}
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Virtual IP Address for Ingress",
Help: "The VIP to be used for ingress to the cluster.",
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
if apiVIP == (ans.(string)) {
return fmt.Errorf("%q should not be equal to the Virtual IP address for the API", ans.(string))
}
return validate.IP((ans).(string))
}),
},
}, &ingressVIP); err != nil {
return "", "", errors.Wrap(err, "failed UserInput")
}
return apiVIP, ingressVIP, nil
}