mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-03 11:45:20 +00:00
Replace NewObjectBackupStore with interface (#3329)
In preparation for modifying the instantiation of `BackupStores` to be able to load credentials, change the function `NewObjectBackupStore` to be an interface that is passed in to all controllers. Previously, the function to get a new backup store was configurable but for many controllers was fixed to use `NewObjectBackupStore`. This change introduces an interface for getting the backup store and wraps the functionality from `NewObjectBackupStore` in a type which implements this interface. This will allow more flexibility when introducing credentials for a specific backup store as it will allow us to create a new `ObjectBackupStoreGetter` type which can be configured to add credentials config when creating the ObjectBackupStore without needing to change the API used by the controllers. Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
This commit is contained in:
@@ -90,7 +90,21 @@ type ObjectStoreGetter interface {
|
||||
GetObjectStore(provider string) (velero.ObjectStore, error)
|
||||
}
|
||||
|
||||
func NewObjectBackupStore(location *velerov1api.BackupStorageLocation, objectStoreGetter ObjectStoreGetter, logger logrus.FieldLogger) (BackupStore, error) {
|
||||
// ObjectBackupStoreGetter is a type that can get a velero.BackupStore for a
|
||||
// given BackupStorageLocation and ObjectStore.
|
||||
type ObjectBackupStoreGetter interface {
|
||||
Get(location *velerov1api.BackupStorageLocation, objectStoreGetter ObjectStoreGetter, logger logrus.FieldLogger) (BackupStore, error)
|
||||
}
|
||||
|
||||
type objectBackupStoreGetter struct{}
|
||||
|
||||
// NewObjectBackupStoreGetter returns a ObjectBackupStoreGetter that can get a
|
||||
// default velero.BackupStore.
|
||||
func NewObjectBackupStoreGetter() ObjectBackupStoreGetter {
|
||||
return &objectBackupStoreGetter{}
|
||||
}
|
||||
|
||||
func (b *objectBackupStoreGetter) Get(location *velerov1api.BackupStorageLocation, objectStoreGetter ObjectStoreGetter, logger logrus.FieldLogger) (BackupStore, error) {
|
||||
if location.Spec.ObjectStorage == nil {
|
||||
return nil, errors.New("backup storage location does not use object storage")
|
||||
}
|
||||
|
||||
@@ -604,10 +604,10 @@ func (osg objectStoreGetter) GetObjectStore(provider string) (velero.ObjectStore
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// TestNewObjectBackupStore runs the NewObjectBackupStore constructor and ensures
|
||||
// that an ObjectBackupStore is constructed correctly or an appropriate error is
|
||||
// returned.
|
||||
func TestNewObjectBackupStore(t *testing.T) {
|
||||
// TestNewObjectBackupStore runs the NewObjectBackupStoreGetter constructor and ensures
|
||||
// that it provides a BackupStore with a correctly constructed ObjectBackupStore or
|
||||
// that an appropriate error is returned.
|
||||
func TestNewObjectBackupStoreGetter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
location *velerov1api.BackupStorageLocation
|
||||
@@ -661,7 +661,8 @@ func TestNewObjectBackupStore(t *testing.T) {
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := NewObjectBackupStore(tc.location, tc.objectStoreGetter, velerotest.NewLogger())
|
||||
getter := NewObjectBackupStoreGetter()
|
||||
res, err := getter.Get(tc.location, tc.objectStoreGetter, velerotest.NewLogger())
|
||||
if tc.wantErr != "" {
|
||||
require.Equal(t, tc.wantErr, err.Error())
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user