From 8d98facb08e15d468bb9b354b47cb2c229dc0b42 Mon Sep 17 00:00:00 2001 From: Fu Leow Date: Thu, 27 Aug 2020 13:20:31 -0700 Subject: [PATCH 1/2] check task groups for the update stanza if not defined at the job level --- levant/deploy.go | 22 +++++++++++++++++++++- levant/deploy_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 levant/deploy_test.go diff --git a/levant/deploy.go b/levant/deploy.go index 7e5047945..95587d5c9 100644 --- a/levant/deploy.go +++ b/levant/deploy.go @@ -158,7 +158,7 @@ func (l *levantDeployment) deploy() (success bool) { // If the service job doesn't have an update stanza, the job will not use // Nomad deployments. - if l.config.Template.Job.Update == nil { + if !l.hasUpdateStanza() { log.Info().Msg("levant/deploy: job is not configured with update stanza, consider adding to use deployments") return l.jobStatusChecker(&eval.EvalID) } @@ -519,3 +519,23 @@ func (l *levantDeployment) isJobZeroCount() bool { } return true } + +// hasUpdateStanza checks if the job has an update stanza at the job level or for all task groups +func (l *levantDeployment) hasUpdateStanza() (hasUpdate bool) { + if l.config.Template.Job.Update != nil { + hasUpdate = true + return + } + + // Check if all task groups have an update stanza + if l.config.Template.Job.TaskGroups != nil { + hasUpdate = true + for _, taskGroup := range l.config.Template.Job.TaskGroups { + if taskGroup.Update == nil { + hasUpdate = false + return + } + } + } + return +} diff --git a/levant/deploy_test.go b/levant/deploy_test.go new file mode 100644 index 000000000..9850a22ee --- /dev/null +++ b/levant/deploy_test.go @@ -0,0 +1,37 @@ +package levant + +import ( + "github.com/hashicorp/levant/levant/structs" + nomad "github.com/hashicorp/nomad/api" + "testing" +) + +func TestHasUpdateStanza(t *testing.T) { + ld1 := levantDeployment{config: &DeployConfig{Template: &structs.TemplateConfig{Job: &nomad.Job{ + Update: nil}}}} + ld2 := levantDeployment{config: &DeployConfig{Template: &structs.TemplateConfig{Job: &nomad.Job{ + Update: &nomad.UpdateStrategy{}}}}} + ld3 := levantDeployment{config: &DeployConfig{Template: &structs.TemplateConfig{Job: &nomad.Job{ + Update: nil, TaskGroups: []*nomad.TaskGroup{{Update: nil}}}}}} + ld4 := levantDeployment{config: &DeployConfig{Template: &structs.TemplateConfig{Job: &nomad.Job{ + Update: nil, TaskGroups: []*nomad.TaskGroup{{Update: &nomad.UpdateStrategy{}}}}}}} + ld5 := levantDeployment{config: &DeployConfig{Template: &structs.TemplateConfig{Job: &nomad.Job{ + Update: nil, TaskGroups: []*nomad.TaskGroup{{Update: nil}, {Update: &nomad.UpdateStrategy{}}}}}}} + + cases := []struct { + ld levantDeployment + expectedTrue bool + }{ + {ld1, false}, + {ld2, true}, + {ld3, false}, + {ld4, true}, + {ld5, false}, + } + + for _, c := range cases { + if c.ld.hasUpdateStanza() != c.expectedTrue { + t.Fatalf("expected hasUpdate to be %t, but got %t", c.ld.hasUpdateStanza(), c.expectedTrue) + } + } +} From dcd346b338bef684c1242a934a1c7793a345e485 Mon Sep 17 00:00:00 2001 From: Fu Leow Date: Thu, 27 Aug 2020 13:33:55 -0700 Subject: [PATCH 2/2] goimports --- levant/deploy_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/levant/deploy_test.go b/levant/deploy_test.go index 9850a22ee..3c4a32ddd 100644 --- a/levant/deploy_test.go +++ b/levant/deploy_test.go @@ -1,9 +1,10 @@ package levant import ( + "testing" + "github.com/hashicorp/levant/levant/structs" nomad "github.com/hashicorp/nomad/api" - "testing" ) func TestHasUpdateStanza(t *testing.T) {