Skip to content

Commit

Permalink
Track names of ns/volumesnapshots to get status via backup_operations…
Browse files Browse the repository at this point in the history
…_controller

Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Dec 2, 2023
1 parent a318e1d commit 3478acb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 36 additions & 0 deletions pkg/backup/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backup

import (
"context"
"fmt"

snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
"github.com/sirupsen/logrus"
Expand All @@ -14,6 +15,40 @@ import (
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
)

// Tracks volume snapshots for this backup.
// Completion status will be updated in backup_operations_controller.go via ProgressOperations information.
var (
backupVolumeSnapshotTracker map[string]map[string]bool // backup name -> namespace/volume snapshot name -> completion status
)

func TrackVolumeSnapshotsForBackup(backup *velerov1api.Backup, volumeSnapshots []snapshotv1api.VolumeSnapshot, backupLog logrus.FieldLogger) {
if backupVolumeSnapshotTracker == nil {
backupVolumeSnapshotTracker = make(map[string]map[string]bool, 0)
}
if backupVolumeSnapshotTracker[backup.Name] == nil {
backupVolumeSnapshotTracker[backup.Name] = make(map[string]bool, 0)
}
for _, vs := range volumeSnapshots {
backupVolumeSnapshotTracker[backup.Name][VolumeSnapshotStatusTrackerName(&vs)] = false
}
backupLog.Infof("Tracking %d volume snapshots for backup %s", len(volumeSnapshots), backup.Name)

Check warning on line 34 in pkg/backup/snapshots.go

View check run for this annotation

Codecov / codecov/patch

pkg/backup/snapshots.go#L24-L34

Added lines #L24 - L34 were not covered by tests
}

func VolumeSnapshotStatusTrackerName(vs *snapshotv1api.VolumeSnapshot) string {
return fmt.Sprintf("%s/%s", vs.Namespace, vs.Name)

Check warning on line 38 in pkg/backup/snapshots.go

View check run for this annotation

Codecov / codecov/patch

pkg/backup/snapshots.go#L37-L38

Added lines #L37 - L38 were not covered by tests
}

func GetVolumeSnapshotTrackerNamesForBackupName(backupName string) map[string]bool {
return backupVolumeSnapshotTracker[backupName]

Check warning on line 42 in pkg/backup/snapshots.go

View check run for this annotation

Codecov / codecov/patch

pkg/backup/snapshots.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

func UntrackVolumeSnapshotsForBackup(backup *velerov1api.Backup) {
if backupVolumeSnapshotTracker == nil {
return
}
delete(backupVolumeSnapshotTracker, backup.Name)

Check warning on line 49 in pkg/backup/snapshots.go

View check run for this annotation

Codecov / codecov/patch

pkg/backup/snapshots.go#L45-L49

Added lines #L45 - L49 were not covered by tests
}

// Common function to update the status of CSI snapshots
// returns VolumeSnapshot, VolumeSnapshotContent, VolumeSnapshotClasses referenced
func UpdateBackupCSISnapshotsStatus(client kbclient.Client, globalCRClient kbclient.Client, backup *velerov1api.Backup, backupLog logrus.FieldLogger) (volumeSnapshots []snapshotv1api.VolumeSnapshot, volumeSnapshotContents []snapshotv1api.VolumeSnapshotContent, volumeSnapshotClasses []snapshotv1api.VolumeSnapshotClass) {
Expand All @@ -30,6 +65,7 @@ func UpdateBackupCSISnapshotsStatus(client kbclient.Client, globalCRClient kbcli
if err != nil {
backupLog.Error(err)
}

volumeSnapshots = append(volumeSnapshots, vsList.Items...)

if err := client.List(context.Background(), vscList, &kbclient.ListOptions{LabelSelector: selector}); err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/backup_finalizer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
backup.Status.CompletionTimestamp = &metav1.Time{Time: r.clock.Now()}
recordBackupMetrics(log, backup, outBackupFile, r.metrics, true)

pkgbackup.UpdateBackupCSISnapshotsStatus(r.client, r.globalCRClient, backup, log)
// update backup metadata in object store
backupJSON := new(bytes.Buffer)
if err := encode.To(backup, "json", backupJSON); err != nil {
Expand Down

0 comments on commit 3478acb

Please sign in to comment.