Skip to content

Commit 54c8e46

Browse files
authored
Merge pull request kubernetes-sigs#10780 from sbueringer/pr-improve-mdms-topo-reconciler
🐛 MD/MS topo reconciler: only add finalizer for owned MD/MS
2 parents 1cbe864 + 1932242 commit 54c8e46

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

internal/controllers/topology/machinedeployment/machinedeployment_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package machinedeployment
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/pkg/errors"
2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -35,6 +36,7 @@ import (
3536
tlog "sigs.k8s.io/cluster-api/internal/log"
3637
"sigs.k8s.io/cluster-api/util"
3738
"sigs.k8s.io/cluster-api/util/annotations"
39+
"sigs.k8s.io/cluster-api/util/labels"
3840
"sigs.k8s.io/cluster-api/util/patch"
3941
"sigs.k8s.io/cluster-api/util/predicates"
4042
)
@@ -133,6 +135,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
133135
return ctrl.Result{}, nil
134136
}
135137

138+
// Return early if the MachineDeployment is not topology owned.
139+
if !labels.IsTopologyOwned(md) {
140+
log.Info(fmt.Sprintf("Reconciliation is skipped because the MachineDeployment does not have the %q label", clusterv1.ClusterTopologyOwnedLabel))
141+
return ctrl.Result{}, nil
142+
}
143+
136144
// Create a patch helper to add or remove the finalizer from the MachineDeployment.
137145
patchHelper, err := patch.NewHelper(md, r.Client)
138146
if err != nil {

internal/controllers/topology/machinedeployment/machinedeployment_controller_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
4141
mdBuilder := builder.MachineDeployment(metav1.NamespaceDefault, "md").
4242
WithClusterName("fake-cluster").
4343
WithBootstrapTemplate(mdBT).
44-
WithInfrastructureTemplate(mdIMT)
45-
44+
WithInfrastructureTemplate(mdIMT).
45+
WithLabels(map[string]string{
46+
clusterv1.ClusterTopologyOwnedLabel: "",
47+
})
4648
md := mdBuilder.Build()
49+
50+
mdWithoutTopologyOwnedLabel := md.DeepCopy()
51+
delete(mdWithoutTopologyOwnedLabel.Labels, clusterv1.ClusterTopologyOwnedLabel)
4752
mdWithFinalizer := mdBuilder.Build()
4853
mdWithFinalizer.Finalizers = []string{clusterv1.MachineDeploymentTopologyFinalizer}
4954

@@ -64,6 +69,11 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
6469
md: mdWithFinalizer,
6570
expectFinalizer: true,
6671
},
72+
{
73+
name: "should not add ClusterTopology finalizer on MachineDeployment without topology owned label",
74+
md: mdWithoutTopologyOwnedLabel,
75+
expectFinalizer: false,
76+
},
6777
}
6878

6979
for _, tc := range testCases {

internal/controllers/topology/machineset/machineset_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package machineset
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/pkg/errors"
2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -36,6 +37,7 @@ import (
3637
tlog "sigs.k8s.io/cluster-api/internal/log"
3738
"sigs.k8s.io/cluster-api/util"
3839
"sigs.k8s.io/cluster-api/util/annotations"
40+
"sigs.k8s.io/cluster-api/util/labels"
3941
clog "sigs.k8s.io/cluster-api/util/log"
4042
"sigs.k8s.io/cluster-api/util/patch"
4143
"sigs.k8s.io/cluster-api/util/predicates"
@@ -140,6 +142,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
140142
return ctrl.Result{}, nil
141143
}
142144

145+
// Return early if the MachineSet is not topology owned.
146+
if !labels.IsTopologyOwned(ms) {
147+
log.Info(fmt.Sprintf("Reconciliation is skipped because the MachineSet does not have the %q label", clusterv1.ClusterTopologyOwnedLabel))
148+
return ctrl.Result{}, nil
149+
}
150+
143151
// Create a patch helper to add or remove the finalizer from the MachineSet.
144152
patchHelper, err := patch.NewHelper(ms, r.Client)
145153
if err != nil {

internal/controllers/topology/machineset/machineset_controller_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
5858
})
5959

6060
ms := msBuilder.Build()
61+
msWithoutTopologyOwnedLabel := ms.DeepCopy()
62+
delete(msWithoutTopologyOwnedLabel.Labels, clusterv1.ClusterTopologyOwnedLabel)
6163
msWithFinalizer := msBuilder.Build()
6264
msWithFinalizer.Finalizers = []string{clusterv1.MachineSetTopologyFinalizer}
6365

@@ -78,6 +80,11 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
7880
ms: msWithFinalizer,
7981
expectFinalizer: true,
8082
},
83+
{
84+
name: "should not add ClusterTopology finalizer on MachineSet without topology owned label",
85+
ms: msWithoutTopologyOwnedLabel,
86+
expectFinalizer: false,
87+
},
8188
}
8289

8390
for _, tc := range testCases {

0 commit comments

Comments
 (0)