Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#10780 from sbueringer/pr-improve-m…
Browse files Browse the repository at this point in the history
…dms-topo-reconciler

🐛 MD/MS topo reconciler: only add finalizer for owned MD/MS
  • Loading branch information
k8s-ci-robot authored Jun 18, 2024
2 parents 1cbe864 + 1932242 commit 54c8e46
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package machinedeployment

import (
"context"
"fmt"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -35,6 +36,7 @@ import (
tlog "sigs.k8s.io/cluster-api/internal/log"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/labels"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
)
Expand Down Expand Up @@ -133,6 +135,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
return ctrl.Result{}, nil
}

// Return early if the MachineDeployment is not topology owned.
if !labels.IsTopologyOwned(md) {
log.Info(fmt.Sprintf("Reconciliation is skipped because the MachineDeployment does not have the %q label", clusterv1.ClusterTopologyOwnedLabel))
return ctrl.Result{}, nil
}

// Create a patch helper to add or remove the finalizer from the MachineDeployment.
patchHelper, err := patch.NewHelper(md, r.Client)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
mdBuilder := builder.MachineDeployment(metav1.NamespaceDefault, "md").
WithClusterName("fake-cluster").
WithBootstrapTemplate(mdBT).
WithInfrastructureTemplate(mdIMT)

WithInfrastructureTemplate(mdIMT).
WithLabels(map[string]string{
clusterv1.ClusterTopologyOwnedLabel: "",
})
md := mdBuilder.Build()

mdWithoutTopologyOwnedLabel := md.DeepCopy()
delete(mdWithoutTopologyOwnedLabel.Labels, clusterv1.ClusterTopologyOwnedLabel)
mdWithFinalizer := mdBuilder.Build()
mdWithFinalizer.Finalizers = []string{clusterv1.MachineDeploymentTopologyFinalizer}

Expand All @@ -64,6 +69,11 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
md: mdWithFinalizer,
expectFinalizer: true,
},
{
name: "should not add ClusterTopology finalizer on MachineDeployment without topology owned label",
md: mdWithoutTopologyOwnedLabel,
expectFinalizer: false,
},
}

for _, tc := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package machineset

import (
"context"
"fmt"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -36,6 +37,7 @@ import (
tlog "sigs.k8s.io/cluster-api/internal/log"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/labels"
clog "sigs.k8s.io/cluster-api/util/log"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
Expand Down Expand Up @@ -140,6 +142,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
return ctrl.Result{}, nil
}

// Return early if the MachineSet is not topology owned.
if !labels.IsTopologyOwned(ms) {
log.Info(fmt.Sprintf("Reconciliation is skipped because the MachineSet does not have the %q label", clusterv1.ClusterTopologyOwnedLabel))
return ctrl.Result{}, nil
}

// Create a patch helper to add or remove the finalizer from the MachineSet.
patchHelper, err := patch.NewHelper(ms, r.Client)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
})

ms := msBuilder.Build()
msWithoutTopologyOwnedLabel := ms.DeepCopy()
delete(msWithoutTopologyOwnedLabel.Labels, clusterv1.ClusterTopologyOwnedLabel)
msWithFinalizer := msBuilder.Build()
msWithFinalizer.Finalizers = []string{clusterv1.MachineSetTopologyFinalizer}

Expand All @@ -78,6 +80,11 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
ms: msWithFinalizer,
expectFinalizer: true,
},
{
name: "should not add ClusterTopology finalizer on MachineSet without topology owned label",
ms: msWithoutTopologyOwnedLabel,
expectFinalizer: false,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 54c8e46

Please sign in to comment.