Skip to content

Commit 32649d9

Browse files
committed
fix: Reflect correct action in message
1 parent ee406cb commit 32649d9

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

pkg/webhook/preflight/nutanix/storagecontainer.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,36 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
5555

5656
// Get cluster identifier based on whether failure domain is configured
5757
var clusterIdentifier *capxv1.NutanixResourceIdentifier
58-
var err error
5958

6059
if c.failureDomainName != "" {
61-
// Get cluster identifier from failure domain
62-
clusterIdentifier, err = c.getClusterIdentifierFromFailureDomain(ctx)
60+
// Use cluster identifier from failure domain
61+
fdObj := &capxv1.NutanixFailureDomain{}
62+
fdKey := ctrlclient.ObjectKey{Name: c.failureDomainName, Namespace: c.namespace}
63+
err := c.kclient.Get(ctx, fdKey, fdObj)
6364
if err != nil {
6465
result.Allowed = false
6566
if errors.IsNotFound(err) {
6667
result.Causes = append(result.Causes, preflight.Cause{
6768
Message: fmt.Sprintf(
68-
"NutanixFailureDomain %q referenced in cluster was not found in the management cluster. Please create it and retry.", //nolint:lll // Message is long.
69+
"NutanixFailureDomain %q was not found in the management cluster. Please create it and retry.", //nolint:lll // Message is long.
6970
c.failureDomainName,
7071
),
71-
Field: c.field,
72+
Field: c.field + ".failureDomain",
7273
})
7374
} else {
7475
result.InternalError = true
7576
result.Causes = append(result.Causes, preflight.Cause{
7677
Message: fmt.Sprintf(
77-
"Failed to get cluster identifier from NutanixFailureDomain %q: %v This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
78+
"Failed to get NutanixFailureDomain %q: %s. This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
7879
c.failureDomainName,
7980
err,
8081
),
81-
Field: c.field,
82+
Field: c.field + ".failureDomain",
8283
})
8384
}
8485
return result
8586
}
87+
clusterIdentifier = &fdObj.Spec.PrismElementCluster
8688
} else {
8789
// Use cluster identifier from machine spec
8890
clusterIdentifier = c.machineSpec.Cluster
@@ -192,19 +194,6 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
192194
return result
193195
}
194196

195-
// getClusterIdentifierFromFailureDomain fetches the failure domain and returns the cluster identifier.
196-
func (c *storageContainerCheck) getClusterIdentifierFromFailureDomain(
197-
ctx context.Context,
198-
) (*capxv1.NutanixResourceIdentifier, error) {
199-
fdObj := &capxv1.NutanixFailureDomain{}
200-
fdKey := ctrlclient.ObjectKey{Name: c.failureDomainName, Namespace: c.namespace}
201-
if err := c.kclient.Get(ctx, fdKey, fdObj); err != nil {
202-
return nil, err
203-
}
204-
205-
return &fdObj.Spec.PrismElementCluster, nil
206-
}
207-
208197
func newStorageContainerChecks(cd *checkDependencies) []preflight.Check {
209198
checks := []preflight.Check{}
210199

pkg/webhook/preflight/nutanix/storagecontainer_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ func TestStorageContainerCheck(t *testing.T) {
398398
expectedAllowed bool
399399
expectedError bool
400400
expectedCauseMessage string
401+
expectedField string
401402
failureDomainName string
402403
kclient ctrlclient.Client
403404
namespace string
@@ -414,7 +415,7 @@ func TestStorageContainerCheck(t *testing.T) {
414415
nclient: nil,
415416
expectedAllowed: false,
416417
expectedError: false,
417-
expectedCauseMessage: "Nutanix CSI Provider configuration is missing storage class configurations",
418+
expectedCauseMessage: "Nutanix CSI Provider configuration is missing storage class configurations. Review the Cluster.", //nolint:lll // The message is long.
418419
},
419420
{
420421
name: "storage class config without parameters",
@@ -1154,7 +1155,8 @@ func TestStorageContainerCheck(t *testing.T) {
11541155
namespace: "test-ns",
11551156
expectedAllowed: false,
11561157
expectedError: false,
1157-
expectedCauseMessage: "NutanixFailureDomain \"non-existent-fd\" referenced in cluster was not found in the management cluster. Please create it and retry.", //nolint:lll // The message is long.
1158+
expectedCauseMessage: "NutanixFailureDomain \"non-existent-fd\" was not found in the management cluster. Please create it and retry.", //nolint:lll // The message is long.
1159+
expectedField: field + ".failureDomain",
11581160
},
11591161
{
11601162
name: "error getting failure domain",
@@ -1183,7 +1185,8 @@ func TestStorageContainerCheck(t *testing.T) {
11831185
namespace: "test-ns",
11841186
expectedAllowed: false,
11851187
expectedError: true,
1186-
expectedCauseMessage: "Failed to get cluster identifier from NutanixFailureDomain \"fd-with-error\": kube API error This is usually a temporary error. Please retry.", //nolint:lll // The message is long.
1188+
expectedCauseMessage: "Failed to get NutanixFailureDomain \"fd-with-error\": kube API error. This is usually a temporary error. Please retry.", //nolint:lll // The message is long.
1189+
expectedField: field + ".failureDomain",
11871190
},
11881191
}
11891192

@@ -1208,12 +1211,16 @@ func TestStorageContainerCheck(t *testing.T) {
12081211
assert.Equal(t, tc.expectedAllowed, result.Allowed)
12091212
assert.Equal(t, tc.expectedError, result.InternalError)
12101213

1211-
if tc.expectedCauseMessage != "" {
1214+
if !tc.expectedAllowed {
12121215
require.NotEmpty(t, result.Causes)
1213-
assert.Contains(t, result.Causes[0].Message, tc.expectedCauseMessage)
1214-
assert.Equal(t, field, result.Causes[0].Field)
1215-
} else {
1216-
assert.Empty(t, result.Causes)
1216+
1217+
if tc.expectedCauseMessage != "" {
1218+
assert.Equal(t, tc.expectedCauseMessage, result.Causes[0].Message)
1219+
}
1220+
1221+
if tc.expectedField != "" {
1222+
assert.Equal(t, tc.expectedField, result.Causes[0].Field)
1223+
}
12171224
}
12181225
})
12191226
}

0 commit comments

Comments
 (0)