Skip to content

Commit

Permalink
neonvm-controller: Patch status even if reconcile fails
Browse files Browse the repository at this point in the history
Fixes #889.

Co-authored-by: Artur Gadelshin <[email protected]>
  • Loading branch information
sharnoff and agadelshin committed Oct 10, 2024
1 parent edbe23c commit f017c80
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions pkg/neonvm/controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,39 @@ func (r *VMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
}

statusBefore := vm.Status.DeepCopy()
if err := r.doReconcile(ctx, &vm); err != nil {
reconcileErr := r.doReconcile(ctx, &vm)
if reconcileErr != nil {
r.Recorder.Eventf(&vm, corev1.EventTypeWarning, "Failed",
"Failed to reconcile (%s): %s", vm.Name, err)
return ctrl.Result{}, err
"Failed to reconcile (%s): %s", vm.Name, reconcileErr)
}

// If the status changed, try to update 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)
return ctrl.Result{}, err

if reconcileErr != nil {
// surface the original error
return ctrl.Result{}, reconcileErr
} else {
// otherwise, return this one
return ctrl.Result{}, err
}
}
}

// Only quickly requeue if we're scaling or migrating. Otherwise, we aren't expecting any
// changes from QEMU, and it's wasteful to repeatedly check.
requeueAfter := time.Second
if vm.Status.Phase == vmv1.VmPending || vm.Status.Phase == vmv1.VmRunning {
requeueAfter = 15 * time.Second
var requeueAfter time.Duration
if reconcileErr != nil {
requeueAfter = time.Second
if vm.Status.Phase == vmv1.VmPending || vm.Status.Phase == vmv1.VmRunning {
requeueAfter = 15 * time.Second
}
}

return ctrl.Result{RequeueAfter: requeueAfter}, nil
return ctrl.Result{RequeueAfter: requeueAfter}, reconcileErr
}

// doFinalizerOperationsForVirtualMachine will perform the required operations before delete the CR.
Expand Down

0 comments on commit f017c80

Please sign in to comment.