[core] Do not start the plugin loader on non-clients (#16111)

The plugin loader loads task and device driver plugins which are not
used on server nodes.
This commit is contained in:
Charlie Voiselle 2023-02-10 15:33:16 -05:00 committed by GitHub
parent 6955d66891
commit 65ce3ec8de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 22 deletions

3
.changelog/16111.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
core: Non-client nodes will now skip loading plugins
```

View File

@ -142,10 +142,6 @@ func NewAgent(config *Config, logger log.InterceptLogger, logOutput io.Writer, i
return nil, fmt.Errorf("Failed to initialize Consul client: %v", err)
}
if err := a.setupPlugins(); err != nil {
return nil, err
}
if err := a.setupServer(); err != nil {
return nil, err
}
@ -568,10 +564,6 @@ func (a *Agent) finalizeServerConfig(c *nomad.Config) {
// Setup the logging
c.Logger = a.logger
c.LogOutput = a.logOutput
// Setup the plugin loaders
c.PluginLoader = a.pluginLoader
c.PluginSingletonLoader = a.pluginSingletonLoader
c.AgentShutdown = func() error { return a.Shutdown() }
}
@ -1001,6 +993,14 @@ func (a *Agent) setupClient() error {
if !a.config.Client.Enabled {
return nil
}
// Plugin setup must happen before the call to clientConfig, because it
// copies the pointers to the plugin loaders from the Agent to the
// Client config.
if err := a.setupPlugins(); err != nil {
return err
}
// Setup the configuration
conf, err := a.clientConfig()
if err != nil {

View File

@ -0,0 +1,33 @@
package agent
import (
"testing"
"github.com/hashicorp/nomad/api"
"github.com/shoenig/test/must"
)
func TestPlugins_WhenNotClientSkip(t *testing.T) {
s, _, _ := testServer(t, false, nil)
must.Nil(t, s.Agent.pluginSingletonLoader)
}
func TestPlugins_WhenClientRun(t *testing.T) {
s, _, _ := testServer(t, true, nil)
must.NotNil(t, s.Agent.pluginSingletonLoader)
}
func testServer(t *testing.T, runClient bool, cb func(*Config)) (*TestAgent, *api.Client, string) {
// Make a new test server
a := NewTestAgent(t, t.Name(), func(config *Config) {
config.Client.Enabled = runClient
if cb != nil {
cb(config)
}
})
t.Cleanup(a.Shutdown)
c := a.Client()
return a, c, a.HTTPAddr()
}

View File

@ -11,7 +11,6 @@ import (
"golang.org/x/exp/slices"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/nomad/helper/pluginutils/loader"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/deploymentwatcher"
@ -373,13 +372,6 @@ type Config struct {
// and this value is ignored.
DefaultSchedulerConfig structs.SchedulerConfiguration `hcl:"default_scheduler_config"`
// PluginLoader is used to load plugins.
PluginLoader loader.PluginCatalog
// PluginSingletonLoader is a plugin loader that will returns singleton
// instances of the plugins.
PluginSingletonLoader loader.PluginCatalog
// RPCHandshakeTimeout is the deadline by which RPC handshakes must
// complete. The RPC handshake includes the first byte read as well as
// the TLS handshake and subsequent byte read if TLS is enabled.

View File

@ -10,8 +10,6 @@ import (
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
"github.com/hashicorp/nomad/helper/pluginutils/singleton"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
@ -85,10 +83,6 @@ func TestServerErr(t *testing.T, cb func(*Config)) (*Server, func(), error) {
config.ServerHealthInterval = 50 * time.Millisecond
config.AutopilotInterval = 100 * time.Millisecond
// Set the plugin loaders
config.PluginLoader = catalog.TestPluginLoader(t)
config.PluginSingletonLoader = singleton.NewSingletonLoader(config.Logger, config.PluginLoader)
// Disable consul autojoining: tests typically join servers directly
config.ConsulConfig.ServerAutoJoin = &f