Restore KubernetesStatus from Backup if set
package manager
import (
+ "encoding/json"
"fmt"
"reflect"
"sort"
@@ -156,6 +157,7 @@ func (m *VolumeManager) Create(name string, spec *types.VolumeSpec) (v *longhorn
size := spec.Size
lastBackup := ""
+ kubeStatus := &types.KubernetesStatus{}
if spec.FromBackup != "" {
backupTarget, err := GenerateBackupTarget(m.ds)
if err != nil {
@@ -166,6 +168,23 @@ func (m *VolumeManager) Create(name string, spec *types.VolumeSpec) (v *longhorn
return nil, fmt.Errorf("cannot get backup %v: %v", spec.FromBackup, err)
}
logrus.Infof("Override size of volume %v to %v because it's from backup", name, backup.VolumeSize)
+
+ // If KubernetesStatus is set on Backup, restore it.
+ if statusJSON, ok := backup.Labels[types.KubernetesStatusLabel]; ok {
+ if err := json.Unmarshal([]byte(statusJSON), kubeStatus); err != nil {
+ logrus.Errorf("could not unmarshal KubernetesStatus json for backup %v: %v",
+ backup.Name, err)
+ } else {
+ // We were able to unmarshal KubernetesStatus. Set the Ref fields.
+ if kubeStatus.PVCName != "" && kubeStatus.LastPVCRefAt == "" {
+ kubeStatus.LastPVCRefAt = backup.SnapshotCreated
+ }
+ if len(kubeStatus.WorkloadsStatus) != 0 && kubeStatus.LastPodRefAt == "" {
+ kubeStatus.LastPodRefAt = backup.SnapshotCreated
+ }
+ }
+ }
+
// set LastBackup for standby volumes only
if spec.Standby {
lastBackup = backup.Name
@@ -241,6 +260,7 @@ func (m *VolumeManager) Create(name string, spec *types.VolumeSpec) (v *longhorn
NodeSelector: spec.NodeSelector,
},
Status: types.VolumeStatus{
+ KubernetesStatus: *kubeStatus,
LastBackup: lastBackup,
},
}
