-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor backup snapshot status updates into UpdateBackupSnapshotsSta…
…tus() Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
- Loading branch information
Showing
5 changed files
with
83 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package backup | ||
|
||
import ( | ||
"context" | ||
|
||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" | ||
snapshotv1listers "github.com/kubernetes-csi/external-snapshotter/client/v4/listers/volumesnapshot/v1" | ||
"github.com/sirupsen/logrus" | ||
"github.com/vmware-tanzu/velero/pkg/label" | ||
"github.com/vmware-tanzu/velero/pkg/util/boolptr" | ||
"github.com/vmware-tanzu/velero/pkg/volume" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
kbclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Common function to update the status of CSI snapshots | ||
// returns VolumeSnapshot, VolumeSnapshotContent, VolumeSnapshotClasses referenced | ||
func UpdateBackupSnapshotsStatus(client kbclient.Client, volumeSnapshotLister snapshotv1listers.VolumeSnapshotLister, backup *Request, backupLog logrus.FieldLogger) ([]snapshotv1api.VolumeSnapshot, []snapshotv1api.VolumeSnapshotContent, []snapshotv1api.VolumeSnapshotClass) { | ||
var volumeSnapshots []snapshotv1api.VolumeSnapshot | ||
var volumeSnapshotContents []snapshotv1api.VolumeSnapshotContent | ||
var volumeSnapshotClasses []snapshotv1api.VolumeSnapshotClass | ||
selector := label.NewSelectorForBackup(backup.Name) | ||
vscList := &snapshotv1api.VolumeSnapshotContentList{} | ||
|
||
if volumeSnapshotLister != nil { | ||
tmpVSs, err := volumeSnapshotLister.List(label.NewSelectorForBackup(backup.Name)) | ||
if err != nil { | ||
backupLog.Error(err) | ||
} | ||
for _, vs := range tmpVSs { | ||
volumeSnapshots = append(volumeSnapshots, *vs) | ||
} | ||
} | ||
|
||
backup.CSISnapshots = volumeSnapshots | ||
|
||
err := client.List(context.Background(), vscList, &kbclient.ListOptions{LabelSelector: selector}) | ||
if err != nil { | ||
backupLog.Error(err) | ||
} | ||
if len(vscList.Items) >= 0 { | ||
volumeSnapshotContents = vscList.Items | ||
} | ||
|
||
vsClassSet := sets.NewString() | ||
for index := range volumeSnapshotContents { | ||
// persist the volumesnapshotclasses referenced by vsc | ||
if volumeSnapshotContents[index].Spec.VolumeSnapshotClassName != nil && !vsClassSet.Has(*volumeSnapshotContents[index].Spec.VolumeSnapshotClassName) { | ||
vsClass := &snapshotv1api.VolumeSnapshotClass{} | ||
if err := client.Get(context.TODO(), kbclient.ObjectKey{Name: *volumeSnapshotContents[index].Spec.VolumeSnapshotClassName}, vsClass); err != nil { | ||
backupLog.Error(err) | ||
} else { | ||
vsClassSet.Insert(*volumeSnapshotContents[index].Spec.VolumeSnapshotClassName) | ||
volumeSnapshotClasses = append(volumeSnapshotClasses, *vsClass) | ||
} | ||
} | ||
} | ||
backup.Status.VolumeSnapshotsAttempted = len(backup.VolumeSnapshots) | ||
for _, snap := range backup.VolumeSnapshots { | ||
if snap.Status.Phase == volume.SnapshotPhaseCompleted { | ||
backup.Status.VolumeSnapshotsCompleted++ | ||
} | ||
} | ||
backup.Status.CSIVolumeSnapshotsAttempted = len(backup.CSISnapshots) | ||
for _, vs := range backup.CSISnapshots { | ||
if vs.Status != nil && boolptr.IsSetToTrue(vs.Status.ReadyToUse) { | ||
backup.Status.CSIVolumeSnapshotsCompleted++ | ||
} | ||
} | ||
return volumeSnapshots, volumeSnapshotContents, volumeSnapshotClasses | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters