Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check task groups for the update stanza if not defined at the job level #355

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion levant/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
38 changes: 38 additions & 0 deletions levant/deploy_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}