mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-13 11:34:54 +00:00
* Update CRDs and CLI to support in-place restore (#10038) Update CRDs(Restore, DataDownload, PodVolumeRestore) and restore create CLI to support in-place restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore (#10066) Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Restore Exposer and PVC CSI to support in-place restore (#10104) 1. Update Restore Exposer to support exposing with existing PV for in-place restore 2. Update PVC CSI RIA to continue the restore process for in-place restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Block uploader to support increase restore (#10244) Update Block uploader to support increase restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Exposer to recreate the target PV if the volume mode is different with the restore PVC (#10257) Update Exposer to recreate the target PV if the volume mode is different with t he restore PVC Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Preserve PVC selected-node annotation via carrier annotation for in-place restore For in-place volume data restore, the existing PVC is deleted and recreated. For StorageClasses with the WaitForFirstConsumer volume binding mode, losing the volume.kubernetes.io/selected-node annotation could let the scheduler place the recreated workload Pod in a different zone than the original PV, leaving it stuck in ContainerCreating. Instead of relying on RestoreItemAction execution order (the generic PVC RIA unconditionally strips the selected-node annotation), the PVC CSI RIA now captures the annotation from the existing PVC right before deleting it and carries it on the target PVC via the Velero-internal restore.velero.io/inplace-restore-selected-node annotation. The restore engine translates the carrier back to the Kubernetes annotation after all RestoreItemActions have run and always strips the carrier so it never lands on the cluster. This makes the behavior independent of RIA ordering: the Kubernetes annotation is stripped by default on every path (including when the target PVC does not exist and Velero falls back to provisioning a new PVC), and preservation only happens when the CSI RIA explicitly captured a value from the existing PVC. Signed-off-by: chlins <chlins.zhang@gmail.com> * Update the control path to make the in-place incremental restore with block data mover work E2E (#10410) Update the control path to make the in-place incremental restore with block data mover work E2E Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> --------- Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> Signed-off-by: chlins <chlins.zhang@gmail.com> Co-authored-by: chlins <chlins.zhang@gmail.com>
399 lines
13 KiB
Go
399 lines
13 KiB
Go
/*
|
|
Copyright The Velero Contributors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/snapshot/upload"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
corev1api "k8s.io/api/core/v1"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
|
|
"github.com/vmware-tanzu/velero/internal/credentials"
|
|
"github.com/vmware-tanzu/velero/internal/credentials/mocks"
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
"github.com/vmware-tanzu/velero/pkg/repository"
|
|
udmrepo "github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
|
|
udmrepomocks "github.com/vmware-tanzu/velero/pkg/repository/udmrepo/mocks"
|
|
"github.com/vmware-tanzu/velero/pkg/uploader"
|
|
"github.com/vmware-tanzu/velero/pkg/uploader/kopia"
|
|
"github.com/vmware-tanzu/velero/pkg/util"
|
|
)
|
|
|
|
type FakeBackupProgressUpdater struct {
|
|
PodVolumeBackup *velerov1api.PodVolumeBackup
|
|
Log logrus.FieldLogger
|
|
Ctx context.Context
|
|
Cli client.Client
|
|
}
|
|
|
|
func (f *FakeBackupProgressUpdater) UpdateProgress(p *uploader.Progress) {}
|
|
|
|
type FakeRestoreProgressUpdater struct {
|
|
PodVolumeRestore *velerov1api.PodVolumeRestore
|
|
Log logrus.FieldLogger
|
|
Ctx context.Context
|
|
Cli client.Client
|
|
}
|
|
|
|
func (f *FakeRestoreProgressUpdater) UpdateProgress(p *uploader.Progress) {}
|
|
|
|
func TestRunBackup(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
hookBackupFunc func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error)
|
|
volMode uploader.PersistentVolumeMode
|
|
notError bool
|
|
}{
|
|
{
|
|
name: "success to backup",
|
|
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
|
|
return &uploader.SnapshotInfo{}, false, nil
|
|
},
|
|
notError: true,
|
|
},
|
|
{
|
|
name: "get error to backup",
|
|
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
|
|
return &uploader.SnapshotInfo{}, false, errors.New("failed to backup")
|
|
},
|
|
notError: false,
|
|
},
|
|
{
|
|
name: "success to backup block mode volume",
|
|
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
|
|
return &uploader.SnapshotInfo{}, false, nil
|
|
},
|
|
volMode: uploader.PersistentVolumeBlock,
|
|
notError: true,
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
mockBRepo := udmrepomocks.NewBackupRepo(t)
|
|
mockBRepo.On("GetAdvancedFeatures").Return(udmrepo.AdvancedFeatureInfo{})
|
|
|
|
var kp kopiaProvider
|
|
kp.log = logrus.New()
|
|
kp.bkRepo = mockBRepo
|
|
updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: kp.log, Ctx: t.Context(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()}
|
|
|
|
if tc.volMode == "" {
|
|
tc.volMode = uploader.PersistentVolumeFilesystem
|
|
}
|
|
kopiaBackupFunc = tc.hookBackupFunc
|
|
_, _, _, _, err := kp.RunBackup(t.Context(), "var", "", nil, false, "", CBTParam{}, tc.volMode, map[string]string{}, &updater)
|
|
if tc.notError {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.Error(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRunRestore(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
hookRestoreFunc func(ctx context.Context, rep repo.RepositoryWriter, progress *kopia.Progress, snapshotID, dest string, incremental bool, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, log logrus.FieldLogger, cancleCh chan struct{}) (int64, int32, error)
|
|
notError bool
|
|
volMode uploader.PersistentVolumeMode
|
|
incremental bool
|
|
}{
|
|
{
|
|
name: "normal restore",
|
|
hookRestoreFunc: func(ctx context.Context, rep repo.RepositoryWriter, progress *kopia.Progress, snapshotID, dest string, incremental bool, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, log logrus.FieldLogger, cancleCh chan struct{}) (int64, int32, error) {
|
|
return 0, 0, nil
|
|
},
|
|
notError: true,
|
|
},
|
|
{
|
|
name: "normal block mode restore",
|
|
hookRestoreFunc: func(ctx context.Context, rep repo.RepositoryWriter, progress *kopia.Progress, snapshotID, dest string, incremental bool, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, log logrus.FieldLogger, cancleCh chan struct{}) (int64, int32, error) {
|
|
return 0, 0, nil
|
|
},
|
|
volMode: uploader.PersistentVolumeBlock,
|
|
notError: true,
|
|
},
|
|
{
|
|
name: "failed to restore",
|
|
hookRestoreFunc: func(ctx context.Context, rep repo.RepositoryWriter, progress *kopia.Progress, snapshotID, dest string, incremental bool, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, log logrus.FieldLogger, cancleCh chan struct{}) (int64, int32, error) {
|
|
return 0, 0, errors.New("failed to restore")
|
|
},
|
|
notError: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var kp kopiaProvider
|
|
kp.log = logrus.New()
|
|
updater := FakeRestoreProgressUpdater{PodVolumeRestore: &velerov1api.PodVolumeRestore{}, Log: kp.log, Ctx: t.Context(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()}
|
|
|
|
if tc.volMode == "" {
|
|
tc.volMode = uploader.PersistentVolumeFilesystem
|
|
}
|
|
kopiaRestoreFunc = tc.hookRestoreFunc
|
|
_, err := kp.RunRestore(t.Context(), "", "/var", tc.incremental, CBTParam{}, tc.volMode, map[string]string{}, &updater)
|
|
if tc.notError {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.Error(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCheckContext(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
finishChan chan struct{}
|
|
restoreChan chan struct{}
|
|
uploader *upload.Uploader
|
|
expectCancel bool
|
|
expectBackup bool
|
|
expectRestore bool
|
|
}{
|
|
{
|
|
name: "FinishChan",
|
|
finishChan: make(chan struct{}),
|
|
restoreChan: make(chan struct{}),
|
|
uploader: &upload.Uploader{},
|
|
expectCancel: false,
|
|
expectBackup: false,
|
|
expectRestore: false,
|
|
},
|
|
{
|
|
name: "nil uploader",
|
|
finishChan: make(chan struct{}),
|
|
restoreChan: make(chan struct{}),
|
|
uploader: nil,
|
|
expectCancel: true,
|
|
expectBackup: false,
|
|
expectRestore: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
|
|
if tc.expectBackup {
|
|
go func() {
|
|
wg.Wait()
|
|
tc.restoreChan <- struct{}{}
|
|
}()
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
cancel()
|
|
wg.Done()
|
|
}()
|
|
|
|
kp := &kopiaProvider{log: logrus.New()}
|
|
kp.CheckContext(ctx, tc.finishChan, tc.restoreChan, tc.uploader)
|
|
|
|
if tc.expectCancel && tc.uploader != nil {
|
|
t.Error("Expected the uploader to be canceled")
|
|
}
|
|
|
|
if tc.expectBackup && tc.uploader == nil && len(tc.restoreChan) > 0 {
|
|
t.Error("Expected the restore channel to be closed")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetPassword(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
empytSecret bool
|
|
credGetterFunc func(*mocks.SecretStore, *corev1api.SecretKeySelector)
|
|
expectError bool
|
|
expectedPass string
|
|
}{
|
|
{
|
|
name: "valid credentials interface",
|
|
credGetterFunc: func(ss *mocks.SecretStore, selector *corev1api.SecretKeySelector) {
|
|
ss.On("Get", selector).Return("test", nil)
|
|
},
|
|
expectError: false,
|
|
expectedPass: "test",
|
|
},
|
|
{
|
|
name: "empty from secret",
|
|
empytSecret: true,
|
|
expectError: true,
|
|
expectedPass: "",
|
|
},
|
|
{
|
|
name: "ErrorGettingPassword",
|
|
credGetterFunc: func(ss *mocks.SecretStore, selector *corev1api.SecretKeySelector) {
|
|
ss.On("Get", selector).Return("", errors.New("error getting password"))
|
|
},
|
|
expectError: true,
|
|
expectedPass: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Mock CredentialGetter
|
|
credGetter := &credentials.CredentialGetter{}
|
|
mockCredGetter := &mocks.SecretStore{}
|
|
if !tc.empytSecret {
|
|
credGetter.FromSecret = mockCredGetter
|
|
}
|
|
repoKeySelector := &corev1api.SecretKeySelector{LocalObjectReference: corev1api.LocalObjectReference{Name: "velero-repo-credentials"}, Key: "repository-password"}
|
|
|
|
if tc.credGetterFunc != nil {
|
|
tc.credGetterFunc(mockCredGetter, repoKeySelector)
|
|
}
|
|
|
|
kp := &kopiaProvider{
|
|
credGetter: credGetter,
|
|
}
|
|
|
|
password, err := kp.GetPassword(nil)
|
|
if tc.expectError {
|
|
require.Error(t, err, "Expected an error")
|
|
} else {
|
|
require.NoError(t, err, "Expected no error")
|
|
}
|
|
|
|
assert.Equal(t, tc.expectedPass, password, "Expected password to match")
|
|
})
|
|
}
|
|
}
|
|
|
|
type MockCredentialGetter struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (m *MockCredentialGetter) GetCredentials() (string, error) {
|
|
args := m.Called()
|
|
return args.String(0), args.Error(1)
|
|
}
|
|
|
|
// MockRepoSvc is a mock implementation of the RepoService interface.
|
|
type MockRepoSvc struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (m *MockRepoSvc) Open(ctx context.Context, opts udmrepo.RepoOptions) (udmrepo.BackupRepo, error) {
|
|
args := m.Called(ctx, opts)
|
|
return args.Get(0).(udmrepo.BackupRepo), args.Error(1)
|
|
}
|
|
|
|
func TestNewKopiaUploaderProvider(t *testing.T) {
|
|
requestorType := "testRequestor"
|
|
ctx := t.Context()
|
|
backupRepo := repository.NewBackupRepository(velerov1api.DefaultNamespace, repository.BackupRepositoryKey{VolumeNamespace: "fake-volume-ns-02", BackupLocation: "fake-bsl-02", RepositoryType: "fake-repository-type-02"})
|
|
mockLog := logrus.New()
|
|
|
|
// Define test cases
|
|
testCases := []struct {
|
|
name string
|
|
mockCredGetter *mocks.SecretStore
|
|
mockBackupRepoService udmrepo.BackupRepoService
|
|
expectedError string
|
|
}{
|
|
{
|
|
name: "Success",
|
|
mockCredGetter: func() *mocks.SecretStore {
|
|
mockCredGetter := &mocks.SecretStore{}
|
|
mockCredGetter.On("Get", mock.Anything).Return("test", nil)
|
|
return mockCredGetter
|
|
}(),
|
|
mockBackupRepoService: func() udmrepo.BackupRepoService {
|
|
backupRepoService := &udmrepomocks.BackupRepoService{}
|
|
var backupRepo udmrepo.BackupRepo
|
|
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, nil)
|
|
return backupRepoService
|
|
}(),
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "Error to get repo options",
|
|
mockCredGetter: func() *mocks.SecretStore {
|
|
mockCredGetter := &mocks.SecretStore{}
|
|
mockCredGetter.On("Get", mock.Anything).Return("test", errors.New("failed to get password"))
|
|
return mockCredGetter
|
|
}(),
|
|
mockBackupRepoService: func() udmrepo.BackupRepoService {
|
|
backupRepoService := &udmrepomocks.BackupRepoService{}
|
|
var backupRepo udmrepo.BackupRepo
|
|
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, nil)
|
|
return backupRepoService
|
|
}(),
|
|
expectedError: "error to get repo options",
|
|
},
|
|
{
|
|
name: "Error open repository service",
|
|
mockCredGetter: func() *mocks.SecretStore {
|
|
mockCredGetter := &mocks.SecretStore{}
|
|
mockCredGetter.On("Get", mock.Anything).Return("test", nil)
|
|
return mockCredGetter
|
|
}(),
|
|
mockBackupRepoService: func() udmrepo.BackupRepoService {
|
|
backupRepoService := &udmrepomocks.BackupRepoService{}
|
|
var backupRepo udmrepo.BackupRepo
|
|
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, errors.New("failed to init repository"))
|
|
return backupRepoService
|
|
}(),
|
|
expectedError: "Failed to find kopia repository",
|
|
},
|
|
}
|
|
|
|
// Iterate through test cases
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
credGetter := &credentials.CredentialGetter{FromSecret: tc.mockCredGetter}
|
|
BackupRepoServiceCreateFunc = func(string, logrus.FieldLogger) udmrepo.BackupRepoService {
|
|
return tc.mockBackupRepoService
|
|
}
|
|
// Call the function being tested.
|
|
_, err := NewKopiaUploaderProvider(requestorType, ctx, credGetter, backupRepo, mockLog)
|
|
|
|
// Assertions
|
|
if tc.expectedError != "" {
|
|
require.ErrorContains(t, err, tc.expectedError)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Verify that the expected methods were called on the mocks.
|
|
tc.mockCredGetter.AssertExpectations(t)
|
|
})
|
|
}
|
|
}
|