From b821b3eab505c83453c2c3cb28862dd8af47cd6a Mon Sep 17 00:00:00 2001 From: Piotr Kazmierczak <470696+pkazmierczak@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:04:48 +0100 Subject: [PATCH] VolumeRequest and VolumeMount update --- api/host_volumes.go | 8 -------- nomad/structs/host_volumes.go | 8 -------- nomad/structs/volumes.go | 15 ++++++++++++--- .../structs/{volume_test.go => volumes_test.go} | 16 ++++++++++++++-- 4 files changed, 26 insertions(+), 21 deletions(-) rename nomad/structs/{volume_test.go => volumes_test.go} (93%) diff --git a/api/host_volumes.go b/api/host_volumes.go index f35a0d147ba..985695fa706 100644 --- a/api/host_volumes.go +++ b/api/host_volumes.go @@ -62,14 +62,6 @@ type HostVolume struct { // created. We record this to make debugging easier. HostPath string `mapstructure:"host_path" hcl:"host_path"` - // Sticky property specifies whether the scheduler should treat this volume - // as assigned to a particular allocation. If marked sticky, the ID of this - // volume will be added to an allocation that uses it during scheduling, - // and every time that allocation gets rescheduled it will only be on a - // node that has this Volume ID present, thus allowing stateful - // deployments. - Sticky bool - // State represents the overall state of the volume. One of pending, ready, // deleted. State HostVolumeState diff --git a/nomad/structs/host_volumes.go b/nomad/structs/host_volumes.go index 1d9f150a54d..2c8e6cf2372 100644 --- a/nomad/structs/host_volumes.go +++ b/nomad/structs/host_volumes.go @@ -70,14 +70,6 @@ type HostVolume struct { // created. We record this to make debugging easier. HostPath string - // Sticky property specifies whether the scheduler should treat this volume - // as assigned to a particular allocation. If marked sticky, the ID of this - // volume will be added to an allocation that uses it during scheduling, - // and every time that allocation gets rescheduled it will only be on a - // node that has this Volume ID present, thus allowing stateful - // deployments. - Sticky bool - // State represents the overall state of the volume. One of pending, ready, // deleted. State HostVolumeState diff --git a/nomad/structs/volumes.go b/nomad/structs/volumes.go index daacd5d8670..c3e5b8d7029 100644 --- a/nomad/structs/volumes.go +++ b/nomad/structs/volumes.go @@ -91,12 +91,14 @@ func HostVolumeSliceMerge(a, b []*ClientHostVolumeConfig) []*ClientHostVolumeCon return n } -// VolumeRequest is a representation of a storage volume that a TaskGroup wishes to use. +// VolumeRequest is a representation of a storage volume that a TaskGroup wishes +// to use. type VolumeRequest struct { Name string Type string Source string ReadOnly bool + Sticky bool AccessMode CSIVolumeAccessMode AttachmentMode CSIVolumeAttachmentMode MountOptions *CSIMountOptions @@ -116,6 +118,8 @@ func (v *VolumeRequest) Equal(o *VolumeRequest) bool { return false case v.ReadOnly != o.ReadOnly: return false + case v.Sticky != o.Sticky: + return false case v.AccessMode != o.AccessMode: return false case v.AttachmentMode != o.AttachmentMode: @@ -129,8 +133,7 @@ func (v *VolumeRequest) Equal(o *VolumeRequest) bool { } func (v *VolumeRequest) Validate(jobType string, taskGroupCount, canaries int) error { - if !(v.Type == VolumeTypeHost || - v.Type == VolumeTypeCSI) { + if !(v.Type == VolumeTypeHost || v.Type == VolumeTypeCSI) { return fmt.Errorf("volume has unrecognized type %s", v.Type) } @@ -165,6 +168,9 @@ func (v *VolumeRequest) Validate(jobType string, taskGroupCount, canaries int) e } case VolumeTypeCSI: + if v.Sticky { + addErr("CSI volumes cannot be set to sticky") + } switch v.AttachmentMode { case CSIVolumeAttachmentModeUnknown: @@ -247,6 +253,7 @@ type VolumeMount struct { Volume string Destination string ReadOnly bool + Sticky bool PropagationMode string SELinuxLabel string } @@ -267,6 +274,8 @@ func (v *VolumeMount) Equal(o *VolumeMount) bool { return false case v.ReadOnly != o.ReadOnly: return false + case v.Sticky != o.Sticky: + return false case v.PropagationMode != o.PropagationMode: return false case v.SELinuxLabel != o.SELinuxLabel: diff --git a/nomad/structs/volume_test.go b/nomad/structs/volumes_test.go similarity index 93% rename from nomad/structs/volume_test.go rename to nomad/structs/volumes_test.go index 02e0715d1a3..1b7efc01d7b 100644 --- a/nomad/structs/volume_test.go +++ b/nomad/structs/volumes_test.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/nomad/ci" "github.com/shoenig/test/must" - "github.com/stretchr/testify/require" ) func TestVolumeRequest_Validate(t *testing.T) { @@ -86,13 +85,26 @@ func TestVolumeRequest_Validate(t *testing.T) { PerAlloc: true, }, }, + { + name: "Sticky CSI", + expected: []string{ + "CSI volumes cannot be set to sticky", + }, + req: &VolumeRequest{ + Source: "source", + Type: VolumeTypeCSI, + Sticky: true, + AttachmentMode: CSIVolumeAttachmentModeBlockDevice, + AccessMode: CSIVolumeAccessModeMultiNodeMultiWriter, + }, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { err := tc.req.Validate(JobTypeSystem, tc.taskGroupCount, tc.canariesCount) for _, expected := range tc.expected { - require.Contains(t, err.Error(), expected) + must.StrContains(t, err.Error(), expected) } }) }