@@ -107,7 +107,7 @@ func (r *Reconciler) computeDesiredState(ctx context.Context, s *scope.Scope) (*
107
107
ctx ,
108
108
desiredState .ControlPlane .Object ,
109
109
selectorForControlPlaneMHC (),
110
- s .Current .Cluster . Name ,
110
+ s .Current .Cluster ,
111
111
s .Blueprint .ControlPlaneMachineHealthCheckClass ())
112
112
}
113
113
@@ -796,7 +796,7 @@ func computeMachineDeployment(ctx context.Context, s *scope.Scope, machineDeploy
796
796
ctx ,
797
797
desiredMachineDeploymentObj ,
798
798
selectorForMachineDeploymentMHC (desiredMachineDeploymentObj ),
799
- s .Current .Cluster . Name ,
799
+ s .Current .Cluster ,
800
800
s .Blueprint .MachineDeploymentMachineHealthCheckClass (& machineDeploymentTopology ))
801
801
}
802
802
return desiredMachineDeployment , nil
@@ -1357,7 +1357,7 @@ func ownerReferenceTo(obj client.Object, gvk schema.GroupVersionKind) *metav1.Ow
1357
1357
}
1358
1358
}
1359
1359
1360
- func computeMachineHealthCheck (ctx context.Context , healthCheckTarget client.Object , selector * metav1.LabelSelector , clusterName string , check * clusterv1.MachineHealthCheckClass ) * clusterv1.MachineHealthCheck {
1360
+ func computeMachineHealthCheck (ctx context.Context , healthCheckTarget client.Object , selector * metav1.LabelSelector , cluster * clusterv1. Cluster , check * clusterv1.MachineHealthCheckClass ) * clusterv1.MachineHealthCheck {
1361
1361
// Create a MachineHealthCheck with the spec given in the ClusterClass.
1362
1362
mhc := & clusterv1.MachineHealthCheck {
1363
1363
TypeMeta : metav1.TypeMeta {
@@ -1370,9 +1370,14 @@ func computeMachineHealthCheck(ctx context.Context, healthCheckTarget client.Obj
1370
1370
Labels : map [string ]string {
1371
1371
clusterv1 .ClusterTopologyOwnedLabel : "" ,
1372
1372
},
1373
+ // Note: we are adding an ownerRef to Cluster so the MHC will be automatically garbage collected
1374
+ // in case deletion is triggered before an object reconcile happens.
1375
+ OwnerReferences : []metav1.OwnerReference {
1376
+ * ownerReferenceTo (cluster , clusterv1 .GroupVersion .WithKind ("Cluster" )),
1377
+ },
1373
1378
},
1374
1379
Spec : clusterv1.MachineHealthCheckSpec {
1375
- ClusterName : clusterName ,
1380
+ ClusterName : cluster . Name ,
1376
1381
Selector : * selector ,
1377
1382
UnhealthyConditions : check .UnhealthyConditions ,
1378
1383
MaxUnhealthy : check .MaxUnhealthy ,
0 commit comments