2019-12-16 13:19:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
2020-03-30 20:26:03 +00:00
|
|
|
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
|
2019-12-16 13:19:59 +00:00
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
2020-04-02 20:04:56 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
|
2019-12-16 13:19:59 +00:00
|
|
|
"github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/csi"
|
|
|
|
)
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
// CSI endpoint is used for interacting with CSI plugins on a client.
|
2019-12-16 13:19:59 +00:00
|
|
|
// TODO: Submit metrics with labels to allow debugging per plugin perf problems.
|
2020-04-02 20:04:56 +00:00
|
|
|
type CSI struct {
|
2019-12-16 13:19:59 +00:00
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CSIPluginRequestTimeout is the timeout that should be used when making reqs
|
|
|
|
// against CSI Plugins. It is copied from Kubernetes as an initial seed value.
|
|
|
|
// https://github.com/kubernetes/kubernetes/blob/e680ad7156f263a6d8129cc0117fda58602e50ad/pkg/volume/csi/csi_plugin.go#L52
|
|
|
|
CSIPluginRequestTimeout = 2 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrPluginTypeError = errors.New("CSI Plugin loaded incorrectly")
|
|
|
|
)
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
// ControllerValidateVolume is used during volume registration to validate
|
2020-02-18 16:08:38 +00:00
|
|
|
// that a volume exists and that the capabilities it was registered with are
|
|
|
|
// supported by the CSI Plugin and external volume configuration.
|
2020-04-02 20:04:56 +00:00
|
|
|
func (c *CSI) ControllerValidateVolume(req *structs.ClientCSIControllerValidateVolumeRequest, resp *structs.ClientCSIControllerValidateVolumeResponse) error {
|
2020-02-18 16:08:38 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "validate_volume"}, time.Now())
|
|
|
|
|
|
|
|
if req.VolumeID == "" {
|
|
|
|
return errors.New("VolumeID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.PluginID == "" {
|
|
|
|
return errors.New("PluginID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
2020-05-15 12:16:01 +00:00
|
|
|
csiReq, err := req.ToCSIRequest()
|
2020-02-18 16:08:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
2020-03-30 20:26:03 +00:00
|
|
|
|
|
|
|
// CSI ValidateVolumeCapabilities errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
2020-05-15 12:16:01 +00:00
|
|
|
return plugin.ControllerValidateCapabilities(ctx, csiReq,
|
2020-03-30 20:26:03 +00:00
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
2020-02-18 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
// ControllerAttachVolume is used to attach a volume from a CSI Cluster to
|
2019-12-16 13:19:59 +00:00
|
|
|
// the storage node provided in the request.
|
2019-12-17 11:37:33 +00:00
|
|
|
//
|
|
|
|
// The controller attachment flow currently works as follows:
|
|
|
|
// 1. Validate the volume request
|
|
|
|
// 2. Call ControllerPublishVolume on the CSI Plugin to trigger a remote attachment
|
|
|
|
//
|
2020-03-16 19:59:42 +00:00
|
|
|
// In the future this may be expanded to request dynamic secrets for attachment.
|
2020-04-02 20:04:56 +00:00
|
|
|
func (c *CSI) ControllerAttachVolume(req *structs.ClientCSIControllerAttachVolumeRequest, resp *structs.ClientCSIControllerAttachVolumeResponse) error {
|
2019-12-16 13:19:59 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "publish_volume"}, time.Now())
|
2020-02-21 10:32:10 +00:00
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
2019-12-16 13:19:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-17 11:38:07 +00:00
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
// The following block of validation checks should not be reached on a
|
|
|
|
// real Nomad cluster as all of this data should be validated when registering
|
|
|
|
// volumes with the cluster. They serve as a defensive check before forwarding
|
|
|
|
// requests to plugins, and to aid with development.
|
2019-12-16 13:19:59 +00:00
|
|
|
|
|
|
|
if req.VolumeID == "" {
|
|
|
|
return errors.New("VolumeID is required")
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:32:10 +00:00
|
|
|
if req.ClientCSINodeID == "" {
|
|
|
|
return errors.New("ClientCSINodeID is required")
|
2019-12-16 13:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 10:55:41 +00:00
|
|
|
csiReq, err := req.ToCSIRequest()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-12-17 11:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Submit the request for a volume to the CSI Plugin.
|
2019-12-16 13:19:59 +00:00
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
2020-03-30 20:26:03 +00:00
|
|
|
// CSI ControllerPublishVolume errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerPublishVolume(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
2019-12-16 13:19:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.PublishContext = cresp.PublishContext
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
// ControllerDetachVolume is used to detach a volume from a CSI Cluster from
|
2020-03-16 19:59:42 +00:00
|
|
|
// the storage node provided in the request.
|
2020-04-02 20:04:56 +00:00
|
|
|
func (c *CSI) ControllerDetachVolume(req *structs.ClientCSIControllerDetachVolumeRequest, resp *structs.ClientCSIControllerDetachVolumeResponse) error {
|
2020-03-16 19:59:42 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "unpublish_volume"}, time.Now())
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
// The following block of validation checks should not be reached on a
|
|
|
|
// real Nomad cluster as all of this data should be validated when registering
|
|
|
|
// volumes with the cluster. They serve as a defensive check before forwarding
|
|
|
|
// requests to plugins, and to aid with development.
|
|
|
|
|
|
|
|
if req.VolumeID == "" {
|
|
|
|
return errors.New("VolumeID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.ClientCSINodeID == "" {
|
|
|
|
return errors.New("ClientCSINodeID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
// Submit the request for a volume to the CSI Plugin.
|
2020-04-30 21:12:05 +00:00
|
|
|
ctx, cancelFn := c.requestContext()
|
2020-03-16 19:59:42 +00:00
|
|
|
defer cancelFn()
|
2020-03-30 20:26:03 +00:00
|
|
|
// CSI ControllerUnpublishVolume errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
_, err = plugin.ControllerUnpublishVolume(ctx, csiReq,
|
2020-04-30 21:12:05 +00:00
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
2020-03-30 20:26:03 +00:00
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
2020-03-16 19:59:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-21 10:32:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
// NodeDetachVolume is used to detach a volume from a CSI Cluster from
|
|
|
|
// the storage node provided in the request.
|
|
|
|
func (c *CSI) NodeDetachVolume(req *structs.ClientCSINodeDetachVolumeRequest, resp *structs.ClientCSINodeDetachVolumeResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_node", "detach_volume"}, time.Now())
|
|
|
|
|
|
|
|
// The following block of validation checks should not be reached on a
|
|
|
|
// real Nomad cluster. They serve as a defensive check before forwarding
|
|
|
|
// requests to plugins, and to aid with development.
|
|
|
|
if req.PluginID == "" {
|
|
|
|
return errors.New("PluginID is required")
|
|
|
|
}
|
|
|
|
if req.VolumeID == "" {
|
|
|
|
return errors.New("VolumeID is required")
|
|
|
|
}
|
|
|
|
if req.AllocID == "" {
|
|
|
|
return errors.New("AllocID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
mounter, err := c.c.csimanager.MounterForPlugin(ctx, req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
usageOpts := &csimanager.UsageOptions{
|
|
|
|
ReadOnly: req.ReadOnly,
|
|
|
|
AttachmentMode: string(req.AttachmentMode),
|
|
|
|
AccessMode: string(req.AccessMode),
|
|
|
|
}
|
|
|
|
|
2020-04-04 15:03:44 +00:00
|
|
|
err = mounter.UnmountVolume(ctx, req.VolumeID, req.ExternalID, req.AllocID, usageOpts)
|
2020-04-02 20:04:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSI) findControllerPlugin(name string) (csi.CSIPlugin, error) {
|
2019-12-16 13:19:59 +00:00
|
|
|
return c.findPlugin(dynamicplugins.PluginTypeCSIController, name)
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
func (c *CSI) findPlugin(ptype, name string) (csi.CSIPlugin, error) {
|
2019-12-16 13:19:59 +00:00
|
|
|
pIface, err := c.c.dynamicRegistry.DispensePlugin(ptype, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin, ok := pIface.(csi.CSIPlugin)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrPluginTypeError
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugin, nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
func (c *CSI) requestContext() (context.Context, context.CancelFunc) {
|
2019-12-16 13:19:59 +00:00
|
|
|
return context.WithTimeout(context.Background(), CSIPluginRequestTimeout)
|
|
|
|
}
|