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

client: always run alloc cleanup hooks on final update #15855

Merged
merged 2 commits into from
Jan 27, 2023
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/15477.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a bug where allocation cleanup hooks would not run
```
12 changes: 11 additions & 1 deletion client/allocrunner/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type allocRunner struct {
allocDir *allocdir.AllocDir

// runnerHooks are alloc runner lifecycle hooks that should be run on state
// transistions.
// transitions.
runnerHooks []interfaces.RunnerHook

// hookState is the output of allocrunner hooks
Expand Down Expand Up @@ -546,7 +546,9 @@ func (ar *allocRunner) handleTaskStateUpdates() {
}
}

// kill remaining live tasks
if len(liveRunners) > 0 {

// if all live runners are sidecars - kill alloc
onlySidecarsRemaining := hasSidecars && !hasNonSidecarTasks(liveRunners)
if killEvent == nil && onlySidecarsRemaining {
Expand Down Expand Up @@ -586,6 +588,14 @@ func (ar *allocRunner) handleTaskStateUpdates() {
}
}
} else {
// there are no live runners left

// run AR pre-kill hooks if this alloc is done, but not if it's because
// the agent is shutting down.
if !ar.isShuttingDown() && done {
ar.preKillHooks()
}

// If there are no live runners left kill all non-poststop task
// runners to unblock them from the alloc restart loop.
for _, tr := range ar.tasks {
Expand Down
1 change: 1 addition & 0 deletions client/allocrunner/alloc_runner_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (ar *allocRunner) destroy() error {
func (ar *allocRunner) preKillHooks() {
for _, hook := range ar.runnerHooks {
pre, ok := hook.(interfaces.RunnerPreKillHook)

if !ok {
continue
}
Expand Down
43 changes: 43 additions & 0 deletions client/allocrunner/alloc_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"

Expand All @@ -23,6 +24,8 @@ import (
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -2398,3 +2401,43 @@ func TestHasSidecarTasks(t *testing.T) {
})
}
}

type allocPreKillHook struct {
ran atomic.Bool
}

func (*allocPreKillHook) Name() string { return "test_prekill" }

func (h *allocPreKillHook) PreKill() {
h.ran.Store(true)
}

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

alloc := mock.Alloc()
task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "mock_driver"
task.Config = map[string]interface{}{"run_for": "2ms"}
alloc.DesiredStatus = "stop"

conf, cleanup := testAllocRunnerConfig(t, alloc.Copy())
t.Cleanup(cleanup)

ar, err := NewAllocRunner(conf)
must.NoError(t, err)

// set our custom prekill hook
hook := new(allocPreKillHook)
ar.runnerHooks = append(ar.runnerHooks, hook)

go ar.Run()
defer destroy(ar)

// wait for completion or timeout
must.Wait(t, wait.InitialSuccess(
wait.BoolFunc(hook.ran.Load),
wait.Timeout(5*time.Second),
wait.Gap(500*time.Millisecond),
))
}
1 change: 0 additions & 1 deletion client/allocrunner/alloc_runner_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package allocrunner

Expand Down