mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-20 06:54:32 +00:00
Merge pull request #7438 from Lyndon-Li/batch-delete-snapshot
Issue 7281: batch delete snapshot
This commit is contained in:
@@ -68,6 +68,7 @@ type backupDeletionReconciler struct {
|
||||
newPluginManager func(logrus.FieldLogger) clientmgmt.Manager
|
||||
backupStoreGetter persistence.ObjectBackupStoreGetter
|
||||
credentialStore credentials.FileStore
|
||||
repoEnsurer *repository.Ensurer
|
||||
}
|
||||
|
||||
// NewBackupDeletionReconciler creates a new backup deletion reconciler.
|
||||
@@ -81,6 +82,7 @@ func NewBackupDeletionReconciler(
|
||||
newPluginManager func(logrus.FieldLogger) clientmgmt.Manager,
|
||||
backupStoreGetter persistence.ObjectBackupStoreGetter,
|
||||
credentialStore credentials.FileStore,
|
||||
repoEnsurer *repository.Ensurer,
|
||||
) *backupDeletionReconciler {
|
||||
return &backupDeletionReconciler{
|
||||
Client: client,
|
||||
@@ -93,6 +95,7 @@ func NewBackupDeletionReconciler(
|
||||
newPluginManager: newPluginManager,
|
||||
backupStoreGetter: backupStoreGetter,
|
||||
credentialStore: credentialStore,
|
||||
repoEnsurer: repoEnsurer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,20 +505,16 @@ func (r *backupDeletionReconciler) deletePodVolumeSnapshots(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
snapshots, err := getSnapshotsInBackup(ctx, backup, r.Client)
|
||||
directSnapshots, err := getSnapshotsInBackup(ctx, backup, r.Client)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
var errs []error
|
||||
for _, snapshot := range snapshots {
|
||||
if err := r.repoMgr.Forget(ctx, snapshot); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return errs
|
||||
return batchDeleteSnapshots(ctx, r.repoEnsurer, r.repoMgr, directSnapshots, backup, r.logger)
|
||||
}
|
||||
|
||||
var batchDeleteSnapshotFunc = batchDeleteSnapshots
|
||||
|
||||
func (r *backupDeletionReconciler) deleteMovedSnapshots(ctx context.Context, backup *velerov1api.Backup) []error {
|
||||
if r.repoMgr == nil {
|
||||
return nil
|
||||
@@ -532,26 +531,52 @@ func (r *backupDeletionReconciler) deleteMovedSnapshots(ctx context.Context, bac
|
||||
return []error{errors.Wrapf(err, "failed to retrieve config for snapshot info")}
|
||||
}
|
||||
var errs []error
|
||||
directSnapshots := map[string][]repository.SnapshotIdentifier{}
|
||||
for i := range list.Items {
|
||||
cm := list.Items[i]
|
||||
snapshot := repository.SnapshotIdentifier{}
|
||||
if cm.Data == nil || len(cm.Data) == 0 {
|
||||
errs = append(errs, errors.New("no snapshot info in config"))
|
||||
continue
|
||||
}
|
||||
|
||||
b, err := json.Marshal(cm.Data)
|
||||
if err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "fail to marshal the snapshot info into JSON"))
|
||||
continue
|
||||
}
|
||||
|
||||
snapshot := repository.SnapshotIdentifier{}
|
||||
if err := json.Unmarshal(b, &snapshot); err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "failed to unmarshal snapshot info"))
|
||||
continue
|
||||
}
|
||||
if err := r.repoMgr.Forget(ctx, snapshot); err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "failed to delete snapshot %s, namespace: %s", snapshot.SnapshotID, snapshot.VolumeNamespace))
|
||||
|
||||
if snapshot.SnapshotID == "" || snapshot.VolumeNamespace == "" || snapshot.RepositoryType == "" {
|
||||
errs = append(errs, errors.Errorf("invalid snapshot, ID %s, namespace %s, repository %s", snapshot.SnapshotID, snapshot.VolumeNamespace, snapshot.RepositoryType))
|
||||
continue
|
||||
}
|
||||
r.logger.Infof("Deleted snapshot %s, namespace: %s, repo type: %s", snapshot.SnapshotID, snapshot.VolumeNamespace, snapshot.RepositoryType)
|
||||
|
||||
if directSnapshots[snapshot.VolumeNamespace] == nil {
|
||||
directSnapshots[snapshot.VolumeNamespace] = []repository.SnapshotIdentifier{}
|
||||
}
|
||||
|
||||
directSnapshots[snapshot.VolumeNamespace] = append(directSnapshots[snapshot.VolumeNamespace], snapshot)
|
||||
|
||||
r.logger.Infof("Deleting snapshot %s, namespace: %s, repo type: %s", snapshot.SnapshotID, snapshot.VolumeNamespace, snapshot.RepositoryType)
|
||||
}
|
||||
|
||||
for i := range list.Items {
|
||||
cm := list.Items[i]
|
||||
if err := r.Client.Delete(ctx, &cm); err != nil {
|
||||
r.logger.Warnf("Failed to delete snapshot info configmap %s/%s: %v", cm.Namespace, cm.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(directSnapshots) > 0 {
|
||||
deleteErrs := batchDeleteSnapshotFunc(ctx, r.repoEnsurer, r.repoMgr, directSnapshots, backup, r.logger)
|
||||
errs = append(errs, deleteErrs...)
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
@@ -592,7 +617,7 @@ func (r *backupDeletionReconciler) patchBackup(ctx context.Context, backup *vele
|
||||
|
||||
// getSnapshotsInBackup returns a list of all pod volume snapshot ids associated with
|
||||
// a given Velero backup.
|
||||
func getSnapshotsInBackup(ctx context.Context, backup *velerov1api.Backup, kbClient client.Client) ([]repository.SnapshotIdentifier, error) {
|
||||
func getSnapshotsInBackup(ctx context.Context, backup *velerov1api.Backup, kbClient client.Client) (map[string][]repository.SnapshotIdentifier, error) {
|
||||
podVolumeBackups := &velerov1api.PodVolumeBackupList{}
|
||||
options := &client.ListOptions{
|
||||
LabelSelector: labels.Set(map[string]string{
|
||||
@@ -607,3 +632,31 @@ func getSnapshotsInBackup(ctx context.Context, backup *velerov1api.Backup, kbCli
|
||||
|
||||
return podvolume.GetSnapshotIdentifier(podVolumeBackups), nil
|
||||
}
|
||||
|
||||
func batchDeleteSnapshots(ctx context.Context, repoEnsurer *repository.Ensurer, repoMgr repository.Manager,
|
||||
directSnapshots map[string][]repository.SnapshotIdentifier, backup *velerov1api.Backup, logger logrus.FieldLogger) []error {
|
||||
var errs []error
|
||||
for volumeNamespace, snapshots := range directSnapshots {
|
||||
batchForget := []string{}
|
||||
for _, snapshot := range snapshots {
|
||||
batchForget = append(batchForget, snapshot.SnapshotID)
|
||||
}
|
||||
|
||||
// For volumes in one backup, the BSL and repositoryType should always be the same
|
||||
repoType := snapshots[0].RepositoryType
|
||||
repo, err := repoEnsurer.EnsureRepo(ctx, backup.Namespace, volumeNamespace, backup.Spec.StorageLocation, repoType)
|
||||
if err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "error to ensure repo %s-%s-%s, skip deleting PVB snapshots %v", backup.Spec.StorageLocation, volumeNamespace, repoType, batchForget))
|
||||
continue
|
||||
}
|
||||
|
||||
if forgetErrs := repoMgr.BatchForget(ctx, repo, batchForget); len(forgetErrs) > 0 {
|
||||
errs = append(errs, forgetErrs...)
|
||||
continue
|
||||
}
|
||||
|
||||
logger.Infof("Batch deleted snapshots %v", batchForget)
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
@@ -53,6 +55,7 @@ import (
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/repository"
|
||||
repomocks "github.com/vmware-tanzu/velero/pkg/repository/mocks"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
@@ -94,6 +97,7 @@ func setupBackupDeletionControllerTest(t *testing.T, req *velerov1api.DeleteBack
|
||||
func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager },
|
||||
NewFakeSingleObjectBackupStoreGetter(backupStore),
|
||||
velerotest.NewFakeCredentialsFileStore("", nil),
|
||||
nil,
|
||||
),
|
||||
req: ctrl.Request{NamespacedName: types.NamespacedName{Namespace: req.Namespace, Name: req.Name}},
|
||||
}
|
||||
@@ -694,13 +698,13 @@ func TestGetSnapshotsInBackup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
podVolumeBackups []velerov1api.PodVolumeBackup
|
||||
expected []repository.SnapshotIdentifier
|
||||
expected map[string][]repository.SnapshotIdentifier
|
||||
longBackupNameEnabled bool
|
||||
}{
|
||||
{
|
||||
name: "no pod volume backups",
|
||||
podVolumeBackups: nil,
|
||||
expected: nil,
|
||||
expected: map[string][]repository.SnapshotIdentifier{},
|
||||
},
|
||||
{
|
||||
name: "no pod volume backups with matching label",
|
||||
@@ -720,7 +724,7 @@ func TestGetSnapshotsInBackup(t *testing.T) {
|
||||
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-2"},
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
expected: map[string][]repository.SnapshotIdentifier{},
|
||||
},
|
||||
{
|
||||
name: "some pod volume backups with matching label",
|
||||
@@ -761,16 +765,18 @@ func TestGetSnapshotsInBackup(t *testing.T) {
|
||||
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: ""},
|
||||
},
|
||||
},
|
||||
expected: []repository.SnapshotIdentifier{
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-3",
|
||||
RepositoryType: "restic",
|
||||
},
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-4",
|
||||
RepositoryType: "restic",
|
||||
expected: map[string][]repository.SnapshotIdentifier{
|
||||
"ns-1": {
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-3",
|
||||
RepositoryType: "restic",
|
||||
},
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-4",
|
||||
RepositoryType: "restic",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -814,11 +820,13 @@ func TestGetSnapshotsInBackup(t *testing.T) {
|
||||
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: ""},
|
||||
},
|
||||
},
|
||||
expected: []repository.SnapshotIdentifier{
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-3",
|
||||
RepositoryType: "restic",
|
||||
expected: map[string][]repository.SnapshotIdentifier{
|
||||
"ns-1": {
|
||||
{
|
||||
VolumeNamespace: "ns-1",
|
||||
SnapshotID: "snap-3",
|
||||
RepositoryType: "restic",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -843,21 +851,179 @@ func TestGetSnapshotsInBackup(t *testing.T) {
|
||||
res, err := getSnapshotsInBackup(context.TODO(), veleroBackup, clientBuilder.Build())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// sort to ensure good compare of slices
|
||||
less := func(snapshots []repository.SnapshotIdentifier) func(i, j int) bool {
|
||||
return func(i, j int) bool {
|
||||
if snapshots[i].VolumeNamespace == snapshots[j].VolumeNamespace {
|
||||
return snapshots[i].SnapshotID < snapshots[j].SnapshotID
|
||||
}
|
||||
return snapshots[i].VolumeNamespace < snapshots[j].VolumeNamespace
|
||||
assert.True(t, reflect.DeepEqual(res, test.expected))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func batchDeleteSucceed(ctx context.Context, repoEnsurer *repository.Ensurer, repoMgr repository.Manager, directSnapshots map[string][]repository.SnapshotIdentifier, backup *velerov1api.Backup, logger logrus.FieldLogger) []error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func batchDeleteFail(ctx context.Context, repoEnsurer *repository.Ensurer, repoMgr repository.Manager, directSnapshots map[string][]repository.SnapshotIdentifier, backup *velerov1api.Backup, logger logrus.FieldLogger) []error {
|
||||
return []error{
|
||||
errors.New("fake-delete-1"),
|
||||
errors.New("fake-delete-2"),
|
||||
}
|
||||
}
|
||||
|
||||
func generateSnapshotData(snapshot *repository.SnapshotIdentifier) (map[string]string, error) {
|
||||
if snapshot == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
b, err := json.Marshal(snapshot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make(map[string]string)
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func TestDeleteMovedSnapshots(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repoMgr repository.Manager
|
||||
batchDeleteSucceed bool
|
||||
backupName string
|
||||
snapshots []*repository.SnapshotIdentifier
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "repoMgr is nil",
|
||||
},
|
||||
{
|
||||
name: "no cm",
|
||||
repoMgr: repomocks.NewManager(t),
|
||||
},
|
||||
{
|
||||
name: "bad cm info",
|
||||
repoMgr: repomocks.NewManager(t),
|
||||
backupName: "backup-01",
|
||||
snapshots: []*repository.SnapshotIdentifier{nil},
|
||||
expected: []string{"no snapshot info in config"},
|
||||
},
|
||||
{
|
||||
name: "invalid snapshots",
|
||||
repoMgr: repomocks.NewManager(t),
|
||||
backupName: "backup-01",
|
||||
snapshots: []*repository.SnapshotIdentifier{
|
||||
{
|
||||
RepositoryType: "repo-1",
|
||||
VolumeNamespace: "ns-1",
|
||||
},
|
||||
{
|
||||
SnapshotID: "snapshot-1",
|
||||
VolumeNamespace: "ns-1",
|
||||
},
|
||||
{
|
||||
SnapshotID: "snapshot-1",
|
||||
RepositoryType: "repo-1",
|
||||
},
|
||||
},
|
||||
batchDeleteSucceed: true,
|
||||
expected: []string{
|
||||
"invalid snapshot, ID , namespace ns-1, repository repo-1",
|
||||
"invalid snapshot, ID snapshot-1, namespace ns-1, repository ",
|
||||
"invalid snapshot, ID snapshot-1, namespace , repository repo-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "batch delete succeed",
|
||||
repoMgr: repomocks.NewManager(t),
|
||||
backupName: "backup-01",
|
||||
snapshots: []*repository.SnapshotIdentifier{
|
||||
|
||||
{
|
||||
SnapshotID: "snapshot-1",
|
||||
RepositoryType: "repo-1",
|
||||
VolumeNamespace: "ns-1",
|
||||
},
|
||||
},
|
||||
batchDeleteSucceed: true,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
name: "batch delete fail",
|
||||
repoMgr: repomocks.NewManager(t),
|
||||
backupName: "backup-01",
|
||||
snapshots: []*repository.SnapshotIdentifier{
|
||||
{
|
||||
RepositoryType: "repo-1",
|
||||
VolumeNamespace: "ns-1",
|
||||
},
|
||||
{
|
||||
SnapshotID: "snapshot-1",
|
||||
RepositoryType: "repo-1",
|
||||
VolumeNamespace: "ns-1",
|
||||
},
|
||||
},
|
||||
expected: []string{"invalid snapshot, ID , namespace ns-1, repository repo-1", "fake-delete-1", "fake-delete-2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
objs := []runtime.Object{}
|
||||
for i, snapshot := range test.snapshots {
|
||||
snapshotData, err := generateSnapshotData(snapshot)
|
||||
require.NoError(t, err)
|
||||
|
||||
cm := corev1api.ConfigMap{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: corev1api.SchemeGroupVersion.String(),
|
||||
Kind: "ConfigMap",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "velero",
|
||||
Name: fmt.Sprintf("du-info-%d", i),
|
||||
Labels: map[string]string{
|
||||
velerov1api.BackupNameLabel: test.backupName,
|
||||
velerov1api.DataUploadSnapshotInfoLabel: "true",
|
||||
},
|
||||
},
|
||||
Data: snapshotData,
|
||||
}
|
||||
|
||||
objs = append(objs, &cm)
|
||||
}
|
||||
|
||||
sort.Slice(test.expected, less(test.expected))
|
||||
sort.Slice(res, less(res))
|
||||
veleroBackup := &velerov1api.Backup{}
|
||||
controller := NewBackupDeletionReconciler(
|
||||
velerotest.NewLogger(),
|
||||
velerotest.NewFakeControllerRuntimeClient(t, objs...),
|
||||
NewBackupTracker(),
|
||||
test.repoMgr,
|
||||
metrics.NewServerMetrics(),
|
||||
nil, // discovery helper
|
||||
func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager },
|
||||
NewFakeSingleObjectBackupStoreGetter(backupStore),
|
||||
velerotest.NewFakeCredentialsFileStore("", nil),
|
||||
nil,
|
||||
)
|
||||
|
||||
assert.Equal(t, test.expected, res)
|
||||
veleroBackup.Name = test.backupName
|
||||
|
||||
if test.batchDeleteSucceed {
|
||||
batchDeleteSnapshotFunc = batchDeleteSucceed
|
||||
} else {
|
||||
batchDeleteSnapshotFunc = batchDeleteFail
|
||||
}
|
||||
|
||||
errs := controller.deleteMovedSnapshots(context.Background(), veleroBackup)
|
||||
if test.expected == nil {
|
||||
assert.Nil(t, errs)
|
||||
} else {
|
||||
assert.Equal(t, len(test.expected), len(errs))
|
||||
for i := range test.expected {
|
||||
assert.EqualError(t, errs[i], test.expected[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,7 +343,7 @@ func (r *DataUploadReconciler) runCancelableDataUpload(ctx context.Context, fsBa
|
||||
velerov1api.AsyncOperationIDLabel: du.Labels[velerov1api.AsyncOperationIDLabel],
|
||||
}
|
||||
|
||||
if err := fsBackup.StartBackup(path, fmt.Sprintf("%s/%s", du.Spec.SourceNamespace, du.Spec.SourcePVC), "", false, tags, du.Spec.DataMoverConfig); err != nil {
|
||||
if err := fsBackup.StartBackup(path, datamover.GetRealSource(du.Spec.SourceNamespace, du.Spec.SourcePVC), "", false, tags, du.Spec.DataMoverConfig); err != nil {
|
||||
return r.errorOut(ctx, du, err, "error starting data path backup", log)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user