Skip to content

Commit 3a16912

Browse files
authored
Merge pull request #10277 from fabriziopandini/improve-handling-of-reconcileCP-errors
🐛 Improve handling of topology orphaned objects
2 parents 128914b + 5f141e8 commit 3a16912

File tree

4 files changed

+665
-94
lines changed

4 files changed

+665
-94
lines changed

internal/controllers/topology/cluster/desired_state.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *Reconciler) computeDesiredState(ctx context.Context, s *scope.Scope) (*
107107
ctx,
108108
desiredState.ControlPlane.Object,
109109
selectorForControlPlaneMHC(),
110-
s.Current.Cluster.Name,
110+
s.Current.Cluster,
111111
s.Blueprint.ControlPlaneMachineHealthCheckClass())
112112
}
113113

@@ -796,7 +796,7 @@ func computeMachineDeployment(ctx context.Context, s *scope.Scope, machineDeploy
796796
ctx,
797797
desiredMachineDeploymentObj,
798798
selectorForMachineDeploymentMHC(desiredMachineDeploymentObj),
799-
s.Current.Cluster.Name,
799+
s.Current.Cluster,
800800
s.Blueprint.MachineDeploymentMachineHealthCheckClass(&machineDeploymentTopology))
801801
}
802802
return desiredMachineDeployment, nil
@@ -1357,7 +1357,7 @@ func ownerReferenceTo(obj client.Object, gvk schema.GroupVersionKind) *metav1.Ow
13571357
}
13581358
}
13591359

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 {
13611361
// Create a MachineHealthCheck with the spec given in the ClusterClass.
13621362
mhc := &clusterv1.MachineHealthCheck{
13631363
TypeMeta: metav1.TypeMeta{
@@ -1370,9 +1370,14 @@ func computeMachineHealthCheck(ctx context.Context, healthCheckTarget client.Obj
13701370
Labels: map[string]string{
13711371
clusterv1.ClusterTopologyOwnedLabel: "",
13721372
},
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+
},
13731378
},
13741379
Spec: clusterv1.MachineHealthCheckSpec{
1375-
ClusterName: clusterName,
1380+
ClusterName: cluster.Name,
13761381
Selector: *selector,
13771382
UnhealthyConditions: check.UnhealthyConditions,
13781383
MaxUnhealthy: check.MaxUnhealthy,

internal/controllers/topology/cluster/desired_state_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,7 +2874,7 @@ func Test_computeMachineHealthCheck(t *testing.T) {
28742874
"foo": "bar",
28752875
}}
28762876
healthCheckTarget := builder.MachineDeployment("ns1", "md1").Build()
2877-
clusterName := "cluster1"
2877+
cluster := builder.Cluster("ns1", "cluster1").Build()
28782878
want := &clusterv1.MachineHealthCheck{
28792879
TypeMeta: metav1.TypeMeta{
28802880
APIVersion: clusterv1.GroupVersion.String(),
@@ -2888,9 +2888,12 @@ func Test_computeMachineHealthCheck(t *testing.T) {
28882888
"cluster.x-k8s.io/cluster-name": "cluster1",
28892889
clusterv1.ClusterTopologyOwnedLabel: "",
28902890
},
2891+
OwnerReferences: []metav1.OwnerReference{
2892+
*ownerReferenceTo(cluster, clusterv1.GroupVersion.WithKind("Cluster")),
2893+
},
28912894
},
28922895
Spec: clusterv1.MachineHealthCheckSpec{
2893-
ClusterName: "cluster1",
2896+
ClusterName: cluster.Name,
28942897
Selector: metav1.LabelSelector{MatchLabels: map[string]string{
28952898
"foo": "bar",
28962899
}},
@@ -2916,7 +2919,7 @@ func Test_computeMachineHealthCheck(t *testing.T) {
29162919
t.Run("set all fields correctly", func(t *testing.T) {
29172920
g := NewWithT(t)
29182921

2919-
got := computeMachineHealthCheck(ctx, healthCheckTarget, selector, clusterName, mhcSpec)
2922+
got := computeMachineHealthCheck(ctx, healthCheckTarget, selector, cluster, mhcSpec)
29202923

29212924
g.Expect(got).To(BeComparableTo(want), cmp.Diff(got, want))
29222925
})

0 commit comments

Comments
 (0)