@@ -541,4 +541,72 @@ var _ = Describe("ServerMaintenance Controller", func() {
541541 Expect (k8sClient .Delete (ctx , unsetPriorityMaintenance )).To (Succeed ())
542542 Expect (k8sClient .Delete (ctx , serverClaim )).To (Succeed ())
543543 })
544+
545+ It ("should complete deletion when the referenced Server is already gone" , func (ctx SpecContext ) {
546+ By ("Creating a ServerMaintenance object" )
547+ serverMaintenance := & metalv1alpha1.ServerMaintenance {
548+ ObjectMeta : metav1.ObjectMeta {
549+ Name : "test-server-maintenance-server-gone" ,
550+ Namespace : ns .Name ,
551+ },
552+ Spec : metalv1alpha1.ServerMaintenanceSpec {
553+ ServerRef : & corev1.LocalObjectReference {Name : server .Name },
554+ Policy : metalv1alpha1 .ServerMaintenancePolicyEnforced ,
555+ ServerPower : metalv1alpha1 .PowerOff ,
556+ },
557+ }
558+ Expect (k8sClient .Create (ctx , serverMaintenance )).To (Succeed ())
559+
560+ By ("Waiting for the ServerMaintenance to reach InMaintenance state" )
561+ Eventually (Object (serverMaintenance )).Should (
562+ HaveField ("Status.State" , metalv1alpha1 .ServerMaintenanceStateInMaintenance ),
563+ )
564+
565+ By ("Deleting the Server before deleting the ServerMaintenance" )
566+ Expect (k8sClient .Delete (ctx , server )).To (Succeed ())
567+ Eventually (Get (server )).ShouldNot (Succeed ())
568+
569+ By ("Deleting the ServerMaintenance" )
570+ Expect (k8sClient .Delete (ctx , serverMaintenance )).To (Succeed ())
571+
572+ By ("Ensuring the ServerMaintenance is fully deleted despite the Server being gone" )
573+ Eventually (Get (serverMaintenance )).ShouldNot (Succeed ())
574+ })
575+
576+ It ("should skip cleanup and remove finalizer when no finalizer is present on deletion" , func (ctx SpecContext ) {
577+ By ("Creating a ServerMaintenance object without going through reconciliation (no finalizer)" )
578+ serverMaintenance := & metalv1alpha1.ServerMaintenance {
579+ ObjectMeta : metav1.ObjectMeta {
580+ Name : "test-server-maintenance-no-finalizer" ,
581+ Namespace : ns .Name ,
582+ },
583+ Spec : metalv1alpha1.ServerMaintenanceSpec {
584+ ServerRef : & corev1.LocalObjectReference {Name : server .Name },
585+ Policy : metalv1alpha1 .ServerMaintenancePolicyEnforced ,
586+ ServerPower : metalv1alpha1 .PowerOff ,
587+ },
588+ }
589+ Expect (k8sClient .Create (ctx , serverMaintenance )).To (Succeed ())
590+
591+ By ("Waiting for the finalizer to be added by the reconciler" )
592+ Eventually (Object (serverMaintenance )).Should (
593+ HaveField ("Finalizers" , ContainElement (serverMaintenanceFinalizer )),
594+ )
595+
596+ By ("Manually removing the finalizer to simulate a no-finalizer state" )
597+ Eventually (Update (serverMaintenance , func () {
598+ serverMaintenance .Finalizers = nil
599+ })).Should (Succeed ())
600+
601+ By ("Deleting the ServerMaintenance" )
602+ Expect (k8sClient .Delete (ctx , serverMaintenance )).To (Succeed ())
603+
604+ By ("Ensuring the ServerMaintenance is deleted immediately without cleanup side-effects" )
605+ Eventually (Get (serverMaintenance )).ShouldNot (Succeed ())
606+
607+ By ("Ensuring the Server was not modified during deletion" )
608+ Consistently (Object (server )).Should (
609+ HaveField ("Spec.ServerMaintenanceRef" , BeNil ()),
610+ )
611+ })
544612})
0 commit comments