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

cli: fix handling of scaling jobs which don't generate evals. #20479

Merged
merged 2 commits into from
Apr 30, 2024
Merged
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
3 changes: 3 additions & 0 deletions .changelog/20479.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fix handling of scaling jobs which don't generate evals
```
40 changes: 34 additions & 6 deletions command/job_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/nomad/api"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -127,13 +128,13 @@ func (j *JobScaleCommand) Run(args []string) int {
// Detail the job so we can perform addition checks before submitting the
// scaling request.
q := &api.QueryOptions{Namespace: namespace}
job, _, err := client.Jobs().ScaleStatus(jobID, q)
jobScaleStatusResp, _, err := client.Jobs().ScaleStatus(jobID, q)
if err != nil {
j.Ui.Error(fmt.Sprintf("Error querying job: %v", err))
return 1
}

if err := j.performGroupCheck(job.TaskGroups, &groupString); err != nil {
if err := j.performGroupCheck(jobScaleStatusResp.TaskGroups, &groupString); err != nil {
j.Ui.Error(err.Error())
return 1
}
Expand All @@ -155,9 +156,36 @@ func (j *JobScaleCommand) Run(args []string) int {
j.Colorize().Color(fmt.Sprintf("[bold][yellow]Job Warnings:\n%s[reset]\n", resp.Warnings)))
}

// If we are to detach, log the evaluation ID and exit.
if detach {
j.Ui.Output("Evaluation ID: " + resp.EvalID)
jobInfo, _, err := client.Jobs().Info(jobID, q)
if err != nil {
j.Ui.Error(fmt.Sprintf("Error looking up job: %s", err))
return 1
}

// Check if the job is periodic or is a parameterized job
isPeriodicJob := jobInfo.IsPeriodic()
isParameterizedJob := jobInfo.IsParameterized()
isMultiregionJob := jobInfo.IsMultiregion()

// Check if we should enter monitor mode
if detach || isPeriodicJob || isParameterizedJob || isMultiregionJob {
j.Ui.Output("Job scale successful")
if isPeriodicJob && !isParameterizedJob {
loc, err := jobInfo.Periodic.GetLocation()
if err == nil {
now := time.Now().In(loc)
next, err := jobInfo.Periodic.Next(now)
if err != nil {
j.Ui.Error(fmt.Sprintf("Error determining next launch time: %v", err))
} else {
j.Ui.Output(fmt.Sprintf("Approximate next launch time: %s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second)))
}
}
} else if !isParameterizedJob {
j.Ui.Output("Evaluation ID: " + resp.EvalID)
}

return 0
}

Expand All @@ -167,7 +195,7 @@ func (j *JobScaleCommand) Run(args []string) int {
length = fullId
}

// Create and monitor the evaluation.
// Detach was not specified, so start monitoring.
mon := newMonitor(j.Ui, client, length)
return mon.monitor(resp.EvalID)
}
Expand Down
44 changes: 41 additions & 3 deletions command/job_scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@ func TestJobScaleCommand_MultiGroup(t *testing.T) {
}
}

func TestJobScaleCommand_Parameterized(t *testing.T) {
ci.Parallel(t)

srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
testutil.WaitForResult(func() (bool, error) {
nodes, _, err := client.Nodes().List(nil)
if err != nil {
return false, err
}
if len(nodes) == 0 {
return false, fmt.Errorf("missing node")
}
if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
return false, fmt.Errorf("mock_driver not ready")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})

ui := cli.NewMockUi()
cmd := &JobScaleCommand{Meta: Meta{Ui: ui}}

// Create and register a parameterized job. The parent jobs do not create
// evaluations, even when scaled.
job := testJob("parameterized_job")
job.ParameterizedJob = &api.ParameterizedJobConfig{}

_, _, err := client.Jobs().Register(job, nil)
must.NoError(t, err)

// Scale the parent job and ensure the output matches what we expect.
code := cmd.Run([]string{"-address=" + url, "parameterized_job", "2"})
must.Eq(t, 0, code)
must.StrContains(t, ui.OutputWriter.String(), "Job scale successful")
}

func TestJobScaleCommand_ACL(t *testing.T) {
ci.Parallel(t)

Expand Down Expand Up @@ -175,7 +213,7 @@ namespace "default" {
capabilities = ["read-job-scaling", "scale-job"]
}
`,
expectedErr: "No evaluation with id",
expectedErr: "Error looking up job",
},
{
name: "read-job-scaling and submit-job allowed but can't monitor eval without read-job",
Expand All @@ -184,7 +222,7 @@ namespace "default" {
capabilities = ["read-job-scaling", "submit-job"]
}
`,
expectedErr: "No evaluation with id",
expectedErr: "Error looking up job",
},
{
name: "read-job-scaling and scale-job allowed and can monitor eval with read-job",
Expand Down Expand Up @@ -220,7 +258,7 @@ namespace "default" {
capabilities = ["read-job-scaling", "scale-job", "list-jobs"]
}
`,
expectedErr: "No evaluation with id",
expectedErr: "Error looking up job",
},
{
name: "job prefix works with list-job and can monitor eval with read-job",
Expand Down
Loading