2019-10-22 13:20:26 +00:00
|
|
|
package structs
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
import (
|
2022-03-07 16:06:59 +00:00
|
|
|
"errors"
|
2020-01-09 18:39:18 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-08-28 19:03:05 +00:00
|
|
|
|
2021-01-14 20:44:50 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2020-08-28 19:03:05 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2020-01-09 18:39:18 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// CSISocketName is the filename that Nomad expects plugins to create inside the
|
|
|
|
// PluginMountDir.
|
|
|
|
const CSISocketName = "csi.sock"
|
|
|
|
|
|
|
|
// CSIIntermediaryDirname is the name of the directory inside the PluginMountDir
|
|
|
|
// where Nomad will expect plugins to create intermediary mounts for volumes.
|
|
|
|
const CSIIntermediaryDirname = "volumes"
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
// VolumeTypeCSI is the type in the volume stanza of a TaskGroup
|
|
|
|
const VolumeTypeCSI = "csi"
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// CSIPluginType is an enum string that encapsulates the valid options for a
|
|
|
|
// CSIPlugin stanza's Type. These modes will allow the plugin to be used in
|
|
|
|
// different ways by the client.
|
|
|
|
type CSIPluginType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CSIPluginTypeNode indicates that Nomad should only use the plugin for
|
|
|
|
// performing Node RPCs against the provided plugin.
|
|
|
|
CSIPluginTypeNode CSIPluginType = "node"
|
|
|
|
|
|
|
|
// CSIPluginTypeController indicates that Nomad should only use the plugin for
|
|
|
|
// performing Controller RPCs against the provided plugin.
|
|
|
|
CSIPluginTypeController CSIPluginType = "controller"
|
|
|
|
|
|
|
|
// CSIPluginTypeMonolith indicates that Nomad can use the provided plugin for
|
|
|
|
// both controller and node rpcs.
|
|
|
|
CSIPluginTypeMonolith CSIPluginType = "monolith"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CSIPluginTypeIsValid validates the given CSIPluginType string and returns
|
|
|
|
// true only when a correct plugin type is specified.
|
|
|
|
func CSIPluginTypeIsValid(pt CSIPluginType) bool {
|
|
|
|
switch pt {
|
|
|
|
case CSIPluginTypeNode, CSIPluginTypeController, CSIPluginTypeMonolith:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskCSIPluginConfig contains the data that is required to setup a task as a
|
|
|
|
// CSI plugin. This will be used by the csi_plugin_supervisor_hook to configure
|
|
|
|
// mounts for the plugin and initiate the connection to the plugin catalog.
|
|
|
|
type TaskCSIPluginConfig struct {
|
|
|
|
// ID is the identifier of the plugin.
|
|
|
|
// Ideally this should be the FQDN of the plugin.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Type instructs Nomad on how to handle processing a plugin
|
|
|
|
Type CSIPluginType
|
|
|
|
|
|
|
|
// MountDir is the destination that nomad should mount in its CSI
|
|
|
|
// directory for the plugin. It will then expect a file called CSISocketName
|
|
|
|
// to be created by the plugin, and will provide references into
|
|
|
|
// "MountDir/CSIIntermediaryDirname/{VolumeName}/{AllocID} for mounts.
|
|
|
|
MountDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskCSIPluginConfig) Copy() *TaskCSIPluginConfig {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nt := new(TaskCSIPluginConfig)
|
|
|
|
*nt = *t
|
|
|
|
|
|
|
|
return nt
|
|
|
|
}
|
2020-01-09 18:39:18 +00:00
|
|
|
|
2021-03-22 13:43:30 +00:00
|
|
|
// CSIVolumeCapability is the requested attachment and access mode for a
|
|
|
|
// volume
|
|
|
|
type CSIVolumeCapability struct {
|
|
|
|
AttachmentMode CSIVolumeAttachmentMode
|
|
|
|
AccessMode CSIVolumeAccessMode
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
// CSIVolumeAttachmentMode chooses the type of storage api that will be used to
|
|
|
|
// interact with the device.
|
|
|
|
type CSIVolumeAttachmentMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
CSIVolumeAttachmentModeUnknown CSIVolumeAttachmentMode = ""
|
|
|
|
CSIVolumeAttachmentModeBlockDevice CSIVolumeAttachmentMode = "block-device"
|
|
|
|
CSIVolumeAttachmentModeFilesystem CSIVolumeAttachmentMode = "file-system"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ValidCSIVolumeAttachmentMode(attachmentMode CSIVolumeAttachmentMode) bool {
|
|
|
|
switch attachmentMode {
|
|
|
|
case CSIVolumeAttachmentModeBlockDevice, CSIVolumeAttachmentModeFilesystem:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSIVolumeAccessMode indicates how a volume should be used in a storage topology
|
|
|
|
// e.g whether the provider should make the volume available concurrently.
|
|
|
|
type CSIVolumeAccessMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
CSIVolumeAccessModeUnknown CSIVolumeAccessMode = ""
|
|
|
|
|
|
|
|
CSIVolumeAccessModeSingleNodeReader CSIVolumeAccessMode = "single-node-reader-only"
|
|
|
|
CSIVolumeAccessModeSingleNodeWriter CSIVolumeAccessMode = "single-node-writer"
|
|
|
|
|
|
|
|
CSIVolumeAccessModeMultiNodeReader CSIVolumeAccessMode = "multi-node-reader-only"
|
|
|
|
CSIVolumeAccessModeMultiNodeSingleWriter CSIVolumeAccessMode = "multi-node-single-writer"
|
|
|
|
CSIVolumeAccessModeMultiNodeMultiWriter CSIVolumeAccessMode = "multi-node-multi-writer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidCSIVolumeAccessMode checks to see that the provided access mode is a valid,
|
|
|
|
// non-empty access mode.
|
|
|
|
func ValidCSIVolumeAccessMode(accessMode CSIVolumeAccessMode) bool {
|
|
|
|
switch accessMode {
|
|
|
|
case CSIVolumeAccessModeSingleNodeReader, CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeReader, CSIVolumeAccessModeMultiNodeSingleWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeMultiWriter:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// ValidCSIVolumeWriteAccessMode checks for a writable access mode.
|
2020-01-09 18:39:18 +00:00
|
|
|
func ValidCSIVolumeWriteAccessMode(accessMode CSIVolumeAccessMode) bool {
|
|
|
|
switch accessMode {
|
|
|
|
case CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeSingleWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeMultiWriter:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:55:26 +00:00
|
|
|
// CSIMountOptions contain optional additional configuration that can be used
|
|
|
|
// when specifying that a Volume should be used with VolumeAccessTypeMount.
|
|
|
|
type CSIMountOptions struct {
|
|
|
|
// FSType is an optional field that allows an operator to specify the type
|
|
|
|
// of the filesystem.
|
|
|
|
FSType string
|
|
|
|
|
|
|
|
// MountFlags contains additional options that may be used when mounting the
|
|
|
|
// volume by the plugin. This may contain sensitive data and should not be
|
|
|
|
// leaked.
|
|
|
|
MountFlags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CSIMountOptions) Copy() *CSIMountOptions {
|
|
|
|
if o == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-28 19:03:05 +00:00
|
|
|
|
|
|
|
no := *o
|
|
|
|
no.MountFlags = helper.CopySliceString(o.MountFlags)
|
|
|
|
return &no
|
2020-03-23 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CSIMountOptions) Merge(p *CSIMountOptions) {
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.FSType != "" {
|
|
|
|
o.FSType = p.FSType
|
|
|
|
}
|
|
|
|
if p.MountFlags != nil {
|
|
|
|
o.MountFlags = p.MountFlags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:06:59 +00:00
|
|
|
func (o *CSIMountOptions) Equal(p *CSIMountOptions) bool {
|
|
|
|
if o == nil && p == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if o == nil || p == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.FSType != p.FSType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return helper.CompareSliceSetString(
|
|
|
|
o.MountFlags, p.MountFlags)
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:23:21 +00:00
|
|
|
// CSIMountOptions implements the Stringer and GoStringer interfaces to prevent
|
2020-03-23 17:55:26 +00:00
|
|
|
// accidental leakage of sensitive mount flags via logs.
|
|
|
|
var _ fmt.Stringer = &CSIMountOptions{}
|
|
|
|
var _ fmt.GoStringer = &CSIMountOptions{}
|
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
func (o *CSIMountOptions) String() string {
|
2020-03-23 17:55:26 +00:00
|
|
|
mountFlagsString := "nil"
|
2021-12-20 10:44:21 +00:00
|
|
|
if len(o.MountFlags) != 0 {
|
2020-03-23 17:55:26 +00:00
|
|
|
mountFlagsString = "[REDACTED]"
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
return fmt.Sprintf("csi.CSIOptions(FSType: %s, MountFlags: %s)", o.FSType, mountFlagsString)
|
2020-03-23 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
func (o *CSIMountOptions) GoString() string {
|
|
|
|
return o.String()
|
2020-03-23 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:12:51 +00:00
|
|
|
// CSISecrets contain optional additional configuration that can be used
|
|
|
|
// when specifying that a Volume should be used with VolumeAccessTypeMount.
|
|
|
|
type CSISecrets map[string]string
|
|
|
|
|
|
|
|
// CSISecrets implements the Stringer and GoStringer interfaces to prevent
|
|
|
|
// accidental leakage of secrets via logs.
|
|
|
|
var _ fmt.Stringer = &CSISecrets{}
|
|
|
|
var _ fmt.GoStringer = &CSISecrets{}
|
|
|
|
|
|
|
|
func (s *CSISecrets) String() string {
|
|
|
|
redacted := map[string]string{}
|
|
|
|
for k := range *s {
|
|
|
|
redacted[k] = "[REDACTED]"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("csi.CSISecrets(%v)", redacted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSISecrets) GoString() string {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:06:23 +00:00
|
|
|
type CSIVolumeClaim struct {
|
2020-08-06 17:51:29 +00:00
|
|
|
AllocationID string
|
|
|
|
NodeID string
|
|
|
|
ExternalNodeID string
|
|
|
|
Mode CSIVolumeClaimMode
|
2021-04-02 18:36:13 +00:00
|
|
|
AccessMode CSIVolumeAccessMode
|
|
|
|
AttachmentMode CSIVolumeAttachmentMode
|
2020-08-06 17:51:29 +00:00
|
|
|
State CSIVolumeClaimState
|
2020-04-23 15:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeClaimState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CSIVolumeClaimStateTaken CSIVolumeClaimState = iota
|
|
|
|
CSIVolumeClaimStateNodeDetached
|
|
|
|
CSIVolumeClaimStateControllerDetached
|
|
|
|
CSIVolumeClaimStateReadyToFree
|
2020-11-11 18:06:30 +00:00
|
|
|
CSIVolumeClaimStateUnpublishing
|
2020-04-23 15:06:23 +00:00
|
|
|
)
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// CSIVolume is the full representation of a CSI Volume
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolume struct {
|
2020-03-03 15:59:58 +00:00
|
|
|
// ID is a namespace unique URL safe identifier for the volume
|
|
|
|
ID string
|
|
|
|
// Name is a display name for the volume, not required to be unique
|
|
|
|
Name string
|
|
|
|
// ExternalID identifies the volume for the CSI interface, may be URL unsafe
|
2022-03-01 15:15:46 +00:00
|
|
|
ExternalID string
|
|
|
|
Namespace string
|
|
|
|
|
|
|
|
// RequestedTopologies are the topologies submitted as options to
|
|
|
|
// the storage provider at the time the volume was created. After
|
|
|
|
// volumes are created, this field is ignored.
|
|
|
|
RequestedTopologies *CSITopologyRequest
|
|
|
|
|
|
|
|
// Topologies are the topologies returned by the storage provider,
|
|
|
|
// based on the RequestedTopologies and what the storage provider
|
|
|
|
// could support. This value cannot be set by the user.
|
|
|
|
Topologies []*CSITopology
|
|
|
|
|
2021-04-02 18:36:13 +00:00
|
|
|
AccessMode CSIVolumeAccessMode // *current* access mode
|
|
|
|
AttachmentMode CSIVolumeAttachmentMode // *current* attachment mode
|
2020-03-23 17:55:26 +00:00
|
|
|
MountOptions *CSIMountOptions
|
2021-04-02 18:36:13 +00:00
|
|
|
|
|
|
|
Secrets CSISecrets
|
|
|
|
Parameters map[string]string
|
|
|
|
Context map[string]string
|
|
|
|
Capacity int64 // bytes
|
2021-03-22 13:43:30 +00:00
|
|
|
|
|
|
|
// These values are used only on volume creation but we record them
|
|
|
|
// so that we can diff the volume later
|
|
|
|
RequestedCapacityMin int64 // bytes
|
|
|
|
RequestedCapacityMax int64 // bytes
|
|
|
|
RequestedCapabilities []*CSIVolumeCapability
|
|
|
|
CloneID string
|
|
|
|
SnapshotID string
|
2020-01-09 18:39:18 +00:00
|
|
|
|
|
|
|
// Allocations, tracking claim status
|
2020-04-23 15:06:23 +00:00
|
|
|
ReadAllocs map[string]*Allocation // AllocID -> Allocation
|
|
|
|
WriteAllocs map[string]*Allocation // AllocID -> Allocation
|
|
|
|
|
|
|
|
ReadClaims map[string]*CSIVolumeClaim // AllocID -> claim
|
|
|
|
WriteClaims map[string]*CSIVolumeClaim // AllocID -> claim
|
|
|
|
PastClaims map[string]*CSIVolumeClaim // AllocID -> claim
|
2020-01-09 18:39:18 +00:00
|
|
|
|
2020-03-03 15:59:58 +00:00
|
|
|
// Schedulable is true if all the denormalized plugin health fields are true, and the
|
2020-01-09 18:39:18 +00:00
|
|
|
// volume has not been marked for garbage collection
|
2020-03-03 15:59:58 +00:00
|
|
|
Schedulable bool
|
2020-01-28 15:28:34 +00:00
|
|
|
PluginID string
|
2020-03-09 13:57:59 +00:00
|
|
|
Provider string
|
|
|
|
ProviderVersion string
|
2020-01-31 15:13:21 +00:00
|
|
|
ControllerRequired bool
|
2020-01-28 15:28:34 +00:00
|
|
|
ControllersHealthy int
|
|
|
|
ControllersExpected int
|
|
|
|
NodesHealthy int
|
|
|
|
NodesExpected int
|
|
|
|
ResourceExhausted time.Time
|
|
|
|
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// CSIVolListStub is partial representation of a CSI Volume for inclusion in lists
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolListStub struct {
|
2020-01-28 15:28:34 +00:00
|
|
|
ID string
|
|
|
|
Namespace string
|
2020-03-03 15:59:58 +00:00
|
|
|
Name string
|
|
|
|
ExternalID string
|
2020-01-28 15:28:34 +00:00
|
|
|
Topologies []*CSITopology
|
|
|
|
AccessMode CSIVolumeAccessMode
|
|
|
|
AttachmentMode CSIVolumeAttachmentMode
|
|
|
|
CurrentReaders int
|
|
|
|
CurrentWriters int
|
2020-03-03 15:59:58 +00:00
|
|
|
Schedulable bool
|
2020-01-28 15:28:34 +00:00
|
|
|
PluginID string
|
2020-03-09 13:57:59 +00:00
|
|
|
Provider string
|
2020-01-28 15:28:34 +00:00
|
|
|
ControllersHealthy int
|
|
|
|
ControllersExpected int
|
|
|
|
NodesHealthy int
|
|
|
|
NodesExpected int
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCSIVolume creates the volume struct. No side-effects
|
2020-03-26 13:43:59 +00:00
|
|
|
func NewCSIVolume(volumeID string, index uint64) *CSIVolume {
|
2020-01-28 15:28:34 +00:00
|
|
|
out := &CSIVolume{
|
2020-03-26 13:43:59 +00:00
|
|
|
ID: volumeID,
|
2020-02-03 16:59:00 +00:00
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out.newStructs()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *CSIVolume) newStructs() {
|
2020-11-18 15:59:25 +00:00
|
|
|
v.Topologies = []*CSITopology{}
|
|
|
|
v.MountOptions = new(CSIMountOptions)
|
|
|
|
v.Secrets = CSISecrets{}
|
|
|
|
v.Parameters = map[string]string{}
|
|
|
|
v.Context = map[string]string{}
|
2020-01-28 15:28:34 +00:00
|
|
|
|
|
|
|
v.ReadAllocs = map[string]*Allocation{}
|
|
|
|
v.WriteAllocs = map[string]*Allocation{}
|
2020-04-23 15:06:23 +00:00
|
|
|
v.ReadClaims = map[string]*CSIVolumeClaim{}
|
|
|
|
v.WriteClaims = map[string]*CSIVolumeClaim{}
|
|
|
|
v.PastClaims = map[string]*CSIVolumeClaim{}
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 19:08:19 +00:00
|
|
|
func (v *CSIVolume) RemoteID() string {
|
|
|
|
if v.ExternalID != "" {
|
|
|
|
return v.ExternalID
|
|
|
|
}
|
|
|
|
return v.ID
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
func (v *CSIVolume) Stub() *CSIVolListStub {
|
|
|
|
stub := CSIVolListStub{
|
2020-03-24 13:42:59 +00:00
|
|
|
ID: v.ID,
|
|
|
|
Namespace: v.Namespace,
|
|
|
|
Name: v.Name,
|
|
|
|
ExternalID: v.ExternalID,
|
|
|
|
Topologies: v.Topologies,
|
|
|
|
AccessMode: v.AccessMode,
|
|
|
|
AttachmentMode: v.AttachmentMode,
|
|
|
|
CurrentReaders: len(v.ReadAllocs),
|
|
|
|
CurrentWriters: len(v.WriteAllocs),
|
|
|
|
Schedulable: v.Schedulable,
|
|
|
|
PluginID: v.PluginID,
|
|
|
|
Provider: v.Provider,
|
|
|
|
ControllersHealthy: v.ControllersHealthy,
|
|
|
|
ControllersExpected: v.ControllersExpected,
|
|
|
|
NodesHealthy: v.NodesHealthy,
|
|
|
|
NodesExpected: v.NodesExpected,
|
|
|
|
CreateIndex: v.CreateIndex,
|
|
|
|
ModifyIndex: v.ModifyIndex,
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &stub
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:13:51 +00:00
|
|
|
// ReadSchedulable determines if the volume is potentially schedulable
|
|
|
|
// for reads, considering only the volume capabilities and plugin
|
|
|
|
// health
|
2020-03-24 01:21:04 +00:00
|
|
|
func (v *CSIVolume) ReadSchedulable() bool {
|
2020-03-03 15:59:58 +00:00
|
|
|
if !v.Schedulable {
|
2020-01-09 18:39:18 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.ResourceExhausted == time.Time{}
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:13:51 +00:00
|
|
|
// WriteSchedulable determines if the volume is potentially
|
|
|
|
// schedulable for writes, considering only volume capabilities and
|
|
|
|
// plugin health
|
2020-03-24 01:21:04 +00:00
|
|
|
func (v *CSIVolume) WriteSchedulable() bool {
|
2020-03-03 15:59:58 +00:00
|
|
|
if !v.Schedulable {
|
2020-01-09 18:39:18 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.AccessMode {
|
2021-04-02 18:36:13 +00:00
|
|
|
case CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeSingleWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeMultiWriter:
|
2020-01-09 18:39:18 +00:00
|
|
|
return v.ResourceExhausted == time.Time{}
|
2021-04-02 18:36:13 +00:00
|
|
|
|
|
|
|
case CSIVolumeAccessModeUnknown:
|
|
|
|
// this volume was created but not currently claimed, so we check what
|
|
|
|
// it's capable of, not what it's been previously assigned
|
|
|
|
for _, cap := range v.RequestedCapabilities {
|
|
|
|
switch cap.AccessMode {
|
|
|
|
case CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeSingleWriter,
|
|
|
|
CSIVolumeAccessModeMultiNodeMultiWriter:
|
|
|
|
return v.ResourceExhausted == time.Time{}
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
2021-04-02 18:36:13 +00:00
|
|
|
return false
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 14:37:37 +00:00
|
|
|
// HasFreeReadClaims determines if there are any free read claims available
|
|
|
|
func (v *CSIVolume) HasFreeReadClaims() bool {
|
|
|
|
switch v.AccessMode {
|
|
|
|
case CSIVolumeAccessModeSingleNodeReader:
|
|
|
|
return len(v.ReadClaims) == 0
|
|
|
|
case CSIVolumeAccessModeSingleNodeWriter:
|
|
|
|
return len(v.ReadClaims) == 0 && len(v.WriteClaims) == 0
|
|
|
|
case CSIVolumeAccessModeUnknown:
|
|
|
|
// This volume was created but not yet claimed, so its
|
|
|
|
// capabilities have been checked in ReadSchedulable
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
// For multi-node AccessModes, the CSI spec doesn't allow for
|
|
|
|
// setting a max number of readers we track node resource
|
|
|
|
// exhaustion through v.ResourceExhausted which is checked in
|
|
|
|
// ReadSchedulable
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:13:51 +00:00
|
|
|
// HasFreeWriteClaims determines if there are any free write claims available
|
|
|
|
func (v *CSIVolume) HasFreeWriteClaims() bool {
|
2020-03-24 01:21:04 +00:00
|
|
|
switch v.AccessMode {
|
2020-10-07 14:43:23 +00:00
|
|
|
case CSIVolumeAccessModeSingleNodeWriter, CSIVolumeAccessModeMultiNodeSingleWriter:
|
2020-11-18 15:59:25 +00:00
|
|
|
return len(v.WriteClaims) == 0
|
2020-10-07 14:43:23 +00:00
|
|
|
case CSIVolumeAccessModeMultiNodeMultiWriter:
|
|
|
|
// the CSI spec doesn't allow for setting a max number of writers.
|
|
|
|
// we track node resource exhaustion through v.ResourceExhausted
|
|
|
|
// which is checked in WriteSchedulable
|
|
|
|
return true
|
2021-04-02 18:36:13 +00:00
|
|
|
case CSIVolumeAccessModeUnknown:
|
2022-02-24 14:37:37 +00:00
|
|
|
// This volume was created but not yet claimed, so its
|
|
|
|
// capabilities have been checked in WriteSchedulable
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
// Reader modes never have free write claims
|
|
|
|
return false
|
2020-03-24 01:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:10:44 +00:00
|
|
|
// InUse tests whether any allocations are actively using the volume
|
|
|
|
func (v *CSIVolume) InUse() bool {
|
|
|
|
return len(v.ReadAllocs) != 0 ||
|
|
|
|
len(v.WriteAllocs) != 0
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// Copy returns a copy of the volume, which shares only the Topologies slice
|
2020-02-03 16:59:00 +00:00
|
|
|
func (v *CSIVolume) Copy() *CSIVolume {
|
2020-11-18 15:59:25 +00:00
|
|
|
out := new(CSIVolume)
|
|
|
|
*out = *v
|
|
|
|
out.newStructs() // zero-out the non-primitive structs
|
|
|
|
|
|
|
|
for _, t := range v.Topologies {
|
|
|
|
out.Topologies = append(out.Topologies, t.Copy())
|
|
|
|
}
|
|
|
|
if v.MountOptions != nil {
|
|
|
|
*out.MountOptions = *v.MountOptions
|
|
|
|
}
|
|
|
|
for k, v := range v.Secrets {
|
|
|
|
out.Secrets[k] = v
|
|
|
|
}
|
2020-05-15 12:16:01 +00:00
|
|
|
for k, v := range v.Parameters {
|
|
|
|
out.Parameters[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range v.Context {
|
|
|
|
out.Context[k] = v
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
|
2020-11-18 15:59:25 +00:00
|
|
|
for k, alloc := range v.ReadAllocs {
|
|
|
|
out.ReadAllocs[k] = alloc.Copy()
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
2020-11-18 15:59:25 +00:00
|
|
|
for k, alloc := range v.WriteAllocs {
|
|
|
|
out.WriteAllocs[k] = alloc.Copy()
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 15:06:23 +00:00
|
|
|
for k, v := range v.ReadClaims {
|
|
|
|
claim := *v
|
|
|
|
out.ReadClaims[k] = &claim
|
|
|
|
}
|
|
|
|
for k, v := range v.WriteClaims {
|
|
|
|
claim := *v
|
|
|
|
out.WriteClaims[k] = &claim
|
|
|
|
}
|
|
|
|
for k, v := range v.PastClaims {
|
|
|
|
claim := *v
|
|
|
|
out.PastClaims[k] = &claim
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Claim updates the allocations and changes the volume state
|
2020-04-23 15:06:23 +00:00
|
|
|
func (v *CSIVolume) Claim(claim *CSIVolumeClaim, alloc *Allocation) error {
|
2021-04-02 18:36:13 +00:00
|
|
|
// COMPAT: volumes registered prior to 1.1.0 will be missing caps for the
|
|
|
|
// volume on any claim. Correct this when we make the first change to a
|
|
|
|
// claim by setting its currently claimed capability as the only requested
|
|
|
|
// capability
|
|
|
|
if len(v.RequestedCapabilities) == 0 && v.AccessMode != "" && v.AttachmentMode != "" {
|
|
|
|
v.RequestedCapabilities = []*CSIVolumeCapability{
|
|
|
|
{
|
|
|
|
AccessMode: v.AccessMode,
|
|
|
|
AttachmentMode: v.AttachmentMode,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.AttachmentMode != CSIVolumeAttachmentModeUnknown &&
|
|
|
|
claim.AttachmentMode != CSIVolumeAttachmentModeUnknown &&
|
|
|
|
v.AttachmentMode != claim.AttachmentMode {
|
|
|
|
return fmt.Errorf("cannot change attachment mode of claimed volume")
|
|
|
|
}
|
2020-11-11 18:06:30 +00:00
|
|
|
|
|
|
|
if claim.State == CSIVolumeClaimStateTaken {
|
|
|
|
switch claim.Mode {
|
|
|
|
case CSIVolumeClaimRead:
|
2021-04-06 17:49:54 +00:00
|
|
|
return v.claimRead(claim, alloc)
|
2020-11-11 18:06:30 +00:00
|
|
|
case CSIVolumeClaimWrite:
|
2021-04-06 17:49:54 +00:00
|
|
|
return v.claimWrite(claim, alloc)
|
2020-11-11 18:06:30 +00:00
|
|
|
}
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
2020-11-11 18:06:30 +00:00
|
|
|
// either GC or a Unpublish checkpoint
|
2021-04-06 17:49:54 +00:00
|
|
|
return v.claimRelease(claim)
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:49:54 +00:00
|
|
|
// claimRead marks an allocation as using a volume read-only
|
|
|
|
func (v *CSIVolume) claimRead(claim *CSIVolumeClaim, alloc *Allocation) error {
|
2020-04-23 15:06:23 +00:00
|
|
|
if _, ok := v.ReadAllocs[claim.AllocationID]; ok {
|
2020-03-24 01:21:04 +00:00
|
|
|
return nil
|
2020-03-13 13:39:24 +00:00
|
|
|
}
|
2020-04-23 15:06:23 +00:00
|
|
|
if alloc == nil {
|
|
|
|
return fmt.Errorf("allocation missing: %s", claim.AllocationID)
|
|
|
|
}
|
2020-03-13 13:39:24 +00:00
|
|
|
|
2020-03-24 01:21:04 +00:00
|
|
|
if !v.ReadSchedulable() {
|
2022-02-23 16:13:51 +00:00
|
|
|
return ErrCSIVolumeUnschedulable
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
2020-03-24 01:21:04 +00:00
|
|
|
|
2022-02-24 14:37:37 +00:00
|
|
|
if !v.HasFreeReadClaims() {
|
|
|
|
return ErrCSIVolumeMaxClaims
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// Allocations are copy on write, so we want to keep the id but don't need the
|
|
|
|
// pointer. We'll get it from the db in denormalize.
|
2020-04-23 15:06:23 +00:00
|
|
|
v.ReadAllocs[claim.AllocationID] = nil
|
|
|
|
delete(v.WriteAllocs, claim.AllocationID)
|
|
|
|
|
|
|
|
v.ReadClaims[claim.AllocationID] = claim
|
|
|
|
delete(v.WriteClaims, claim.AllocationID)
|
|
|
|
delete(v.PastClaims, claim.AllocationID)
|
|
|
|
|
2021-04-02 18:36:13 +00:00
|
|
|
v.setModesFromClaim(claim)
|
2020-03-24 01:21:04 +00:00
|
|
|
return nil
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:49:54 +00:00
|
|
|
// claimWrite marks an allocation as using a volume as a writer
|
|
|
|
func (v *CSIVolume) claimWrite(claim *CSIVolumeClaim, alloc *Allocation) error {
|
2020-04-23 15:06:23 +00:00
|
|
|
if _, ok := v.WriteAllocs[claim.AllocationID]; ok {
|
2020-03-24 01:21:04 +00:00
|
|
|
return nil
|
2020-03-13 13:39:24 +00:00
|
|
|
}
|
2020-04-23 15:06:23 +00:00
|
|
|
if alloc == nil {
|
|
|
|
return fmt.Errorf("allocation missing: %s", claim.AllocationID)
|
|
|
|
}
|
2020-03-13 13:39:24 +00:00
|
|
|
|
2020-03-24 01:21:04 +00:00
|
|
|
if !v.WriteSchedulable() {
|
2022-02-23 16:13:51 +00:00
|
|
|
return ErrCSIVolumeUnschedulable
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
2020-03-24 01:21:04 +00:00
|
|
|
|
2022-02-23 16:13:51 +00:00
|
|
|
if !v.HasFreeWriteClaims() {
|
2022-02-24 14:37:37 +00:00
|
|
|
return ErrCSIVolumeMaxClaims
|
2020-03-24 01:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// Allocations are copy on write, so we want to keep the id but don't need the
|
|
|
|
// pointer. We'll get it from the db in denormalize.
|
|
|
|
v.WriteAllocs[alloc.ID] = nil
|
2020-01-09 18:39:18 +00:00
|
|
|
delete(v.ReadAllocs, alloc.ID)
|
2020-04-23 15:06:23 +00:00
|
|
|
|
|
|
|
v.WriteClaims[alloc.ID] = claim
|
|
|
|
delete(v.ReadClaims, alloc.ID)
|
|
|
|
delete(v.PastClaims, alloc.ID)
|
|
|
|
|
2021-04-02 18:36:13 +00:00
|
|
|
v.setModesFromClaim(claim)
|
2020-03-24 01:21:04 +00:00
|
|
|
return nil
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 18:36:13 +00:00
|
|
|
// setModesFromClaim sets the volume AttachmentMode and AccessMode based on
|
|
|
|
// the first claim we make. Originally the volume AccessMode and
|
|
|
|
// AttachmentMode were set during registration, but this is incorrect once we
|
|
|
|
// started creating volumes ourselves. But we still want these values for CLI
|
|
|
|
// and UI status.
|
|
|
|
func (v *CSIVolume) setModesFromClaim(claim *CSIVolumeClaim) {
|
|
|
|
if v.AttachmentMode == CSIVolumeAttachmentModeUnknown {
|
|
|
|
v.AttachmentMode = claim.AttachmentMode
|
|
|
|
}
|
|
|
|
if v.AccessMode == CSIVolumeAccessModeUnknown {
|
|
|
|
v.AccessMode = claim.AccessMode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 17:49:54 +00:00
|
|
|
// claimRelease is called when the allocation has terminated and
|
2020-04-23 15:06:23 +00:00
|
|
|
// already stopped using the volume
|
2021-04-06 17:49:54 +00:00
|
|
|
func (v *CSIVolume) claimRelease(claim *CSIVolumeClaim) error {
|
2020-04-23 15:06:23 +00:00
|
|
|
if claim.State == CSIVolumeClaimStateReadyToFree {
|
2020-04-30 21:11:31 +00:00
|
|
|
delete(v.ReadAllocs, claim.AllocationID)
|
|
|
|
delete(v.WriteAllocs, claim.AllocationID)
|
|
|
|
delete(v.ReadClaims, claim.AllocationID)
|
|
|
|
delete(v.WriteClaims, claim.AllocationID)
|
2020-04-23 15:06:23 +00:00
|
|
|
delete(v.PastClaims, claim.AllocationID)
|
2021-04-02 18:36:13 +00:00
|
|
|
|
|
|
|
// remove AccessMode/AttachmentMode if this is the last claim
|
|
|
|
if len(v.ReadClaims) == 0 && len(v.WriteClaims) == 0 && len(v.PastClaims) == 0 {
|
|
|
|
v.AccessMode = CSIVolumeAccessModeUnknown
|
|
|
|
v.AttachmentMode = CSIVolumeAttachmentModeUnknown
|
|
|
|
}
|
2020-04-23 15:06:23 +00:00
|
|
|
} else {
|
|
|
|
v.PastClaims[claim.AllocationID] = claim
|
2020-04-21 12:32:24 +00:00
|
|
|
}
|
2020-03-24 01:21:04 +00:00
|
|
|
return nil
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// Equal checks equality by value.
|
2020-01-09 18:39:18 +00:00
|
|
|
func (v *CSIVolume) Equal(o *CSIVolume) bool {
|
|
|
|
if v == nil || o == nil {
|
|
|
|
return v == o
|
|
|
|
}
|
|
|
|
|
|
|
|
// Omit the plugin health fields, their values are controlled by plugin jobs
|
|
|
|
if v.ID == o.ID &&
|
|
|
|
v.Namespace == o.Namespace &&
|
|
|
|
v.AccessMode == o.AccessMode &&
|
|
|
|
v.AttachmentMode == o.AttachmentMode &&
|
2020-01-28 15:28:34 +00:00
|
|
|
v.PluginID == o.PluginID {
|
2020-01-09 18:39:18 +00:00
|
|
|
// Setwise equality of topologies
|
|
|
|
var ok bool
|
|
|
|
for _, t := range v.Topologies {
|
|
|
|
ok = false
|
|
|
|
for _, u := range o.Topologies {
|
|
|
|
if t.Equal(u) {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the volume struct, returning all validation errors at once
|
|
|
|
func (v *CSIVolume) Validate() error {
|
|
|
|
errs := []string{}
|
|
|
|
|
|
|
|
if v.ID == "" {
|
|
|
|
errs = append(errs, "missing volume id")
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
if v.PluginID == "" {
|
|
|
|
errs = append(errs, "missing plugin id")
|
2020-01-09 18:39:18 +00:00
|
|
|
}
|
|
|
|
if v.Namespace == "" {
|
|
|
|
errs = append(errs, "missing namespace")
|
|
|
|
}
|
2021-03-22 13:43:30 +00:00
|
|
|
if v.SnapshotID != "" && v.CloneID != "" {
|
|
|
|
errs = append(errs, "only one of snapshot_id and clone_id is allowed")
|
|
|
|
}
|
2021-04-08 14:42:19 +00:00
|
|
|
if len(v.RequestedCapabilities) == 0 {
|
|
|
|
errs = append(errs, "must include at least one capability block")
|
|
|
|
}
|
2022-03-01 15:15:46 +00:00
|
|
|
if v.RequestedTopologies != nil {
|
|
|
|
for _, t := range v.RequestedTopologies.Required {
|
|
|
|
if t != nil && len(t.Segments) == 0 {
|
|
|
|
errs = append(errs, "required topology is missing segments field")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, t := range v.RequestedTopologies.Preferred {
|
|
|
|
if t != nil && len(t.Segments) == 0 {
|
|
|
|
errs = append(errs, "preferred topology is missing segments field")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 18:39:18 +00:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("validation: %s", strings.Join(errs, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:06:59 +00:00
|
|
|
// Merge updates the mutable fields of a volume with those from
|
|
|
|
// another volume. CSIVolume has many user-defined fields which are
|
|
|
|
// immutable once set, and many fields that are not
|
|
|
|
// user-settable. Merge will return an error if we try to mutate the
|
|
|
|
// user-defined immutable fields after they're set, but silently
|
|
|
|
// ignore fields that are controlled by Nomad.
|
|
|
|
func (v *CSIVolume) Merge(other *CSIVolume) error {
|
|
|
|
if other == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs *multierror.Error
|
|
|
|
|
|
|
|
if v.Name != other.Name && other.Name != "" {
|
|
|
|
errs = multierror.Append(errs, errors.New("volume name cannot be updated"))
|
|
|
|
}
|
|
|
|
if v.ExternalID != other.ExternalID && other.ExternalID != "" {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume external ID cannot be updated"))
|
|
|
|
}
|
|
|
|
if v.PluginID != other.PluginID {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume plugin ID cannot be updated"))
|
|
|
|
}
|
|
|
|
if v.CloneID != other.CloneID && other.CloneID != "" {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume clone ID cannot be updated"))
|
|
|
|
}
|
|
|
|
if v.SnapshotID != other.SnapshotID && other.SnapshotID != "" {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume snapshot ID cannot be updated"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be compatible with capacity range
|
|
|
|
// TODO: when ExpandVolume is implemented we'll need to update
|
|
|
|
// this logic https://github.com/hashicorp/nomad/issues/10324
|
|
|
|
if v.Capacity != 0 {
|
|
|
|
if other.RequestedCapacityMax < v.Capacity ||
|
|
|
|
other.RequestedCapacityMin > v.Capacity {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume requested capacity update was not compatible with existing capacity"))
|
|
|
|
} else {
|
|
|
|
v.RequestedCapacityMin = other.RequestedCapacityMin
|
|
|
|
v.RequestedCapacityMax = other.RequestedCapacityMax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be compatible with volume_capabilities
|
|
|
|
if v.AccessMode != CSIVolumeAccessModeUnknown ||
|
|
|
|
v.AttachmentMode != CSIVolumeAttachmentModeUnknown {
|
|
|
|
var ok bool
|
|
|
|
for _, cap := range other.RequestedCapabilities {
|
|
|
|
if cap.AccessMode == v.AccessMode &&
|
|
|
|
cap.AttachmentMode == v.AttachmentMode {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
v.RequestedCapabilities = other.RequestedCapabilities
|
|
|
|
} else {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume requested capabilities update was not compatible with existing capability in use"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.RequestedCapabilities = other.RequestedCapabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
// topologies are immutable, so topology request changes must be
|
|
|
|
// compatible with the existing topology, if any
|
|
|
|
if len(v.Topologies) > 0 {
|
|
|
|
if !v.RequestedTopologies.Equal(other.RequestedTopologies) {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume topology request update was not compatible with existing topology"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountOptions can be updated so long as the volume isn't in use,
|
|
|
|
// but the caller will reject updating an in-use volume
|
|
|
|
v.MountOptions = other.MountOptions
|
|
|
|
|
|
|
|
// Secrets can be updated freely
|
|
|
|
v.Secrets = other.Secrets
|
|
|
|
|
|
|
|
// must be compatible with parameters set by from CreateVolumeResponse
|
|
|
|
|
|
|
|
if len(other.Parameters) != 0 && !helper.CompareMapStringString(v.Parameters, other.Parameters) {
|
|
|
|
errs = multierror.Append(errs, errors.New(
|
|
|
|
"volume parameters cannot be updated"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context is mutable and will be used during controller
|
|
|
|
// validation
|
|
|
|
v.Context = other.Context
|
|
|
|
return errs.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
// Request and response wrappers
|
|
|
|
type CSIVolumeRegisterRequest struct {
|
|
|
|
Volumes []*CSIVolume
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeRegisterResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeDeregisterRequest struct {
|
|
|
|
VolumeIDs []string
|
2020-07-01 16:17:51 +00:00
|
|
|
Force bool
|
2020-01-09 18:39:18 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeDeregisterResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:43:30 +00:00
|
|
|
type CSIVolumeCreateRequest struct {
|
|
|
|
Volumes []*CSIVolume
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeCreateResponse struct {
|
|
|
|
Volumes []*CSIVolume
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeDeleteRequest struct {
|
|
|
|
VolumeIDs []string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeDeleteResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolumeClaimMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CSIVolumeClaimRead CSIVolumeClaimMode = iota
|
|
|
|
CSIVolumeClaimWrite
|
2020-11-11 18:06:30 +00:00
|
|
|
|
|
|
|
// for GC we don't have a specific claim to set the state on, so instead we
|
|
|
|
// create a new claim for GC in order to bump the ModifyIndex and trigger
|
|
|
|
// volumewatcher
|
|
|
|
CSIVolumeClaimGC
|
2020-01-09 18:39:18 +00:00
|
|
|
)
|
|
|
|
|
2020-04-30 13:13:00 +00:00
|
|
|
type CSIVolumeClaimBatchRequest struct {
|
|
|
|
Claims []CSIVolumeClaimRequest
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolumeClaimRequest struct {
|
2020-08-06 17:51:29 +00:00
|
|
|
VolumeID string
|
|
|
|
AllocationID string
|
|
|
|
NodeID string
|
|
|
|
ExternalNodeID string
|
|
|
|
Claim CSIVolumeClaimMode
|
2021-04-02 18:36:13 +00:00
|
|
|
AccessMode CSIVolumeAccessMode
|
|
|
|
AttachmentMode CSIVolumeAttachmentMode
|
2020-08-06 17:51:29 +00:00
|
|
|
State CSIVolumeClaimState
|
2020-01-09 18:39:18 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:06:23 +00:00
|
|
|
func (req *CSIVolumeClaimRequest) ToClaim() *CSIVolumeClaim {
|
|
|
|
return &CSIVolumeClaim{
|
2020-08-06 17:51:29 +00:00
|
|
|
AllocationID: req.AllocationID,
|
|
|
|
NodeID: req.NodeID,
|
|
|
|
ExternalNodeID: req.ExternalNodeID,
|
|
|
|
Mode: req.Claim,
|
2021-04-02 18:36:13 +00:00
|
|
|
AccessMode: req.AccessMode,
|
|
|
|
AttachmentMode: req.AttachmentMode,
|
2020-08-06 17:51:29 +00:00
|
|
|
State: req.State,
|
2020-04-23 15:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 13:00:00 +00:00
|
|
|
type CSIVolumeClaimResponse struct {
|
2020-02-10 21:32:40 +00:00
|
|
|
// Opaque static publish properties of the volume. SP MAY use this
|
|
|
|
// field to ensure subsequent `NodeStageVolume` or `NodePublishVolume`
|
|
|
|
// calls calls have contextual information.
|
|
|
|
// The contents of this field SHALL be opaque to nomad.
|
|
|
|
// The contents of this field SHALL NOT be mutable.
|
|
|
|
// The contents of this field SHALL be safe for the nomad to cache.
|
|
|
|
// The contents of this field SHOULD NOT contain sensitive
|
|
|
|
// information.
|
|
|
|
// The contents of this field SHOULD NOT be used for uniquely
|
|
|
|
// identifying a volume. The `volume_id` alone SHOULD be sufficient to
|
|
|
|
// identify the volume.
|
|
|
|
// This field is OPTIONAL and when present MUST be passed to
|
|
|
|
// `NodeStageVolume` or `NodePublishVolume` calls on the client
|
|
|
|
PublishContext map[string]string
|
2020-02-17 12:50:37 +00:00
|
|
|
|
|
|
|
// Volume contains the expanded CSIVolume for use on the client after a Claim
|
|
|
|
// has completed.
|
|
|
|
Volume *CSIVolume
|
|
|
|
|
2020-02-04 13:00:00 +00:00
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolumeListRequest struct {
|
2020-01-28 15:28:34 +00:00
|
|
|
PluginID string
|
2020-03-11 16:47:14 +00:00
|
|
|
NodeID string
|
2020-01-09 18:39:18 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeListResponse struct {
|
|
|
|
Volumes []*CSIVolListStub
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:43:30 +00:00
|
|
|
// CSIVolumeExternalListRequest is a request to a controller plugin to list
|
|
|
|
// all the volumes known to the the storage provider. This request is
|
2021-03-23 13:45:14 +00:00
|
|
|
// paginated by the plugin and accepts the QueryOptions.PerPage and
|
|
|
|
// QueryOptions.NextToken fields
|
2021-03-22 13:43:30 +00:00
|
|
|
type CSIVolumeExternalListRequest struct {
|
2021-03-23 13:45:14 +00:00
|
|
|
PluginID string
|
2021-03-22 13:43:30 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeExternalListResponse struct {
|
|
|
|
Volumes []*CSIVolumeExternalStub
|
|
|
|
NextToken string
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSIVolumeExternalStub is the storage provider's view of a volume, as
|
|
|
|
// returned from the controller plugin; all IDs are for external resources
|
|
|
|
type CSIVolumeExternalStub struct {
|
|
|
|
ExternalID string
|
|
|
|
CapacityBytes int64
|
|
|
|
VolumeContext map[string]string
|
|
|
|
CloneID string
|
|
|
|
SnapshotID string
|
|
|
|
|
|
|
|
PublishedExternalNodeIDs []string
|
|
|
|
IsAbnormal bool
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:39:18 +00:00
|
|
|
type CSIVolumeGetRequest struct {
|
|
|
|
ID string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeGetResponse struct {
|
|
|
|
Volume *CSIVolume
|
|
|
|
QueryMeta
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
|
2020-08-06 17:51:29 +00:00
|
|
|
type CSIVolumeUnpublishRequest struct {
|
|
|
|
VolumeID string
|
|
|
|
Claim *CSIVolumeClaim
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIVolumeUnpublishResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:16:52 +00:00
|
|
|
// CSISnapshot is the storage provider's view of a volume snapshot
|
|
|
|
type CSISnapshot struct {
|
|
|
|
// These fields map to those returned by the storage provider plugin
|
|
|
|
ID string // storage provider's ID
|
|
|
|
ExternalSourceVolumeID string // storage provider's ID for volume
|
|
|
|
SizeBytes int64
|
|
|
|
CreateTime int64
|
|
|
|
IsReady bool
|
|
|
|
|
|
|
|
// These fields are controlled by Nomad
|
|
|
|
SourceVolumeID string
|
|
|
|
PluginID string
|
|
|
|
|
|
|
|
// These field are only used during snapshot creation and will not be
|
|
|
|
// populated when the snapshot is returned
|
|
|
|
Name string
|
|
|
|
Secrets CSISecrets
|
|
|
|
Parameters map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSISnapshotCreateRequest struct {
|
|
|
|
Snapshots []*CSISnapshot
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSISnapshotCreateResponse struct {
|
|
|
|
Snapshots []*CSISnapshot
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSISnapshotDeleteRequest struct {
|
|
|
|
Snapshots []*CSISnapshot
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSISnapshotDeleteResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSISnapshotListRequest is a request to a controller plugin to list all the
|
|
|
|
// snapshot known to the the storage provider. This request is paginated by
|
|
|
|
// the plugin and accepts the QueryOptions.PerPage and QueryOptions.NextToken
|
|
|
|
// fields
|
|
|
|
type CSISnapshotListRequest struct {
|
|
|
|
PluginID string
|
2021-07-03 00:46:41 +00:00
|
|
|
Secrets CSISecrets
|
2021-04-01 15:16:52 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSISnapshotListResponse struct {
|
|
|
|
Snapshots []*CSISnapshot
|
|
|
|
NextToken string
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// CSIPlugin collects fingerprint info context for the plugin for clients
|
2020-01-28 15:28:34 +00:00
|
|
|
type CSIPlugin struct {
|
2020-02-21 19:48:16 +00:00
|
|
|
ID string
|
2020-03-09 13:57:59 +00:00
|
|
|
Provider string // the vendor name from CSI GetPluginInfoResponse
|
|
|
|
Version string // the vendor verson from CSI GetPluginInfoResponse
|
2020-02-21 19:48:16 +00:00
|
|
|
ControllerRequired bool
|
2020-01-28 15:28:34 +00:00
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// Map Node.IDs to fingerprint results, split by type. Monolith type plugins have
|
|
|
|
// both sets of fingerprinting results.
|
|
|
|
Controllers map[string]*CSIInfo
|
|
|
|
Nodes map[string]*CSIInfo
|
2020-01-28 15:28:34 +00:00
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// Allocations are populated by denormalize to show running allocations
|
|
|
|
Allocations []*AllocListStub
|
|
|
|
|
2020-08-27 21:20:00 +00:00
|
|
|
// Jobs are populated to by job update to support expected counts and the UI
|
|
|
|
ControllerJobs JobDescriptions
|
|
|
|
NodeJobs JobDescriptions
|
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// Cache the count of healthy plugins
|
2020-08-27 21:20:00 +00:00
|
|
|
ControllersHealthy int
|
|
|
|
ControllersExpected int
|
|
|
|
NodesHealthy int
|
|
|
|
NodesExpected int
|
2020-01-28 15:28:34 +00:00
|
|
|
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCSIPlugin creates the plugin struct. No side-effects
|
|
|
|
func NewCSIPlugin(id string, index uint64) *CSIPlugin {
|
|
|
|
out := &CSIPlugin{
|
|
|
|
ID: id,
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
}
|
|
|
|
|
|
|
|
out.newStructs()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CSIPlugin) newStructs() {
|
|
|
|
p.Controllers = map[string]*CSIInfo{}
|
|
|
|
p.Nodes = map[string]*CSIInfo{}
|
2020-08-27 21:20:00 +00:00
|
|
|
p.ControllerJobs = make(JobDescriptions)
|
|
|
|
p.NodeJobs = make(JobDescriptions)
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 16:59:00 +00:00
|
|
|
func (p *CSIPlugin) Copy() *CSIPlugin {
|
2020-01-28 15:28:34 +00:00
|
|
|
copy := *p
|
|
|
|
out := ©
|
|
|
|
out.newStructs()
|
|
|
|
|
|
|
|
for k, v := range p.Controllers {
|
2020-11-18 15:59:25 +00:00
|
|
|
out.Controllers[k] = v.Copy()
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range p.Nodes {
|
2020-11-18 15:59:25 +00:00
|
|
|
out.Nodes[k] = v.Copy()
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 21:20:00 +00:00
|
|
|
for k, v := range p.ControllerJobs {
|
2020-11-18 15:59:25 +00:00
|
|
|
out.ControllerJobs[k] = v.Copy()
|
2020-08-27 21:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range p.NodeJobs {
|
2020-11-18 15:59:25 +00:00
|
|
|
out.NodeJobs[k] = v.Copy()
|
2020-08-27 21:20:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:10:44 +00:00
|
|
|
type CSIControllerCapability byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CSIControllerSupportsCreateDelete indicates plugin support for
|
|
|
|
// CREATE_DELETE_VOLUME
|
|
|
|
CSIControllerSupportsCreateDelete CSIControllerCapability = 0
|
|
|
|
|
|
|
|
// CSIControllerSupportsAttachDetach is true when the controller
|
|
|
|
// implements the methods required to attach and detach volumes. If this
|
|
|
|
// is false Nomad should skip the controller attachment flow.
|
|
|
|
CSIControllerSupportsAttachDetach CSIControllerCapability = 1
|
|
|
|
|
|
|
|
// CSIControllerSupportsListVolumes is true when the controller implements
|
|
|
|
// the ListVolumes RPC. NOTE: This does not guarantee that attached nodes
|
|
|
|
// will be returned unless SupportsListVolumesAttachedNodes is also true.
|
|
|
|
CSIControllerSupportsListVolumes CSIControllerCapability = 2
|
|
|
|
|
|
|
|
// CSIControllerSupportsGetCapacity indicates plugin support for
|
|
|
|
// GET_CAPACITY
|
|
|
|
CSIControllerSupportsGetCapacity CSIControllerCapability = 3
|
|
|
|
|
|
|
|
// CSIControllerSupportsCreateDeleteSnapshot indicates plugin support for
|
|
|
|
// CREATE_DELETE_SNAPSHOT
|
|
|
|
CSIControllerSupportsCreateDeleteSnapshot CSIControllerCapability = 4
|
|
|
|
|
|
|
|
// CSIControllerSupportsListSnapshots indicates plugin support for
|
|
|
|
// LIST_SNAPSHOTS
|
|
|
|
CSIControllerSupportsListSnapshots CSIControllerCapability = 5
|
|
|
|
|
|
|
|
// CSIControllerSupportsClone indicates plugin support for CLONE_VOLUME
|
|
|
|
CSIControllerSupportsClone CSIControllerCapability = 6
|
|
|
|
|
|
|
|
// CSIControllerSupportsReadOnlyAttach is set to true when the controller
|
|
|
|
// returns the ATTACH_READONLY capability.
|
|
|
|
CSIControllerSupportsReadOnlyAttach CSIControllerCapability = 7
|
|
|
|
|
|
|
|
// CSIControllerSupportsExpand indicates plugin support for EXPAND_VOLUME
|
|
|
|
CSIControllerSupportsExpand CSIControllerCapability = 8
|
|
|
|
|
|
|
|
// CSIControllerSupportsListVolumesAttachedNodes indicates whether the
|
|
|
|
// plugin will return attached nodes data when making ListVolume RPCs
|
|
|
|
// (plugin support for LIST_VOLUMES_PUBLISHED_NODES)
|
|
|
|
CSIControllerSupportsListVolumesAttachedNodes CSIControllerCapability = 9
|
|
|
|
|
|
|
|
// CSIControllerSupportsCondition indicates plugin support for
|
|
|
|
// VOLUME_CONDITION
|
|
|
|
CSIControllerSupportsCondition CSIControllerCapability = 10
|
|
|
|
|
|
|
|
// CSIControllerSupportsGet indicates plugin support for GET_VOLUME
|
|
|
|
CSIControllerSupportsGet CSIControllerCapability = 11
|
|
|
|
)
|
|
|
|
|
2021-04-01 19:39:38 +00:00
|
|
|
type CSINodeCapability byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
// CSINodeSupportsStageVolume indicates whether the client should
|
|
|
|
// Stage/Unstage volumes on this node.
|
|
|
|
CSINodeSupportsStageVolume CSINodeCapability = 0
|
|
|
|
|
|
|
|
// CSINodeSupportsStats indicates plugin support for GET_VOLUME_STATS
|
|
|
|
CSINodeSupportsStats CSINodeCapability = 1
|
|
|
|
|
|
|
|
// CSINodeSupportsExpand indicates plugin support for EXPAND_VOLUME
|
|
|
|
CSINodeSupportsExpand CSINodeCapability = 2
|
|
|
|
|
|
|
|
// CSINodeSupportsCondition indicates plugin support for VOLUME_CONDITION
|
|
|
|
CSINodeSupportsCondition CSINodeCapability = 3
|
|
|
|
)
|
|
|
|
|
2021-03-24 18:10:44 +00:00
|
|
|
func (p *CSIPlugin) HasControllerCapability(cap CSIControllerCapability) bool {
|
|
|
|
if len(p.Controllers) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// we're picking the first controller because they should be uniform
|
|
|
|
// across the same version of the plugin
|
|
|
|
for _, c := range p.Controllers {
|
|
|
|
switch cap {
|
|
|
|
case CSIControllerSupportsCreateDelete:
|
|
|
|
return c.ControllerInfo.SupportsCreateDelete
|
|
|
|
case CSIControllerSupportsAttachDetach:
|
|
|
|
return c.ControllerInfo.SupportsAttachDetach
|
|
|
|
case CSIControllerSupportsListVolumes:
|
|
|
|
return c.ControllerInfo.SupportsListVolumes
|
|
|
|
case CSIControllerSupportsGetCapacity:
|
|
|
|
return c.ControllerInfo.SupportsGetCapacity
|
|
|
|
case CSIControllerSupportsCreateDeleteSnapshot:
|
|
|
|
return c.ControllerInfo.SupportsCreateDeleteSnapshot
|
|
|
|
case CSIControllerSupportsListSnapshots:
|
|
|
|
return c.ControllerInfo.SupportsListSnapshots
|
|
|
|
case CSIControllerSupportsClone:
|
|
|
|
return c.ControllerInfo.SupportsClone
|
|
|
|
case CSIControllerSupportsReadOnlyAttach:
|
|
|
|
return c.ControllerInfo.SupportsReadOnlyAttach
|
|
|
|
case CSIControllerSupportsExpand:
|
|
|
|
return c.ControllerInfo.SupportsExpand
|
|
|
|
case CSIControllerSupportsListVolumesAttachedNodes:
|
|
|
|
return c.ControllerInfo.SupportsListVolumesAttachedNodes
|
|
|
|
case CSIControllerSupportsCondition:
|
|
|
|
return c.ControllerInfo.SupportsCondition
|
|
|
|
case CSIControllerSupportsGet:
|
|
|
|
return c.ControllerInfo.SupportsGet
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:39:38 +00:00
|
|
|
func (p *CSIPlugin) HasNodeCapability(cap CSINodeCapability) bool {
|
|
|
|
if len(p.Nodes) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// we're picking the first node because they should be uniform
|
|
|
|
// across the same version of the plugin
|
|
|
|
for _, c := range p.Nodes {
|
|
|
|
switch cap {
|
|
|
|
case CSINodeSupportsStageVolume:
|
|
|
|
return c.NodeInfo.RequiresNodeStageVolume
|
|
|
|
case CSINodeSupportsStats:
|
|
|
|
return c.NodeInfo.SupportsStats
|
|
|
|
case CSINodeSupportsExpand:
|
|
|
|
return c.NodeInfo.SupportsExpand
|
|
|
|
case CSINodeSupportsCondition:
|
|
|
|
return c.NodeInfo.SupportsCondition
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// AddPlugin adds a single plugin running on the node. Called from state.NodeUpdate in a
|
|
|
|
// transaction
|
2020-03-26 21:07:18 +00:00
|
|
|
func (p *CSIPlugin) AddPlugin(nodeID string, info *CSIInfo) error {
|
2020-01-28 15:28:34 +00:00
|
|
|
if info.ControllerInfo != nil {
|
2021-03-24 18:10:44 +00:00
|
|
|
p.ControllerRequired = info.RequiresControllerPlugin
|
2020-01-28 15:28:34 +00:00
|
|
|
prev, ok := p.Controllers[nodeID]
|
2020-03-26 21:07:18 +00:00
|
|
|
if ok {
|
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing controller: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.Healthy {
|
|
|
|
p.ControllersHealthy -= 1
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
2020-05-05 19:39:57 +00:00
|
|
|
|
2020-05-05 14:30:50 +00:00
|
|
|
// note: for this to work as expected, only a single
|
|
|
|
// controller for a given plugin can be on a given Nomad
|
|
|
|
// client, they also conflict on the client so this should be
|
|
|
|
// ok
|
2020-05-05 19:39:57 +00:00
|
|
|
if prev != nil || info.Healthy {
|
|
|
|
p.Controllers[nodeID] = info
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
if info.Healthy {
|
|
|
|
p.ControllersHealthy += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.NodeInfo != nil {
|
|
|
|
prev, ok := p.Nodes[nodeID]
|
2020-03-26 21:07:18 +00:00
|
|
|
if ok {
|
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing node: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.Healthy {
|
|
|
|
p.NodesHealthy -= 1
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
2020-05-05 19:39:57 +00:00
|
|
|
if prev != nil || info.Healthy {
|
|
|
|
p.Nodes[nodeID] = info
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
if info.Healthy {
|
|
|
|
p.NodesHealthy += 1
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 21:07:18 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNode removes all plugins from the node. Called from state.DeleteNode in a
|
|
|
|
// transaction
|
2020-03-26 21:07:18 +00:00
|
|
|
func (p *CSIPlugin) DeleteNode(nodeID string) error {
|
|
|
|
return p.DeleteNodeForType(nodeID, CSIPluginTypeMonolith)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNodeForType deletes a client node from the list of controllers or node instance of
|
|
|
|
// a plugin. Called from deleteJobFromPlugin during job deregistration, in a transaction
|
|
|
|
func (p *CSIPlugin) DeleteNodeForType(nodeID string, pluginType CSIPluginType) error {
|
|
|
|
switch pluginType {
|
|
|
|
case CSIPluginTypeController:
|
2021-01-14 20:44:50 +00:00
|
|
|
if prev, ok := p.Controllers[nodeID]; ok {
|
2020-03-26 21:07:18 +00:00
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing controller: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.Healthy {
|
2021-01-14 20:44:50 +00:00
|
|
|
p.ControllersHealthy--
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
2021-01-14 20:44:50 +00:00
|
|
|
delete(p.Controllers, nodeID)
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case CSIPluginTypeNode:
|
2021-01-14 20:44:50 +00:00
|
|
|
if prev, ok := p.Nodes[nodeID]; ok {
|
2020-03-26 21:07:18 +00:00
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing node: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.Healthy {
|
2021-01-14 20:44:50 +00:00
|
|
|
p.NodesHealthy--
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
2021-01-14 20:44:50 +00:00
|
|
|
delete(p.Nodes, nodeID)
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case CSIPluginTypeMonolith:
|
2021-01-14 20:44:50 +00:00
|
|
|
var result error
|
|
|
|
|
|
|
|
err := p.DeleteNodeForType(nodeID, CSIPluginTypeController)
|
|
|
|
if err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.DeleteNodeForType(nodeID, CSIPluginTypeNode)
|
|
|
|
if err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAlloc removes the fingerprint info for the allocation
|
|
|
|
func (p *CSIPlugin) DeleteAlloc(allocID, nodeID string) error {
|
2020-01-28 15:28:34 +00:00
|
|
|
prev, ok := p.Controllers[nodeID]
|
2020-03-26 21:07:18 +00:00
|
|
|
if ok {
|
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing controller: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.AllocID == allocID {
|
|
|
|
if prev.Healthy {
|
|
|
|
p.ControllersHealthy -= 1
|
|
|
|
}
|
|
|
|
delete(p.Controllers, nodeID)
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev, ok = p.Nodes[nodeID]
|
2020-03-26 21:07:18 +00:00
|
|
|
if ok {
|
|
|
|
if prev == nil {
|
|
|
|
return fmt.Errorf("plugin missing node: %s", nodeID)
|
|
|
|
}
|
|
|
|
if prev.AllocID == allocID {
|
|
|
|
if prev.Healthy {
|
|
|
|
p.NodesHealthy -= 1
|
|
|
|
}
|
|
|
|
delete(p.Nodes, nodeID)
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
2020-03-26 21:07:18 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 21:20:00 +00:00
|
|
|
// AddJob adds a job to the plugin and increments expected
|
|
|
|
func (p *CSIPlugin) AddJob(job *Job, summary *JobSummary) {
|
|
|
|
p.UpdateExpectedWithJob(job, summary, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteJob removes the job from the plugin and decrements expected
|
|
|
|
func (p *CSIPlugin) DeleteJob(job *Job, summary *JobSummary) {
|
|
|
|
p.UpdateExpectedWithJob(job, summary, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateExpectedWithJob maintains the expected instance count
|
|
|
|
// we use the summary to add non-allocation expected counts
|
|
|
|
func (p *CSIPlugin) UpdateExpectedWithJob(job *Job, summary *JobSummary, terminal bool) {
|
|
|
|
var count int
|
|
|
|
|
|
|
|
for _, tg := range job.TaskGroups {
|
|
|
|
if job.Type == JobTypeSystem {
|
|
|
|
if summary == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := summary.Summary[tg.Name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
count = s.Running + s.Queued + s.Starting
|
|
|
|
} else {
|
|
|
|
count = tg.Count
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
if t.CSIPluginConfig == nil ||
|
|
|
|
t.CSIPluginConfig.ID != p.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the correct plugin expected, monolith should change both
|
|
|
|
if t.CSIPluginConfig.Type == CSIPluginTypeController ||
|
|
|
|
t.CSIPluginConfig.Type == CSIPluginTypeMonolith {
|
|
|
|
if terminal {
|
|
|
|
p.ControllerJobs.Delete(job)
|
|
|
|
} else {
|
|
|
|
p.ControllerJobs.Add(job, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.CSIPluginConfig.Type == CSIPluginTypeNode ||
|
|
|
|
t.CSIPluginConfig.Type == CSIPluginTypeMonolith {
|
|
|
|
if terminal {
|
|
|
|
p.NodeJobs.Delete(job)
|
|
|
|
} else {
|
|
|
|
p.NodeJobs.Add(job, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ControllersExpected = p.ControllerJobs.Count()
|
|
|
|
p.NodesExpected = p.NodeJobs.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobDescription records Job identification and the count of expected plugin instances
|
|
|
|
type JobDescription struct {
|
|
|
|
Namespace string
|
|
|
|
ID string
|
|
|
|
Expected int
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobNamespacedDescriptions maps Job.ID to JobDescription
|
|
|
|
type JobNamespacedDescriptions map[string]JobDescription
|
|
|
|
|
2020-11-18 15:59:25 +00:00
|
|
|
func (j JobNamespacedDescriptions) Copy() JobNamespacedDescriptions {
|
|
|
|
copy := JobNamespacedDescriptions{}
|
|
|
|
for k, v := range j {
|
|
|
|
copy[k] = v
|
|
|
|
}
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
2020-08-27 21:20:00 +00:00
|
|
|
// JobDescriptions maps Namespace to a mapping of Job.ID to JobDescription
|
|
|
|
type JobDescriptions map[string]JobNamespacedDescriptions
|
|
|
|
|
|
|
|
// Add the Job to the JobDescriptions, creating maps as necessary
|
|
|
|
func (j JobDescriptions) Add(job *Job, expected int) {
|
|
|
|
if j == nil {
|
|
|
|
j = make(JobDescriptions)
|
|
|
|
}
|
|
|
|
if j[job.Namespace] == nil {
|
|
|
|
j[job.Namespace] = make(JobNamespacedDescriptions)
|
|
|
|
}
|
|
|
|
j[job.Namespace][job.ID] = JobDescription{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
ID: job.ID,
|
|
|
|
Expected: expected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count the Expected instances for all JobDescriptions
|
|
|
|
func (j JobDescriptions) Count() int {
|
|
|
|
if j == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
count := 0
|
|
|
|
for _, jnd := range j {
|
|
|
|
for _, jd := range jnd {
|
|
|
|
count += jd.Expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the Job from the JobDescriptions
|
|
|
|
func (j JobDescriptions) Delete(job *Job) {
|
|
|
|
if j != nil &&
|
|
|
|
j[job.Namespace] != nil {
|
|
|
|
delete(j[job.Namespace], job.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
type CSIPluginListStub struct {
|
|
|
|
ID string
|
2020-03-09 13:57:59 +00:00
|
|
|
Provider string
|
2020-03-03 15:59:58 +00:00
|
|
|
ControllerRequired bool
|
2020-01-28 15:28:34 +00:00
|
|
|
ControllersHealthy int
|
|
|
|
ControllersExpected int
|
|
|
|
NodesHealthy int
|
|
|
|
NodesExpected int
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CSIPlugin) Stub() *CSIPluginListStub {
|
|
|
|
return &CSIPluginListStub{
|
|
|
|
ID: p.ID,
|
2020-03-09 13:57:59 +00:00
|
|
|
Provider: p.Provider,
|
2020-03-03 15:59:58 +00:00
|
|
|
ControllerRequired: p.ControllerRequired,
|
2020-01-28 15:28:34 +00:00
|
|
|
ControllersHealthy: p.ControllersHealthy,
|
2020-08-27 21:20:00 +00:00
|
|
|
ControllersExpected: p.ControllersExpected,
|
2020-01-28 15:28:34 +00:00
|
|
|
NodesHealthy: p.NodesHealthy,
|
2020-08-27 21:20:00 +00:00
|
|
|
NodesExpected: p.NodesExpected,
|
2020-01-28 15:28:34 +00:00
|
|
|
CreateIndex: p.CreateIndex,
|
|
|
|
ModifyIndex: p.ModifyIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CSIPlugin) IsEmpty() bool {
|
2020-08-27 21:20:00 +00:00
|
|
|
return p == nil ||
|
|
|
|
len(p.Controllers) == 0 &&
|
|
|
|
len(p.Nodes) == 0 &&
|
|
|
|
p.ControllerJobs.Count() == 0 &&
|
|
|
|
p.NodeJobs.Count() == 0
|
2020-01-28 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CSIPluginListRequest struct {
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIPluginListResponse struct {
|
|
|
|
Plugins []*CSIPluginListStub
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIPluginGetRequest struct {
|
|
|
|
ID string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIPluginGetResponse struct {
|
|
|
|
Plugin *CSIPlugin
|
|
|
|
QueryMeta
|
|
|
|
}
|
2020-05-06 20:49:12 +00:00
|
|
|
|
|
|
|
type CSIPluginDeleteRequest struct {
|
|
|
|
ID string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type CSIPluginDeleteResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|