2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-12-16 13:19:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-08-07 15:01:36 +00:00
|
|
|
"fmt"
|
2019-12-16 13:19:59 +00:00
|
|
|
"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"
|
2023-09-14 19:38:05 +00:00
|
|
|
|
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"
|
2020-08-07 15:01:36 +00:00
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
2019-12-16 13:19:59 +00:00
|
|
|
"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 == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerValidateVolume: VolumeID is required")
|
2020-02-18 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.PluginID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerValidateVolume: PluginID is required")
|
2020-02-18 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
2020-08-07 15:01:36 +00:00
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerValidateVolume: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
2020-02-18 16:08:38 +00:00
|
|
|
}
|
|
|
|
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 {
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerValidateVolume: %v", err)
|
2020-02-18 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-03-22 13:43:30 +00:00
|
|
|
err = 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)))
|
2021-03-22 13:43:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerValidateVolume: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
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 {
|
2020-08-07 15:01:36 +00:00
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerAttachVolume: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
2019-12-16 13:19:59 +00:00
|
|
|
}
|
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 == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerAttachVolume: VolumeID is required")
|
2019-12-16 13:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 10:32:10 +00:00
|
|
|
if req.ClientCSINodeID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerAttachVolume: 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 {
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerAttachVolume: %v", 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 {
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerAttachVolume: %v", err)
|
2019-12-16 13:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-08-07 15:01:36 +00:00
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerDetachVolume: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
2020-03-16 19:59:42 +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.
|
|
|
|
|
|
|
|
if req.VolumeID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerDetachVolume: VolumeID is required")
|
2020-03-16 19:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.ClientCSINodeID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.ControllerDetachVolume: ClientCSINodeID is required")
|
2020-03-16 19:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
2021-03-22 13:43:30 +00:00
|
|
|
if errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
// if the controller detach previously happened but the server failed to
|
|
|
|
// checkpoint, we'll get an error from the plugin but can safely ignore it.
|
2021-04-01 15:16:52 +00:00
|
|
|
c.c.logger.Debug("could not unpublish volume", "error", err)
|
2021-03-22 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-16 19:59:42 +00:00
|
|
|
if err != nil {
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.ControllerDetachVolume: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSI) ControllerCreateVolume(req *structs.ClientCSIControllerCreateVolumeRequest, resp *structs.ClientCSIControllerCreateVolumeResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "create_volume"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateVolume: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq, err := req.ToCSIRequest()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateVolume: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerCreateVolume errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerCreateVolume(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateVolume: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cresp == nil || cresp.Volume == nil {
|
|
|
|
c.c.logger.Warn("plugin did not return error or volume; this is a bug in the plugin and should be reported to the plugin author")
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateVolume: plugin did not return error or volume")
|
|
|
|
}
|
|
|
|
resp.ExternalVolumeID = cresp.Volume.ExternalVolumeID
|
|
|
|
resp.CapacityBytes = cresp.Volume.CapacityBytes
|
|
|
|
resp.VolumeContext = cresp.Volume.VolumeContext
|
|
|
|
|
2022-03-07 16:06:59 +00:00
|
|
|
// Note: we safely throw away cresp.Volume.ContentSource here
|
|
|
|
// because it's just round-tripping the value set by the user in
|
|
|
|
// the server RPC call
|
|
|
|
|
2022-03-01 15:15:46 +00:00
|
|
|
resp.Topologies = make([]*nstructs.CSITopology, len(cresp.Volume.AccessibleTopology))
|
|
|
|
for _, topo := range cresp.Volume.AccessibleTopology {
|
|
|
|
resp.Topologies = append(resp.Topologies,
|
|
|
|
&nstructs.CSITopology{Segments: topo.Segments})
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:38:05 +00:00
|
|
|
func (c *CSI) ControllerExpandVolume(req *structs.ClientCSIControllerExpandVolumeRequest, resp *structs.ClientCSIControllerExpandVolumeResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "expand_volume"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerExpandVolume could not find plugin: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerExpandVolume errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerExpandVolume(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
// if the volume was deleted out-of-band, we'll get an error from
|
|
|
|
// the plugin but can safely ignore it
|
|
|
|
c.c.logger.Debug("could not expand volume", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerExpandVolume: %v", err)
|
|
|
|
}
|
|
|
|
if cresp == nil {
|
|
|
|
c.c.logger.Warn("plugin did not return error or response; this is a bug in the plugin and should be reported to the plugin author")
|
|
|
|
return fmt.Errorf("CSI.ControllerExpandVolume: plugin did not return error or response")
|
|
|
|
}
|
|
|
|
resp.CapacityBytes = cresp.CapacityBytes
|
|
|
|
resp.NodeExpansionRequired = cresp.NodeExpansionRequired
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:43:30 +00:00
|
|
|
func (c *CSI) ControllerDeleteVolume(req *structs.ClientCSIControllerDeleteVolumeRequest, resp *structs.ClientCSIControllerDeleteVolumeResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "delete_volume"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerDeleteVolume: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerDeleteVolume errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
err = plugin.ControllerDeleteVolume(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
// if the volume was deleted out-of-band, we'll get an error from
|
|
|
|
// the plugin but can safely ignore it
|
2021-04-01 15:16:52 +00:00
|
|
|
c.c.logger.Debug("could not delete volume", "error", err)
|
2021-03-22 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerDeleteVolume: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSI) ControllerListVolumes(req *structs.ClientCSIControllerListVolumesRequest, resp *structs.ClientCSIControllerListVolumesResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "list_volumes"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerListVolumes: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerListVolumes errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerListVolumes(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerListVolumes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.NextToken = cresp.NextToken
|
|
|
|
resp.Entries = []*nstructs.CSIVolumeExternalStub{}
|
|
|
|
|
|
|
|
for _, entry := range cresp.Entries {
|
|
|
|
if entry.Volume == nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerListVolumes: plugin returned an invalid entry")
|
|
|
|
}
|
|
|
|
vol := &nstructs.CSIVolumeExternalStub{
|
|
|
|
ExternalID: entry.Volume.ExternalVolumeID,
|
|
|
|
CapacityBytes: entry.Volume.CapacityBytes,
|
|
|
|
VolumeContext: entry.Volume.VolumeContext,
|
|
|
|
CloneID: entry.Volume.ContentSource.CloneID,
|
|
|
|
SnapshotID: entry.Volume.ContentSource.SnapshotID,
|
|
|
|
}
|
|
|
|
if entry.Status != nil {
|
|
|
|
vol.PublishedExternalNodeIDs = entry.Status.PublishedNodeIds
|
|
|
|
vol.IsAbnormal = entry.Status.VolumeCondition.Abnormal
|
|
|
|
if entry.Status.VolumeCondition != nil {
|
|
|
|
vol.Status = entry.Status.VolumeCondition.Message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Entries = append(resp.Entries, vol)
|
|
|
|
if req.MaxEntries != 0 && int32(len(resp.Entries)) == req.MaxEntries {
|
|
|
|
break
|
2020-08-07 15:01:36 +00:00
|
|
|
}
|
2020-03-16 19:59:42 +00:00
|
|
|
}
|
2021-03-22 13:43:30 +00:00
|
|
|
|
2020-03-16 19:59:42 +00:00
|
|
|
return nil
|
2020-02-21 10:32:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 15:16:52 +00:00
|
|
|
func (c *CSI) ControllerCreateSnapshot(req *structs.ClientCSIControllerCreateSnapshotRequest, resp *structs.ClientCSIControllerCreateSnapshotResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "create_snapshot"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateSnapshot: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq, err := req.ToCSIRequest()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateSnapshot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerCreateSnapshot errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerCreateSnapshot(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateSnapshot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cresp == nil || cresp.Snapshot == nil {
|
|
|
|
c.c.logger.Warn("plugin did not return error or snapshot; this is a bug in the plugin and should be reported to the plugin author")
|
|
|
|
return fmt.Errorf("CSI.ControllerCreateSnapshot: plugin did not return error or snapshot")
|
|
|
|
}
|
|
|
|
resp.ID = cresp.Snapshot.ID
|
|
|
|
resp.ExternalSourceVolumeID = cresp.Snapshot.SourceVolumeID
|
|
|
|
resp.SizeBytes = cresp.Snapshot.SizeBytes
|
|
|
|
resp.CreateTime = cresp.Snapshot.CreateTime
|
|
|
|
resp.IsReady = cresp.Snapshot.IsReady
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSI) ControllerDeleteSnapshot(req *structs.ClientCSIControllerDeleteSnapshotRequest, resp *structs.ClientCSIControllerDeleteSnapshotResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "delete_snapshot"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerDeleteSnapshot: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerDeleteSnapshot errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
err = plugin.ControllerDeleteSnapshot(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
// if the snapshot was deleted out-of-band, we'll get an error from
|
|
|
|
// the plugin but can safely ignore it
|
|
|
|
c.c.logger.Debug("could not delete snapshot", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerDeleteSnapshot: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSI) ControllerListSnapshots(req *structs.ClientCSIControllerListSnapshotsRequest, resp *structs.ClientCSIControllerListSnapshotsResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_controller", "list_snapshots"}, time.Now())
|
|
|
|
|
|
|
|
plugin, err := c.findControllerPlugin(req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
// the server's view of the plugin health is stale, so let it know it
|
|
|
|
// should retry with another controller instance
|
|
|
|
return fmt.Errorf("CSI.ControllerListSnapshots: %w: %v",
|
|
|
|
nstructs.ErrCSIClientRPCRetryable, err)
|
|
|
|
}
|
|
|
|
defer plugin.Close()
|
|
|
|
|
|
|
|
csiReq := req.ToCSIRequest()
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
// CSI ControllerListSnapshots errors for timeout, codes.Unavailable and
|
|
|
|
// codes.ResourceExhausted are retried; all other errors are fatal.
|
|
|
|
cresp, err := plugin.ControllerListSnapshots(ctx, csiReq,
|
|
|
|
grpc_retry.WithPerRetryTimeout(CSIPluginRequestTimeout),
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100*time.Millisecond)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerListSnapshots: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.NextToken = cresp.NextToken
|
|
|
|
resp.Entries = []*nstructs.CSISnapshot{}
|
|
|
|
|
|
|
|
for _, entry := range cresp.Entries {
|
|
|
|
if entry.Snapshot == nil {
|
|
|
|
return fmt.Errorf("CSI.ControllerListSnapshot: plugin returned an invalid entry")
|
|
|
|
}
|
|
|
|
snap := &nstructs.CSISnapshot{
|
|
|
|
ID: entry.Snapshot.ID,
|
|
|
|
ExternalSourceVolumeID: entry.Snapshot.SourceVolumeID,
|
|
|
|
SizeBytes: entry.Snapshot.SizeBytes,
|
|
|
|
CreateTime: entry.Snapshot.CreateTime,
|
|
|
|
IsReady: entry.Snapshot.IsReady,
|
|
|
|
PluginID: req.PluginID,
|
|
|
|
}
|
|
|
|
resp.Entries = append(resp.Entries, snap)
|
|
|
|
if req.MaxEntries != 0 && int32(len(resp.Entries)) == req.MaxEntries {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.NodeDetachVolume: PluginID is required")
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
if req.VolumeID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.NodeDetachVolume: VolumeID is required")
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
if req.AllocID == "" {
|
2021-03-22 13:43:30 +00:00
|
|
|
return errors.New("CSI.NodeDetachVolume: AllocID is required")
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := c.requestContext()
|
|
|
|
defer cancelFn()
|
|
|
|
|
2023-09-08 22:10:25 +00:00
|
|
|
manager, err := c.c.csimanager.ManagerForPlugin(ctx, req.PluginID)
|
2020-04-02 20:04:56 +00:00
|
|
|
if err != nil {
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.NodeDetachVolume: %v", err)
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usageOpts := &csimanager.UsageOptions{
|
|
|
|
ReadOnly: req.ReadOnly,
|
2021-04-02 18:36:13 +00:00
|
|
|
AttachmentMode: req.AttachmentMode,
|
|
|
|
AccessMode: req.AccessMode,
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 22:10:25 +00:00
|
|
|
err = manager.UnmountVolume(ctx, req.VolumeID, req.ExternalID, req.AllocID, usageOpts)
|
2020-08-07 15:01:36 +00:00
|
|
|
if err != nil && !errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
// if the unmounting previously happened but the server failed to
|
|
|
|
// checkpoint, we'll get an error from Unmount but can safely
|
|
|
|
// ignore it.
|
2021-03-22 13:43:30 +00:00
|
|
|
return fmt.Errorf("CSI.NodeDetachVolume: %v", err)
|
2020-04-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:29:29 +00:00
|
|
|
// NodeExpandVolume instructs the node plugin to complete a volume expansion
|
|
|
|
// for a particular claim held by an allocation.
|
|
|
|
func (c *CSI) NodeExpandVolume(req *structs.ClientCSINodeExpandVolumeRequest, resp *structs.ClientCSINodeExpandVolumeResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "csi_node", "expand_volume"}, time.Now())
|
|
|
|
|
|
|
|
if err := req.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
usageOpts := &csimanager.UsageOptions{
|
|
|
|
// Claim will not be nil here, per req.Validate() above.
|
|
|
|
ReadOnly: req.Claim.Mode == nstructs.CSIVolumeClaimRead,
|
|
|
|
AttachmentMode: req.Claim.AttachmentMode,
|
|
|
|
AccessMode: req.Claim.AccessMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := c.requestContext() // note: this has a 2-minute timeout
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err := c.c.csimanager.WaitForPlugin(ctx, dynamicplugins.PluginTypeCSINode, req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager, err := c.c.csimanager.ManagerForPlugin(ctx, req.PluginID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newCapacity, err := manager.ExpandVolume(ctx,
|
|
|
|
req.VolumeID, req.ExternalID, req.Claim.AllocationID, usageOpts, req.Capacity)
|
|
|
|
|
|
|
|
if err != nil && !errors.Is(err, nstructs.ErrCSIClientRPCIgnorable) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.CapacityBytes = newCapacity
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:04:56 +00:00
|
|
|
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)
|
|
|
|
}
|