2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-10-01 13:59:55 +00:00
|
|
|
//go:build !windows
|
2020-01-30 22:16:45 +00:00
|
|
|
// +build !windows
|
2021-10-01 13:59:55 +00:00
|
|
|
|
2020-01-30 22:16:45 +00:00
|
|
|
// todo(shoenig): Once Connect is supported on Windows, we'll need to make this
|
|
|
|
// set of tests work there too.
|
|
|
|
|
2019-11-27 21:41:45 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2020-01-30 21:33:20 +00:00
|
|
|
"path/filepath"
|
2019-11-27 21:41:45 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-11-27 21:41:45 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2020-01-30 22:16:45 +00:00
|
|
|
consulapi "github.com/hashicorp/nomad/client/consul"
|
2019-11-27 21:41:45 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2020-01-15 15:29:47 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2020-01-30 22:16:45 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2019-11-27 21:41:45 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-30 22:16:45 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-11-27 21:41:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ interfaces.TaskPrestartHook = (*sidsHook)(nil)
|
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
func sidecar(task string) (string, structs.TaskKind) {
|
|
|
|
name := structs.ConnectProxyPrefix + "-" + task
|
|
|
|
kind := structs.TaskKind(structs.ConnectProxyPrefix + ":" + task)
|
|
|
|
return name, kind
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:41:45 +00:00
|
|
|
func TestSIDSHook_recoverToken(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-27 21:41:45 +00:00
|
|
|
r := require.New(t)
|
2020-01-15 15:56:48 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2019-11-27 21:41:45 +00:00
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
taskName, taskKind := sidecar("foo")
|
2019-11-27 21:41:45 +00:00
|
|
|
h := newSIDSHook(sidsHookConfig{
|
2019-12-18 16:23:16 +00:00
|
|
|
task: &structs.Task{
|
|
|
|
Name: taskName,
|
|
|
|
Kind: taskKind,
|
|
|
|
},
|
2019-11-27 21:41:45 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
|
|
|
})
|
|
|
|
|
2020-01-15 15:29:47 +00:00
|
|
|
expected := uuid.Generate()
|
2019-11-27 21:41:45 +00:00
|
|
|
err := h.writeToken(secrets, expected)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
token, err := h.recoverToken(secrets)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(expected, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIDSHook_recoverToken_empty(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-27 21:41:45 +00:00
|
|
|
r := require.New(t)
|
2020-01-15 15:56:48 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2019-11-27 21:41:45 +00:00
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
taskName, taskKind := sidecar("foo")
|
2019-11-27 21:41:45 +00:00
|
|
|
h := newSIDSHook(sidsHookConfig{
|
2019-12-18 16:23:16 +00:00
|
|
|
task: &structs.Task{
|
|
|
|
Name: taskName,
|
|
|
|
Kind: taskKind,
|
|
|
|
},
|
2019-11-27 21:41:45 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
|
|
|
})
|
|
|
|
|
|
|
|
token, err := h.recoverToken(secrets)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Empty(token)
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:33:20 +00:00
|
|
|
func TestSIDSHook_recoverToken_unReadable(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 22:16:45 +00:00
|
|
|
// This test fails when running as root because the test case for checking
|
|
|
|
// the error condition when the file is unreadable fails (root can read the
|
|
|
|
// file even though the permissions are set to 0200).
|
|
|
|
if unix.Geteuid() == 0 {
|
|
|
|
t.Skip("test only works as non-root")
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:41:45 +00:00
|
|
|
r := require.New(t)
|
2020-01-15 15:56:48 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2019-11-27 21:41:45 +00:00
|
|
|
|
2020-01-30 21:33:20 +00:00
|
|
|
err := os.Chmod(secrets, 0000)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
taskName, taskKind := sidecar("foo")
|
|
|
|
h := newSIDSHook(sidsHookConfig{
|
|
|
|
task: &structs.Task{
|
|
|
|
Name: taskName,
|
|
|
|
Kind: taskKind,
|
|
|
|
},
|
|
|
|
logger: testlog.HCLogger(t),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = h.recoverToken(secrets)
|
|
|
|
r.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIDSHook_writeToken(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 21:33:20 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2020-01-30 21:33:20 +00:00
|
|
|
|
|
|
|
id := uuid.Generate()
|
|
|
|
h := new(sidsHook)
|
|
|
|
err := h.writeToken(secrets, id)
|
|
|
|
r.NoError(err)
|
|
|
|
|
2023-03-08 19:25:10 +00:00
|
|
|
content, err := os.ReadFile(filepath.Join(secrets, sidsTokenFile))
|
2020-01-30 21:33:20 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(id, string(content))
|
|
|
|
}
|
2019-12-18 16:23:16 +00:00
|
|
|
|
2020-01-30 21:33:20 +00:00
|
|
|
func TestSIDSHook_writeToken_unWritable(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 22:16:45 +00:00
|
|
|
// This test fails when running as root because the test case for checking
|
|
|
|
// the error condition when the file is unreadable fails (root can read the
|
|
|
|
// file even though the permissions are set to 0200).
|
|
|
|
if unix.Geteuid() == 0 {
|
|
|
|
t.Skip("test only works as non-root")
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:33:20 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2020-01-30 21:33:20 +00:00
|
|
|
|
|
|
|
err := os.Chmod(secrets, 0000)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
id := uuid.Generate()
|
|
|
|
h := new(sidsHook)
|
|
|
|
err = h.writeToken(secrets, id)
|
|
|
|
r.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_SIDSHook_writeToken_nonExistent(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 21:33:20 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
base := t.TempDir()
|
2020-01-30 21:33:20 +00:00
|
|
|
secrets := filepath.Join(base, "does/not/exist")
|
|
|
|
|
|
|
|
id := uuid.Generate()
|
|
|
|
h := new(sidsHook)
|
|
|
|
err := h.writeToken(secrets, id)
|
|
|
|
r.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIDSHook_deriveSIToken(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 21:33:20 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
taskName, taskKind := sidecar("task1")
|
2019-11-27 21:41:45 +00:00
|
|
|
h := newSIDSHook(sidsHookConfig{
|
2019-12-18 16:23:16 +00:00
|
|
|
alloc: &structs.Allocation{ID: "a1"},
|
|
|
|
task: &structs.Task{
|
|
|
|
Name: taskName,
|
|
|
|
Kind: taskKind,
|
|
|
|
},
|
2019-11-27 21:41:45 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2021-06-11 07:39:22 +00:00
|
|
|
sidsClient: consulapi.NewMockServiceIdentitiesClient(),
|
2019-11-27 21:41:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
token, err := h.deriveSIToken(ctx)
|
|
|
|
r.NoError(err)
|
2019-12-18 16:23:16 +00:00
|
|
|
r.True(helper.IsUUID(token), "token: %q", token)
|
2019-11-27 21:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 15:56:48 +00:00
|
|
|
func TestSIDSHook_deriveSIToken_timeout(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-15 15:56:48 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
siClient := consulapi.NewMockServiceIdentitiesClient()
|
2020-01-15 15:56:48 +00:00
|
|
|
siClient.DeriveTokenFn = func(allocation *structs.Allocation, strings []string) (m map[string]string, err error) {
|
|
|
|
select {
|
|
|
|
// block forever, hopefully triggering a timeout in the caller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:33:20 +00:00
|
|
|
taskName, taskKind := sidecar("task1")
|
2020-01-15 15:56:48 +00:00
|
|
|
h := newSIDSHook(sidsHookConfig{
|
|
|
|
alloc: &structs.Allocation{ID: "a1"},
|
|
|
|
task: &structs.Task{
|
|
|
|
Name: taskName,
|
|
|
|
Kind: taskKind,
|
|
|
|
},
|
|
|
|
logger: testlog.HCLogger(t),
|
|
|
|
sidsClient: siClient,
|
|
|
|
})
|
|
|
|
|
|
|
|
// set the timeout to a really small value for testing
|
|
|
|
h.derivationTimeout = time.Duration(1 * time.Millisecond)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
_, err := h.deriveSIToken(ctx)
|
|
|
|
r.EqualError(err, "context deadline exceeded")
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:41:45 +00:00
|
|
|
func TestSIDSHook_computeBackoff(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-27 21:41:45 +00:00
|
|
|
|
|
|
|
try := func(i int, exp time.Duration) {
|
|
|
|
result := computeBackoff(i)
|
|
|
|
require.Equal(t, exp, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
try(0, time.Duration(0))
|
2019-12-06 20:46:46 +00:00
|
|
|
try(1, 100*time.Millisecond)
|
|
|
|
try(2, 10*time.Second)
|
|
|
|
try(3, 15*time.Second)
|
|
|
|
try(4, 20*time.Second)
|
|
|
|
try(5, 25*time.Second)
|
2019-11-27 21:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIDSHook_backoff(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-27 21:41:45 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
stop := !backoff(ctx, 0)
|
|
|
|
r.False(stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIDSHook_backoffKilled(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-27 21:41:45 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stop := !backoff(ctx, 1000)
|
|
|
|
r.True(stop)
|
|
|
|
}
|
2020-01-30 22:16:45 +00:00
|
|
|
|
|
|
|
func TestTaskRunner_DeriveSIToken_UnWritableTokenFile(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-30 22:16:45 +00:00
|
|
|
// Normally this test would live in test_runner_test.go, but since it requires
|
|
|
|
// root and the check for root doesn't like Windows, we put this file in here
|
|
|
|
// for now.
|
|
|
|
|
|
|
|
// This test fails when running as root because the test case for checking
|
|
|
|
// the error condition when the file is unreadable fails (root can read the
|
|
|
|
// file even though the permissions are set to 0200).
|
|
|
|
if unix.Geteuid() == 0 {
|
|
|
|
t.Skip("test only works as non-root")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.BatchConnectAlloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
task.Config = map[string]interface{}{
|
|
|
|
"run_for": "0s",
|
|
|
|
}
|
|
|
|
|
|
|
|
trConfig, cleanup := testTaskRunnerConfig(t, alloc, task.Name)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// make the si_token file un-writable, triggering a failure after a
|
|
|
|
// successful token derivation
|
2022-05-12 15:42:40 +00:00
|
|
|
secrets := t.TempDir()
|
2020-01-30 22:16:45 +00:00
|
|
|
trConfig.TaskDir.SecretsDir = secrets
|
2023-03-08 19:25:10 +00:00
|
|
|
err := os.WriteFile(filepath.Join(secrets, sidsTokenFile), nil, 0400)
|
2020-01-30 22:16:45 +00:00
|
|
|
r.NoError(err)
|
|
|
|
|
2020-01-31 01:30:48 +00:00
|
|
|
// set a consul token for the nomad client, which is what triggers the
|
|
|
|
// SIDS hook to be applied
|
|
|
|
trConfig.ClientConfig.ConsulConfig.Token = uuid.Generate()
|
|
|
|
|
2020-01-30 22:16:45 +00:00
|
|
|
// derive token works just fine
|
|
|
|
deriveFn := func(*structs.Allocation, []string) (map[string]string, error) {
|
|
|
|
return map[string]string{task.Name: uuid.Generate()}, nil
|
|
|
|
}
|
|
|
|
siClient := trConfig.ConsulSI.(*consulapi.MockServiceIdentitiesClient)
|
|
|
|
siClient.DeriveTokenFn = deriveFn
|
|
|
|
|
|
|
|
// start the task runner
|
|
|
|
tr, err := NewTaskRunner(trConfig)
|
|
|
|
r.NoError(err)
|
|
|
|
defer tr.Kill(context.Background(), structs.NewTaskEvent("cleanup"))
|
|
|
|
useMockEnvoyBootstrapHook(tr) // mock the envoy bootstrap
|
|
|
|
|
|
|
|
go tr.Run()
|
|
|
|
|
|
|
|
// wait for task runner to finish running
|
Task lifecycle restart (#14127)
* allocrunner: handle lifecycle when all tasks die
When all tasks die the Coordinator must transition to its terminal
state, coordinatorStatePoststop, to unblock poststop tasks. Since this
could happen at any time (for example, a prestart task dies), all states
must be able to transition to this terminal state.
* allocrunner: implement different alloc restarts
Add a new alloc restart mode where all tasks are restarted, even if they
have already exited. Also unifies the alloc restart logic to use the
implementation that restarts tasks concurrently and ignores
ErrTaskNotRunning errors since those are expected when restarting the
allocation.
* allocrunner: allow tasks to run again
Prevent the task runner Run() method from exiting to allow a dead task
to run again. When the task runner is signaled to restart, the function
will jump back to the MAIN loop and run it again.
The task runner determines if a task needs to run again based on two new
task events that were added to differentiate between a request to
restart a specific task, the tasks that are currently running, or all
tasks that have already run.
* api/cli: add support for all tasks alloc restart
Implement the new -all-tasks alloc restart CLI flag and its API
counterpar, AllTasks. The client endpoint calls the appropriate restart
method from the allocrunner depending on the restart parameters used.
* test: fix tasklifecycle Coordinator test
* allocrunner: kill taskrunners if all tasks are dead
When all non-poststop tasks are dead we need to kill the taskrunners so
we don't leak their goroutines, which are blocked in the alloc restart
loop. This also ensures the allocrunner exits on its own.
* taskrunner: fix tests that waited on WaitCh
Now that "dead" tasks may run again, the taskrunner Run() method will
not return when the task finishes running, so tests must wait for the
task state to be "dead" instead of using the WaitCh, since it won't be
closed until the taskrunner is killed.
* tests: add tests for all tasks alloc restart
* changelog: add entry for #14127
* taskrunner: fix restore logic.
The first implementation of the task runner restore process relied on
server data (`tr.Alloc().TerminalStatus()`) which may not be available
to the client at the time of restore.
It also had the incorrect code path. When restoring a dead task the
driver handle always needs to be clear cleanly using `clearDriverHandle`
otherwise, after exiting the MAIN loop, the task may be killed by
`tr.handleKill`.
The fix is to store the state of the Run() loop in the task runner local
client state: if the task runner ever exits this loop cleanly (not with
a shutdown) it will never be able to run again. So if the Run() loops
starts with this local state flag set, it must exit early.
This local state flag is also being checked on task restart requests. If
the task is "dead" and its Run() loop is not active it will never be
able to run again.
* address code review requests
* apply more code review changes
* taskrunner: add different Restart modes
Using the task event to differentiate between the allocrunner restart
methods proved to be confusing for developers to understand how it all
worked.
So instead of relying on the event type, this commit separated the logic
of restarting an taskRunner into two methods:
- `Restart` will retain the current behaviour and only will only restart
the task if it's currently running.
- `ForceRestart` is the new method where a `dead` task is allowed to
restart if its `Run()` method is still active. Callers will need to
restart the allocRunner taskCoordinator to make sure it will allow the
task to run again.
* minor fixes
2022-08-24 21:43:07 +00:00
|
|
|
testWaitForTaskToDie(t, tr)
|
2020-01-30 22:16:45 +00:00
|
|
|
|
|
|
|
// assert task exited un-successfully
|
|
|
|
finalState := tr.TaskState()
|
|
|
|
r.Equal(structs.TaskStateDead, finalState.State)
|
|
|
|
r.True(finalState.Failed) // should have failed to write SI token
|
|
|
|
r.Contains(finalState.Events[2].DisplayMessage, "failed to write SI token")
|
|
|
|
|
|
|
|
// assert the token is *not* on disk, as secrets dir was un-writable
|
|
|
|
tokenPath := filepath.Join(trConfig.TaskDir.SecretsDir, sidsTokenFile)
|
2023-03-08 19:25:10 +00:00
|
|
|
token, err := os.ReadFile(tokenPath)
|
2020-01-30 22:16:45 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Empty(token)
|
|
|
|
}
|