backport of commit d2849b8a7678110f2c4643d5036ab55e6872770b (#19161)

Co-authored-by: Luiz Aoqui <luiz@hashicorp.com>
This commit is contained in:
hc-github-team-nomad-core 2023-11-23 15:15:10 -06:00 committed by GitHub
parent 17e7e0d330
commit 4701162b2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

3
.changelog/19155.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed a bug that caused the `nomad job restart` command to miscount the allocations to restart
```

View File

@ -633,6 +633,19 @@ func (c *JobRestartCommand) filterAllocs(stubs []AllocationListStubWithJob) []Al
continue
}
// Skip allocations that have already been replaced.
if stub.NextAllocation != "" {
if c.verbose {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[dark_gray] %s: Skipping allocation %q because it has already been replaced by %q[reset]",
formatTime(time.Now()),
shortAllocID,
limit(stub.NextAllocation, c.length),
)))
}
continue
}
// Skip allocations for groups that were not requested.
if c.groups.Size() > 0 {
if !c.groups.Contains(stub.TaskGroup) {

View File

@ -1245,6 +1245,21 @@ func TestJobRestartCommand_filterAllocs(t *testing.T) {
}
allocs[key] = alloc
allAllocs = append(allAllocs, alloc)
// Allocations with a replacement must always be skipped.
replacedAlloc := AllocationListStubWithJob{
AllocationListStub: &api.AllocationListStub{
ID: key,
JobVersion: *job.Version,
TaskGroup: *tg.Name,
DesiredStatus: desired,
ClientStatus: client,
NextAllocation: alloc.ID,
},
Job: job,
}
allocs[key+"_replaced"] = replacedAlloc
allAllocs = append(allAllocs, replacedAlloc)
}
}
}