From fe7d565803054de4eab0205b092c8bde48ee957f Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Wed, 19 Oct 2016 11:12:25 -0700 Subject: [PATCH] Ensuring that ephemeral disk is present before deciding if we should migrate (#1827) --- nomad/structs/structs.go | 12 +++++++++++- nomad/structs/structs_test.go | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index b6d3e2581..ac2c48443 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -3074,7 +3074,17 @@ func (a *Allocation) ShouldMigrate() bool { return false } - if tg := a.Job.LookupTaskGroup(a.TaskGroup); tg != nil && !tg.EphemeralDisk.Migrate || !tg.EphemeralDisk.Sticky { + tg := a.Job.LookupTaskGroup(a.TaskGroup) + + // if the task group is nil or the ephemeral disk block isn't present then + // we won't migrate + if tg == nil || tg.EphemeralDisk == nil { + return false + } + + // We won't migrate any data is the user hasn't enabled migration or the + // disk is not marked as sticky + if !tg.EphemeralDisk.Migrate || !tg.EphemeralDisk.Sticky { return false } diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 7ce4cfc86..8475c7d94 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -1223,6 +1223,21 @@ func TestAllocation_ShouldMigrate(t *testing.T) { if alloc2.ShouldMigrate() { t.Fatalf("bad: %v", alloc) } + + alloc3 := Allocation{ + TaskGroup: "foo", + Job: &Job{ + TaskGroups: []*TaskGroup{ + { + Name: "foo", + }, + }, + }, + } + + if alloc3.ShouldMigrate() { + t.Fatalf("bad: %v", alloc) + } } func TestTaskArtifact_Validate_Checksum(t *testing.T) {