2019-10-22 13:20:26 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
ti "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/csi"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// csiPluginSupervisorHook manages supervising plugins that are running as Nomad
|
|
|
|
// tasks. These plugins will be fingerprinted and it will manage connecting them
|
|
|
|
// to their requisite plugin manager.
|
|
|
|
//
|
2022-02-23 20:23:07 +00:00
|
|
|
// It provides a few things to a plugin task running inside Nomad. These are:
|
2022-08-16 14:06:30 +00:00
|
|
|
// - A mount to the `csi_plugin.mount_dir` where the plugin will create its csi.sock
|
|
|
|
// - A mount to `local/csi` that node plugins will use to stage volume mounts.
|
|
|
|
// - When the task has started, it starts a loop of attempting to connect to the
|
|
|
|
// plugin, to perform initial fingerprinting of the plugins capabilities before
|
|
|
|
// notifying the plugin manager of the plugin.
|
2019-10-22 13:20:26 +00:00
|
|
|
type csiPluginSupervisorHook struct {
|
2022-02-23 20:23:07 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
alloc *structs.Allocation
|
|
|
|
task *structs.Task
|
|
|
|
runner *TaskRunner
|
|
|
|
mountPoint string
|
|
|
|
socketMountPoint string
|
|
|
|
socketPath string
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2022-02-11 13:46:21 +00:00
|
|
|
caps *drivers.Capabilities
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// eventEmitter is used to emit events to the task
|
|
|
|
eventEmitter ti.EventEmitter
|
2022-02-15 21:57:29 +00:00
|
|
|
lifecycle ti.TaskLifecycle
|
2019-10-22 13:20:26 +00:00
|
|
|
|
|
|
|
shutdownCtx context.Context
|
|
|
|
shutdownCancelFn context.CancelFunc
|
2022-04-26 14:38:50 +00:00
|
|
|
runOnce sync.Once
|
2019-10-22 13:20:26 +00:00
|
|
|
|
|
|
|
// previousHealthstate is used by the supervisor goroutine to track historic
|
|
|
|
// health states for gating task events.
|
|
|
|
previousHealthState bool
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:46:21 +00:00
|
|
|
type csiPluginSupervisorHookConfig struct {
|
|
|
|
clientStateDirPath string
|
|
|
|
events ti.EventEmitter
|
|
|
|
runner *TaskRunner
|
2022-02-15 21:57:29 +00:00
|
|
|
lifecycle ti.TaskLifecycle
|
2022-02-11 13:46:21 +00:00
|
|
|
capabilities *drivers.Capabilities
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// The plugin supervisor uses the PrestartHook mechanism to setup the requisite
|
|
|
|
// mount points and configuration for the task that exposes a CSI plugin.
|
|
|
|
var _ interfaces.TaskPrestartHook = &csiPluginSupervisorHook{}
|
|
|
|
|
|
|
|
// The plugin supervisor uses the PoststartHook mechanism to start polling the
|
|
|
|
// plugin for readiness and supported functionality before registering the
|
|
|
|
// plugin with the catalog.
|
|
|
|
var _ interfaces.TaskPoststartHook = &csiPluginSupervisorHook{}
|
|
|
|
|
|
|
|
// The plugin supervisor uses the StopHook mechanism to deregister the plugin
|
|
|
|
// with the catalog and to ensure any mounts are cleaned up.
|
|
|
|
var _ interfaces.TaskStopHook = &csiPluginSupervisorHook{}
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
// This hook creates a csi/ directory within the client's datadir used to
|
|
|
|
// manage plugins and mount points volumes. The layout is as follows:
|
|
|
|
|
|
|
|
// plugins/
|
|
|
|
// {alloc-id}/csi.sock
|
|
|
|
// Per-allocation directories of unix domain sockets used to communicate
|
|
|
|
// with the CSI plugin. Nomad creates the directory and the plugin creates
|
|
|
|
// the socket file. This directory is bind-mounted to the
|
2022-08-02 13:42:44 +00:00
|
|
|
// csi_plugin.mount_dir in the plugin task.
|
2022-02-23 20:23:07 +00:00
|
|
|
//
|
|
|
|
// {plugin-type}/{plugin-id}/
|
|
|
|
// staging/
|
|
|
|
// {volume-id}/{usage-mode}/
|
|
|
|
// Intermediate mount point used by node plugins that support
|
|
|
|
// NODE_STAGE_UNSTAGE capability.
|
|
|
|
//
|
|
|
|
// per-alloc/
|
|
|
|
// {alloc-id}/{volume-id}/{usage-mode}/
|
|
|
|
// Mount point bound from the staging directory into tasks that use
|
|
|
|
// the mounted volumes
|
|
|
|
|
2022-02-11 13:46:21 +00:00
|
|
|
func newCSIPluginSupervisorHook(config *csiPluginSupervisorHookConfig) *csiPluginSupervisorHook {
|
|
|
|
task := config.runner.Task()
|
2020-01-08 12:47:07 +00:00
|
|
|
|
2022-02-11 13:46:21 +00:00
|
|
|
pluginRoot := filepath.Join(config.clientStateDirPath, "csi",
|
|
|
|
string(task.CSIPluginConfig.Type), task.CSIPluginConfig.ID)
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
socketMountPoint := filepath.Join(config.clientStateDirPath, "csi",
|
|
|
|
"plugins", config.runner.Alloc().ID)
|
|
|
|
|
2022-08-02 13:42:44 +00:00
|
|
|
// In v1.3.0, Nomad started instructing CSI plugins to stage and publish
|
|
|
|
// within /local/csi. Plugins deployed after the introduction of
|
|
|
|
// StagePublishBaseDir default to StagePublishBaseDir = /local/csi. However,
|
|
|
|
// plugins deployed between v1.3.0 and the introduction of
|
|
|
|
// StagePublishBaseDir have StagePublishBaseDir = "". Default to /local/csi here
|
|
|
|
// to avoid breaking plugins that aren't redeployed.
|
|
|
|
if task.CSIPluginConfig.StagePublishBaseDir == "" {
|
|
|
|
task.CSIPluginConfig.StagePublishBaseDir = filepath.Join("/local", "csi")
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:04:16 +00:00
|
|
|
if task.CSIPluginConfig.HealthTimeout == 0 {
|
|
|
|
task.CSIPluginConfig.HealthTimeout = 30 * time.Second
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
shutdownCtx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
hook := &csiPluginSupervisorHook{
|
2022-02-11 13:46:21 +00:00
|
|
|
alloc: config.runner.Alloc(),
|
|
|
|
runner: config.runner,
|
2022-02-15 21:57:29 +00:00
|
|
|
lifecycle: config.lifecycle,
|
2022-02-11 13:46:21 +00:00
|
|
|
logger: config.logger,
|
2019-10-22 13:20:26 +00:00
|
|
|
task: task,
|
|
|
|
mountPoint: pluginRoot,
|
2022-02-23 20:23:07 +00:00
|
|
|
socketMountPoint: socketMountPoint,
|
2022-02-11 13:46:21 +00:00
|
|
|
caps: config.capabilities,
|
2019-10-22 13:20:26 +00:00
|
|
|
shutdownCtx: shutdownCtx,
|
|
|
|
shutdownCancelFn: cancelFn,
|
2022-02-11 13:46:21 +00:00
|
|
|
eventEmitter: config.events,
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hook
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*csiPluginSupervisorHook) Name() string {
|
|
|
|
return "csi_plugin_supervisor"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prestart is called before the task is started including after every
|
2022-04-22 17:07:47 +00:00
|
|
|
// restart. This requires that the mount paths for a plugin be
|
|
|
|
// idempotent, despite us not knowing the name of the plugin ahead of
|
|
|
|
// time. Because of this, we use the allocid_taskname as the unique
|
|
|
|
// identifier for a plugin on the filesystem.
|
2019-10-22 13:20:26 +00:00
|
|
|
func (h *csiPluginSupervisorHook) Prestart(ctx context.Context,
|
|
|
|
req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
2022-03-01 15:22:52 +00:00
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// Create the mount directory that the container will access if it doesn't
|
2020-01-08 12:47:07 +00:00
|
|
|
// already exist. Default to only nomad user access.
|
2019-10-22 13:20:26 +00:00
|
|
|
if err := os.MkdirAll(h.mountPoint, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("failed to create mount point: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
if err := os.MkdirAll(h.socketMountPoint, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("failed to create socket mount point: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// where the socket will be mounted
|
2019-10-22 13:20:26 +00:00
|
|
|
configMount := &drivers.MountConfig{
|
|
|
|
TaskPath: h.task.CSIPluginConfig.MountDir,
|
2022-02-23 20:23:07 +00:00
|
|
|
HostPath: h.socketMountPoint,
|
|
|
|
Readonly: false,
|
|
|
|
PropagationMode: "bidirectional",
|
|
|
|
}
|
|
|
|
// where the staging and per-alloc directories will be mounted
|
|
|
|
volumeStagingMounts := &drivers.MountConfig{
|
2022-08-02 13:42:44 +00:00
|
|
|
TaskPath: h.task.CSIPluginConfig.StagePublishBaseDir,
|
2019-10-22 13:20:26 +00:00
|
|
|
HostPath: h.mountPoint,
|
|
|
|
Readonly: false,
|
|
|
|
PropagationMode: "bidirectional",
|
|
|
|
}
|
2022-02-23 20:23:07 +00:00
|
|
|
// devices from the host
|
2020-02-21 09:37:12 +00:00
|
|
|
devMount := &drivers.MountConfig{
|
|
|
|
TaskPath: "/dev",
|
|
|
|
HostPath: "/dev",
|
|
|
|
Readonly: false,
|
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2022-03-01 15:22:52 +00:00
|
|
|
h.setSocketHook()
|
2022-02-23 20:23:07 +00:00
|
|
|
|
2022-03-21 15:48:47 +00:00
|
|
|
if _, ok := h.task.Env["CSI_ENDPOINT"]; !ok {
|
|
|
|
switch h.caps.FSIsolation {
|
|
|
|
case drivers.FSIsolationNone:
|
|
|
|
// Plugin tasks with no filesystem isolation won't have the
|
|
|
|
// plugin dir bind-mounted to their alloc dir, but we can
|
|
|
|
// provide them the path to the socket. These Nomad-only
|
|
|
|
// plugins will need to be aware of the csi directory layout
|
|
|
|
// in the client data dir
|
|
|
|
resp.Env = map[string]string{
|
|
|
|
"CSI_ENDPOINT": "unix://" + h.socketPath}
|
|
|
|
default:
|
|
|
|
resp.Env = map[string]string{
|
|
|
|
"CSI_ENDPOINT": "unix://" + filepath.Join(
|
|
|
|
h.task.CSIPluginConfig.MountDir, structs.CSISocketName)}
|
|
|
|
}
|
2022-02-11 13:46:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
mounts := ensureMountpointInserted(h.runner.hookResources.getMounts(), configMount)
|
2022-02-23 20:23:07 +00:00
|
|
|
mounts = ensureMountpointInserted(mounts, volumeStagingMounts)
|
2020-02-21 09:37:12 +00:00
|
|
|
mounts = ensureMountpointInserted(mounts, devMount)
|
|
|
|
|
2022-04-22 17:07:47 +00:00
|
|
|
// we normally would set resp.Mounts here but without setting the
|
|
|
|
// hookResources before returning we can get a postrun hook that's
|
|
|
|
// missing resources.
|
2019-10-22 13:20:26 +00:00
|
|
|
h.runner.hookResources.setMounts(mounts)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:22:52 +00:00
|
|
|
func (h *csiPluginSupervisorHook) setSocketHook() {
|
|
|
|
|
|
|
|
// TODO(tgross): https://github.com/hashicorp/nomad/issues/11786
|
|
|
|
// If we're already registered, we should be able to update the
|
|
|
|
// definition in the update hook
|
|
|
|
|
|
|
|
// For backwards compatibility, ensure that we don't overwrite the
|
|
|
|
// socketPath on client restart with existing plugin allocations.
|
|
|
|
pluginInfo, _ := h.runner.dynamicRegistry.PluginForAlloc(
|
|
|
|
string(h.task.CSIPluginConfig.Type), h.task.CSIPluginConfig.ID, h.alloc.ID)
|
|
|
|
if pluginInfo != nil && pluginInfo.ConnectionInfo.SocketPath != "" {
|
|
|
|
h.socketPath = pluginInfo.ConnectionInfo.SocketPath
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.socketPath = filepath.Join(h.socketMountPoint, structs.CSISocketName)
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// Poststart is called after the task has started. Poststart is not
|
|
|
|
// called if the allocation is terminal.
|
|
|
|
//
|
|
|
|
// The context is cancelled if the task is killed.
|
|
|
|
func (h *csiPluginSupervisorHook) Poststart(_ context.Context, _ *interfaces.TaskPoststartRequest, _ *interfaces.TaskPoststartResponse) error {
|
2022-03-01 15:22:52 +00:00
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// If we're already running the supervisor routine, then we don't need to try
|
|
|
|
// and restart it here as it only terminates on `Stop` hooks.
|
2022-04-26 14:38:50 +00:00
|
|
|
h.runOnce.Do(func() {
|
|
|
|
h.setSocketHook()
|
|
|
|
go h.ensureSupervisorLoop(h.shutdownCtx)
|
|
|
|
})
|
2022-03-01 15:22:52 +00:00
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureSupervisorLoop should be called in a goroutine. It will terminate when
|
|
|
|
// the passed in context is terminated.
|
|
|
|
//
|
|
|
|
// The supervisor works by:
|
2022-08-16 14:06:30 +00:00
|
|
|
// - Initially waiting for the plugin to become available. This loop is expensive
|
|
|
|
// and may do things like create new gRPC Clients on every iteration.
|
|
|
|
// - After receiving an initial healthy status, it will inform the plugin catalog
|
|
|
|
// of the plugin, registering it with the plugins fingerprinted capabilities.
|
|
|
|
// - We then perform a more lightweight check, simply probing the plugin on a less
|
|
|
|
// frequent interval to ensure it is still alive, emitting task events when this
|
|
|
|
// status changes.
|
2019-10-22 13:20:26 +00:00
|
|
|
//
|
|
|
|
// Deeper fingerprinting of the plugin is implemented by the csimanager.
|
|
|
|
func (h *csiPluginSupervisorHook) ensureSupervisorLoop(ctx context.Context) {
|
2022-02-23 20:23:07 +00:00
|
|
|
client := csi.NewClient(h.socketPath, h.logger.Named("csi_client").With(
|
2022-02-15 21:57:29 +00:00
|
|
|
"plugin.name", h.task.CSIPluginConfig.ID,
|
|
|
|
"plugin.type", h.task.CSIPluginConfig.Type))
|
|
|
|
defer client.Close()
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
t := time.NewTimer(0)
|
|
|
|
|
2022-02-15 21:57:29 +00:00
|
|
|
// We're in Poststart at this point, so if we can't connect within
|
|
|
|
// this deadline, assume it's broken so we can restart the task
|
2022-06-14 14:04:16 +00:00
|
|
|
startCtx, startCancelFn := context.WithTimeout(ctx, h.task.CSIPluginConfig.HealthTimeout)
|
2022-02-15 21:57:29 +00:00
|
|
|
defer startCancelFn()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var pluginHealthy bool
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// Step 1: Wait for the plugin to initially become available.
|
|
|
|
WAITFORREADY:
|
|
|
|
for {
|
|
|
|
select {
|
2022-02-15 21:57:29 +00:00
|
|
|
case <-startCtx.Done():
|
|
|
|
h.kill(ctx, fmt.Errorf("CSI plugin failed probe: %v", err))
|
2019-10-22 13:20:26 +00:00
|
|
|
return
|
|
|
|
case <-t.C:
|
2022-02-15 21:57:29 +00:00
|
|
|
pluginHealthy, err = h.supervisorLoopOnce(startCtx, client)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil || !pluginHealthy {
|
2022-02-15 21:57:29 +00:00
|
|
|
h.logger.Debug("CSI plugin not ready", "error", err)
|
|
|
|
// Use only a short delay here to optimize for quickly
|
|
|
|
// bringing up a plugin
|
2019-10-22 13:20:26 +00:00
|
|
|
t.Reset(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the plugin as healthy in a task event
|
2022-02-15 21:57:29 +00:00
|
|
|
h.logger.Debug("CSI plugin is ready")
|
2019-10-22 13:20:26 +00:00
|
|
|
h.previousHealthState = pluginHealthy
|
|
|
|
event := structs.NewTaskEvent(structs.TaskPluginHealthy)
|
|
|
|
event.SetMessage(fmt.Sprintf("plugin: %s", h.task.CSIPluginConfig.ID))
|
|
|
|
h.eventEmitter.EmitEvent(event)
|
|
|
|
|
|
|
|
break WAITFORREADY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Register the plugin with the catalog.
|
2022-02-23 20:23:07 +00:00
|
|
|
deregisterPluginFn, err := h.registerPlugin(client, h.socketPath)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil {
|
2022-02-15 21:57:29 +00:00
|
|
|
h.kill(ctx, fmt.Errorf("CSI plugin failed to register: %v", err))
|
|
|
|
return
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
2022-04-26 14:38:50 +00:00
|
|
|
// De-register plugins on task shutdown
|
|
|
|
defer deregisterPluginFn()
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2022-04-26 14:38:50 +00:00
|
|
|
// Step 3: Start the lightweight supervisor loop. At this point,
|
|
|
|
// probe failures don't cause the task to restart
|
2019-10-22 13:20:26 +00:00
|
|
|
t.Reset(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
2022-02-15 21:57:29 +00:00
|
|
|
pluginHealthy, err := h.supervisorLoopOnce(ctx, client)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil {
|
2022-02-15 21:57:29 +00:00
|
|
|
h.logger.Error("CSI plugin fingerprinting failed", "error", err)
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The plugin has transitioned to a healthy state. Emit an event.
|
|
|
|
if !h.previousHealthState && pluginHealthy {
|
|
|
|
event := structs.NewTaskEvent(structs.TaskPluginHealthy)
|
|
|
|
event.SetMessage(fmt.Sprintf("plugin: %s", h.task.CSIPluginConfig.ID))
|
|
|
|
h.eventEmitter.EmitEvent(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The plugin has transitioned to an unhealthy state. Emit an event.
|
|
|
|
if h.previousHealthState && !pluginHealthy {
|
|
|
|
event := structs.NewTaskEvent(structs.TaskPluginUnhealthy)
|
|
|
|
if err != nil {
|
2022-02-15 21:57:29 +00:00
|
|
|
event.SetMessage(fmt.Sprintf("Error: %v", err))
|
2019-10-22 13:20:26 +00:00
|
|
|
} else {
|
|
|
|
event.SetMessage("Unknown Reason")
|
|
|
|
}
|
|
|
|
h.eventEmitter.EmitEvent(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.previousHealthState = pluginHealthy
|
|
|
|
|
|
|
|
// This loop is informational and in some plugins this may be expensive to
|
|
|
|
// validate. We use a longer timeout (30s) to avoid causing undue work.
|
|
|
|
t.Reset(30 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:57:29 +00:00
|
|
|
func (h *csiPluginSupervisorHook) registerPlugin(client csi.CSIPlugin, socketPath string) (func(), error) {
|
2020-03-09 13:57:59 +00:00
|
|
|
// At this point we know the plugin is ready and we can fingerprint it
|
|
|
|
// to get its vendor name and version
|
|
|
|
info, err := client.PluginInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to probe plugin: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
mkInfoFn := func(pluginType string) *dynamicplugins.PluginInfo {
|
|
|
|
return &dynamicplugins.PluginInfo{
|
|
|
|
Type: pluginType,
|
|
|
|
Name: h.task.CSIPluginConfig.ID,
|
2020-03-09 13:57:59 +00:00
|
|
|
Version: info.PluginVersion,
|
2019-10-22 13:20:26 +00:00
|
|
|
ConnectionInfo: &dynamicplugins.PluginConnectionInfo{
|
|
|
|
SocketPath: socketPath,
|
|
|
|
},
|
2020-02-21 19:48:16 +00:00
|
|
|
AllocID: h.alloc.ID,
|
2020-01-08 12:47:07 +00:00
|
|
|
Options: map[string]string{
|
2020-03-09 13:57:59 +00:00
|
|
|
"Provider": info.Name, // vendor name
|
2020-02-06 14:26:29 +00:00
|
|
|
"MountPoint": h.mountPoint,
|
2022-08-02 13:42:44 +00:00
|
|
|
"ContainerMountPoint": h.task.CSIPluginConfig.StagePublishBaseDir,
|
2020-01-08 12:47:07 +00:00
|
|
|
},
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registrations := []*dynamicplugins.PluginInfo{}
|
|
|
|
|
|
|
|
switch h.task.CSIPluginConfig.Type {
|
|
|
|
case structs.CSIPluginTypeController:
|
|
|
|
registrations = append(registrations, mkInfoFn(dynamicplugins.PluginTypeCSIController))
|
|
|
|
case structs.CSIPluginTypeNode:
|
|
|
|
registrations = append(registrations, mkInfoFn(dynamicplugins.PluginTypeCSINode))
|
|
|
|
case structs.CSIPluginTypeMonolith:
|
|
|
|
registrations = append(registrations, mkInfoFn(dynamicplugins.PluginTypeCSIController))
|
|
|
|
registrations = append(registrations, mkInfoFn(dynamicplugins.PluginTypeCSINode))
|
|
|
|
}
|
|
|
|
|
|
|
|
deregistrationFns := []func(){}
|
|
|
|
|
|
|
|
for _, reg := range registrations {
|
|
|
|
if err := h.runner.dynamicRegistry.RegisterPlugin(reg); err != nil {
|
|
|
|
for _, fn := range deregistrationFns {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:27:48 +00:00
|
|
|
// need to rebind these so that each deregistration function
|
|
|
|
// closes over its own registration
|
|
|
|
rname := reg.Name
|
|
|
|
rtype := reg.Type
|
2022-02-23 20:23:07 +00:00
|
|
|
allocID := reg.AllocID
|
2019-10-22 13:20:26 +00:00
|
|
|
deregistrationFns = append(deregistrationFns, func() {
|
2022-02-23 20:23:07 +00:00
|
|
|
err := h.runner.dynamicRegistry.DeregisterPlugin(rtype, rname, allocID)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil {
|
2020-03-19 15:27:48 +00:00
|
|
|
h.logger.Error("failed to deregister csi plugin", "name", rname, "type", rtype, "error", err)
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
for _, fn := range deregistrationFns {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:57:29 +00:00
|
|
|
func (h *csiPluginSupervisorHook) supervisorLoopOnce(ctx context.Context, client csi.CSIPlugin) (bool, error) {
|
|
|
|
probeCtx, probeCancelFn := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer probeCancelFn()
|
2019-10-22 13:20:26 +00:00
|
|
|
|
2022-02-15 21:57:29 +00:00
|
|
|
healthy, err := client.PluginProbe(probeCtx)
|
2019-10-22 13:20:26 +00:00
|
|
|
if err != nil {
|
2022-02-15 21:57:29 +00:00
|
|
|
return false, err
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return healthy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is called after the task has exited and will not be started
|
|
|
|
// again. It is the only hook guaranteed to be executed whenever
|
|
|
|
// TaskRunner.Run is called (and not gracefully shutting down).
|
|
|
|
// Therefore it may be called even when prestart and the other hooks
|
|
|
|
// have not.
|
|
|
|
//
|
|
|
|
// Stop hooks must be idempotent. The context is cancelled prematurely if the
|
|
|
|
// task is killed.
|
|
|
|
func (h *csiPluginSupervisorHook) Stop(_ context.Context, req *interfaces.TaskStopRequest, _ *interfaces.TaskStopResponse) error {
|
2022-02-23 20:23:07 +00:00
|
|
|
err := os.RemoveAll(h.socketMountPoint)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Error("could not remove plugin socket directory", "dir", h.socketMountPoint, "error", err)
|
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
h.shutdownCancelFn()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:57:29 +00:00
|
|
|
func (h *csiPluginSupervisorHook) kill(ctx context.Context, reason error) {
|
|
|
|
h.logger.Error("killing task because plugin failed", "error", reason)
|
|
|
|
event := structs.NewTaskEvent(structs.TaskPluginUnhealthy)
|
|
|
|
event.SetMessage(fmt.Sprintf("Error: %v", reason.Error()))
|
|
|
|
h.eventEmitter.EmitEvent(event)
|
|
|
|
|
|
|
|
if err := h.lifecycle.Kill(ctx,
|
|
|
|
structs.NewTaskEvent(structs.TaskKilling).
|
|
|
|
SetFailsTask().
|
2022-06-14 14:04:16 +00:00
|
|
|
SetDisplayMessage(fmt.Sprintf("CSI plugin did not become healthy before configured %v health timeout", h.task.CSIPluginConfig.HealthTimeout.String())),
|
2022-02-15 21:57:29 +00:00
|
|
|
); err != nil {
|
|
|
|
h.logger.Error("failed to kill task", "kill_reason", reason, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
func ensureMountpointInserted(mounts []*drivers.MountConfig, mount *drivers.MountConfig) []*drivers.MountConfig {
|
|
|
|
for _, mnt := range mounts {
|
|
|
|
if mnt.IsEqual(mount) {
|
|
|
|
return mounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts = append(mounts, mount)
|
|
|
|
return mounts
|
|
|
|
}
|