diff --git a/plugins/base/base.go b/plugins/base/base.go index 7a9f7c5f6..f0939da79 100644 --- a/plugins/base/base.go +++ b/plugins/base/base.go @@ -15,7 +15,7 @@ type BasePlugin interface { // SetConfig is used to set the configuration by passing a MessagePack // encoding of it. - SetConfig(data []byte, config *ClientAgentConfig) error + SetConfig(c *Config) error } // PluginInfoResponse returns basic information about the plugin such that Nomad @@ -24,9 +24,9 @@ type PluginInfoResponse struct { // Type returns the plugins type Type string - // PluginApiVersion returns the version of the Nomad plugin API it is built - // against. - PluginApiVersion string + // PluginApiVersions returns the versions of the Nomad plugin API that the + // plugin supports. + PluginApiVersions []string // PluginVersion is the version of the plugin. PluginVersion string @@ -35,8 +35,21 @@ type PluginInfoResponse struct { Name string } -// ClientAgentConfig is the nomad client configuration sent to all plugins -type ClientAgentConfig struct { +// Config contains the configuration for the plugin. +type Config struct { + // ApiVersion is the negotiated plugin API version to use. + ApiVersion string + + // PluginConfig is the MessagePack encoding of the plugins user + // configuration. + PluginConfig []byte + + // AgentConfig is the Nomad agents configuration as applicable to plugins + AgentConfig *AgentConfig +} + +// AgentConfig is the Nomad agent's configuration sent to all plugins +type AgentConfig struct { Driver *ClientDriverConfig } @@ -51,7 +64,7 @@ type ClientDriverConfig struct { ClientMinPort uint } -func (c *ClientAgentConfig) toProto() *proto.NomadConfig { +func (c *AgentConfig) toProto() *proto.NomadConfig { if c == nil { return nil } @@ -68,12 +81,12 @@ func (c *ClientAgentConfig) toProto() *proto.NomadConfig { return cfg } -func nomadConfigFromProto(pb *proto.NomadConfig) *ClientAgentConfig { +func nomadConfigFromProto(pb *proto.NomadConfig) *AgentConfig { if pb == nil { return nil } - cfg := &ClientAgentConfig{} + cfg := &AgentConfig{} if pb.Driver != nil { cfg.Driver = &ClientDriverConfig{ ClientMaxPort: uint(pb.Driver.ClientMaxPort), diff --git a/plugins/base/client.go b/plugins/base/client.go index 6baf9a07d..80bc7ef4b 100644 --- a/plugins/base/client.go +++ b/plugins/base/client.go @@ -34,10 +34,10 @@ func (b *BasePluginClient) PluginInfo() (*PluginInfoResponse, error) { } resp := &PluginInfoResponse{ - Type: ptype, - PluginApiVersion: presp.GetPluginApiVersion(), - PluginVersion: presp.GetPluginVersion(), - Name: presp.GetName(), + Type: ptype, + PluginApiVersions: presp.GetPluginApiVersions(), + PluginVersion: presp.GetPluginVersion(), + Name: presp.GetName(), } return resp, nil @@ -52,11 +52,12 @@ func (b *BasePluginClient) ConfigSchema() (*hclspec.Spec, error) { return presp.GetSpec(), nil } -func (b *BasePluginClient) SetConfig(data []byte, config *ClientAgentConfig) error { +func (b *BasePluginClient) SetConfig(c *Config) error { // Send the config _, err := b.Client.SetConfig(b.DoneCtx, &proto.SetConfigRequest{ - MsgpackConfig: data, - NomadConfig: config.toProto(), + MsgpackConfig: c.PluginConfig, + NomadConfig: c.AgentConfig.toProto(), + PluginApiVersion: c.ApiVersion, }) return err diff --git a/plugins/base/plugin_test.go b/plugins/base/plugin_test.go index b64ba0edf..19172f009 100644 --- a/plugins/base/plugin_test.go +++ b/plugins/base/plugin_test.go @@ -16,27 +16,30 @@ func TestBasePlugin_PluginInfo_GRPC(t *testing.T) { t.Parallel() require := require.New(t) + var ( + apiVersions = []string{"v0.1.0", "v0.1.1"} + ) + const ( - apiVersion = "v0.1.0" pluginVersion = "v0.2.1" pluginName = "mock" ) knownType := func() (*PluginInfoResponse, error) { info := &PluginInfoResponse{ - Type: PluginTypeDriver, - PluginApiVersion: apiVersion, - PluginVersion: pluginVersion, - Name: pluginName, + Type: PluginTypeDriver, + PluginApiVersions: apiVersions, + PluginVersion: pluginVersion, + Name: pluginName, } return info, nil } unknownType := func() (*PluginInfoResponse, error) { info := &PluginInfoResponse{ - Type: "bad", - PluginApiVersion: apiVersion, - PluginVersion: pluginVersion, - Name: pluginName, + Type: "bad", + PluginApiVersions: apiVersions, + PluginVersion: pluginVersion, + Name: pluginName, } return info, nil } @@ -63,7 +66,7 @@ func TestBasePlugin_PluginInfo_GRPC(t *testing.T) { resp, err := impl.PluginInfo() require.NoError(err) - require.Equal(apiVersion, resp.PluginApiVersion) + require.Equal(apiVersions, resp.PluginApiVersions) require.Equal(pluginVersion, resp.PluginVersion) require.Equal(pluginName, resp.Name) require.Equal(PluginTypeDriver, resp.Type) @@ -118,8 +121,8 @@ func TestBasePlugin_SetConfig(t *testing.T) { ConfigSchemaF: func() (*hclspec.Spec, error) { return TestSpec, nil }, - SetConfigF: func(data []byte, cfg *ClientAgentConfig) error { - receivedData = data + SetConfigF: func(cfg *Config) error { + receivedData = cfg.PluginConfig return nil }, } @@ -147,7 +150,7 @@ func TestBasePlugin_SetConfig(t *testing.T) { }) cdata, err := msgpack.Marshal(config, config.Type()) require.NoError(err) - require.NoError(impl.SetConfig(cdata, &ClientAgentConfig{})) + require.NoError(impl.SetConfig(&Config{PluginConfig: cdata})) require.Equal(cdata, receivedData) // Decode the value back diff --git a/plugins/base/server.go b/plugins/base/server.go index f7ccc3fb7..af01faee4 100644 --- a/plugins/base/server.go +++ b/plugins/base/server.go @@ -31,10 +31,10 @@ func (b *basePluginServer) PluginInfo(context.Context, *proto.PluginInfoRequest) } presp := &proto.PluginInfoResponse{ - Type: ptype, - PluginApiVersion: resp.PluginApiVersion, - PluginVersion: resp.PluginVersion, - Name: resp.Name, + Type: ptype, + PluginApiVersions: resp.PluginApiVersions, + PluginVersion: resp.PluginVersion, + Name: resp.Name, } return presp, nil @@ -61,7 +61,7 @@ func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRe // Client configuration is filtered based on plugin type cfg := nomadConfigFromProto(req.GetNomadConfig()) - filteredCfg := new(ClientAgentConfig) + filteredCfg := new(AgentConfig) if cfg != nil { switch info.Type { @@ -70,8 +70,15 @@ func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRe } } + // Build the config request + c := &Config{ + ApiVersion: req.GetPluginApiVersion(), + PluginConfig: req.GetMsgpackConfig(), + AgentConfig: filteredCfg, + } + // Set the config - if err := b.impl.SetConfig(req.GetMsgpackConfig(), filteredCfg); err != nil { + if err := b.impl.SetConfig(c); err != nil { return nil, fmt.Errorf("SetConfig failed: %v", err) } diff --git a/plugins/base/testing.go b/plugins/base/testing.go index bf72fe1f9..f3107a55e 100644 --- a/plugins/base/testing.go +++ b/plugins/base/testing.go @@ -48,7 +48,7 @@ type TestConfig struct { type PluginInfoFn func() (*PluginInfoResponse, error) type ConfigSchemaFn func() (*hclspec.Spec, error) -type SetConfigFn func([]byte, *ClientAgentConfig) error +type SetConfigFn func(*Config) error // MockPlugin is used for testing. // Each function can be set as a closure to make assertions about how data @@ -61,8 +61,8 @@ type MockPlugin struct { func (p *MockPlugin) PluginInfo() (*PluginInfoResponse, error) { return p.PluginInfoF() } func (p *MockPlugin) ConfigSchema() (*hclspec.Spec, error) { return p.ConfigSchemaF() } -func (p *MockPlugin) SetConfig(data []byte, cfg *ClientAgentConfig) error { - return p.SetConfigF(data, cfg) +func (p *MockPlugin) SetConfig(cfg *Config) error { + return p.SetConfigF(cfg) } // Below are static implementations of the base plugin functions @@ -89,5 +89,5 @@ func TestConfigSchema() ConfigSchemaFn { // NoopSetConfig is a noop implementation of set config func NoopSetConfig() SetConfigFn { - return func(_ []byte, _ *ClientAgentConfig) error { return nil } + return func(_ *Config) error { return nil } }