Skip to content

Commit 98103a7

Browse files
committed
fixup! feat: Nutanix VM image preflight check
Run VM Image check only if Nutanix clients are initialized. This avoids returning errors that are not actionable. The user will see an error from the credentials check only.
1 parent af39d0c commit 98103a7

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

pkg/webhook/preflight/nutanix/checker.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ type nutanixChecker struct {
3939
nutanixClusterConfigSpec *carenv1.NutanixClusterConfigSpec
4040
nutanixWorkerNodeConfigSpecByMachineDeploymentName map[string]*carenv1.NutanixWorkerNodeConfigSpec
4141

42-
credentials prismgoclient.Credentials
43-
4442
v3client v3client
4543
v3clientFactory func(prismgoclient.Credentials) (v3client, error)
4644

pkg/webhook/preflight/nutanix/credentials.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func initCredentialsCheck(
146146
}
147147

148148
// Initialize the credentials.
149-
n.credentials = prismgoclient.Credentials{
149+
credentials := prismgoclient.Credentials{
150150
Endpoint: fmt.Sprintf("%s:%d", host, port),
151151
URL: fmt.Sprintf("https://%s:%d", host, port),
152152
Username: usernamePassword.Username,
@@ -155,7 +155,7 @@ func initCredentialsCheck(
155155
}
156156

157157
// Initialize the clients.
158-
n.v4client, err = n.v4clientFactory(n.credentials)
158+
v4client, err := n.v4clientFactory(credentials)
159159
if err != nil {
160160
result.Allowed = false
161161
result.Error = true
@@ -167,7 +167,7 @@ func initCredentialsCheck(
167167
)
168168
}
169169

170-
n.v3client, err = n.v3clientFactory(n.credentials)
170+
v3client, err := n.v3clientFactory(credentials)
171171
if err != nil {
172172
result.Allowed = false
173173
result.Error = true
@@ -177,21 +177,35 @@ func initCredentialsCheck(
177177
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials",
178178
},
179179
)
180-
} else {
181-
// Validate the credentials using an API call.
182-
_, err = n.v3client.GetCurrentLoggedInUser(ctx)
183-
if err != nil {
184-
result.Allowed = false
185-
result.Error = true
186-
result.Causes = append(result.Causes,
187-
preflight.Cause{
188-
Message: fmt.Sprintf("failed to validate credentials using the v3 API client: %s", err),
189-
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials",
190-
},
191-
)
180+
}
181+
182+
if v3client == nil || v4client == nil {
183+
return func(ctx context.Context) preflight.CheckResult {
184+
return result
192185
}
193186
}
194187

188+
// Validate the credentials using an API call.
189+
_, err = v3client.GetCurrentLoggedInUser(ctx)
190+
if err != nil {
191+
result.Allowed = false
192+
result.Error = true
193+
result.Causes = append(result.Causes,
194+
preflight.Cause{
195+
Message: fmt.Sprintf("Failed to validate credentials using the v3 API client. "+
196+
"The URL and/or credentials may be incorrect. (Error: %q)", err),
197+
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint",
198+
},
199+
)
200+
return func(ctx context.Context) preflight.CheckResult {
201+
return result
202+
}
203+
}
204+
205+
// We initialized both clients, and verified the credentials using the v3 client.
206+
n.v3client = v3client
207+
n.v4client = v4client
208+
195209
return func(ctx context.Context) preflight.CheckResult {
196210
return result
197211
}

pkg/webhook/preflight/nutanix/image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func initVMImageChecks(
1919
) []preflight.Check {
2020
checks := []preflight.Check{}
2121

22+
if n.v3client == nil || n.v4client == nil {
23+
return checks
24+
}
25+
2226
if n.nutanixClusterConfigSpec != nil && n.nutanixClusterConfigSpec.ControlPlane != nil &&
2327
n.nutanixClusterConfigSpec.ControlPlane.Nutanix != nil {
2428
checks = append(checks,

0 commit comments

Comments
 (0)