mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-06 13:26:26 +00:00
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>
76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
/*
|
|
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
|
|
}
|