mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-08 22:23:15 +00:00
plugin/framework refactoring for BackupItemAction v1
Refactors the framework package to implement the plugin versioning changes needed for BIA v1 and overall package refactoring to support plugin versions in different packages. This should be all that's needed to move on to v2 for BackupItemAction. The remaining plugin types still need similar refactoring to what's being done here for BIA before attempting a v2 implementation. Signed-off-by: Scott Seago <sseago@redhat.com>
This commit is contained in:
75
pkg/plugin/framework/common/client_dispenser.go
Normal file
75
pkg/plugin/framework/common/client_dispenser.go
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// ClientBase implements client and contains shared fields common to all clients.
|
||||
type ClientBase struct {
|
||||
Plugin string
|
||||
Logger logrus.FieldLogger
|
||||
}
|
||||
|
||||
type ClientDispenser interface {
|
||||
ClientFor(name string) interface{}
|
||||
}
|
||||
|
||||
// clientDispenser supports the initialization and retrieval of multiple implementations for a single plugin kind, such as
|
||||
// "aws" and "azure" implementations of the object store plugin.
|
||||
type clientDispenser struct {
|
||||
// logger is the log the plugin should use.
|
||||
logger logrus.FieldLogger
|
||||
// clienConn is shared among all implementations for this client.
|
||||
clientConn *grpc.ClientConn
|
||||
// initFunc returns a client that implements a plugin interface, such as ObjectStore.
|
||||
initFunc clientInitFunc
|
||||
// clients keeps track of all the initialized implementations.
|
||||
clients map[string]interface{}
|
||||
}
|
||||
|
||||
type clientInitFunc func(base *ClientBase, clientConn *grpc.ClientConn) interface{}
|
||||
|
||||
// newClientDispenser creates a new clientDispenser.
|
||||
func NewClientDispenser(logger logrus.FieldLogger, clientConn *grpc.ClientConn, initFunc clientInitFunc) *clientDispenser {
|
||||
return &clientDispenser{
|
||||
clientConn: clientConn,
|
||||
logger: logger,
|
||||
initFunc: initFunc,
|
||||
clients: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
// ClientFor returns a gRPC client stub for the implementation of a plugin named name. If the client stub does not
|
||||
// currently exist, clientFor creates it.
|
||||
func (cd *clientDispenser) ClientFor(name string) interface{} {
|
||||
if client, found := cd.clients[name]; found {
|
||||
return client
|
||||
}
|
||||
|
||||
base := &ClientBase{
|
||||
Plugin: name,
|
||||
Logger: cd.logger,
|
||||
}
|
||||
// Initialize the plugin (e.g. newBackupItemActionGRPCClient())
|
||||
client := cd.initFunc(base, cd.clientConn)
|
||||
cd.clients[name] = client
|
||||
|
||||
return client
|
||||
}
|
||||
81
pkg/plugin/framework/common/client_dispenser_test.go
Normal file
81
pkg/plugin/framework/common/client_dispenser_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
type fakeClient struct {
|
||||
base *ClientBase
|
||||
clientConn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func TestNewClientDispenser(t *testing.T) {
|
||||
logger := test.NewLogger()
|
||||
|
||||
clientConn := new(grpc.ClientConn)
|
||||
|
||||
c := 3
|
||||
initFunc := func(base *ClientBase, clientConn *grpc.ClientConn) interface{} {
|
||||
return c
|
||||
}
|
||||
|
||||
cd := NewClientDispenser(logger, clientConn, initFunc)
|
||||
assert.Equal(t, clientConn, cd.clientConn)
|
||||
assert.NotNil(t, cd.clients)
|
||||
assert.Empty(t, cd.clients)
|
||||
}
|
||||
|
||||
func TestClientFor(t *testing.T) {
|
||||
logger := test.NewLogger()
|
||||
clientConn := new(grpc.ClientConn)
|
||||
|
||||
c := new(fakeClient)
|
||||
count := 0
|
||||
initFunc := func(base *ClientBase, clientConn *grpc.ClientConn) interface{} {
|
||||
c.base = base
|
||||
c.clientConn = clientConn
|
||||
count++
|
||||
return c
|
||||
}
|
||||
|
||||
cd := NewClientDispenser(logger, clientConn, initFunc)
|
||||
|
||||
actual := cd.ClientFor("pod")
|
||||
require.IsType(t, &fakeClient{}, actual)
|
||||
typed := actual.(*fakeClient)
|
||||
assert.Equal(t, 1, count)
|
||||
assert.Equal(t, &typed, &c)
|
||||
expectedBase := &ClientBase{
|
||||
Plugin: "pod",
|
||||
Logger: logger,
|
||||
}
|
||||
assert.Equal(t, expectedBase, typed.base)
|
||||
assert.Equal(t, clientConn, typed.clientConn)
|
||||
|
||||
// Make sure we reuse a previous client
|
||||
actual = cd.ClientFor("pod")
|
||||
require.IsType(t, &fakeClient{}, actual)
|
||||
typed = actual.(*fakeClient)
|
||||
assert.Equal(t, 1, count)
|
||||
}
|
||||
79
pkg/plugin/framework/common/client_errors.go
Normal file
79
pkg/plugin/framework/common/client_errors.go
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
Copyright 2019 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 common
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
proto "github.com/vmware-tanzu/velero/pkg/plugin/generated"
|
||||
)
|
||||
|
||||
// FromGRPCError takes a gRPC status error, extracts a stack trace
|
||||
// from the details if it exists, and returns an error that can
|
||||
// provide information about where it was created.
|
||||
//
|
||||
// This function should be used in the internal plugin client code to convert
|
||||
// all errors returned from the plugin server before they're passed back to
|
||||
// the rest of the Velero codebase. This will enable them to display location
|
||||
// information when they're logged.
|
||||
func FromGRPCError(err error) error {
|
||||
statusErr, ok := status.FromError(err)
|
||||
if !ok {
|
||||
return statusErr.Err()
|
||||
}
|
||||
|
||||
for _, detail := range statusErr.Details() {
|
||||
switch t := detail.(type) {
|
||||
case *proto.Stack:
|
||||
return &ProtoStackError{
|
||||
error: err,
|
||||
stack: t,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type ProtoStackError struct {
|
||||
error
|
||||
stack *proto.Stack
|
||||
}
|
||||
|
||||
func (e *ProtoStackError) File() string {
|
||||
if e.stack == nil || len(e.stack.Frames) < 1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return e.stack.Frames[0].File
|
||||
}
|
||||
|
||||
func (e *ProtoStackError) Line() int32 {
|
||||
if e.stack == nil || len(e.stack.Frames) < 1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return e.stack.Frames[0].Line
|
||||
}
|
||||
|
||||
func (e *ProtoStackError) Function() string {
|
||||
if e.stack == nil || len(e.stack.Frames) < 1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return e.stack.Frames[0].Function
|
||||
}
|
||||
49
pkg/plugin/framework/common/handle_panic.go
Normal file
49
pkg/plugin/framework/common/handle_panic.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2019 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 common
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// HandlePanic is a panic handler for the server half of velero plugins.
|
||||
func HandlePanic(p interface{}) error {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If p is an error with a stack trace, we want to retain
|
||||
// it to preserve the stack trace. Otherwise, create a new
|
||||
// error here.
|
||||
var err error
|
||||
|
||||
if panicErr, ok := p.(error); !ok {
|
||||
err = errors.Errorf("plugin panicked: %v", p)
|
||||
} else {
|
||||
if _, ok := panicErr.(StackTracer); ok {
|
||||
err = panicErr
|
||||
} else {
|
||||
errWithStacktrace := errors.Errorf("%v, stack trace: %s", panicErr, debug.Stack())
|
||||
err = errors.Wrap(errWithStacktrace, "plugin panicked")
|
||||
}
|
||||
}
|
||||
|
||||
return NewGRPCErrorWithCode(err, codes.Aborted)
|
||||
}
|
||||
48
pkg/plugin/framework/common/plugin_base.go
Normal file
48
pkg/plugin/framework/common/plugin_base.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type PluginBase struct {
|
||||
ClientLogger logrus.FieldLogger
|
||||
*ServerMux
|
||||
}
|
||||
|
||||
func NewPluginBase(options ...PluginOption) *PluginBase {
|
||||
base := new(PluginBase)
|
||||
for _, option := range options {
|
||||
option(base)
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
type PluginOption func(base *PluginBase)
|
||||
|
||||
func ClientLogger(logger logrus.FieldLogger) PluginOption {
|
||||
return func(base *PluginBase) {
|
||||
base.ClientLogger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func ServerLogger(logger logrus.FieldLogger) PluginOption {
|
||||
return func(base *PluginBase) {
|
||||
base.ServerMux = NewServerMux(logger)
|
||||
}
|
||||
}
|
||||
40
pkg/plugin/framework/common/plugin_base_test.go
Normal file
40
pkg/plugin/framework/common/plugin_base_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
func TestClientLogger(t *testing.T) {
|
||||
base := &PluginBase{}
|
||||
logger := test.NewLogger()
|
||||
f := ClientLogger(logger)
|
||||
f(base)
|
||||
assert.Equal(t, logger, base.ClientLogger)
|
||||
}
|
||||
|
||||
func TestServerLogger(t *testing.T) {
|
||||
base := &PluginBase{}
|
||||
logger := test.NewLogger()
|
||||
f := ServerLogger(logger)
|
||||
f(base)
|
||||
assert.Equal(t, NewServerMux(logger), base.ServerMux)
|
||||
}
|
||||
67
pkg/plugin/framework/common/plugin_kinds.go
Normal file
67
pkg/plugin/framework/common/plugin_kinds.go
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
// PluginKind is a type alias for a string that describes
|
||||
// the kind of a Velero-supported plugin.
|
||||
type PluginKind string
|
||||
|
||||
// String returns the string for k.
|
||||
func (k PluginKind) String() string {
|
||||
return string(k)
|
||||
}
|
||||
|
||||
const (
|
||||
// PluginKindObjectStore represents an object store plugin.
|
||||
PluginKindObjectStore PluginKind = "ObjectStore"
|
||||
|
||||
// PluginKindVolumeSnapshotter represents a volume snapshotter plugin.
|
||||
PluginKindVolumeSnapshotter PluginKind = "VolumeSnapshotter"
|
||||
|
||||
// PluginKindBackupItemAction represents a backup item action plugin.
|
||||
PluginKindBackupItemAction PluginKind = "BackupItemAction"
|
||||
|
||||
// PluginKindRestoreItemAction represents a restore item action plugin.
|
||||
PluginKindRestoreItemAction PluginKind = "RestoreItemAction"
|
||||
|
||||
// PluginKindDeleteItemAction represents a delete item action plugin.
|
||||
PluginKindDeleteItemAction PluginKind = "DeleteItemAction"
|
||||
|
||||
// PluginKindItemSnapshotter represents an item snapshotter plugin
|
||||
PluginKindItemSnapshotter PluginKind = "ItemSnapshotter"
|
||||
|
||||
// PluginKindPluginLister represents a plugin lister plugin.
|
||||
PluginKindPluginLister PluginKind = "PluginLister"
|
||||
)
|
||||
|
||||
// If there are plugin kinds that are adaptable to newer API versions, list them here.
|
||||
// The older (adaptable) version is the key, and the value is the full list of newer
|
||||
// plugin kinds that are capable of adapting it.
|
||||
var PluginKindsAdaptableTo = map[PluginKind][]PluginKind{}
|
||||
|
||||
// AllPluginKinds contains all the valid plugin kinds that Velero supports, excluding PluginLister because that is not a
|
||||
// kind that a developer would ever need to implement (it's handled by Velero and the Velero plugin library code).
|
||||
func AllPluginKinds() map[string]PluginKind {
|
||||
allPluginKinds := make(map[string]PluginKind)
|
||||
allPluginKinds[PluginKindObjectStore.String()] = PluginKindObjectStore
|
||||
allPluginKinds[PluginKindVolumeSnapshotter.String()] = PluginKindVolumeSnapshotter
|
||||
allPluginKinds[PluginKindBackupItemAction.String()] = PluginKindBackupItemAction
|
||||
allPluginKinds[PluginKindRestoreItemAction.String()] = PluginKindRestoreItemAction
|
||||
allPluginKinds[PluginKindDeleteItemAction.String()] = PluginKindDeleteItemAction
|
||||
allPluginKinds[PluginKindItemSnapshotter.String()] = PluginKindItemSnapshotter
|
||||
return allPluginKinds
|
||||
}
|
||||
85
pkg/plugin/framework/common/server_errors.go
Normal file
85
pkg/plugin/framework/common/server_errors.go
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Copyright 2019 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 common
|
||||
|
||||
import (
|
||||
goproto "github.com/golang/protobuf/proto"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
proto "github.com/vmware-tanzu/velero/pkg/plugin/generated"
|
||||
"github.com/vmware-tanzu/velero/pkg/util/logging"
|
||||
)
|
||||
|
||||
// NewGRPCErrorWithCode wraps err in a gRPC status error with the error's stack trace
|
||||
// included in the details if it exists. This provides an easy way to send
|
||||
// stack traces from plugin servers across the wire to the plugin client.
|
||||
//
|
||||
// This function should be used in the internal plugin server code to wrap
|
||||
// all errors before they're returned.
|
||||
func NewGRPCErrorWithCode(err error, code codes.Code, details ...goproto.Message) error {
|
||||
// if it's already a gRPC status error, use it; otherwise, create a new one
|
||||
statusErr, ok := status.FromError(err)
|
||||
if !ok {
|
||||
statusErr = status.New(code, err.Error())
|
||||
}
|
||||
|
||||
// get a Stack for the error and add it to details
|
||||
if stack := ErrorStack(err); stack != nil {
|
||||
details = append(details, stack)
|
||||
}
|
||||
|
||||
statusErr, err = statusErr.WithDetails(details...)
|
||||
if err != nil {
|
||||
return status.Errorf(codes.Unknown, "error adding details to the gRPC error: %v", err)
|
||||
}
|
||||
|
||||
return statusErr.Err()
|
||||
}
|
||||
|
||||
// NewGRPCError is a convenience function for creating a new gRPC error
|
||||
// with code = codes.Unknown
|
||||
func NewGRPCError(err error, details ...goproto.Message) error {
|
||||
return NewGRPCErrorWithCode(err, codes.Unknown, details...)
|
||||
}
|
||||
|
||||
// ErrorStack gets a stack trace, if it exists, from the provided error, and
|
||||
// returns it as a *proto.Stack.
|
||||
func ErrorStack(err error) *proto.Stack {
|
||||
stackTracer, ok := err.(StackTracer)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
stackTrace := new(proto.Stack)
|
||||
for _, frame := range stackTracer.StackTrace() {
|
||||
location := logging.GetFrameLocationInfo(frame)
|
||||
|
||||
stackTrace.Frames = append(stackTrace.Frames, &proto.StackFrame{
|
||||
File: location.File,
|
||||
Line: int32(location.Line),
|
||||
Function: location.Function,
|
||||
})
|
||||
}
|
||||
|
||||
return stackTrace
|
||||
}
|
||||
|
||||
type StackTracer interface {
|
||||
StackTrace() errors.StackTrace
|
||||
}
|
||||
115
pkg/plugin/framework/common/server_mux.go
Normal file
115
pkg/plugin/framework/common/server_mux.go
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
Copyright 2018, 2019 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 common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
)
|
||||
|
||||
// HandlerInitializer is a function that initializes and returns a new instance of one of Velero's plugin interfaces
|
||||
// (ObjectStore, VolumeSnapshotter, BackupItemAction, RestoreItemAction).
|
||||
type HandlerInitializer func(logger logrus.FieldLogger) (interface{}, error)
|
||||
|
||||
// ServerMux manages multiple implementations of a single plugin kind, such as pod and pvc BackupItemActions.
|
||||
type ServerMux struct {
|
||||
kind PluginKind
|
||||
initializers map[string]HandlerInitializer
|
||||
Handlers map[string]interface{}
|
||||
ServerLog logrus.FieldLogger
|
||||
}
|
||||
|
||||
// NewServerMux returns a new ServerMux.
|
||||
func NewServerMux(logger logrus.FieldLogger) *ServerMux {
|
||||
return &ServerMux{
|
||||
initializers: make(map[string]HandlerInitializer),
|
||||
Handlers: make(map[string]interface{}),
|
||||
ServerLog: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// register validates the plugin name and registers the
|
||||
// initializer for the given name.
|
||||
func (m *ServerMux) Register(name string, f HandlerInitializer) {
|
||||
if err := ValidatePluginName(name, m.Names()); err != nil {
|
||||
m.ServerLog.Errorf("invalid plugin name %q: %s", name, err)
|
||||
return
|
||||
}
|
||||
m.initializers[name] = f
|
||||
}
|
||||
|
||||
// names returns a list of all registered implementations.
|
||||
func (m *ServerMux) Names() []string {
|
||||
return sets.StringKeySet(m.initializers).List()
|
||||
}
|
||||
|
||||
// GetHandler returns the instance for a plugin with the given name. If an instance has already been initialized,
|
||||
// that is returned. Otherwise, the instance is initialized by calling its initialization function.
|
||||
func (m *ServerMux) GetHandler(name string) (interface{}, error) {
|
||||
if instance, found := m.Handlers[name]; found {
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
initializer, found := m.initializers[name]
|
||||
if !found {
|
||||
return nil, errors.Errorf("%v plugin: %s was not found or has an invalid name format", m.kind, name)
|
||||
}
|
||||
|
||||
instance, err := initializer(m.ServerLog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.Handlers[name] = instance
|
||||
|
||||
return m.Handlers[name], nil
|
||||
}
|
||||
|
||||
// ValidatePluginName checks if the given name:
|
||||
// - the plugin name has two parts separated by '/'
|
||||
// - non of the above parts is empty
|
||||
// - the prefix is a valid DNS subdomain name
|
||||
// - a plugin with the same name does not already exist (if list of existing names is passed in)
|
||||
func ValidatePluginName(name string, existingNames []string) error {
|
||||
// validate there is one "/" and two parts
|
||||
parts := strings.Split(name, "/")
|
||||
if len(parts) != 2 {
|
||||
return errors.Errorf("plugin name must have exactly two parts separated by a `/`. Accepted format: <DNS subdomain>/<non-empty name>. %s is invalid", name)
|
||||
}
|
||||
|
||||
// validate both prefix and name are non-empty
|
||||
if parts[0] == "" || parts[1] == "" {
|
||||
return errors.Errorf("both parts of the plugin name must be non-empty. Accepted format: <DNS subdomain>/<non-empty name>. %s is invalid", name)
|
||||
}
|
||||
|
||||
// validate that the prefix is a DNS subdomain
|
||||
if errs := validation.IsDNS1123Subdomain(parts[0]); len(errs) != 0 {
|
||||
return errors.Errorf("first part of the plugin name must be a valid DNS subdomain. Accepted format: <DNS subdomain>/<non-empty name>. first part %q is invalid: %s", parts[0], strings.Join(errs, "; "))
|
||||
}
|
||||
|
||||
for _, existingName := range existingNames {
|
||||
if strings.Compare(name, existingName) == 0 {
|
||||
return errors.New("plugin name " + existingName + " already exists")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
71
pkg/plugin/framework/common/server_mux_test.go
Normal file
71
pkg/plugin/framework/common/server_mux_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright 2019 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 common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidPluginName(t *testing.T) {
|
||||
successCases := []struct {
|
||||
pluginName string
|
||||
existingNames []string
|
||||
}{
|
||||
{"example.io/azure", []string{"velero.io/aws"}},
|
||||
{"with-dashes/name", []string{"velero.io/aws"}},
|
||||
{"prefix/Uppercase_Is_OK_123", []string{"velero.io/aws"}},
|
||||
{"example-with-dash.io/azure", []string{"velero.io/aws"}},
|
||||
{"1.2.3.4/5678", []string{"velero.io/aws"}},
|
||||
{"example.io/azure", []string{"velero.io/aws"}},
|
||||
{"example.io/azure", []string{""}},
|
||||
{"example.io/azure", nil},
|
||||
{strings.Repeat("a", 253) + "/name", []string{"velero.io/aws"}},
|
||||
}
|
||||
for i, tt := range successCases {
|
||||
t.Run(tt.pluginName, func(t *testing.T) {
|
||||
if err := ValidatePluginName(tt.pluginName, tt.existingNames); err != nil {
|
||||
t.Errorf("case[%d]: %q: expected success: %v", i, successCases[i], err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
errorCases := []struct {
|
||||
pluginName string
|
||||
existingNames []string
|
||||
}{
|
||||
{"", []string{"velero.io/aws"}},
|
||||
{"single", []string{"velero.io/aws"}},
|
||||
{"/", []string{"velero.io/aws"}},
|
||||
{"//", []string{"velero.io/aws"}},
|
||||
{"///", []string{"velero.io/aws"}},
|
||||
{"a/", []string{"velero.io/aws"}},
|
||||
{"/a", []string{"velero.io/aws"}},
|
||||
{"velero.io/aws", []string{"velero.io/aws"}},
|
||||
{"Uppercase_Is_OK_123/name", []string{"velero.io/aws"}},
|
||||
{strings.Repeat("a", 254) + "/name", []string{"velero.io/aws"}},
|
||||
{"ospecialchars%^=@", []string{"velero.io/aws"}},
|
||||
}
|
||||
|
||||
for i, tt := range errorCases {
|
||||
t.Run(tt.pluginName, func(t *testing.T) {
|
||||
if err := ValidatePluginName(tt.pluginName, tt.existingNames); err == nil {
|
||||
t.Errorf("case[%d]: %q: expected failure.", i, errorCases[i])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user