Skip to content

Commit

Permalink
chore(backup): cleanup volume annotations (#5056)
Browse files Browse the repository at this point in the history
* chore(backup): add helper methods to extract info based on velero annotations

* chore(backup): add more volume tests

* chore(backup): add more volume tests

* chore(backup): move some annotations to types

* chore(backup): cleanup volume annotations
  • Loading branch information
JGAntunes authored Dec 17, 2024
1 parent ce84b16 commit 3cd76c8
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 105 deletions.
122 changes: 26 additions & 96 deletions pkg/kotsadmsnapshot/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func CreateApplicationBackup(ctx context.Context, a *apptypes.App, isScheduled b

veleroBackup.Spec.IncludedNamespaces = prepareIncludedNamespaces(includedNamespaces)

snapshotTrigger := "manual"
snapshotTrigger := types.BackupTriggerManual
if isScheduled {
snapshotTrigger = "schedule"
snapshotTrigger = types.BackupTriggerSchedule
}

veleroBackup.Name = ""
Expand All @@ -156,7 +156,7 @@ func CreateApplicationBackup(ctx context.Context, a *apptypes.App, isScheduled b
if veleroBackup.Annotations == nil {
veleroBackup.Annotations = make(map[string]string, 0)
}
veleroBackup.Annotations["kots.io/snapshot-trigger"] = snapshotTrigger
veleroBackup.Annotations[types.BackupTriggerAnnotation] = snapshotTrigger
veleroBackup.Annotations["kots.io/app-id"] = a.ID
veleroBackup.Annotations["kots.io/app-sequence"] = strconv.FormatInt(parentSequence, 10)
veleroBackup.Annotations["kots.io/snapshot-requested"] = time.Now().UTC().Format(time.RFC3339)
Expand Down Expand Up @@ -659,9 +659,9 @@ func appendCommonAnnotations(k8sClient kubernetes.Interface, annotations map[str
return nil, errors.Wrap(err, "failed to find kotsadm image")
}

snapshotTrigger := "manual"
snapshotTrigger := types.BackupTriggerManual
if metadata.isScheduled {
snapshotTrigger = "schedule"
snapshotTrigger = types.BackupTriggerSchedule
}

appSequences := map[string]int64{}
Expand Down Expand Up @@ -689,11 +689,11 @@ func appendCommonAnnotations(k8sClient kubernetes.Interface, annotations map[str
if annotations == nil {
annotations = make(map[string]string, 0)
}
annotations["kots.io/snapshot-trigger"] = snapshotTrigger
annotations[types.BackupTriggerAnnotation] = snapshotTrigger
annotations["kots.io/snapshot-requested"] = metadata.backupReqestedAt.Format(time.RFC3339)
annotations["kots.io/kotsadm-image"] = kotsadmImage
annotations["kots.io/kotsadm-deploy-namespace"] = metadata.kotsadmNamespace
annotations["kots.io/apps-sequences"] = marshalledAppSequences
annotations[types.BackupAppsSequencesAnnotation] = marshalledAppSequences
annotations["kots.io/apps-versions"] = marshalledAppVersions
annotations["kots.io/is-airgap"] = strconv.FormatBool(kotsadm.IsAirgap())
embeddedRegistryHost, _, _ := kotsutil.GetEmbeddedRegistryCreds(k8sClient)
Expand Down Expand Up @@ -773,7 +773,7 @@ func ListBackupsForApp(ctx context.Context, kotsadmNamespace string, appID strin
backup.Status = "New"
}

trigger, ok := veleroBackup.Annotations["kots.io/snapshot-trigger"]
trigger, ok := veleroBackup.Annotations[types.BackupTriggerAnnotation]
if ok {
backup.Trigger = trigger
}
Expand All @@ -783,56 +783,16 @@ func ListBackupsForApp(ctx context.Context, kotsadmNamespace string, appID strin
backup.SupportBundleID = supportBundleID
}

volumeCount, volumeCountOk := veleroBackup.Annotations["kots.io/snapshot-volume-count"]
if volumeCountOk {
i, err := strconv.Atoi(volumeCount)
if backup.Status != "New" && backup.Status != "InProgress" {
volumeSummary, err := getSnapshotVolumeSummary(ctx, &veleroBackup)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-count")
return nil, errors.Wrap(err, "failed to get volume summary")
}
backup.VolumeCount = i
}

volumeSuccessCount, volumeSuccessCountOk := veleroBackup.Annotations["kots.io/snapshot-volume-success-count"]
if volumeSuccessCountOk {
i, err := strconv.Atoi(volumeSuccessCount)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-success-count")
}
backup.VolumeSuccessCount = i
}

volumeBytes, volumeBytesOk := veleroBackup.Annotations["kots.io/snapshot-volume-bytes"]
if volumeBytesOk {
i, err := strconv.ParseInt(volumeBytes, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-bytes")
}
backup.VolumeBytes = i
backup.VolumeSizeHuman = units.HumanSize(float64(i))
}

if backup.Status != "New" && backup.Status != "InProgress" {
if !volumeBytesOk || !volumeSuccessCountOk {
// save computed summary as annotations if snapshot is finished
volumeSummary, err := getSnapshotVolumeSummary(ctx, &veleroBackup)
if err != nil {
return nil, errors.Wrap(err, "failed to get volume summary")
}

backup.VolumeCount = volumeSummary.VolumeCount
backup.VolumeSuccessCount = volumeSummary.VolumeSuccessCount
backup.VolumeBytes = volumeSummary.VolumeBytes
backup.VolumeSizeHuman = volumeSummary.VolumeSizeHuman

// This is failing with "the server could not find the requested resource (put backups.velero.io scheduled-1586536961)"
// veleroBackup.Annotations["kots.io/snapshot-volume-count"] = strconv.Itoa(backup.VolumeCount)
// veleroBackup.Annotations["kots.io/snapshot-volume-success-count"] = strconv.Itoa(backup.VolumeSuccessCount)
// veleroBackup.Annotations["kots.io/snapshot-volume-bytes"] = strconv.FormatInt(backup.VolumeBytes, 10)

// if _, err = veleroClient.Backups(backendStorageLocation.Namespace).UpdateStatus(&veleroBackup); err != nil {
// return nil, errors.Wrap(err, "failed to update velero backup")
// }
}
backup.VolumeCount = volumeSummary.VolumeCount
backup.VolumeSuccessCount = volumeSummary.VolumeSuccessCount
backup.VolumeBytes = volumeSummary.VolumeBytes
backup.VolumeSizeHuman = volumeSummary.VolumeSizeHuman
}

backups = append(backups, &backup)
Expand Down Expand Up @@ -898,40 +858,12 @@ func ListInstanceBackups(ctx context.Context, kotsadmNamespace string) ([]*types
backup.Status = "New"
}

trigger, ok := veleroBackup.Annotations["kots.io/snapshot-trigger"]
trigger, ok := veleroBackup.Annotations[types.BackupTriggerAnnotation]
if ok {
backup.Trigger = trigger
}

volumeCount, volumeCountOk := veleroBackup.Annotations["kots.io/snapshot-volume-count"]
if volumeCountOk {
i, err := strconv.Atoi(volumeCount)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-count")
}
backup.VolumeCount = i
}

volumeSuccessCount, volumeSuccessCountOk := veleroBackup.Annotations["kots.io/snapshot-volume-success-count"]
if volumeSuccessCountOk {
i, err := strconv.Atoi(volumeSuccessCount)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-success-count")
}
backup.VolumeSuccessCount = i
}

volumeBytes, volumeBytesOk := veleroBackup.Annotations["kots.io/snapshot-volume-bytes"]
if volumeBytesOk {
i, err := strconv.ParseInt(volumeBytes, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "failed to convert volume-bytes")
}
backup.VolumeBytes = i
backup.VolumeSizeHuman = units.HumanSize(float64(i))
}

appAnnotationStr, _ := veleroBackup.Annotations["kots.io/apps-sequences"]
appAnnotationStr, _ := veleroBackup.Annotations[types.BackupAppsSequencesAnnotation]
if len(appAnnotationStr) > 0 {
var apps map[string]int64
if err := json.Unmarshal([]byte(appAnnotationStr), &apps); err != nil {
Expand All @@ -956,19 +888,17 @@ func ListInstanceBackups(ctx context.Context, kotsadmNamespace string) ([]*types
}
}

// get volume information
if backup.Status != "New" && backup.Status != "InProgress" {
if !volumeBytesOk || !volumeSuccessCountOk {
// save computed summary as annotations if snapshot is finished
volumeSummary, err := getSnapshotVolumeSummary(ctx, &veleroBackup)
if err != nil {
return nil, errors.Wrap(err, "failed to get volume summary")
}

backup.VolumeCount = volumeSummary.VolumeCount
backup.VolumeSuccessCount = volumeSummary.VolumeSuccessCount
backup.VolumeBytes = volumeSummary.VolumeBytes
backup.VolumeSizeHuman = volumeSummary.VolumeSizeHuman
volumeSummary, err := getSnapshotVolumeSummary(ctx, &veleroBackup)
if err != nil {
return nil, errors.Wrap(err, "failed to get volume summary")
}

backup.VolumeCount = volumeSummary.VolumeCount
backup.VolumeSuccessCount = volumeSummary.VolumeSuccessCount
backup.VolumeBytes = volumeSummary.VolumeBytes
backup.VolumeSizeHuman = volumeSummary.VolumeSizeHuman
}

// group the velero backups by the name we present to the user
Expand Down
Loading

0 comments on commit 3cd76c8

Please sign in to comment.