plugin versioning v1 refactor for VolumeSnapshotter

Signed-off-by: Scott Seago <sseago@redhat.com>
This commit is contained in:
Scott Seago
2022-09-08 18:09:18 -04:00
parent ee254c644f
commit b6088356e6
16 changed files with 142 additions and 110 deletions

View File

@@ -27,11 +27,13 @@ import (
biav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/backupitemaction/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process"
riav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/restoreitemaction/v1"
vsv1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/volumesnapshotter/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
biav1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v1"
isv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/item_snapshotter/v1"
riav1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/restoreitemaction/v1"
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
)
// Manager manages the lifecycles of plugins.
@@ -40,7 +42,7 @@ type Manager interface {
GetObjectStore(name string) (velero.ObjectStore, error)
// GetVolumeSnapshotter returns the VolumeSnapshotter plugin for name.
GetVolumeSnapshotter(name string) (velero.VolumeSnapshotter, error)
GetVolumeSnapshotter(name string) (vsv1.VolumeSnapshotter, error)
// GetBackupItemActions returns all v1 backup item action plugins.
GetBackupItemActions() ([]biav1.BackupItemAction, error)
@@ -161,17 +163,21 @@ func (m *manager) GetObjectStore(name string) (velero.ObjectStore, error) {
}
// GetVolumeSnapshotter returns a restartableVolumeSnapshotter for name.
func (m *manager) GetVolumeSnapshotter(name string) (velero.VolumeSnapshotter, error) {
func (m *manager) GetVolumeSnapshotter(name string) (vsv1.VolumeSnapshotter, error) {
name = sanitizeName(name)
restartableProcess, err := m.getRestartableProcess(common.PluginKindVolumeSnapshotter, name)
if err != nil {
return nil, err
for _, adaptedVolumeSnapshotter := range vsv1cli.AdaptedVolumeSnapshotters() {
restartableProcess, err := m.getRestartableProcess(adaptedVolumeSnapshotter.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedVolumeSnapshotter.GetRestartable(name, restartableProcess), nil
}
r := NewRestartableVolumeSnapshotter(name, restartableProcess)
return r, nil
return nil, fmt.Errorf("unable to get valid VolumeSnapshotter for %q", name)
}
// GetBackupItemActions returns all backup item actions as restartableBackupItemActions.

View File

@@ -30,6 +30,7 @@ import (
biav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/backupitemaction/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process"
riav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/restoreitemaction/v1"
vsv1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/volumesnapshotter/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/framework"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
"github.com/vmware-tanzu/velero/pkg/test"
@@ -176,9 +177,9 @@ func TestGetVolumeSnapshotter(t *testing.T) {
return m.GetVolumeSnapshotter(name)
},
func(name string, sharedPluginProcess process.RestartableProcess) interface{} {
return &restartableVolumeSnapshotter{
key: process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name},
sharedPluginProcess: sharedPluginProcess,
return &vsv1cli.RestartableVolumeSnapshotter{
Key: process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name},
SharedPluginProcess: sharedPluginProcess,
}
},
true,

View File

@@ -1,5 +1,5 @@
/*
Copyright 2018 the Velero contributors.
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.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package clientmgmt
package v1
import (
"github.com/pkg/errors"
@@ -22,25 +22,44 @@ import (
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
)
// AdaptedVolumeSnapshotter is a volume snapshotter adapted to the v1 VolumeSnapshotter API
type AdaptedVolumeSnapshotter struct {
Kind common.PluginKind
// Get returns a restartable VolumeSnapshotter for the given name and process, wrapping if necessary
GetRestartable func(name string, restartableProcess process.RestartableProcess) vsv1.VolumeSnapshotter
}
func AdaptedVolumeSnapshotters() []AdaptedVolumeSnapshotter {
return []AdaptedVolumeSnapshotter{
{
Kind: common.PluginKindVolumeSnapshotter,
GetRestartable: func(name string, restartableProcess process.RestartableProcess) vsv1.VolumeSnapshotter {
return NewRestartableVolumeSnapshotter(name, restartableProcess)
},
},
}
}
// RestartableVolumeSnapshotter is a volume snapshotter for a given implementation (such as "aws"). It is associated with
// a restartableProcess, which may be shared and used to run multiple plugins. At the beginning of each method
// call, the restartableVolumeSnapshotter asks its restartableProcess to restart itself if needed (e.g. if the
// process terminated for any reason), then it proceeds with the actual call.
type restartableVolumeSnapshotter struct {
key process.KindAndName
sharedPluginProcess process.RestartableProcess
type RestartableVolumeSnapshotter struct {
Key process.KindAndName
SharedPluginProcess process.RestartableProcess
config map[string]string
}
// NewRestartableVolumeSnapshotter returns a new restartableVolumeSnapshotter.
func NewRestartableVolumeSnapshotter(name string, sharedPluginProcess process.RestartableProcess) *restartableVolumeSnapshotter {
func NewRestartableVolumeSnapshotter(name string, sharedPluginProcess process.RestartableProcess) *RestartableVolumeSnapshotter {
key := process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name}
r := &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: sharedPluginProcess,
r := &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: sharedPluginProcess,
}
// Register our reinitializer so we can reinitialize after a restart with r.config.
@@ -50,8 +69,8 @@ func NewRestartableVolumeSnapshotter(name string, sharedPluginProcess process.Re
}
// reinitialize reinitializes a re-dispensed plugin using the initial data passed to Init().
func (r *restartableVolumeSnapshotter) Reinitialize(dispensed interface{}) error {
volumeSnapshotter, ok := dispensed.(velero.VolumeSnapshotter)
func (r *RestartableVolumeSnapshotter) Reinitialize(dispensed interface{}) error {
volumeSnapshotter, ok := dispensed.(vsv1.VolumeSnapshotter)
if !ok {
return errors.Errorf("%T is not a VolumeSnapshotter!", dispensed)
}
@@ -60,13 +79,13 @@ func (r *restartableVolumeSnapshotter) Reinitialize(dispensed interface{}) error
// getVolumeSnapshotter returns the volume snapshotter for this restartableVolumeSnapshotter. It does *not* restart the
// plugin process.
func (r *restartableVolumeSnapshotter) getVolumeSnapshotter() (velero.VolumeSnapshotter, error) {
plugin, err := r.sharedPluginProcess.GetByKindAndName(r.key)
func (r *RestartableVolumeSnapshotter) getVolumeSnapshotter() (vsv1.VolumeSnapshotter, error) {
plugin, err := r.SharedPluginProcess.GetByKindAndName(r.Key)
if err != nil {
return nil, err
}
volumeSnapshotter, ok := plugin.(velero.VolumeSnapshotter)
volumeSnapshotter, ok := plugin.(vsv1.VolumeSnapshotter)
if !ok {
return nil, errors.Errorf("%T is not a VolumeSnapshotter!", plugin)
}
@@ -74,9 +93,9 @@ func (r *restartableVolumeSnapshotter) getVolumeSnapshotter() (velero.VolumeSnap
return volumeSnapshotter, nil
}
// getDelegate restarts the plugin process (if needed) and returns the volume snapshotter for this restartableVolumeSnapshotter.
func (r *restartableVolumeSnapshotter) getDelegate() (velero.VolumeSnapshotter, error) {
if err := r.sharedPluginProcess.ResetIfNeeded(); err != nil {
// getDelegate restarts the plugin process (if needed) and returns the volume snapshotter for this RestartableVolumeSnapshotter.
func (r *RestartableVolumeSnapshotter) getDelegate() (vsv1.VolumeSnapshotter, error) {
if err := r.SharedPluginProcess.ResetIfNeeded(); err != nil {
return nil, err
}
@@ -85,7 +104,7 @@ func (r *restartableVolumeSnapshotter) getDelegate() (velero.VolumeSnapshotter,
// Init initializes the volume snapshotter instance using config. If this is the first invocation, r stores config for future
// reinitialization needs. Init does NOT restart the shared plugin process. Init may only be called once.
func (r *restartableVolumeSnapshotter) Init(config map[string]string) error {
func (r *RestartableVolumeSnapshotter) Init(config map[string]string) error {
if r.config != nil {
return errors.Errorf("already initialized")
}
@@ -103,12 +122,12 @@ func (r *restartableVolumeSnapshotter) Init(config map[string]string) error {
// init calls Init on volumeSnapshotter with config. This is split out from Init() so that both Init() and reinitialize() may
// call it using a specific VolumeSnapshotter.
func (r *restartableVolumeSnapshotter) init(volumeSnapshotter velero.VolumeSnapshotter, config map[string]string) error {
func (r *RestartableVolumeSnapshotter) init(volumeSnapshotter vsv1.VolumeSnapshotter, config map[string]string) error {
return volumeSnapshotter.Init(config)
}
// CreateVolumeFromSnapshot restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) CreateVolumeFromSnapshot(snapshotID string, volumeType string, volumeAZ string, iops *int64) (volumeID string, err error) {
func (r *RestartableVolumeSnapshotter) CreateVolumeFromSnapshot(snapshotID string, volumeType string, volumeAZ string, iops *int64) (volumeID string, err error) {
delegate, err := r.getDelegate()
if err != nil {
return "", err
@@ -117,7 +136,7 @@ func (r *restartableVolumeSnapshotter) CreateVolumeFromSnapshot(snapshotID strin
}
// GetVolumeID restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) GetVolumeID(pv runtime.Unstructured) (string, error) {
func (r *RestartableVolumeSnapshotter) GetVolumeID(pv runtime.Unstructured) (string, error) {
delegate, err := r.getDelegate()
if err != nil {
return "", err
@@ -126,7 +145,7 @@ func (r *restartableVolumeSnapshotter) GetVolumeID(pv runtime.Unstructured) (str
}
// SetVolumeID restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
func (r *RestartableVolumeSnapshotter) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
delegate, err := r.getDelegate()
if err != nil {
return nil, err
@@ -135,7 +154,7 @@ func (r *restartableVolumeSnapshotter) SetVolumeID(pv runtime.Unstructured, volu
}
// GetVolumeInfo restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) GetVolumeInfo(volumeID string, volumeAZ string) (string, *int64, error) {
func (r *RestartableVolumeSnapshotter) GetVolumeInfo(volumeID string, volumeAZ string) (string, *int64, error) {
delegate, err := r.getDelegate()
if err != nil {
return "", nil, err
@@ -144,7 +163,7 @@ func (r *restartableVolumeSnapshotter) GetVolumeInfo(volumeID string, volumeAZ s
}
// CreateSnapshot restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) CreateSnapshot(volumeID string, volumeAZ string, tags map[string]string) (snapshotID string, err error) {
func (r *RestartableVolumeSnapshotter) CreateSnapshot(volumeID string, volumeAZ string, tags map[string]string) (snapshotID string, err error) {
delegate, err := r.getDelegate()
if err != nil {
return "", err
@@ -153,7 +172,7 @@ func (r *restartableVolumeSnapshotter) CreateSnapshot(volumeID string, volumeAZ
}
// DeleteSnapshot restarts the plugin's process if needed, then delegates the call.
func (r *restartableVolumeSnapshotter) DeleteSnapshot(snapshotID string) error {
func (r *RestartableVolumeSnapshotter) DeleteSnapshot(snapshotID string) error {
delegate, err := r.getDelegate()
if err != nil {
return err

View File

@@ -1,5 +1,5 @@
/*
Copyright 2018 the Velero contributors.
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.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package clientmgmt
package v1
import (
"testing"
@@ -28,7 +28,7 @@ import (
"github.com/vmware-tanzu/velero/internal/restartabletest"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
providermocks "github.com/vmware-tanzu/velero/pkg/plugin/velero/mocks"
providermocks "github.com/vmware-tanzu/velero/pkg/plugin/velero/mocks/volumesnapshotter/v1"
)
func TestRestartableGetVolumeSnapshotter(t *testing.T) {
@@ -64,9 +64,9 @@ func TestRestartableGetVolumeSnapshotter(t *testing.T) {
key := process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name}
p.On("GetByKindAndName", key).Return(tc.plugin, tc.getError)
r := &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: p,
r := &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: p,
}
a, err := r.getVolumeSnapshotter()
if tc.expectedError != "" {
@@ -87,9 +87,9 @@ func TestRestartableVolumeSnapshotterReinitialize(t *testing.T) {
name := "aws"
key := process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name}
r := &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: p,
r := &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: p,
config: map[string]string{
"color": "blue",
},
@@ -120,9 +120,9 @@ func TestRestartableVolumeSnapshotterGetDelegate(t *testing.T) {
p.On("ResetIfNeeded").Return(errors.Errorf("reset error")).Once()
name := "aws"
key := process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name}
r := &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: p,
r := &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: p,
}
a, err := r.getDelegate()
assert.Nil(t, a)
@@ -148,9 +148,9 @@ func TestRestartableVolumeSnapshotterInit(t *testing.T) {
// getVolumeSnapshottererror
name := "aws"
key := process.KindAndName{Kind: common.PluginKindVolumeSnapshotter, Name: name}
r := &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: p,
r := &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: p,
}
p.On("GetByKindAndName", key).Return(nil, errors.Errorf("GetByKindAndName error")).Once()
@@ -201,9 +201,9 @@ func TestRestartableVolumeSnapshotterDelegatedFunctions(t *testing.T) {
t,
common.PluginKindVolumeSnapshotter,
func(key process.KindAndName, p process.RestartableProcess) interface{} {
return &restartableVolumeSnapshotter{
key: key,
sharedPluginProcess: p,
return &RestartableVolumeSnapshotter{
Key: key,
SharedPluginProcess: p,
}
},
func() restartabletest.Mockable {

View File

@@ -1,5 +1,5 @@
/*
Copyright 2017, 2019 the Velero contributors.
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.
@@ -25,7 +25,7 @@ import (
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
proto "github.com/vmware-tanzu/velero/pkg/plugin/generated"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
)
// VolumeSnapshotterGRPCServer implements the proto-generated VolumeSnapshotterServer interface, and accepts
@@ -34,13 +34,13 @@ type VolumeSnapshotterGRPCServer struct {
mux *common.ServerMux
}
func (s *VolumeSnapshotterGRPCServer) getImpl(name string) (velero.VolumeSnapshotter, error) {
func (s *VolumeSnapshotterGRPCServer) getImpl(name string) (vsv1.VolumeSnapshotter, error) {
impl, err := s.mux.GetHandler(name)
if err != nil {
return nil, err
}
volumeSnapshotter, ok := impl.(velero.VolumeSnapshotter)
volumeSnapshotter, ok := impl.(vsv1.VolumeSnapshotter)
if !ok {
return nil, errors.Errorf("%T is not a volume snapshotter", impl)
}

View File

@@ -11,6 +11,8 @@ import (
v1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v1"
velero "github.com/vmware-tanzu/velero/pkg/plugin/velero"
volumesnapshotterv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
)
// Manager is an autogenerated mock type for the Manager type
@@ -231,15 +233,15 @@ func (_m *Manager) GetRestoreItemActions() ([]restoreitemactionv1.RestoreItemAct
}
// GetVolumeSnapshotter provides a mock function with given fields: name
func (_m *Manager) GetVolumeSnapshotter(name string) (velero.VolumeSnapshotter, error) {
func (_m *Manager) GetVolumeSnapshotter(name string) (volumesnapshotterv1.VolumeSnapshotter, error) {
ret := _m.Called(name)
var r0 velero.VolumeSnapshotter
if rf, ok := ret.Get(0).(func(string) velero.VolumeSnapshotter); ok {
var r0 volumesnapshotterv1.VolumeSnapshotter
if rf, ok := ret.Get(0).(func(string) volumesnapshotterv1.VolumeSnapshotter); ok {
r0 = rf(name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(velero.VolumeSnapshotter)
r0 = ret.Get(0).(volumesnapshotterv1.VolumeSnapshotter)
}
}

View File

@@ -1,5 +1,5 @@
/*
Copyright 2017, 2019 the Velero contributors.
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.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package velero
package v1
import (
"k8s.io/apimachinery/pkg/runtime"