Skip to content

Commit a5256a7

Browse files
authored
volumes: add task environment interpolation to volume_mount (#7364)
1 parent c2d5a7c commit a5256a7

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

client/allocrunner/taskrunner/volume_hook.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import (
77
log "github.com/hashicorp/go-hclog"
88
multierror "github.com/hashicorp/go-multierror"
99
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
10+
"github.com/hashicorp/nomad/client/taskenv"
1011
"github.com/hashicorp/nomad/nomad/structs"
1112
"github.com/hashicorp/nomad/plugins/drivers"
1213
)
1314

1415
type volumeHook struct {
15-
alloc *structs.Allocation
16-
runner *TaskRunner
17-
logger log.Logger
16+
alloc *structs.Allocation
17+
runner *TaskRunner
18+
logger log.Logger
19+
taskEnv *taskenv.TaskEnv
1820
}
1921

2022
func newVolumeHook(runner *TaskRunner, logger log.Logger) *volumeHook {
@@ -169,6 +171,9 @@ func (h *volumeHook) prepareCSIVolumes(req *interfaces.TaskPrestartRequest, volu
169171
}
170172

171173
func (h *volumeHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
174+
h.taskEnv = req.TaskEnv
175+
interpolateVolumeMounts(req.Task.VolumeMounts, h.taskEnv)
176+
172177
volumes := partitionVolumesByType(h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup).Volumes)
173178

174179
hostVolumeMounts, err := h.prepareHostVolumes(req, volumes[structs.VolumeTypeHost])
@@ -196,3 +201,11 @@ func (h *volumeHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartR
196201

197202
return nil
198203
}
204+
205+
func interpolateVolumeMounts(mounts []*structs.VolumeMount, taskEnv *taskenv.TaskEnv) {
206+
for _, mount := range mounts {
207+
mount.Volume = taskEnv.ReplaceEnv(mount.Volume)
208+
mount.Destination = taskEnv.ReplaceEnv(mount.Destination)
209+
mount.PropagationMode = taskEnv.ReplaceEnv(mount.PropagationMode)
210+
}
211+
}

client/allocrunner/taskrunner/volume_hook_test.go

+72-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
77
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
88
cstructs "github.com/hashicorp/nomad/client/structs"
9+
"github.com/hashicorp/nomad/client/taskenv"
910
"github.com/hashicorp/nomad/helper/testlog"
11+
"github.com/hashicorp/nomad/nomad/mock"
1012
"github.com/hashicorp/nomad/nomad/structs"
1113
"github.com/hashicorp/nomad/plugins/drivers"
1214
"github.com/stretchr/testify/require"
@@ -86,7 +88,7 @@ func TestVolumeHook_prepareCSIVolumes(t *testing.T) {
8688
tr := &TaskRunner{
8789
allocHookResources: &cstructs.AllocHookResources{
8890
CSIMounts: map[string]*csimanager.MountInfo{
89-
"foo": &csimanager.MountInfo{
91+
"foo": {
9092
Source: "/mnt/my-test-volume",
9193
},
9294
},
@@ -109,3 +111,72 @@ func TestVolumeHook_prepareCSIVolumes(t *testing.T) {
109111
require.NoError(t, err)
110112
require.Equal(t, expected, mounts)
111113
}
114+
115+
func TestVolumeHook_Interpolation(t *testing.T) {
116+
117+
alloc := mock.Alloc()
118+
task := alloc.Job.TaskGroups[0].Tasks[0]
119+
taskEnv := taskenv.NewBuilder(mock.Node(), alloc, task, "global").SetHookEnv("volume",
120+
map[string]string{
121+
"PROPAGATION_MODE": "private",
122+
"VOLUME_ID": "my-other-volume",
123+
},
124+
).Build()
125+
126+
mounts := []*structs.VolumeMount{
127+
{
128+
Volume: "foo",
129+
Destination: "/tmp",
130+
ReadOnly: false,
131+
PropagationMode: "bidirectional",
132+
},
133+
{
134+
Volume: "foo",
135+
Destination: "/bar-${NOMAD_JOB_NAME}",
136+
ReadOnly: false,
137+
PropagationMode: "bidirectional",
138+
},
139+
{
140+
Volume: "${VOLUME_ID}",
141+
Destination: "/baz",
142+
ReadOnly: false,
143+
PropagationMode: "bidirectional",
144+
},
145+
{
146+
Volume: "foo",
147+
Destination: "/quux",
148+
ReadOnly: false,
149+
PropagationMode: "${PROPAGATION_MODE}",
150+
},
151+
}
152+
153+
expected := []*structs.VolumeMount{
154+
{
155+
Volume: "foo",
156+
Destination: "/tmp",
157+
ReadOnly: false,
158+
PropagationMode: "bidirectional",
159+
},
160+
{
161+
Volume: "foo",
162+
Destination: "/bar-my-job",
163+
ReadOnly: false,
164+
PropagationMode: "bidirectional",
165+
},
166+
{
167+
Volume: "my-other-volume",
168+
Destination: "/baz",
169+
ReadOnly: false,
170+
PropagationMode: "bidirectional",
171+
},
172+
{
173+
Volume: "foo",
174+
Destination: "/quux",
175+
ReadOnly: false,
176+
PropagationMode: "private",
177+
},
178+
}
179+
180+
interpolateVolumeMounts(mounts, taskEnv)
181+
require.Equal(t, expected, mounts)
182+
}

0 commit comments

Comments
 (0)