Skip to content

Commit

Permalink
Allow deletion of the ServerClaim when Server does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikdr committed Jul 4, 2024
1 parent 59fa511 commit c248b55
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
27 changes: 15 additions & 12 deletions internal/controller/serverclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (r *ServerClaimReconciler) delete(ctx context.Context, log logr.Logger, cla

server := &metalv1alpha1.Server{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: claim.Spec.ServerRef.Name}, server); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get server: %w", err)
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to get server: %w", err)
}
log.V(1).Info("Server gone")
}

if server.Spec.ServerClaimRef != nil {
Expand All @@ -71,18 +74,18 @@ func (r *ServerClaimReconciler) delete(ctx context.Context, log logr.Logger, cla
}
}

config := &metalv1alpha1.ServerBootConfiguration{}
if err := r.Get(ctx, client.ObjectKey{Namespace: claim.Namespace, Name: claim.Name}, config); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
config := &metalv1alpha1.ServerBootConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: claim.Name,
Namespace: claim.Namespace,
},
}

if err := r.Delete(ctx, config); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to delete config: %w", err)
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to delete serverbootconfig: %w", err)
}
log.V(1).Info("ServerBootConfiguration gone")
}
log.V(1).Info("Removed boot config")

if err := r.removeBootConfigRefFromServerAndPowerOff(ctx, config, server); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to remove boot config ref from server: %w", err)
Expand Down Expand Up @@ -231,14 +234,14 @@ func (r *ServerClaimReconciler) removeClaimRefFromServer(ctx context.Context, se
}

func (r *ServerClaimReconciler) removeBootConfigRefFromServerAndPowerOff(ctx context.Context, config *metalv1alpha1.ServerBootConfiguration, server *metalv1alpha1.Server) error {
if ref := server.Spec.BootConfigurationRef; ref == nil || ref.UID != config.UID {
if ref := server.Spec.BootConfigurationRef; ref == nil || (ref.Name != config.Name && ref.Namespace != config.Namespace) {
return nil
}

serverBase := server.DeepCopy()
server.Spec.BootConfigurationRef = nil
server.Spec.Power = metalv1alpha1.PowerOff
if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil {
if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
Expand Down
22 changes: 22 additions & 0 deletions internal/controller/serverclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,26 @@ var _ = Describe("ServerClaim Controller", func() {
}
Eventually(Get(config)).Should(Satisfy(apierrors.IsNotFound))
})

It("should allow deletion of ServerClaim without a Server", func(ctx SpecContext) {
By("Creating a ServerClaim")
claim := &metalv1alpha1.ServerClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns.Name,
GenerateName: "test-",
},
Spec: metalv1alpha1.ServerClaimSpec{
Power: metalv1alpha1.PowerOn,
ServerRef: &v1.LocalObjectReference{Name: "non-existent-server"},
Image: "foo:bar",
},
}
Expect(k8sClient.Create(ctx, claim)).To(Succeed())

By("Deleting the ServerClaim")
Expect(k8sClient.Delete(ctx, claim)).To(Succeed())

By("Ensuring that the ServerClaim is deleted")
Eventually(Get(claim)).Should(Satisfy(apierrors.IsNotFound))
})
})

0 comments on commit c248b55

Please sign in to comment.