diff --git a/.changelog/20049.txt b/.changelog/20049.txt new file mode 100644 index 000000000..2f04b6b0c --- /dev/null +++ b/.changelog/20049.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: Fixed a bug where the `nomad job restart` command could crash if the job type was not present in a response from the server +``` diff --git a/command/job_restart.go b/command/job_restart.go index f7e1d3db5..102cd9874 100644 --- a/command/job_restart.go +++ b/command/job_restart.go @@ -965,7 +965,7 @@ func (c *JobRestartCommand) stopAlloc(alloc AllocationListStubWithJob) error { // Allocations for system jobs do not get replaced by the scheduler after // being stopped, so an eval is needed to trigger the reconciler. - if *alloc.Job.Type == api.JobTypeSystem { + if alloc.isSystemJob() { opts := api.EvalOptions{ ForceReschedule: true, } @@ -1241,3 +1241,9 @@ func (a *AllocationListStubWithJob) IsRunning() bool { return a.ClientStatus == api.AllocClientStatusRunning || a.DesiredStatus == api.AllocDesiredStatusRun } + +// isSystemJob returns true if allocation's job type +// is "system", false otherwise +func (a *AllocationListStubWithJob) isSystemJob() bool { + return a.Job != nil && a.Job.Type != nil && *a.Job.Type == api.JobTypeSystem +}