Skip to content

Commit

Permalink
volumes: add task environment interpolation to volume_mount
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Mar 17, 2020
1 parent c2d5a7c commit cb50d12
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
19 changes: 16 additions & 3 deletions client/allocrunner/taskrunner/volume_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
log "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
)

type volumeHook struct {
alloc *structs.Allocation
runner *TaskRunner
logger log.Logger
alloc *structs.Allocation
runner *TaskRunner
logger log.Logger
taskEnv *taskenv.TaskEnv
}

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

func (h *volumeHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
h.taskEnv = req.TaskEnv
interpolateVolumeMounts(req.Task.VolumeMounts, h.taskEnv)

volumes := partitionVolumesByType(h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup).Volumes)

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

return nil
}

func interpolateVolumeMounts(mounts []*structs.VolumeMount, taskEnv *taskenv.TaskEnv) {
for _, mount := range mounts {
mount.Volume = taskEnv.ReplaceEnv(mount.Volume)
mount.Destination = taskEnv.ReplaceEnv(mount.Destination)
mount.PropagationMode = taskEnv.ReplaceEnv(mount.PropagationMode)
}
}
73 changes: 72 additions & 1 deletion client/allocrunner/taskrunner/volume_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -86,7 +88,7 @@ func TestVolumeHook_prepareCSIVolumes(t *testing.T) {
tr := &TaskRunner{
allocHookResources: &cstructs.AllocHookResources{
CSIMounts: map[string]*csimanager.MountInfo{
"foo": &csimanager.MountInfo{
"foo": {
Source: "/mnt/my-test-volume",
},
},
Expand All @@ -109,3 +111,72 @@ func TestVolumeHook_prepareCSIVolumes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, mounts)
}

func TestVolumeHook_Interpolation(t *testing.T) {

alloc := mock.Alloc()
task := alloc.Job.TaskGroups[0].Tasks[0]
taskEnv := taskenv.NewBuilder(mock.Node(), alloc, task, "global").SetHookEnv("volume",
map[string]string{
"PROPAGATION_MODE": "private",
"VOLUME_ID": "my-other-volume",
},
).Build()

mounts := []*structs.VolumeMount{
{
Volume: "foo",
Destination: "/tmp",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "foo",
Destination: "/bar-${NOMAD_JOB_NAME}",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "${VOLUME_ID}",
Destination: "/baz",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "foo",
Destination: "/quux",
ReadOnly: false,
PropagationMode: "${PROPAGATION_MODE}",
},
}

expected := []*structs.VolumeMount{
{
Volume: "foo",
Destination: "/tmp",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "foo",
Destination: "/bar-my-job",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "my-other-volume",
Destination: "/baz",
ReadOnly: false,
PropagationMode: "bidirectional",
},
{
Volume: "foo",
Destination: "/quux",
ReadOnly: false,
PropagationMode: "private",
},
}

interpolateVolumeMounts(mounts, taskEnv)
require.Equal(t, expected, mounts)
}

0 comments on commit cb50d12

Please sign in to comment.