Skip to content

Commit

Permalink
fix error message in condition to show it in UI
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Yang <poan.yang@suse.com>
  • Loading branch information
FrankYang0529 authored and guangbochen committed Jan 20, 2022
1 parent 8d0d8ae commit 1be9cb5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/master/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ func (h *Handler) setStatusError(vmBackup *harvesterv1.VirtualMachineBackup, err
Time: currentTime(),
Message: pointer.StringPtr(err.Error()),
}
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionFalse, "In error state"))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionFalse, "Error"))
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionFalse, "Error", err.Error()))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionFalse, "", "Not Ready"))

if _, updateErr := h.vmBackups.Update(vmBackupCpy); updateErr != nil {
return updateErr
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/master/backup/backup_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
func (h *Handler) updateConditions(vmBackup *harvesterv1.VirtualMachineBackup) error {
var vmBackupCpy = vmBackup.DeepCopy()
if isBackupProgressing(vmBackupCpy) {
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionTrue, "Operation in progress"))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionFalse, "Not ready"))
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionTrue, "", "Operation in progress"))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionFalse, "", "Not ready"))
}

ready := true
Expand All @@ -41,8 +41,8 @@ func (h *Handler) updateConditions(vmBackup *harvesterv1.VirtualMachineBackup) e
if ready && (vmBackupCpy.Status.ReadyToUse == nil || !*vmBackupCpy.Status.ReadyToUse) {
vmBackupCpy.Status.CreationTime = currentTime()
vmBackupCpy.Status.Error = nil
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionFalse, "Operation complete"))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionTrue, "Operation complete"))
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionFalse, "", "Operation complete"))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionTrue, "", "Operation complete"))
}

// check if the status need to update the error status
Expand All @@ -51,6 +51,8 @@ func (h *Handler) updateConditions(vmBackup *harvesterv1.VirtualMachineBackup) e
Time: currentTime(),
Message: pointer.StringPtr(errorMessage),
}
updateBackupCondition(vmBackupCpy, newProgressingCondition(corev1.ConditionFalse, "Error", errorMessage))
updateBackupCondition(vmBackupCpy, newReadyCondition(corev1.ConditionFalse, "", "Not Ready"))
}

vmBackupCpy.Status.ReadyToUse = pointer.BoolPtr(ready)
Expand Down
22 changes: 11 additions & 11 deletions pkg/controller/master/backup/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ func (h *RestoreHandler) updateOwnerRefAndTargetUID(vmRestore *harvesterv1.Virtu

// set vmRestore owner reference to the target VM
restoreCpy.SetOwnerReferences(configVMOwner(vm))
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionTrue, "Initializing VirtualMachineRestore"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "Initializing VirtualMachineRestore"))
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionTrue, "", "Initializing VirtualMachineRestore"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "", "Initializing VirtualMachineRestore"))

if _, err := h.restores.Update(restoreCpy); err != nil {
return err
Expand Down Expand Up @@ -603,8 +603,8 @@ func (h *RestoreHandler) updateStatus(
) error {
restoreCpy := vmRestore.DeepCopy()
if !isVolumesReady {
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionTrue, "Creating new PVCs"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "Waiting for new PVCs"))
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionTrue, "", "Creating new PVCs"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "", "Waiting for new PVCs"))
if !reflect.DeepEqual(vmRestore, restoreCpy) {
if _, err := h.restores.Update(restoreCpy); err != nil {
return err
Expand All @@ -614,9 +614,9 @@ func (h *RestoreHandler) updateStatus(
}

if !vm.Status.Ready {
reason := "Waiting for target vm to be ready"
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, reason))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, reason))
message := "Waiting for target vm to be ready"
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, "", message))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "", message))
if !reflect.DeepEqual(vmRestore, restoreCpy) {
if _, err := h.restores.Update(restoreCpy); err != nil {
return err
Expand All @@ -643,8 +643,8 @@ func (h *RestoreHandler) updateStatus(

restoreCpy.Status.RestoreTime = currentTime()
restoreCpy.Status.Complete = pointer.BoolPtr(true)
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, "Operation complete"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionTrue, "Operation complete"))
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, "", "Operation complete"))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionTrue, "", "Operation complete"))
if _, err := h.restores.Update(restoreCpy); err != nil {
return err
}
Expand All @@ -653,8 +653,8 @@ func (h *RestoreHandler) updateStatus(

func (h *RestoreHandler) updateStatusError(restore *harvesterv1.VirtualMachineRestore, err error, createEvent bool) error {
restoreCpy := restore.DeepCopy()
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, err.Error()))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, err.Error()))
updateRestoreCondition(restoreCpy, newProgressingCondition(corev1.ConditionFalse, "Error", err.Error()))
updateRestoreCondition(restoreCpy, newReadyCondition(corev1.ConditionFalse, "Error", err.Error()))

