Skip to content

Commit 406b50a

Browse files
wwitzel3skriss
authored andcommitted
update restore process using snapshot locations
Signed-off-by: Wayne Witzel III <wayne@riotousliving.com>
1 parent 268080a commit 406b50a

File tree

7 files changed

+285
-138
lines changed

7 files changed

+285
-138
lines changed

pkg/backup/item_backupper.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (ib *defaultItemBackupper) takePVSnapshot(obj runtime.Unstructured, log log
459459
}
460460

461461
log.Info("Snapshotting PersistentVolume")
462-
snapshot := volumeSnapshot(ib.backupRequest.Backup, volumeID, volumeType, pvFailureDomainZone, location, iops)
462+
snapshot := volumeSnapshot(ib.backupRequest.Backup, metadata.GetName(), volumeID, volumeType, pvFailureDomainZone, location, iops)
463463

464464
var errs []error
465465
snapshotID, err := blockStore.CreateSnapshot(snapshot.Spec.ProviderVolumeID, snapshot.Spec.VolumeAZ, tags)
@@ -477,16 +477,17 @@ func (ib *defaultItemBackupper) takePVSnapshot(obj runtime.Unstructured, log log
477477
return kubeerrs.NewAggregate(errs)
478478
}
479479

480-
func volumeSnapshot(backup *api.Backup, volumeID, volumeType, az, location string, iops *int64) *volume.Snapshot {
480+
func volumeSnapshot(backup *api.Backup, volumeName, volumeID, volumeType, az, location string, iops *int64) *volume.Snapshot {
481481
return &volume.Snapshot{
482482
Spec: volume.SnapshotSpec{
483-
BackupName: backup.Name,
484-
BackupUID: string(backup.UID),
485-
Location: location,
486-
ProviderVolumeID: volumeID,
487-
VolumeType: volumeType,
488-
VolumeAZ: az,
489-
VolumeIOPS: iops,
483+
BackupName: backup.Name,
484+
BackupUID: string(backup.UID),
485+
Location: location,
486+
PersistentVolumeName: volumeName,
487+
ProviderVolumeID: volumeID,
488+
VolumeType: volumeType,
489+
VolumeAZ: az,
490+
VolumeIOPS: iops,
490491
},
491492
Status: volume.SnapshotStatus{
492493
Phase: volume.SnapshotPhaseNew,

pkg/cmd/server/server.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,7 @@ func (s *server) runControllers(config *api.Config, defaultVolumeSnapshotLocatio
717717
restorer, err := restore.NewKubernetesRestorer(
718718
s.discoveryHelper,
719719
client.NewDynamicFactory(s.dynamicClient),
720-
nil,
721720
s.config.restoreResourcePriorities,
722-
s.arkClient.ArkV1(),
723721
s.kubeClient.CoreV1().Namespaces(),
724722
s.resticManager,
725723
s.config.podVolumeOperationTimeout,
@@ -735,6 +733,7 @@ func (s *server) runControllers(config *api.Config, defaultVolumeSnapshotLocatio
735733
restorer,
736734
s.sharedInformerFactory.Ark().V1().Backups(),
737735
s.sharedInformerFactory.Ark().V1().BackupStorageLocations(),
736+
s.sharedInformerFactory.Ark().V1().VolumeSnapshotLocations(),
738737
false,
739738
s.logger,
740739
s.logLevel,

pkg/controller/restore_controller.go

+38-24
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,18 @@ var nonRestorableResources = []string{
6868
type restoreController struct {
6969
*genericController
7070

71-
namespace string
72-
restoreClient arkv1client.RestoresGetter
73-
backupClient arkv1client.BackupsGetter
74-
restorer restore.Restorer
75-
pvProviderExists bool
76-
backupLister listers.BackupLister
77-
restoreLister listers.RestoreLister
78-
backupLocationLister listers.BackupStorageLocationLister
79-
restoreLogLevel logrus.Level
80-
defaultBackupLocation string
81-
metrics *metrics.ServerMetrics
71+
namespace string
72+
restoreClient arkv1client.RestoresGetter
73+
backupClient arkv1client.BackupsGetter
74+
restorer restore.Restorer
75+
pvProviderExists bool
76+
backupLister listers.BackupLister
77+
restoreLister listers.RestoreLister
78+
backupLocationLister listers.BackupStorageLocationLister
79+
snapshotLocationLister listers.VolumeSnapshotLocationLister
80+
restoreLogLevel logrus.Level
81+
defaultBackupLocation string
82+
metrics *metrics.ServerMetrics
8283

8384
newPluginManager func(logger logrus.FieldLogger) plugin.Manager
8485
newBackupStore func(*api.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error)
@@ -92,6 +93,7 @@ func NewRestoreController(
9293
restorer restore.Restorer,
9394
backupInformer informers.BackupInformer,
9495
backupLocationInformer informers.BackupStorageLocationInformer,
96+
snapshotLocationInformer informers.VolumeSnapshotLocationInformer,
9597
pvProviderExists bool,
9698
logger logrus.FieldLogger,
9799
restoreLogLevel logrus.Level,
@@ -100,18 +102,19 @@ func NewRestoreController(
100102
metrics *metrics.ServerMetrics,
101103
) Interface {
102104
c := &restoreController{
103-
genericController: newGenericController("restore", logger),
104-
namespace: namespace,
105-
restoreClient: restoreClient,
106-
backupClient: backupClient,
107-
restorer: restorer,
108-
pvProviderExists: pvProviderExists,
109-
backupLister: backupInformer.Lister(),
110-
restoreLister: restoreInformer.Lister(),
111-
backupLocationLister: backupLocationInformer.Lister(),
112-
restoreLogLevel: restoreLogLevel,
113-
defaultBackupLocation: defaultBackupLocation,
114-
metrics: metrics,
105+
genericController: newGenericController("restore", logger),
106+
namespace: namespace,
107+
restoreClient: restoreClient,
108+
backupClient: backupClient,
109+
restorer: restorer,
110+
pvProviderExists: pvProviderExists,
111+
backupLister: backupInformer.Lister(),
112+
restoreLister: restoreInformer.Lister(),
113+
backupLocationLister: backupLocationInformer.Lister(),
114+
snapshotLocationLister: snapshotLocationInformer.Lister(),
115+
restoreLogLevel: restoreLogLevel,
116+
defaultBackupLocation: defaultBackupLocation,
117+
metrics: metrics,
115118

116119
// use variables to refer to these functions so they can be
117120
// replaced with fakes for testing.
@@ -124,6 +127,7 @@ func NewRestoreController(
124127
backupInformer.Informer().HasSynced,
125128
restoreInformer.Informer().HasSynced,
126129
backupLocationInformer.Informer().HasSynced,
130+
snapshotLocationInformer.Informer().HasSynced,
127131
)
128132

129133
restoreInformer.Informer().AddEventHandler(
@@ -233,6 +237,7 @@ func (c *restoreController) processRestore(key string) error {
233237
restore,
234238
actions,
235239
info,
240+
pluginManager,
236241
)
237242

238243
restore.Status.Warnings = len(restoreWarnings.Ark) + len(restoreWarnings.Cluster)
@@ -482,6 +487,7 @@ func (c *restoreController) runRestore(
482487
restore *api.Restore,
483488
actions []restore.ItemAction,
484489
info backupInfo,
490+
pluginManager plugin.Manager,
485491
) (restoreWarnings, restoreErrors api.RestoreResult, restoreFailure error) {
486492
logFile, err := ioutil.TempFile("", "")
487493
if err != nil {
@@ -531,10 +537,18 @@ func (c *restoreController) runRestore(
531537
}
532538
defer closeAndRemoveFile(resultsFile, c.logger)
533539

540+
volumeSnapshots, err := info.backupStore.GetBackupVolumeSnapshots(restore.Spec.BackupName)
541+
if err != nil {
542+
log.WithError(errors.WithStack(err)).Error("Error fetching volume snapshots")
543+
restoreErrors.Ark = append(restoreErrors.Ark, err.Error())
544+
restoreFailure = err
545+
return
546+
}
547+
534548
// Any return statement above this line means a total restore failure
535549
// Some failures after this line *may* be a total restore failure
536550
log.Info("starting restore")
537-
restoreWarnings, restoreErrors = c.restorer.Restore(log, restore, info.backup, backupFile, actions)
551+
restoreWarnings, restoreErrors = c.restorer.Restore(log, restore, info.backup, volumeSnapshots, backupFile, actions, c.snapshotLocationLister, pluginManager)
538552
log.Info("restore completed")
539553

540554
// Try to upload the log file. This is best-effort. If we fail, we'll add to the ark errors.

pkg/controller/restore_controller_test.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,10 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/pkg/errors"
28-
"github.com/sirupsen/logrus"
29-
"github.com/stretchr/testify/assert"
30-
"github.com/stretchr/testify/mock"
31-
"github.com/stretchr/testify/require"
32-
33-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34-
"k8s.io/apimachinery/pkg/runtime"
35-
core "k8s.io/client-go/testing"
36-
"k8s.io/client-go/tools/cache"
37-
3827
api "github.com/heptio/ark/pkg/apis/ark/v1"
3928
"github.com/heptio/ark/pkg/generated/clientset/versioned/fake"
4029
informers "github.com/heptio/ark/pkg/generated/informers/externalversions"
30+
listers "github.com/heptio/ark/pkg/generated/listers/ark/v1"
4131
"github.com/heptio/ark/pkg/metrics"
4232
"github.com/heptio/ark/pkg/persistence"
4333
persistencemocks "github.com/heptio/ark/pkg/persistence/mocks"
@@ -46,6 +36,16 @@ import (
4636
"github.com/heptio/ark/pkg/restore"
4737
"github.com/heptio/ark/pkg/util/collections"
4838
arktest "github.com/heptio/ark/pkg/util/test"
39+
"github.com/heptio/ark/pkg/volume"
40+
"github.com/pkg/errors"
41+
"github.com/sirupsen/logrus"
42+
"github.com/stretchr/testify/assert"
43+
"github.com/stretchr/testify/mock"
44+
"github.com/stretchr/testify/require"
45+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
46+
"k8s.io/apimachinery/pkg/runtime"
47+
core "k8s.io/client-go/testing"
48+
"k8s.io/client-go/tools/cache"
4949
)
5050

5151
func TestFetchBackupInfo(t *testing.T) {
@@ -104,6 +104,7 @@ func TestFetchBackupInfo(t *testing.T) {
104104
restorer,
105105
sharedInformers.Ark().V1().Backups(),
106106
sharedInformers.Ark().V1().BackupStorageLocations(),
107+
sharedInformers.Ark().V1().VolumeSnapshotLocations(),
107108
false,
108109
logger,
109110
logrus.InfoLevel,
@@ -197,6 +198,7 @@ func TestProcessRestoreSkips(t *testing.T) {
197198
restorer,
198199
sharedInformers.Ark().V1().Backups(),
199200
sharedInformers.Ark().V1().BackupStorageLocations(),
201+
sharedInformers.Ark().V1().VolumeSnapshotLocations(),
200202
false, // pvProviderExists
201203
logger,
202204
logrus.InfoLevel,
@@ -422,6 +424,7 @@ func TestProcessRestore(t *testing.T) {
422424
restorer,
423425
sharedInformers.Ark().V1().Backups(),
424426
sharedInformers.Ark().V1().BackupStorageLocations(),
427+
sharedInformers.Ark().V1().VolumeSnapshotLocations(),
425428
test.allowRestoreSnapshots,
426429
logger,
427430
logrus.InfoLevel,
@@ -498,6 +501,16 @@ func TestProcessRestore(t *testing.T) {
498501
backupStore.On("PutRestoreLog", test.backup.Name, test.restore.Name, mock.Anything).Return(test.putRestoreLogErr)
499502

500503
backupStore.On("PutRestoreResults", test.backup.Name, test.restore.Name, mock.Anything).Return(nil)
504+
505+
volumeSnapshots := []*volume.Snapshot{
506+
{
507+
Spec: volume.SnapshotSpec{
508+
PersistentVolumeName: "test-pv",
509+
BackupName: test.backup.Name,
510+
},
511+
},
512+
}
513+
backupStore.On("GetBackupVolumeSnapshots", test.backup.Name).Return(volumeSnapshots, nil)
501514
}
502515

503516
var (
@@ -629,6 +642,7 @@ func TestvalidateAndCompleteWhenScheduleNameSpecified(t *testing.T) {
629642
nil,
630643
sharedInformers.Ark().V1().Backups(),
631644
sharedInformers.Ark().V1().BackupStorageLocations(),
645+
sharedInformers.Ark().V1().VolumeSnapshotLocations(),
632646
false,
633647
logger,
634648
logrus.DebugLevel,
@@ -815,8 +829,11 @@ func (r *fakeRestorer) Restore(
815829
log logrus.FieldLogger,
816830
restore *api.Restore,
817831
backup *api.Backup,
832+
volumeSnapshots []*volume.Snapshot,
818833
backupReader io.Reader,
819834
actions []restore.ItemAction,
835+
snapshotLocationLister listers.VolumeSnapshotLocationLister,
836+
blockStoreGetter restore.BlockStoreGetter,
820837
) (api.RestoreResult, api.RestoreResult) {
821838
res := r.Called(log, restore, backup, backupReader, actions)
822839

0 commit comments

Comments
 (0)