Skip to content

Commit

Permalink
feat(backup): upgrade path for backup backing images
Browse files Browse the repository at this point in the history
Ref: longhorn/longhorn 5411

Signed-off-by: James Lu <james.lu@suse.com>
  • Loading branch information
mantissahz committed May 22, 2024
1 parent 38a1b3d commit 89cf826
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion datastore/longhorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3981,7 +3981,7 @@ func (s *DataStore) ListBackupsWithBackupVolumeNameRO(backupVolumeName string) (
}
bvName := volumeName + "-" + backup.Spec.BackupTargetName
if backup.Annotations != nil {
if _, exists := backup.Annotations[types.UpgradedOldBackupFrom15x]; exists {
if _, exists := backup.Annotations[types.UpgradedOldBackupFrom16x]; exists {
bvName = volumeName
}
}
Expand Down
3 changes: 2 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
LonghornKindOrphan = "Orphan"

LonghornKindBackingImageDataSource = "BackingImageDataSource"
LonghornKindBackupBackingImage = "BackupBackingImage"

LonghornKindEngineImageList = "EngineImageList"
LonghornKindRecurringJobList = "RecurringJobList"
Expand Down Expand Up @@ -129,7 +130,7 @@ const (
ConfigMapResourceVersionKey = "configmap-resource-version"
UpdateSettingFromLonghorn = "update-setting-from-longhorn"

UpgradedOldBackupFrom15x = "upgraded-dld-backup-from-15x"
UpgradedOldBackupFrom16x = "upgraded-dld-backup-from-16x"
DeleteBackupTargetFromLonghorn = "delete-backup-target-from-longhorn"
UpdateBackupTargetFromLonghorn = "update-backup-target-from-longhorn"

Expand Down
20 changes: 20 additions & 0 deletions upgrade/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,26 @@ func ListAndUpdateBackingImageDataSourcesInProvidedCache(namespace string, lhCli
return bidss, nil
}

// ListAndUpdateBackupBackingImagesInProvidedCache list all backupBackingImages and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateBackupBackingImagesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.BackupBackingImage, error) {
if v, ok := resourceMaps[types.LonghornKindBackupBackingImage]; ok {
return v.(map[string]*longhorn.BackupBackingImage), nil
}

bbis := map[string]*longhorn.BackupBackingImage{}
bbisList, err := lhClient.LonghornV1beta2().BackupBackingImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, bids := range bbisList.Items {
bbis[bids.Name] = &bbisList.Items[i]
}

resourceMaps[types.LonghornKindBackingImageDataSource] = bbis

return bbis, nil
}

// ListAndUpdateRecurringJobsInProvidedCache list all recurringJobs and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateRecurringJobsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.RecurringJob, error) {
if v, ok := resourceMaps[types.LonghornKindRecurringJob]; ok {
Expand Down
39 changes: 38 additions & 1 deletion upgrade/v16xto170/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func UpgradeResources(namespace string, lhClient *lhclientset.Clientset, kubeCli
return err
}

if err := upgradeBackupBackingImages(namespace, lhClient, resourceMaps); err != nil {
return err
}

if err := upgradeRecurringJobs(namespace, lhClient, resourceMaps); err != nil {
return err
}
Expand Down Expand Up @@ -213,7 +217,7 @@ func upgradeBackups(namespace string, lhClient *lhclientset.Clientset, resourceM
if b.Annotations == nil {
b.Annotations = make(map[string]string)
}
b.Annotations[types.UpgradedOldBackupFrom15x] = ""
b.Annotations[types.UpgradedOldBackupFrom16x] = ""
if b.Spec.BackupTargetName == "" {
b.Spec.BackupTargetName = backupTarget.Name
}
Expand All @@ -225,6 +229,39 @@ func upgradeBackups(namespace string, lhClient *lhclientset.Clientset, resourceM
return nil
}

func upgradeBackupBackingImages(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (err error) {
defer func() {
err = errors.Wrapf(err, upgradeLogPrefix+"upgrade backup backing images failed")
}()

backupTarget, err := getOldDefaultBackupTarget(namespace, lhClient, resourceMaps)
if err != nil {
return errors.Wrapf(err, "failed to get old default backup target during the Longhorn Backup Backing Images upgrade")
}

backupBackingImageMap, err := upgradeutil.ListAndUpdateBackupBackingImagesInProvidedCache(namespace, lhClient, resourceMaps)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, "failed to list all existing Longhorn Backups during the Longhorn Backup Backing Images upgrade")
}

for _, bbi := range backupBackingImageMap {
if bbi.Spec.BackingImage == "" {
bbi.Spec.BackingImage = bbi.Status.BackingImage
}
if bbi.Spec.BackupTargetName == "" {
bbi.Spec.BackupTargetName = backupTarget.Name
}
if bbi.Spec.BackupTargetURL == "" {
bbi.Spec.BackupTargetURL = backupTarget.Spec.BackupTargetURL
}
}

return nil
}

func upgradeRecurringJobs(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (err error) {
defer func() {
err = errors.Wrapf(err, upgradeLogPrefix+"upgrade recurring job failed")
Expand Down

0 comments on commit 89cf826

Please sign in to comment.