Pass Velero server command args to the plugins

Pass Velero server command args to the plugins

Fixes #7806

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
This commit is contained in:
Wenkai Yin(尹文开)
2024-07-23 14:54:41 +08:00
parent 981f30cb25
commit dc6eeafe98
22 changed files with 500 additions and 483 deletions

View File

@@ -25,7 +25,6 @@ import (
hcplugin "github.com/hashicorp/go-plugin"
"github.com/sirupsen/logrus"
"github.com/vmware-tanzu/velero/pkg/features"
"github.com/vmware-tanzu/velero/pkg/plugin/framework"
biav2 "github.com/vmware-tanzu/velero/pkg/plugin/framework/backupitemaction/v2"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
@@ -53,11 +52,8 @@ func newClientBuilder(command string, logger logrus.FieldLogger, logLevel logrus
// For plugins compiled into the velero executable, we need to run "velero run-plugins"
b.commandArgs = []string{"run-plugins"}
}
b.commandArgs = append(b.commandArgs, "--log-level", logLevel.String())
if len(features.All()) > 0 {
b.commandArgs = append(b.commandArgs, "--features", features.Serialize())
}
// exclude "velero" and "server" from "velero server --flags ..."
b.commandArgs = append(b.commandArgs, os.Args[2:]...)
return b
}

View File

@@ -25,7 +25,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vmware-tanzu/velero/pkg/features"
"github.com/vmware-tanzu/velero/pkg/plugin/framework"
biav2 "github.com/vmware-tanzu/velero/pkg/plugin/framework/backupitemaction/v2"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
@@ -39,19 +38,11 @@ func TestNewClientBuilder(t *testing.T) {
logLevel := logrus.InfoLevel
cb := newClientBuilder("velero", logger, logLevel)
assert.Equal(t, "velero", cb.commandName)
assert.Equal(t, []string{"--log-level", "info"}, cb.commandArgs)
assert.Equal(t, newLogrusAdapter(logger, logLevel), cb.pluginLogger)
cb = newClientBuilder(os.Args[0], logger, logLevel)
assert.Equal(t, cb.commandName, os.Args[0])
assert.Equal(t, []string{"run-plugins", "--log-level", "info"}, cb.commandArgs)
assert.Equal(t, newLogrusAdapter(logger, logLevel), cb.pluginLogger)
features.NewFeatureFlagSet("feature1", "feature2")
cb = newClientBuilder(os.Args[0], logger, logLevel)
assert.Equal(t, []string{"run-plugins", "--log-level", "info", "--features", "feature1,feature2"}, cb.commandArgs)
// Clear the features list in case other tests run in the same process.
features.NewFeatureFlagSet()
}
func TestClientConfig(t *testing.T) {

View File

@@ -17,8 +17,6 @@ limitations under the License.
package process
import (
"strings"
plugin "github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -61,32 +59,7 @@ func newProcess(command string, logger logrus.FieldLogger, logLevel logrus.Level
// This launches the plugin process.
protocolClient, err := client.Client()
if err != nil {
if !strings.Contains(err.Error(), "unknown flag: --features") {
return nil, err
}
// Velero started passing the --features flag to plugins in v1.2, however existing plugins
// may not support that flag and may not silently ignore unknown flags. The plugin server
// code that we make available to plugin authors has since been updated to ignore unknown
// flags, but to avoid breaking plugins that haven't updated to that code and don't support
// the --features flag, we specifically handle not passing the flag if we can detect that
// it's not supported.
logger.Debug("Plugin process does not support the --features flag, removing it and trying again")
builder.commandArgs = removeFeaturesFlag(builder.commandArgs)
logger.Debugf("Updated command args after removing --features flag: %v", builder.commandArgs)
// re-get the client and protocol client now that --features has been removed
// from the command args.
client = builder.client()
protocolClient, err = client.Client()
if err != nil {
return nil, err
}
logger.Debug("Plugin process successfully started without the --features flag")
return nil, err
}
p := &process{

View File

@@ -17,7 +17,6 @@ limitations under the License.
package framework
import (
"fmt"
"os"
"strings"
@@ -25,11 +24,11 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/vmware-tanzu/velero/pkg/cmd/server/config"
biav2 "github.com/vmware-tanzu/velero/pkg/plugin/framework/backupitemaction/v2"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
ibav1 "github.com/vmware-tanzu/velero/pkg/plugin/framework/itemblockaction/v1"
riav2 "github.com/vmware-tanzu/velero/pkg/plugin/framework/restoreitemaction/v2"
"github.com/vmware-tanzu/velero/pkg/util/logging"
)
// Server serves registered plugin implementations.
@@ -42,6 +41,9 @@ type Server interface {
// This method must be called prior to calling .Serve().
BindFlags(flags *pflag.FlagSet) Server
// GetConfig return the config parsed from the flags
GetConfig() *config.Config
// RegisterBackupItemAction registers a backup item action. Accepted format
// for the plugin name is <DNS subdomain>/<non-empty name>.
RegisterBackupItemAction(pluginName string, initializer common.HandlerInitializer) Server
@@ -104,8 +106,8 @@ type Server interface {
// server implements Server.
type server struct {
config *config.Config
log *logrus.Logger
logLevelFlag *logging.LevelFlag
flagSet *pflag.FlagSet
backupItemAction *BackupItemActionPlugin
backupItemActionV2 *biav2.BackupItemActionPlugin
@@ -122,8 +124,8 @@ func NewServer() Server {
log := newLogger()
return &server{
config: config.GetDefaultConfig(),
log: log,
logLevelFlag: logging.LogLevelFlag(log.Level),
backupItemAction: NewBackupItemActionPlugin(common.ServerLogger(log)),
backupItemActionV2: biav2.NewBackupItemActionPlugin(common.ServerLogger(log)),
volumeSnapshotter: NewVolumeSnapshotterPlugin(common.ServerLogger(log)),
@@ -136,13 +138,16 @@ func NewServer() Server {
}
func (s *server) BindFlags(flags *pflag.FlagSet) Server {
flags.Var(s.logLevelFlag, "log-level", fmt.Sprintf("The level at which to log. Valid values are %s.", strings.Join(s.logLevelFlag.AllowedValues(), ", ")))
s.flagSet = flags
s.config.BindFlags(flags)
s.flagSet.ParseErrorsWhitelist.UnknownFlags = true // Velero.io word list : ignore
return s
}
func (s *server) GetConfig() *config.Config {
return s.config
}
func (s *server) RegisterBackupItemAction(name string, initializer common.HandlerInitializer) Server {
s.backupItemAction.Register(name, initializer)
return s
@@ -260,7 +265,7 @@ func (s *server) Serve() {
}
}
s.log.Level = s.logLevelFlag.Parse()
s.log.Level = s.config.LogLevel.Parse()
s.log.Debugf("Setting log level to %s", strings.ToUpper(s.log.Level.String()))
command := os.Args[0]