Skip to content

Commit 50e70b0

Browse files
committed
Address PR review: transition to Pending state instead of auto-deleting
When the referenced Server is not found, transition ServerMaintenance to Pending instead of deleting it. This follows the Kubernetes PVC pattern where a resource waits in Pending when its dependency is missing, and keeps the creator responsible for cleanup.
1 parent 73cea4d commit 50e70b0

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

internal/controller/servermaintenance_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ func (r *ServerMaintenanceReconciler) reconcile(ctx context.Context, maintenance
7373
server := &metalv1alpha1.Server{}
7474
if err := r.Get(ctx, client.ObjectKey{Name: maintenance.Spec.ServerRef.Name}, server); err != nil {
7575
if apierrors.IsNotFound(err) {
76+
log.V(1).Info("Referenced Server not found, transitioning to Pending state",
77+
"ServerMaintenance", client.ObjectKeyFromObject(maintenance),
78+
"Server", maintenance.Spec.ServerRef.Name,
79+
)
80+
if modified, err := r.patchMaintenanceState(ctx, maintenance, metalv1alpha1.ServerMaintenanceStatePending); err != nil || modified {
81+
return ctrl.Result{}, err
82+
}
7683
return ctrl.Result{}, nil
7784
}
7885
return ctrl.Result{}, fmt.Errorf("failed to get Server: %w", err)

internal/controller/servermaintenance_controller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,29 @@ var _ = Describe("ServerMaintenance Controller", func() {
543543
Expect(k8sClient.Delete(ctx, serverClaim)).To(Succeed())
544544
})
545545

546+
It("should transition ServerMaintenance to Pending state when its referenced Server no longer exists", func(ctx SpecContext) {
547+
By("Creating a ServerMaintenance object")
548+
serverMaintenance := &metalv1alpha1.ServerMaintenance{
549+
ObjectMeta: metav1.ObjectMeta{
550+
Name: "test-server-maintenance-orphan",
551+
Namespace: ns.Name,
552+
},
553+
Spec: metalv1alpha1.ServerMaintenanceSpec{
554+
ServerRef: &corev1.LocalObjectReference{Name: server.Name},
555+
Policy: metalv1alpha1.ServerMaintenancePolicyEnforced,
556+
},
557+
}
558+
Expect(k8sClient.Create(ctx, serverMaintenance)).To(Succeed())
559+
DeferCleanup(k8sClient.Delete, serverMaintenance)
560+
561+
By("Deleting the Server")
562+
Expect(k8sClient.Delete(ctx, server)).To(Succeed())
563+
Eventually(Get(server)).ShouldNot(Succeed())
564+
565+
By("Expecting the ServerMaintenance to transition to Pending state")
566+
Eventually(Object(serverMaintenance)).Should(HaveField("Status.State", Equal(metalv1alpha1.ServerMaintenanceStatePending)))
567+
})
568+
546569
It("should complete deletion when the referenced Server is already gone", func(ctx SpecContext) {
547570
By("Creating a ServerMaintenance object")
548571
serverMaintenance := &metalv1alpha1.ServerMaintenance{

0 commit comments

Comments
 (0)