@@ -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