mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
move catalog + grpcutils
This commit is contained in:
committed by
Michael Schurter
parent
7e81a9e3c4
commit
b9f36134dc
53
pluginutils/catalog/catalog.go
Normal file
53
pluginutils/catalog/catalog.go
Normal file
@@ -0,0 +1,53 @@
|
||||
// Package catalog is used to register internal plugins such that they can be
|
||||
// loaded.
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/nomad/plugins/shared/loader"
|
||||
)
|
||||
|
||||
var (
|
||||
// catalog is the set of registered internal plugins
|
||||
catalog = map[loader.PluginID]*Registration{}
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
// Registration is the registration of an internal plugin
|
||||
type Registration struct {
|
||||
Config *loader.InternalPluginConfig
|
||||
ConfigLoader ConfigFromOptions
|
||||
}
|
||||
|
||||
// ConfigFromOptions is used to retrieve a plugin config when passed a node's
|
||||
// option map. This allows upgrade pathing from the old configuration format to
|
||||
// the new config format.
|
||||
type ConfigFromOptions func(options map[string]string) (config map[string]interface{}, err error)
|
||||
|
||||
// Register is used to register an internal plugin.
|
||||
func Register(id loader.PluginID, config *loader.InternalPluginConfig) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
catalog[id] = &Registration{
|
||||
Config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterDeferredConfig is used to register an internal plugin that sets its
|
||||
// config using the client's option map.
|
||||
func RegisterDeferredConfig(id loader.PluginID, config *loader.InternalPluginConfig, configLoader ConfigFromOptions) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
catalog[id] = &Registration{
|
||||
Config: config,
|
||||
ConfigLoader: configLoader,
|
||||
}
|
||||
}
|
||||
|
||||
// Catalog returns the catalog of internal plugins
|
||||
func Catalog() map[loader.PluginID]*Registration {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return catalog
|
||||
}
|
||||
20
pluginutils/catalog/register.go
Normal file
20
pluginutils/catalog/register.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/nomad/drivers/docker"
|
||||
"github.com/hashicorp/nomad/drivers/exec"
|
||||
"github.com/hashicorp/nomad/drivers/java"
|
||||
"github.com/hashicorp/nomad/drivers/qemu"
|
||||
"github.com/hashicorp/nomad/drivers/rawexec"
|
||||
)
|
||||
|
||||
// This file is where all builtin plugins should be registered in the catalog.
|
||||
// Plugins with build restrictions should be placed in the appropriate
|
||||
// register_XXX.go file.
|
||||
func init() {
|
||||
RegisterDeferredConfig(rawexec.PluginID, rawexec.PluginConfig, rawexec.PluginLoader)
|
||||
Register(exec.PluginID, exec.PluginConfig)
|
||||
Register(qemu.PluginID, qemu.PluginConfig)
|
||||
Register(java.PluginID, java.PluginConfig)
|
||||
RegisterDeferredConfig(docker.PluginID, docker.PluginConfig, docker.PluginLoader)
|
||||
}
|
||||
10
pluginutils/catalog/register_linux.go
Normal file
10
pluginutils/catalog/register_linux.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package catalog
|
||||
|
||||
import "github.com/hashicorp/nomad/drivers/rkt"
|
||||
|
||||
// This file is where all builtin plugins should be registered in the catalog.
|
||||
// Plugins with build restrictions should be placed in the appropriate
|
||||
// register_XXX.go file.
|
||||
func init() {
|
||||
RegisterDeferredConfig(rkt.PluginID, rkt.PluginConfig, rkt.PluginLoader)
|
||||
}
|
||||
12
pluginutils/catalog/register_testing.go
Normal file
12
pluginutils/catalog/register_testing.go
Normal file
@@ -0,0 +1,12 @@
|
||||
// +build !release
|
||||
|
||||
package catalog
|
||||
|
||||
import "github.com/hashicorp/nomad/drivers/mock"
|
||||
|
||||
// Register the mock driver with the builtin driver plugin catalog. All builtin
|
||||
// plugins that are intended for production use should be registered in
|
||||
// register.go as this file is not built as part of a release.
|
||||
func init() {
|
||||
Register(mock.PluginID, mock.PluginConfig)
|
||||
}
|
||||
66
pluginutils/catalog/testing.go
Normal file
66
pluginutils/catalog/testing.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/nomad/helper/testlog"
|
||||
"github.com/hashicorp/nomad/nomad/structs/config"
|
||||
"github.com/hashicorp/nomad/plugins/shared/loader"
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
)
|
||||
|
||||
// TestPluginLoader returns a plugin loader populated only with internal plugins
|
||||
func TestPluginLoader(t testing.T) loader.PluginCatalog {
|
||||
return TestPluginLoaderWithOptions(t, "", nil, nil)
|
||||
}
|
||||
|
||||
// TestPluginLoaderWithOptions allows configuring the plugin loader fully.
|
||||
func TestPluginLoaderWithOptions(t testing.T,
|
||||
pluginDir string,
|
||||
options map[string]string,
|
||||
configs []*config.PluginConfig) loader.PluginCatalog {
|
||||
|
||||
// Get a logger
|
||||
logger := testlog.HCLogger(t)
|
||||
|
||||
// Get the registered plugins
|
||||
catalog := Catalog()
|
||||
|
||||
// Create our map of plugins
|
||||
internal := make(map[loader.PluginID]*loader.InternalPluginConfig, len(catalog))
|
||||
|
||||
for id, reg := range catalog {
|
||||
if reg.Config == nil {
|
||||
logger.Warn("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 {
|
||||
t.Fatalf("failed to retrieve config for internal plugin %v: %v", id, err)
|
||||
}
|
||||
|
||||
pluginConfig = pc
|
||||
}
|
||||
|
||||
internal[id] = &loader.InternalPluginConfig{
|
||||
Factory: reg.Config.Factory,
|
||||
Config: pluginConfig,
|
||||
}
|
||||
}
|
||||
|
||||
// Build the plugin loader
|
||||
config := &loader.PluginLoaderConfig{
|
||||
Logger: logger,
|
||||
PluginDir: "",
|
||||
Configs: configs,
|
||||
InternalPlugins: internal,
|
||||
SupportedVersions: loader.AgentSupportedApiVersions,
|
||||
}
|
||||
l, err := loader.NewPluginLoader(config)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create plugin loader: %v", err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
Reference in New Issue
Block a user