Skip to content

Commit 55455f5

Browse files
authored
fix: More fixes to preflight result messages and fields (#1240)
**What problem does this PR solve?**: I identified more mistakes in preflight check messages and fields. The fixes are in separate commits. **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent 4a69f52 commit 55455f5

File tree

8 files changed

+43
-45
lines changed

8 files changed

+43
-45
lines changed

pkg/webhook/preflight/generic/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newConfigurationCheck(
5050
carenv1.ClusterConfigVariableName,
5151
err,
5252
),
53-
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].genericClusterConfigSpec",
53+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"]",
5454
},
5555
)
5656
}

pkg/webhook/preflight/generic/spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestNewConfigurationCheck(t *testing.T) {
107107
Causes: []preflight.Cause{
108108
{
109109
Message: "Failed to unmarshal cluster variable \"clusterConfig\": failed to unmarshal json: invalid character 'i' looking for beginning of object key string. Review the Cluster.", ///nolint:lll // The message is long.
110-
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].genericClusterConfigSpec",
110+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"]",
111111
},
112112
},
113113
},

pkg/webhook/preflight/nutanix/credentials.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func newCredentialsCheck(
222222
"Failed to validate credentials: %s. This is usually a temporary error. Please retry.", ///nolint:lll // Message is long.
223223
err,
224224
),
225+
// We do not add ".url" or ".credentials.secretRef" to the field, because we do not know
226+
// if the error is related to the URL, or the credentials.
225227
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix.prismCentralEndpoint",
226228
},
227229
)

