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..3c4a32ddd --- /dev/null +++ b/levant/deploy_test.go @@ -0,0 +1,38 @@ +package levant + +import ( + "testing" + + "github.com/hashicorp/levant/levant/structs" + nomad "github.com/hashicorp/nomad/api" +) + +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) + } + } +}