@@ -18,38 +18,47 @@ import (
18
18
19
19
const credentialsSecretDataKey = "credentials"
20
20
21
+ type credentialsCheck struct {
22
+ result preflight.CheckResult
23
+ }
24
+
25
+ func (c * credentialsCheck ) Name () string {
26
+ return "NutanixCredentials"
27
+ }
28
+
29
+ func (c * credentialsCheck ) Run (_ context.Context ) preflight.CheckResult {
30
+ return c .result
31
+ }
32
+
21
33
func initCredentialsCheck (
22
34
ctx context.Context ,
23
35
n * nutanixChecker ,
24
36
) preflight.Check {
25
37
n .log .V (5 ).Info ("Initializing Nutanix credentials check" )
26
38
27
- result := preflight.CheckResult {
28
- Name : "NutanixCredentials" ,
29
- Allowed : true ,
39
+ credentialsCheck := & credentialsCheck {
40
+ result : preflight.CheckResult {
41
+ Allowed : true ,
42
+ },
30
43
}
31
44
32
45
if n .nutanixClusterConfigSpec == nil && len (n .nutanixWorkerNodeConfigSpecByMachineDeploymentName ) == 0 {
33
46
// If there is no Nutanix configuration at all, the credentials check is not needed.
34
- return func (ctx context.Context ) preflight.CheckResult {
35
- return result
36
- }
47
+ return credentialsCheck
37
48
}
38
49
39
50
// There is some Nutanix configuration, so the credentials check is needed.
40
51
// However, the credentials configuration is missing, so we cannot perform the check.
41
52
if n .nutanixClusterConfigSpec == nil || n .nutanixClusterConfigSpec .Nutanix == nil {
42
- result .Allowed = false
43
- result .Error = true
44
- result .Causes = append (result .Causes ,
53
+ credentialsCheck . result .Allowed = false
54
+ credentialsCheck . result .Error = true
55
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
45
56
preflight.Cause {
46
57
Message : "Nutanix cluster configuration is not defined in the cluster spec" ,
47
58
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix" ,
48
59
},
49
60
)
50
- return func (ctx context.Context ) preflight.CheckResult {
51
- return result
52
- }
61
+ return credentialsCheck
53
62
}
54
63
55
64
// Get the credentials data in order to initialize the credentials and clients.
@@ -58,17 +67,15 @@ func initCredentialsCheck(
58
67
host , port , err := prismCentralEndpointSpec .ParseURL ()
59
68
if err != nil {
60
69
// Should not happen if the cluster passed CEL validation rules.
61
- result .Allowed = false
62
- result .Error = true
63
- result .Causes = append (result .Causes ,
70
+ credentialsCheck . result .Allowed = false
71
+ credentialsCheck . result .Error = true
72
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
64
73
preflight.Cause {
65
74
Message : fmt .Sprintf ("failed to parse Prism Central endpoint URL: %s" , err ),
66
75
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.url" ,
67
76
},
68
77
)
69
- return func (ctx context.Context ) preflight.CheckResult {
70
- return result
71
- }
78
+ return credentialsCheck
72
79
}
73
80
74
81
credentialsSecret := & corev1.Secret {}
@@ -81,23 +88,21 @@ func initCredentialsCheck(
81
88
credentialsSecret ,
82
89
)
83
90
if err != nil {
84
- result .Allowed = false
85
- result .Error = true
86
- result .Causes = append (result .Causes ,
91
+ credentialsCheck . result .Allowed = false
92
+ credentialsCheck . result .Error = true
93
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
87
94
preflight.Cause {
88
95
Message : fmt .Sprintf ("failed to get Prism Central credentials Secret: %s" , err ),
89
96
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials.secretRef" ,
90
97
},
91
98
)
92
- return func (ctx context.Context ) preflight.CheckResult {
93
- return result
94
- }
99
+ return credentialsCheck
95
100
}
96
101
97
102
if len (credentialsSecret .Data ) == 0 {
98
- result .Allowed = false
99
- result .Error = true
100
- result .Causes = append (result .Causes ,
103
+ credentialsCheck . result .Allowed = false
104
+ credentialsCheck . result .Error = true
105
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
101
106
preflight.Cause {
102
107
Message : fmt .Sprintf (
103
108
"credentials Secret '%s' is empty" ,
@@ -106,16 +111,14 @@ func initCredentialsCheck(
106
111
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials.secretRef" ,
107
112
},
108
113
)
109
- return func (ctx context.Context ) preflight.CheckResult {
110
- return result
111
- }
114
+ return credentialsCheck
112
115
}
113
116
114
117
data , ok := credentialsSecret .Data [credentialsSecretDataKey ]
115
118
if ! ok {
116
- result .Allowed = false
117
- result .Error = true
118
- result .Causes = append (result .Causes ,
119
+ credentialsCheck . result .Allowed = false
120
+ credentialsCheck . result .Error = true
121
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
119
122
preflight.Cause {
120
123
Message : fmt .Sprintf (
121
124
"credentials Secret '%s' does not contain key '%s'" ,
@@ -125,24 +128,20 @@ func initCredentialsCheck(
125
128
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials.secretRef" ,
126
129
},
127
130
)
128
- return func (ctx context.Context ) preflight.CheckResult {
129
- return result
130
- }
131
+ return credentialsCheck
131
132
}
132
133
133
134
usernamePassword , err := prismcredentials .ParseCredentials (data )
134
135
if err != nil {
135
- result .Allowed = false
136
- result .Error = true
137
- result .Causes = append (result .Causes ,
136
+ credentialsCheck . result .Allowed = false
137
+ credentialsCheck . result .Error = true
138
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
138
139
preflight.Cause {
139
140
Message : fmt .Sprintf ("failed to parse Prism Central credentials: %s" , err ),
140
141
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials" ,
141
142
},
142
143
)
143
- return func (ctx context.Context ) preflight.CheckResult {
144
- return result
145
- }
144
+ return credentialsCheck
146
145
}
147
146
148
147
// Initialize the credentials.
@@ -157,40 +156,34 @@ func initCredentialsCheck(
157
156
// Initialize the Nutanix client.
158
157
nclient , err := n .nclientFactory (credentials )
159
158
if err != nil {
160
- result .Allowed = false
161
- result .Error = true
162
- result .Causes = append (result .Causes ,
159
+ credentialsCheck . result .Allowed = false
160
+ credentialsCheck . result .Error = true
161
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
163
162
preflight.Cause {
164
163
Message : fmt .Sprintf ("Failed to initialize Nutanix client: %s" , err ),
165
164
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials" ,
166
165
},
167
166
)
168
- return func (ctx context.Context ) preflight.CheckResult {
169
- return result
170
- }
167
+ return credentialsCheck
171
168
}
172
169
173
170
// Validate the credentials using an API call.
174
171
_ , err = nclient .GetCurrentLoggedInUser (ctx )
175
172
if err != nil {
176
- result .Allowed = false
177
- result .Error = true
178
- result .Causes = append (result .Causes ,
173
+ credentialsCheck . result .Allowed = false
174
+ credentialsCheck . result .Error = true
175
+ credentialsCheck . result .Causes = append (credentialsCheck . result .Causes ,
179
176
preflight.Cause {
180
177
Message : fmt .Sprintf ("Failed to validate credentials using the v3 API client. " +
181
178
"The URL and/or credentials may be incorrect. (Error: %q)" , err ),
182
179
Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint" ,
183
180
},
184
181
)
185
- return func (ctx context.Context ) preflight.CheckResult {
186
- return result
187
- }
182
+ return credentialsCheck
188
183
}
189
184
190
185
// We initialized both clients, and verified the credentials using the v3 client.
191
186
n .nclient = nclient
192
187
193
- return func (ctx context.Context ) preflight.CheckResult {
194
- return result
195
- }
188
+ return credentialsCheck
196
189
}
0 commit comments