2019-10-22 13:20:26 +00:00
|
|
|
package csimanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
|
|
|
"github.com/hashicorp/nomad/plugins/csi"
|
|
|
|
)
|
|
|
|
|
|
|
|
const managerFingerprintInterval = 30 * time.Second
|
|
|
|
|
|
|
|
// instanceManager is used to manage the fingerprinting and supervision of a
|
|
|
|
// single CSI Plugin.
|
|
|
|
type instanceManager struct {
|
|
|
|
info *dynamicplugins.PluginInfo
|
|
|
|
logger hclog.Logger
|
|
|
|
|
|
|
|
updater UpdateNodeCSIInfoFunc
|
|
|
|
|
|
|
|
shutdownCtx context.Context
|
|
|
|
shutdownCtxCancelFn context.CancelFunc
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
|
2020-01-08 12:47:07 +00:00
|
|
|
// mountPoint is the root of the mount dir where plugin specific data may be
|
|
|
|
// stored and where mount points will be created
|
|
|
|
mountPoint string
|
|
|
|
|
2020-02-06 14:26:29 +00:00
|
|
|
// containerMountPoint is the location _inside_ the plugin container that the
|
|
|
|
// `mountPoint` is bound in to.
|
|
|
|
containerMountPoint string
|
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// AllocID is the allocation id of the task group running the dynamic plugin
|
|
|
|
allocID string
|
|
|
|
|
2020-01-08 12:47:07 +00:00
|
|
|
fp *pluginFingerprinter
|
|
|
|
|
|
|
|
volumeManager *volumeManager
|
|
|
|
volumeManagerSetupCh chan struct{}
|
2019-10-22 13:20:26 +00:00
|
|
|
|
|
|
|
client csi.CSIPlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInstanceManager(logger hclog.Logger, updater UpdateNodeCSIInfoFunc, p *dynamicplugins.PluginInfo) *instanceManager {
|
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
2020-01-08 12:47:07 +00:00
|
|
|
logger = logger.Named(p.Name)
|
2019-10-22 13:20:26 +00:00
|
|
|
return &instanceManager{
|
2020-01-08 12:47:07 +00:00
|
|
|
logger: logger,
|
2019-10-22 13:20:26 +00:00
|
|
|
info: p,
|
|
|
|
updater: updater,
|
|
|
|
|
2020-01-08 12:47:07 +00:00
|
|
|
fp: &pluginFingerprinter{
|
2020-02-06 14:46:47 +00:00
|
|
|
logger: logger.Named("fingerprinter"),
|
|
|
|
info: p,
|
|
|
|
fingerprintNode: p.Type == dynamicplugins.PluginTypeCSINode,
|
|
|
|
fingerprintController: p.Type == dynamicplugins.PluginTypeCSIController,
|
|
|
|
hadFirstSuccessfulFingerprintCh: make(chan struct{}),
|
2020-01-08 12:47:07 +00:00
|
|
|
},
|
|
|
|
|
2020-02-06 14:26:29 +00:00
|
|
|
mountPoint: p.Options["MountPoint"],
|
|
|
|
containerMountPoint: p.Options["ContainerMountPoint"],
|
2020-02-21 19:48:16 +00:00
|
|
|
allocID: p.AllocID,
|
2020-01-08 12:47:07 +00:00
|
|
|
|
|
|
|
volumeManagerSetupCh: make(chan struct{}),
|
2019-10-22 13:20:26 +00:00
|
|
|
|
|
|
|
shutdownCtx: ctx,
|
|
|
|
shutdownCtxCancelFn: cancelFn,
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instanceManager) run() {
|
2020-01-08 12:47:07 +00:00
|
|
|
c, err := csi.NewClient(i.info.ConnectionInfo.SocketPath, i.logger)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil {
|
|
|
|
i.logger.Error("failed to setup instance manager client", "error", err)
|
|
|
|
close(i.shutdownCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.client = c
|
2020-02-05 11:13:08 +00:00
|
|
|
i.fp.client = c
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2020-02-06 14:46:47 +00:00
|
|
|
go i.setupVolumeManager()
|
2019-10-22 13:20:26 +00:00
|
|
|
go i.runLoop()
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:46:47 +00:00
|
|
|
func (i *instanceManager) setupVolumeManager() {
|
|
|
|
if i.info.Type != dynamicplugins.PluginTypeCSINode {
|
2020-03-19 15:27:48 +00:00
|
|
|
i.logger.Debug("not a node plugin, skipping volume manager setup", "type", i.info.Type)
|
2020-02-06 14:46:47 +00:00
|
|
|
return
|
2020-01-08 12:47:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:46:47 +00:00
|
|
|
select {
|
|
|
|
case <-i.shutdownCtx.Done():
|
|
|
|
return
|
|
|
|
case <-i.fp.hadFirstSuccessfulFingerprintCh:
|
2020-02-07 10:44:57 +00:00
|
|
|
i.volumeManager = newVolumeManager(i.logger, i.client, i.mountPoint, i.containerMountPoint, i.fp.requiresStaging)
|
2020-03-19 15:27:48 +00:00
|
|
|
i.logger.Debug("volume manager setup complete")
|
2020-02-06 14:46:47 +00:00
|
|
|
close(i.volumeManagerSetupCh)
|
|
|
|
return
|
2020-01-08 12:47:07 +00:00
|
|
|
}
|
2020-02-06 14:46:47 +00:00
|
|
|
}
|
2020-01-08 12:47:07 +00:00
|
|
|
|
2020-02-06 14:46:47 +00:00
|
|
|
// VolumeMounter returns the volume manager that is configured for the given plugin
|
|
|
|
// instance. If called before the volume manager has been setup, it will block until
|
|
|
|
// the volume manager is ready or the context is closed.
|
|
|
|
func (i *instanceManager) VolumeMounter(ctx context.Context) (VolumeMounter, error) {
|
2020-01-08 12:47:07 +00:00
|
|
|
select {
|
|
|
|
case <-i.volumeManagerSetupCh:
|
2020-02-06 14:46:47 +00:00
|
|
|
return i.volumeManager, nil
|
2020-01-08 12:47:07 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
func (i *instanceManager) requestCtxWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(i.shutdownCtx, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instanceManager) runLoop() {
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-i.shutdownCtx.Done():
|
|
|
|
if i.client != nil {
|
|
|
|
i.client.Close()
|
|
|
|
i.client = nil
|
|
|
|
}
|
2020-03-19 21:09:49 +00:00
|
|
|
|
|
|
|
// run one last fingerprint so that we mark the plugin as unhealthy.
|
|
|
|
// the client has been closed so this will return quickly with the
|
|
|
|
// plugin's basic info
|
|
|
|
ctx, cancelFn := i.requestCtxWithTimeout(time.Second)
|
|
|
|
info := i.fp.fingerprint(ctx)
|
|
|
|
cancelFn()
|
|
|
|
if info != nil {
|
|
|
|
i.updater(i.info.Name, info)
|
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
close(i.shutdownCh)
|
|
|
|
return
|
2020-03-19 21:09:49 +00:00
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
case <-timer.C:
|
|
|
|
ctx, cancelFn := i.requestCtxWithTimeout(managerFingerprintInterval)
|
2020-01-08 12:47:07 +00:00
|
|
|
info := i.fp.fingerprint(ctx)
|
|
|
|
cancelFn()
|
2020-03-19 21:09:49 +00:00
|
|
|
if info != nil {
|
|
|
|
i.updater(i.info.Name, info)
|
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
timer.Reset(managerFingerprintInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instanceManager) shutdown() {
|
|
|
|
i.shutdownCtxCancelFn()
|
|
|
|
<-i.shutdownCh
|
|
|
|
}
|