if !reflect.DeepEqual(restore, restoreCpy) {
if createEvent {
Expand Down
20 changes: 12 additions & 8 deletions pkg/controller/master/backup/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,37 @@ func getVMBackupError(vmBackup *harvesterv1.VirtualMachineBackup) *harvesterv1.E
return nil
}

func newReadyCondition(status corev1.ConditionStatus, message string) harvesterv1.Condition {
func newReadyCondition(status corev1.ConditionStatus, reason string, message string) harvesterv1.Condition {
return harvesterv1.Condition{
Type: harvesterv1.BackupConditionReady,
Status: status,
Message: message,
Reason: reason,
LastTransitionTime: currentTime().Format(time.RFC3339),
}
}

func newProgressingCondition(status corev1.ConditionStatus, message string) harvesterv1.Condition {
func newProgressingCondition(status corev1.ConditionStatus, reason string, message string) harvesterv1.Condition {
return harvesterv1.Condition{
Type: harvesterv1.BackupConditionProgressing,
Status: status,
Type: harvesterv1.BackupConditionProgressing,
Status: status,
// wrangler use Reason to determine whether an object is in error state.
// ref: https://github.com/rancher/wrangler/blob/6970ad98ba7bd2755312ccfc6540a92bc9a9e316/pkg/summary/summarizers.go#L220-L243
Reason: reason,
Message: message,
LastTransitionTime: currentTime().Format(time.RFC3339),
}
}

func updateBackupCondition(ss *harvesterv1.VirtualMachineBackup, c harvesterv1.Condition) {
ss.Status.Conditions = updateCondition(ss.Status.Conditions, c, false)
ss.Status.Conditions = updateCondition(ss.Status.Conditions, c)
}

func updateCondition(conditions []harvesterv1.Condition, c harvesterv1.Condition, includeReason bool) []harvesterv1.Condition {
func updateCondition(conditions []harvesterv1.Condition, c harvesterv1.Condition) []harvesterv1.Condition {
found := false
for i := range conditions {
if conditions[i].Type == c.Type {
if conditions[i].Status != c.Status || (includeReason && conditions[i].Reason != c.Reason) {
if conditions[i].Status != c.Status || (conditions[i].Reason != c.Reason) || (conditions[i].Message != c.Message) {
conditions[i] = c
}
found = true
Expand Down Expand Up @@ -121,7 +125,7 @@ func getRestoreID(vmRestore *harvesterv1.VirtualMachineRestore) string {
}

func updateRestoreCondition(r *harvesterv1.VirtualMachineRestore, c harvesterv1.Condition) {
r.Status.Conditions = updateCondition(r.Status.Conditions, c, true)
r.Status.Conditions = updateCondition(r.Status.Conditions, c)
}

func getNewVolumes(vm *kv1.VirtualMachineSpec, vmRestore *harvesterv1.VirtualMachineRestore) ([]kv1.Volume, error) {
Expand Down

0 comments on commit 1be9cb5

Please sign in to comment.