Skip to content

Commit 002ec4f

Browse files
sbueringerk8s-infra-cherrypick-robot
authored andcommitted
Remove finalizer from VSphereMachine during deletion if ownerRef was never set
Signed-off-by: Stefan Büringer [email protected]
1 parent 0730cb7 commit 002ec4f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

controllers/vspheremachine_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ func (r *machineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
210210
return reconcile.Result{}, errors.Wrapf(err, "failed to get Machine for VSphereMachine")
211211
}
212212
if machine == nil {
213+
// Note: If ownerRef was not set, there is nothing to delete. Remove finalizer so deletion can succeed.
214+
if !machineContext.GetVSphereMachine().GetDeletionTimestamp().IsZero() {
215+
if ctrlutil.ContainsFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer) {
216+
patchHelper, err := patch.NewHelper(machineContext.GetVSphereMachine(), r.Client)
217+
if err != nil {
218+
return reconcile.Result{}, err
219+
}
220+
ctrlutil.RemoveFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer)
221+
if err := patchHelper.Patch(ctx, machineContext.GetVSphereMachine()); err != nil {
222+
return ctrl.Result{}, err
223+
}
224+
}
225+
return ctrl.Result{}, nil
226+
}
227+
213228
log.Info("Waiting for Machine controller to set OwnerRef on VSphereMachine")
214229
return reconcile.Result{}, nil
215230
}

controllers/vspheremachine_controller_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
2424
corev1 "k8s.io/api/core/v1"
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/utils/ptr"
2728
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
@@ -356,4 +357,42 @@ func Test_machineReconciler_Metadata(t *testing.T) {
356357
!capiutil.HasOwner(vSphereMachine.GetOwnerReferences(), infrav1.GroupVersion.String(), []string{"VSphereCluster"})
357358
}, timeout).Should(BeTrue())
358359
})
360+
361+
t.Run("Should complete deletion even without Machine owner", func(t *testing.T) {
362+
g := NewWithT(t)
363+
364+
vSphereMachine := &infrav1.VSphereMachine{
365+
ObjectMeta: metav1.ObjectMeta{
366+
Name: "vsphere-machine-no-ownerrefs",
367+
Namespace: ns.Name,
368+
// no ownerRefs
369+
},
370+
Spec: infrav1.VSphereMachineSpec{
371+
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
372+
Template: "ubuntu-k9s-1.19",
373+
Network: infrav1.NetworkSpec{
374+
Devices: []infrav1.NetworkDeviceSpec{
375+
{NetworkName: "network-1", DHCP4: true},
376+
},
377+
},
378+
},
379+
},
380+
}
381+
382+
g.Expect(testEnv.Create(ctx, vSphereMachine)).To(Succeed())
383+
384+
// Make sure the VSphereMachine has the finalizer.
385+
g.Eventually(func(g Gomega) {
386+
g.Expect(testEnv.Get(ctx, client.ObjectKeyFromObject(vSphereMachine), vSphereMachine)).To(Succeed())
387+
g.Expect(ctrlutil.ContainsFinalizer(vSphereMachine, infrav1.MachineFinalizer)).To(BeTrue())
388+
}, timeout).Should(Succeed())
389+
390+
g.Expect(testEnv.Delete(ctx, vSphereMachine)).To(Succeed())
391+
392+
// Make sure the VSphereMachine is gone.
393+
g.Eventually(func(g Gomega) {
394+
err := testEnv.Get(ctx, client.ObjectKeyFromObject(vSphereMachine), vSphereMachine)
395+
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
396+
}, timeout).Should(Succeed())
397+
})
359398
}

0 commit comments

Comments
 (0)