pkg/webhook/preflight/nutanix/failuredomain.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (fdc *failureDomainCheck) Run(ctx context.Context) preflight.CheckResult {
8888
result.Allowed = false
8989
result.Causes = append(result.Causes, preflight.Cause{
9090
Message: fmt.Sprintf(
91-
"NutanixFailureDomain %q referenced in cluster was not found in the management cluster. Please create it and retry.", //nolint:lll // Message is long.
91+
"NutanixFailureDomain %q was not found in the management cluster. Please create it and retry.", //nolint:lll // Message is long.
9292
fdc.failureDomainName,
9393
),
9494
Field: fdc.field,
@@ -100,7 +100,7 @@ func (fdc *failureDomainCheck) Run(ctx context.Context) preflight.CheckResult {
100100
result.InternalError = true
101101
result.Causes = append(result.Causes, preflight.Cause{
102102
Message: fmt.Sprintf(
103-
"Failed to get NutanixFailureDomain %q: %v This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
103+
"Failed to get NutanixFailureDomain %q: %s This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
104104
fdc.failureDomainName,
105105
err,
106106
),
@@ -118,7 +118,7 @@ func (fdc *failureDomainCheck) Run(ctx context.Context) preflight.CheckResult {
118118
result.InternalError = true
119119
result.Causes = append(result.Causes, preflight.Cause{
120120
Message: fmt.Sprintf(
121-
"Failed to check if the Prism Element cluster %q, referenced by Failure Domain %q, exists: %v This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
121+
"Failed to check if the Prism Element cluster %q, referenced by Failure Domain %q, exists: %s. This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
122122
peIdentifier,
123123
fdc.failureDomainName,
124124
err,
@@ -149,7 +149,7 @@ func (fdc *failureDomainCheck) Run(ctx context.Context) preflight.CheckResult {
149149
result.InternalError = true
150150
result.Causes = append(result.Causes, preflight.Cause{
151151
Message: fmt.Sprintf(
152-
"Failed to get subnet %q referenced by the Failure Domain %q: %v This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
152+
"Failed to get subnet %q referenced by the Failure Domain %q: %s. This is usually a temporary error. Please retry.", //nolint:lll // Message is long.
153153
id,
154154
fdc.failureDomainName,
155155
err,

pkg/webhook/preflight/nutanix/specs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func newConfigurationCheck(
5656
carenv1.ClusterConfigVariableName,
5757
err,
5858
),
59-
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix",
59+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"]",
6060
},
6161
)
6262
}
@@ -88,7 +88,7 @@ func newConfigurationCheck(
8888
)
8989
if workerConfigVar != nil {
9090
workerConfigFieldPath = fmt.Sprintf(
91-
"$.spec.topology.workers.machineDeployments[[email protected]==%q].variables[[email protected]=='%s'].value.nutanix",
91+
"$.spec.topology.workers.machineDeployments[[email protected]==%q].variables[[email protected]=='%s']",
9292
md.Name,
9393
carenv1.WorkerConfigVariableName,
9494
)
@@ -101,7 +101,7 @@ func newConfigurationCheck(
101101
)
102102
if workerConfigVar != nil {
103103
workerConfigFieldPath = fmt.Sprintf(
104-
"$.spec.topology.variables[[email protected]=='%s'].value.nutanix",
104+
"$.spec.topology.variables[[email protected]=='%s']",
105105
carenv1.WorkerConfigVariableName,
106106
)
107107
}

pkg/webhook/preflight/nutanix/specs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestNewConfigurationCheck(t *testing.T) {
138138
Causes: []preflight.Cause{
139139
{
140140
Message: "Failed to unmarshal cluster variable \"clusterConfig\": failed to unmarshal json: invalid character 'i' looking for beginning of object key string. Review the Cluster.", ///nolint:lll // The message is long.
141-
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix",
141+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"]",
142142
},
143143
},
144144
},
@@ -185,7 +185,7 @@ func TestNewConfigurationCheck(t *testing.T) {
185185
Causes: []preflight.Cause{
186186
{
187187
Message: "Failed to unmarshal variable \"workerConfig\": failed to unmarshal json: invalid character 'i' looking for beginning of object key string. Review the Cluster.", ///nolint:lll // The message is long.
188-
Field: "$.spec.topology.workers.machineDeployments[[email protected]==\"md-0\"].variables[[email protected]=='workerConfig'].value.nutanix", ///nolint:lll // The field is long.
188+
Field: "$.spec.topology.workers.machineDeployments[[email protected]==\"md-0\"].variables[[email protected]=='workerConfig']", ///nolint:lll // The field is long.
189189
},
190190
},
191191
},

pkg/webhook/preflight/nutanix/storagecontainer.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,44 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
4747
result.Allowed = false
4848
result.Causes = append(result.Causes, preflight.Cause{
4949
Message: "Nutanix CSI Provider configuration is missing storage class configurations. Review the Cluster.", //nolint:lll // Message is long.
50-
Field: c.field,
50+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.addons.csi.providers.nutanix", //nolint:lll // The field is long.
5151
})
5252

5353
return result
5454
}
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
@@ -118,7 +120,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
118120
clusterIdentifier,
119121
err,
120122
),
121-
Field: c.field,
123+
Field: c.field + ".cluster",
122124
})
123125
continue
124126
}
@@ -131,7 +133,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
131133
len(clusters),
132134
clusterIdentifier,
133135
),
134-
Field: c.field,
136+
Field: c.field + ".cluster",
135137
})
136138
continue
137139
}
@@ -150,7 +152,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
150152
clusterIdentifier,
151153
err,
152154
),
153-
Field: c.field,
155+
Field: c.field + ".cluster",
154156
})
155157
continue
156158
}
@@ -167,7 +169,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
167169
clusterIdentifier,
168170
clusterIdentifier,
169171
),
170-
Field: c.field,
172+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.addons.csi.providers.nutanix.storageClassConfigs.volume.parameters.storageContainer", //nolint:lll // The field is long.
171173
})
172174
case 1:
173175
continue
@@ -184,27 +186,14 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
184186
storageContainerName,
185187
clusterIdentifier,
186188
),
187-
Field: c.field,
189+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.addons.csi.providers.nutanix.storageClassConfigs.volume.parameters.storageContainer", //nolint:lll // The field is long.
188190
})
189191
}
190192
}
191193

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)