Skip to content

Commit

Permalink
neonvm-controller: patch status if reconcile fails (neondatabase#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
agadelshin committed Jul 22, 2024
1 parent 5a5fd62 commit 3bf2d00
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions neonvm/controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,42 @@ func (r *VMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
if err := r.doReconcile(ctx, &vm); err != nil {
r.Recorder.Eventf(&vm, corev1.EventTypeWarning, "Failed",
"Failed to reconcile (%s): %s", vm.Name, err)
if !DeepEqual(statusBefore, vm.Status) {
if err := r.patchStatus(ctx, req.NamespacedName, vm.Status); err != nil {
log.Error(err, "Unable to update status for VirtualMachine", "virtualmachine", vm.Name)
}
}
return ctrl.Result{}, err
}

// If the status changed, try to update the object
// If the status changed, try to patch the object
if !DeepEqual(statusBefore, vm.Status) {
if err := r.Status().Update(ctx, &vm); err != nil {
log.Error(err, "Failed to update VirtualMachine status after reconcile loop",
"virtualmachine", vm.Name)
if err := r.patchStatus(ctx, req.NamespacedName, vm.Status); err != nil {
log.Error(err, "Unable to update status for VirtualMachine", "virtualmachine", vm.Name)
return ctrl.Result{}, err
}
}

return ctrl.Result{RequeueAfter: time.Second}, nil
}

// patchStatus create a patch for VirtualMachine and send a Patch request with a new status
func (r *VMReconciler) patchStatus(ctx context.Context, name types.NamespacedName, newStatus vmv1.VirtualMachineStatus) error {
log := log.FromContext(ctx)

var vm vmv1.VirtualMachine
if err := r.Get(ctx, name, &vm); err != nil {
log.Error(err, "Unable to fetch VirtualMachine")
return err
}

patch := client.MergeFrom(vm.DeepCopy())
vm.Status = newStatus

return r.Status().Patch(ctx, &vm, patch)

}

// doFinalizerOperationsForVirtualMachine will perform the required operations before delete the CR.
func (r *VMReconciler) doFinalizerOperationsForVirtualMachine(ctx context.Context, vm *vmv1.VirtualMachine) {
// Note: It is not recommended to use finalizers with the purpose of delete resources which are
Expand Down

0 comments on commit 3bf2d00

Please sign in to comment.