@@ -148,11 +148,12 @@ func (webhook *Cluster) ValidateDelete(_ context.Context, obj runtime.Object) er
148
148
149
149
func (webhook * Cluster ) validate (ctx context.Context , oldCluster , newCluster * clusterv1.Cluster ) error {
150
150
var allErrs field.ErrorList
151
+ specPath := field .NewPath ("spec" )
151
152
if newCluster .Spec .InfrastructureRef != nil && newCluster .Spec .InfrastructureRef .Namespace != newCluster .Namespace {
152
153
allErrs = append (
153
154
allErrs ,
154
155
field .Invalid (
155
- field . NewPath ( "spec" , "infrastructureRef" , "namespace" ),
156
+ specPath . Child ( "infrastructureRef" , "namespace" ),
156
157
newCluster .Spec .InfrastructureRef .Namespace ,
157
158
"must match metadata.namespace" ,
158
159
),
@@ -163,24 +164,25 @@ func (webhook *Cluster) validate(ctx context.Context, oldCluster, newCluster *cl
163
164
allErrs = append (
164
165
allErrs ,
165
166
field .Invalid (
166
- field . NewPath ( "spec" , "controlPlaneRef" , "namespace" ),
167
+ specPath . Child ( "controlPlaneRef" , "namespace" ),
167
168
newCluster .Spec .ControlPlaneRef .Namespace ,
168
169
"must match metadata.namespace" ,
169
170
),
170
171
)
171
172
}
173
+ topologyPath := specPath .Child ("topology" )
172
174
173
175
// Validate the managed topology, if defined.
174
176
if newCluster .Spec .Topology != nil {
175
- allErrs = append (allErrs , webhook .validateTopology (ctx , oldCluster , newCluster )... )
177
+ allErrs = append (allErrs , webhook .validateTopology (ctx , oldCluster , newCluster , topologyPath )... )
176
178
}
177
179
178
180
// On update.
179
181
if oldCluster != nil {
180
182
// Error if the update moves the cluster from Managed to Unmanaged i.e. the managed topology is removed on update.
181
183
if oldCluster .Spec .Topology != nil && newCluster .Spec .Topology == nil {
182
184
allErrs = append (allErrs , field .Forbidden (
183
- field . NewPath ( "spec" , "topology" ) ,
185
+ topologyPath ,
184
186
"cannot be removed from an existing Cluster" ,
185
187
))
186
188
}
@@ -192,13 +194,13 @@ func (webhook *Cluster) validate(ctx context.Context, oldCluster, newCluster *cl
192
194
return nil
193
195
}
194
196
195
- func (webhook * Cluster ) validateTopology (ctx context.Context , oldCluster , newCluster * clusterv1.Cluster ) field.ErrorList {
197
+ func (webhook * Cluster ) validateTopology (ctx context.Context , oldCluster , newCluster * clusterv1.Cluster , fldPath * field. Path ) field.ErrorList {
196
198
// NOTE: ClusterClass and managed topologies are behind ClusterTopology feature gate flag; the web hook
197
199
// must prevent the usage of Cluster.Topology in case the feature flag is disabled.
198
200
if ! feature .Gates .Enabled (feature .ClusterTopology ) {
199
201
return field.ErrorList {
200
202
field .Forbidden (
201
- field . NewPath ( "spec" , "topology" ) ,
203
+ fldPath ,
202
204
"can be set only if the ClusterTopology feature flag is enabled" ,
203
205
),
204
206
}
@@ -211,7 +213,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
211
213
allErrs = append (
212
214
allErrs ,
213
215
field .Required (
214
- field . NewPath ( "spec" , "topology" , "class" ),
216
+ fldPath . Child ( "class" ),
215
217
"class cannot be empty" ,
216
218
),
217
219
)
@@ -222,7 +224,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
222
224
allErrs = append (
223
225
allErrs ,
224
226
field .Invalid (
225
- field . NewPath ( "spec" , "topology" , "version" ),
227
+ fldPath . Child ( "version" ),
226
228
newCluster .Spec .Topology .Version ,
227
229
"version must be a valid semantic version" ,
228
230
),
@@ -235,7 +237,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
235
237
if err := webhook .Client .Get (ctx , client.ObjectKey {Namespace : newCluster .Namespace , Name : newCluster .Spec .Topology .Class }, clusterClass ); err != nil {
236
238
allErrs = append (
237
239
allErrs , field .Invalid (
238
- field . NewPath ( "spec" , "topology" , "class" ),
240
+ fldPath . Child ( "class" ),
239
241
newCluster .Name ,
240
242
fmt .Sprintf ("ClusterClass with name %q could not be found" , newCluster .Spec .Topology .Class )))
241
243
return allErrs
@@ -245,7 +247,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
245
247
246
248
// Check if the variables defined in the ClusterClass are valid.
247
249
allErrs = append (allErrs , variables .ValidateClusterVariables (newCluster .Spec .Topology .Variables , clusterClass .Spec .Variables ,
248
- field . NewPath ( "spec" , "topology" , "variables" ))... )
250
+ fldPath . Child ( "variables" ))... )
249
251
250
252
if newCluster .Spec .Topology .Workers != nil {
251
253
for i , md := range newCluster .Spec .Topology .Workers .MachineDeployments {
@@ -255,9 +257,10 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
255
257
}
256
258
257
259
allErrs = append (allErrs , variables .ValidateTopLevelClusterVariablesExist (md .Variables .Overrides , newCluster .Spec .Topology .Variables ,
258
- field .NewPath ("spec" , "topology" , "workers" , "machineDeployments" ).Index (i ).Child ("variables" , "overrides" ))... )
260
+ fldPath .Child ("workers" , "machineDeployments" ).Index (i ).Child ("variables" , "overrides" ))... )
261
+
259
262
allErrs = append (allErrs , variables .ValidateMachineDeploymentVariables (md .Variables .Overrides , clusterClass .Spec .Variables ,
260
- field . NewPath ( "spec" , "topology" , "workers" , "machineDeployments" ).Index (i ).Child ("variables" , "overrides" ))... )
263
+ fldPath . Child ( "workers" , "machineDeployments" ).Index (i ).Child ("variables" , "overrides" ))... )
261
264
}
262
265
}
263
266
@@ -267,7 +270,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
267
270
allErrs = append (
268
271
allErrs ,
269
272
field .Forbidden (
270
- field . NewPath ( "spec" , "topology" , "class" ),
273
+ fldPath . Child ( "class" ),
271
274
"class cannot be set on an existing Cluster" ,
272
275
),
273
276
)
@@ -281,7 +284,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
281
284
allErrs = append (
282
285
allErrs ,
283
286
field .Invalid (
284
- field . NewPath ( "spec" , "topology" , "version" ),
287
+ fldPath . Child ( "version" ),
285
288
newCluster .Spec .Topology .Version ,
286
289
"version must be a valid semantic version" ,
287
290
),
@@ -293,7 +296,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
293
296
allErrs = append (
294
297
allErrs ,
295
298
field .Invalid (
296
- field . NewPath ( "spec" , "topology" , "version" ),
299
+ fldPath . Child ( "version" ),
297
300
oldCluster .Spec .Topology .Version ,
298
301
fmt .Sprintf ("old version %q cannot be compared with %q" , oldVersion , inVersion ),
299
302
),
@@ -303,7 +306,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
303
306
allErrs = append (
304
307
allErrs ,
305
308
field .Invalid (
306
- field . NewPath ( "spec" , "topology" , "version" ),
309
+ fldPath . Child ( "version" ),
307
310
newCluster .Spec .Topology .Version ,
308
311
fmt .Sprintf ("version cannot be decreased from %q to %q" , oldVersion , inVersion ),
309
312
),
@@ -319,7 +322,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
319
322
allErrs = append (
320
323
allErrs ,
321
324
field .Forbidden (
322
- field . NewPath ( "spec" , "topology" , "version" ),
325
+ fldPath . Child ( "version" ),
323
326
fmt .Sprintf ("version cannot be increased from %q to %q" , oldVersion , inVersion ),
324
327
),
325
328
)
@@ -332,7 +335,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
332
335
if err != nil {
333
336
allErrs = append (
334
337
allErrs , field .Forbidden (
335
- field . NewPath ( "spec" , "topology" , "class" ),
338
+ fldPath . Child ( "class" ),
336
339
fmt .Sprintf ("ClusterClass with name %q could not be found, change from class %[1]q to class %q cannot be validated" ,
337
340
oldCluster .Spec .Topology .Class , newCluster .Spec .Topology .Class )))
338
341
0 commit comments