mirror of
https://github.com/kemko/nomad.git
synced 2026-01-05 01:45:44 +03:00
This changeset implements the initial registration and fingerprinting of CSI Plugins as part of #5378. At a high level, it introduces the following: * A `csi_plugin` stanza as part of a Nomad task configuration, to allow a task to expose that it is a plugin. * A new task runner hook: `csi_plugin_supervisor`. This hook does two things. When the `csi_plugin` stanza is detected, it will automatically configure the plugin task to receive bidirectional mounts to the CSI intermediary directory. At runtime, it will then perform an initial heartbeat of the plugin and handle submitting it to the new `dynamicplugins.Registry` for further use by the client, and then run a lightweight heartbeat loop that will emit task events when health changes. * The `dynamicplugins.Registry` for handling plugins that run as Nomad tasks, in contrast to the existing catalog that requires `go-plugin` type plugins and to know the plugin configuration in advance. * The `csimanager` which fingerprints CSI plugins, in a similar way to `drivermanager` and `devicemanager`. It currently only fingerprints the NodeID from the plugin, and assumes that all plugins are monolithic. Missing features * We do not use the live updates of the `dynamicplugin` registry in the `csimanager` yet. * We do not deregister the plugins from the client when they shutdown yet, they just become indefinitely marked as unhealthy. This is deliberate until we figure out how we should manage deploying new versions of plugins/transitioning them.
339 lines
8.7 KiB
Go
339 lines
8.7 KiB
Go
// dynamicplugins is a package that manages dynamic plugins in Nomad.
|
|
// It exposes a registry that allows for plugins to be registered/deregistered
|
|
// and also allows subscribers to receive real time updates of these events.
|
|
package dynamicplugins
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
PluginTypeCSIController = "csi-controller"
|
|
PluginTypeCSINode = "csi-node"
|
|
)
|
|
|
|
// Registry is an interface that allows for the dynamic registration of plugins
|
|
// that are running as Nomad Tasks.
|
|
type Registry interface {
|
|
RegisterPlugin(info *PluginInfo) error
|
|
DeregisterPlugin(ptype, name string) error
|
|
|
|
ListPlugins(ptype string) []*PluginInfo
|
|
DispensePlugin(ptype, name string) (interface{}, error)
|
|
|
|
PluginsUpdatedCh(ctx context.Context, ptype string) <-chan *PluginUpdateEvent
|
|
|
|
Shutdown()
|
|
}
|
|
|
|
type PluginDispenser func(info *PluginInfo) (interface{}, error)
|
|
|
|
// NewRegistry takes a map of `plugintype` to PluginDispenser functions
|
|
// that should be used to vend clients for plugins to be used.
|
|
func NewRegistry(dispensers map[string]PluginDispenser) Registry {
|
|
return &dynamicRegistry{
|
|
plugins: make(map[string]map[string]*PluginInfo),
|
|
broadcasters: make(map[string]*pluginEventBroadcaster),
|
|
dispensers: dispensers,
|
|
}
|
|
}
|
|
|
|
// PluginInfo is the metadata that is stored by the registry for a given plugin.
|
|
type PluginInfo struct {
|
|
Name string
|
|
Type string
|
|
Version string
|
|
|
|
// ConnectionInfo should only be used externally during `RegisterPlugin` and
|
|
// may not be exposed in the future.
|
|
ConnectionInfo *PluginConnectionInfo
|
|
}
|
|
|
|
// PluginConnectionInfo is the data required to connect to the plugin.
|
|
// note: We currently only support Unix Domain Sockets, but this may be expanded
|
|
// to support other connection modes in the future.
|
|
type PluginConnectionInfo struct {
|
|
// SocketPath is the path to the plugins api socket.
|
|
SocketPath string
|
|
}
|
|
|
|
// EventType is the enum of events that will be emitted by a Registry's
|
|
// PluginsUpdatedCh.
|
|
type EventType string
|
|
|
|
const (
|
|
// EventTypeRegistered is emitted by the Registry when a new plugin has been
|
|
// registered.
|
|
EventTypeRegistered EventType = "registered"
|
|
// EventTypeDeregistered is emitted by the Registry when a plugin has been
|
|
// removed.
|
|
EventTypeDeregistered EventType = "deregistered"
|
|
)
|
|
|
|
// PluginUpdateEvent is a struct that is sent over a PluginsUpdatedCh when
|
|
// plugins are added or removed from the registry.
|
|
type PluginUpdateEvent struct {
|
|
EventType EventType
|
|
Info *PluginInfo
|
|
}
|
|
|
|
type dynamicRegistry struct {
|
|
plugins map[string]map[string]*PluginInfo
|
|
pluginsLock sync.RWMutex
|
|
|
|
broadcasters map[string]*pluginEventBroadcaster
|
|
broadcastersLock sync.Mutex
|
|
|
|
dispensers map[string]PluginDispenser
|
|
}
|
|
|
|
func (d *dynamicRegistry) RegisterPlugin(info *PluginInfo) error {
|
|
if info.Type == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return errors.New("Plugin.Type must not be empty")
|
|
}
|
|
|
|
if info.ConnectionInfo == nil {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return errors.New("Plugin.ConnectionInfo must not be nil")
|
|
}
|
|
|
|
if info.Name == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return errors.New("Plugin.Name must not be empty")
|
|
}
|
|
|
|
d.pluginsLock.Lock()
|
|
defer d.pluginsLock.Unlock()
|
|
|
|
pmap, ok := d.plugins[info.Type]
|
|
if !ok {
|
|
pmap = make(map[string]*PluginInfo, 1)
|
|
d.plugins[info.Type] = pmap
|
|
}
|
|
|
|
pmap[info.Name] = info
|
|
|
|
broadcaster := d.broadcasterForPluginType(info.Type)
|
|
event := &PluginUpdateEvent{
|
|
EventType: EventTypeRegistered,
|
|
Info: info,
|
|
}
|
|
broadcaster.broadcast(event)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *dynamicRegistry) broadcasterForPluginType(ptype string) *pluginEventBroadcaster {
|
|
d.broadcastersLock.Lock()
|
|
defer d.broadcastersLock.Unlock()
|
|
|
|
broadcaster, ok := d.broadcasters[ptype]
|
|
if !ok {
|
|
broadcaster = newPluginEventBroadcaster()
|
|
d.broadcasters[ptype] = broadcaster
|
|
}
|
|
|
|
return broadcaster
|
|
}
|
|
|
|
func (d *dynamicRegistry) DeregisterPlugin(ptype, name string) error {
|
|
d.pluginsLock.Lock()
|
|
defer d.pluginsLock.Unlock()
|
|
|
|
if ptype == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return errors.New("must specify plugin type to deregister")
|
|
}
|
|
if name == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return errors.New("must specify plugin name to deregister")
|
|
}
|
|
|
|
pmap, ok := d.plugins[ptype]
|
|
if !ok {
|
|
// If this occurs there's a bug in the registration handler.
|
|
return fmt.Errorf("no plugins registered for type: %s", ptype)
|
|
}
|
|
|
|
info, ok := pmap[name]
|
|
if !ok {
|
|
// plugin already deregistered, don't send events or try re-deleting.
|
|
return nil
|
|
}
|
|
delete(pmap, name)
|
|
|
|
broadcaster := d.broadcasterForPluginType(ptype)
|
|
event := &PluginUpdateEvent{
|
|
EventType: EventTypeDeregistered,
|
|
Info: info,
|
|
}
|
|
broadcaster.broadcast(event)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *dynamicRegistry) ListPlugins(ptype string) []*PluginInfo {
|
|
d.pluginsLock.RLock()
|
|
defer d.pluginsLock.RUnlock()
|
|
|
|
pmap, ok := d.plugins[ptype]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
plugins := make([]*PluginInfo, 0, len(pmap))
|
|
|
|
for _, info := range pmap {
|
|
plugins = append(plugins, info)
|
|
}
|
|
|
|
return plugins
|
|
}
|
|
|
|
func (d *dynamicRegistry) DispensePlugin(ptype string, name string) (interface{}, error) {
|
|
d.pluginsLock.Lock()
|
|
defer d.pluginsLock.Unlock()
|
|
|
|
if ptype == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return nil, errors.New("must specify plugin type to deregister")
|
|
}
|
|
if name == "" {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return nil, errors.New("must specify plugin name to deregister")
|
|
}
|
|
|
|
dispenseFunc, ok := d.dispensers[ptype]
|
|
if !ok {
|
|
// This error shouldn't make it to a production cluster and is to aid
|
|
// developers during the development of new plugin types.
|
|
return nil, fmt.Errorf("no plugin dispenser found for type: %s", ptype)
|
|
}
|
|
|
|
pmap, ok := d.plugins[ptype]
|
|
if !ok {
|
|
return nil, fmt.Errorf("no plugins registered for type: %s", ptype)
|
|
}
|
|
|
|
info, ok := pmap[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("plugin %s for type %s not found", name, ptype)
|
|
}
|
|
|
|
return dispenseFunc(info)
|
|
}
|
|
|
|
// PluginsUpdatedCh returns a channel over which plugin events for the requested
|
|
// plugin type will be emitted. These events are strongly ordered and will never
|
|
// be dropped.
|
|
//
|
|
// The receiving channel _must not_ be closed before the provided context is
|
|
// cancelled.
|
|
func (d *dynamicRegistry) PluginsUpdatedCh(ctx context.Context, ptype string) <-chan *PluginUpdateEvent {
|
|
b := d.broadcasterForPluginType(ptype)
|
|
ch := b.subscribe()
|
|
go func() {
|
|
select {
|
|
case <-b.shutdownCh:
|
|
return
|
|
case <-ctx.Done():
|
|
b.unsubscribe(ch)
|
|
}
|
|
}()
|
|
|
|
return ch
|
|
}
|
|
|
|
func (d *dynamicRegistry) Shutdown() {
|
|
for _, b := range d.broadcasters {
|
|
b.shutdown()
|
|
}
|
|
}
|
|
|
|
type pluginEventBroadcaster struct {
|
|
stopCh chan struct{}
|
|
shutdownCh chan struct{}
|
|
publishCh chan *PluginUpdateEvent
|
|
|
|
subscriptions map[chan *PluginUpdateEvent]struct{}
|
|
subscriptionsLock sync.RWMutex
|
|
}
|
|
|
|
func newPluginEventBroadcaster() *pluginEventBroadcaster {
|
|
b := &pluginEventBroadcaster{
|
|
stopCh: make(chan struct{}),
|
|
shutdownCh: make(chan struct{}),
|
|
publishCh: make(chan *PluginUpdateEvent, 1),
|
|
subscriptions: make(map[chan *PluginUpdateEvent]struct{}),
|
|
}
|
|
go b.run()
|
|
return b
|
|
}
|
|
|
|
func (p *pluginEventBroadcaster) run() {
|
|
for {
|
|
select {
|
|
case <-p.stopCh:
|
|
close(p.shutdownCh)
|
|
return
|
|
case msg := <-p.publishCh:
|
|
p.subscriptionsLock.RLock()
|
|
for msgCh := range p.subscriptions {
|
|
select {
|
|
case msgCh <- msg:
|
|
}
|
|
}
|
|
p.subscriptionsLock.RUnlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *pluginEventBroadcaster) shutdown() {
|
|
close(p.stopCh)
|
|
|
|
// Wait for loop to exit before closing subscriptions
|
|
<-p.shutdownCh
|
|
|
|
p.subscriptionsLock.Lock()
|
|
for sub := range p.subscriptions {
|
|
delete(p.subscriptions, sub)
|
|
close(sub)
|
|
}
|
|
p.subscriptionsLock.Unlock()
|
|
}
|
|
|
|
func (p *pluginEventBroadcaster) broadcast(e *PluginUpdateEvent) {
|
|
p.publishCh <- e
|
|
}
|
|
|
|
func (p *pluginEventBroadcaster) subscribe() chan *PluginUpdateEvent {
|
|
p.subscriptionsLock.Lock()
|
|
defer p.subscriptionsLock.Unlock()
|
|
|
|
ch := make(chan *PluginUpdateEvent, 1)
|
|
p.subscriptions[ch] = struct{}{}
|
|
return ch
|
|
}
|
|
|
|
func (p *pluginEventBroadcaster) unsubscribe(ch chan *PluginUpdateEvent) {
|
|
p.subscriptionsLock.Lock()
|
|
defer p.subscriptionsLock.Unlock()
|
|
|
|
_, ok := p.subscriptions[ch]
|
|
if ok {
|
|
delete(p.subscriptions, ch)
|
|
close(ch)
|
|
}
|
|
}
|