@@ -366,64 +366,64 @@ func (webhook *Cluster) getClusterClassForCluster(ctx context.Context, cluster *
366
366
func validateMachineHealthChecks (cluster * clusterv1.Cluster , clusterClass * clusterv1.ClusterClass ) field.ErrorList {
367
367
var allErrs field.ErrorList
368
368
369
- // Validate ControlPlane MachineHealthCheck if defined.
370
- if cluster .Spec .Topology .ControlPlane .MachineHealthCheck != nil && ! cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass .IsZero () {
371
- // Ensure ControlPlane does not define a MachineHealthCheck if the ClusterClass does not define MachineInfrastructure.
372
- if clusterClass .Spec .ControlPlane .MachineInfrastructure == nil {
373
- allErrs = append (allErrs , field .Forbidden (
374
- field .NewPath ("spec" , "topology" , "controlPlane" , "machineHealthCheck" ),
375
- "can be set only if spec.controlPlane.machineInfrastructure is set in ClusterClass" ,
376
- ))
377
- }
378
- // Ensure ControlPlane MachineHealthCheck defines UnhealthyConditions.
379
- if len (cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass .UnhealthyConditions ) == 0 {
380
- allErrs = append (allErrs , field .Forbidden (
381
- field .NewPath ("spec" , "topology" , "controlPlane" , "machineHealthCheck" , "unhealthyConditions" ),
382
- "must have at least one value" ,
383
- ))
369
+ if cluster .Spec .Topology .ControlPlane .MachineHealthCheck != nil {
370
+ fldPath := field .NewPath ("spec" , "topology" , "controlPlane" , "machineHealthCheck" )
371
+
372
+ // Validate ControlPlane MachineHealthCheck if defined.
373
+ if ! cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass .IsZero () {
374
+ // Ensure ControlPlane does not define a MachineHealthCheck if the ClusterClass does not define MachineInfrastructure.
375
+ if clusterClass .Spec .ControlPlane .MachineInfrastructure == nil {
376
+ allErrs = append (allErrs , field .Forbidden (
377
+ fldPath ,
378
+ "can be set only if spec.controlPlane.machineInfrastructure is set in ClusterClass" ,
379
+ ))
380
+ }
381
+ allErrs = append (allErrs , validateMachineHealthCheckClass (fldPath , cluster .Namespace ,
382
+ & cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass )... )
384
383
}
385
- }
386
384
387
- // If MachineHealthCheck is explicitly enabled then make sure that a MachineHealthCheck definition is
388
- // available either in the Cluster topology or in the ClusterClass.
389
- // (One of these definitions will be used in the controller to create the MachineHealthCheck)
390
- if cluster .Spec .Topology .ControlPlane .MachineHealthCheck != nil &&
391
- cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable != nil &&
392
- * cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable &&
393
- cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass .IsZero () &&
394
- clusterClass .Spec .ControlPlane .MachineHealthCheck == nil {
395
- allErrs = append (allErrs , field .Forbidden (
396
- field .NewPath ("spec" , "topology" , "controlPlane" , "machineHealthCheck" , "enable" ),
397
- fmt .Sprintf ("cannot be set to %t as MachineHealthCheck definition is not available in the Cluster topology or the ClusterClass" , * cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable ),
398
- ))
385
+ // If MachineHealthCheck is explicitly enabled then make sure that a MachineHealthCheck definition is
386
+ // available either in the Cluster topology or in the ClusterClass.
387
+ // (One of these definitions will be used in the controller to create the MachineHealthCheck)
388
+
389
+ // Check if the machineHealthCheck is explicitly enabled in the ControlPlaneTopology.
390
+ if cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable != nil && * cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable {
391
+ // Ensure the MHC is defined in at least one of the ControlPlaneTopology of the Cluster or the ControlPlaneClass of the ClusterClass.
392
+ if cluster .Spec .Topology .ControlPlane .MachineHealthCheck .MachineHealthCheckClass .IsZero () && clusterClass .Spec .ControlPlane .MachineHealthCheck == nil {
393
+ allErrs = append (allErrs , field .Forbidden (
394
+ fldPath .Child ("enable" ),
395
+ fmt .Sprintf ("cannot be set to %t as MachineHealthCheck definition is not available in the Cluster topology or the ClusterClass" , * cluster .Spec .Topology .ControlPlane .MachineHealthCheck .Enable ),
396
+ ))
397
+ }
398
+ }
399
399
}
400
400
401
401
if cluster .Spec .Topology .Workers != nil {
402
402
for i , md := range cluster .Spec .Topology .Workers .MachineDeployments {
403
- // If MachineHealthCheck is defined ensure it defines UnhealthyConditions.
404
- if md . MachineHealthCheck != nil && ! md . MachineHealthCheck . MachineHealthCheckClass . IsZero () {
405
- if len ( md . MachineHealthCheck . MachineHealthCheckClass . UnhealthyConditions ) == 0 {
406
- allErrs = append ( allErrs , field . Forbidden (
407
- field . NewPath ( "spec" , "topology" , "workers" , "machineDeployments" , "machineHealthCheck" ). Index ( i ). Child ( "unhealthyConditions" ),
408
- "must have at least one value" ,
409
- ) )
403
+ if md . MachineHealthCheck != nil {
404
+ fldPath := field . NewPath ( "spec" , "topology" , "workers" , "machineDeployments" , "machineHealthCheck" ). Index ( i )
405
+
406
+ // Validate the MachineDeployment MachineHealthCheck if defined.
407
+ if ! md . MachineHealthCheck . MachineHealthCheckClass . IsZero () {
408
+ allErrs = append ( allErrs , validateMachineHealthCheckClass ( fldPath , cluster . Namespace ,
409
+ & md . MachineHealthCheck . MachineHealthCheckClass ) ... )
410
410
}
411
- }
412
411
413
- // If MachineHealthCheck is explicitly enabled then make sure that a MachineHealthCheck definition is
414
- // available either in the Cluster topology or in the ClusterClass.
415
- // (One of these definitions will be used in the controller to create the MachineHealthCheck)
416
- mdClass := machineDeploymentClassOfName (clusterClass , md .Class )
417
- if mdClass != nil { // Note: we skip handling the nil case here as it is already handled in previous validations.
418
- if md .MachineHealthCheck != nil &&
419
- md .MachineHealthCheck .Enable != nil &&
420
- * md .MachineHealthCheck .Enable &&
421
- md .MachineHealthCheck .MachineHealthCheckClass .IsZero () &&
422
- mdClass .MachineHealthCheck == nil {
423
- allErrs = append (allErrs , field .Forbidden (
424
- field .NewPath ("spec" , "topology" , "workers" , "machineDeployments" , "machineHealthCheck" ).Index (i ).Child ("enable" ),
425
- fmt .Sprintf ("cannot be set to %t as MachineHealthCheck definition is not available in the Cluster topology or the ClusterClass" , * md .MachineHealthCheck .Enable ),
426
- ))
412
+ // If MachineHealthCheck is explicitly enabled then make sure that a MachineHealthCheck definition is
413
+ // available either in the Cluster topology or in the ClusterClass.
414
+ // (One of these definitions will be used in the controller to create the MachineHealthCheck)
415
+ mdClass := machineDeploymentClassOfName (clusterClass , md .Class )
416
+ if mdClass != nil { // Note: we skip handling the nil case here as it is already handled in previous validations.
417
+ // Check if the machineHealthCheck is explicitly enabled in the machineDeploymentTopology.
418
+ if md .MachineHealthCheck .Enable != nil && * md .MachineHealthCheck .Enable {
419
+ // Ensure the MHC is defined in at least one of the MachineDeploymentTopology of the Cluster or the MachineDeploymentClass of the ClusterClass.
420
+ if md .MachineHealthCheck .MachineHealthCheckClass .IsZero () && mdClass .MachineHealthCheck == nil {
421
+ allErrs = append (allErrs , field .Forbidden (
422
+ fldPath .Child ("enable" ),
423
+ fmt .Sprintf ("cannot be set to %t as MachineHealthCheck definition is not available in the Cluster topology or the ClusterClass" , * md .MachineHealthCheck .Enable ),
424
+ ))
425
+ }
426
+ }
427
427
}
428
428
}
429
429
}
@@ -433,7 +433,7 @@ func validateMachineHealthChecks(cluster *clusterv1.Cluster, clusterClass *clust
433
433
}
434
434
435
435
// machineDeploymentClassOfName find a MachineDeploymentClass of the given name in the provided ClusterClass.
436
- // Returns nill if can not find one.
436
+ // Returns nil if it can not find one.
437
437
// TODO: Check if there is already a helper function that can do this.
438
438
func machineDeploymentClassOfName (clusterClass * clusterv1.ClusterClass , name string ) * clusterv1.MachineDeploymentClass {
439
439
for _ , mdClass := range clusterClass .Spec .Workers .MachineDeployments {
0 commit comments