forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
83 lines (67 loc) · 2.17 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package agent
import (
"fmt"
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
"github.com/hashicorp/nomad/helper/pluginutils/loader"
"github.com/hashicorp/nomad/helper/pluginutils/singleton"
)
// setupPlugins is used to setup the plugin loaders.
func (a *Agent) setupPlugins() error {
// Get our internal plugins
internal, err := a.internalPluginConfigs()
if err != nil {
return err
}
// Build the plugin loader
config := &loader.PluginLoaderConfig{
Logger: a.logger,
PluginDir: a.config.PluginDir,
Configs: a.config.Plugins,
InternalPlugins: internal,
SupportedVersions: loader.AgentSupportedApiVersions,
}
l, err := loader.NewPluginLoader(config)
if err != nil {
return fmt.Errorf("failed to create plugin loader: %v", err)
}
a.pluginLoader = l
// Wrap the loader to get our singleton loader
a.pluginSingletonLoader = singleton.NewSingletonLoader(a.logger, l)
for k, plugins := range a.pluginLoader.Catalog() {
for _, p := range plugins {
a.logger.Info("detected plugin", "name", p.Name, "type", k, "plugin_version", p.PluginVersion)
}
}
return nil
}
func (a *Agent) internalPluginConfigs() (map[loader.PluginID]*loader.InternalPluginConfig, error) {
// Get the registered plugins
catalog := catalog.Catalog()
// Create our map of plugins
internal := make(map[loader.PluginID]*loader.InternalPluginConfig, len(catalog))
// Grab the client options map if we can
var options map[string]string
if a.config != nil && a.config.Client != nil {
options = a.config.Client.Options
}
for id, reg := range catalog {
if reg.Config == nil {
a.logger.Error("skipping loading internal plugin because it is missing its configuration", "plugin", id)
continue
}
pluginConfig := reg.Config.Config
if reg.ConfigLoader != nil {
pc, err := reg.ConfigLoader(options)
if err != nil {
return nil, fmt.Errorf("failed to retrieve config for internal plugin %v: %v", id, err)
}
pluginConfig = pc
// TODO We should log the config to warn users about upgrade pathing
}
internal[id] = &loader.InternalPluginConfig{
Factory: reg.Config.Factory,
Config: pluginConfig,
}
}
return internal, nil
}