Skip to content

Commit

Permalink
trhooks: Add TaskStopHook interface to services
Browse files Browse the repository at this point in the history
We currently only run cleanup Service Hooks when a task is either
Killed, or Exited. However, due to the implementation of a task runner,
tasks are only Exited if they every correctly started running, which is
not true when you recieve an error early in the task start flow, such as
not being able to pull secrets from Vault.

This updates the service hook to also call consul deregistration
routines during a task `Stop` lifecycle event, to ensure that any
registered checks and services are cleared in such cases.
  • Loading branch information
endocrimes committed Jun 12, 2019
1 parent d7edf9b commit 036f939
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client/allocrunner/alloc_runner_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ func TestAllocRunner_Restore_RunningTerminal(t *testing.T) {
// Assert consul was cleaned up:
// 2 removals (canary+noncanary) during prekill
// 2 removals (canary+noncanary) during exited
// 2 removals (canary+noncanary) during stop
consulOps := conf2.Consul.(*consul.MockConsulServiceClient).GetOps()
require.Len(t, consulOps, 4)
require.Len(t, consulOps, 6)
for _, op := range consulOps {
require.Equal(t, "remove", op.Op)
}
Expand Down
10 changes: 10 additions & 0 deletions client/allocrunner/taskrunner/service_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/hashicorp/nomad/plugins/drivers"
)

var _ interfaces.TaskExitedHook = &serviceHook{}
var _ interfaces.TaskStopHook = &serviceHook{}

type serviceHookConfig struct {
alloc *structs.Allocation
task *structs.Task
Expand Down Expand Up @@ -179,6 +182,13 @@ func (h *serviceHook) deregister() {

}

func (h *serviceHook) Stop(ctx context.Context, req *interfaces.TaskStopRequest, resp *interfaces.TaskStopResponse) error {
h.mu.Lock()
defer h.mu.Unlock()
h.deregister()
return nil
}

func (h *serviceHook) getTaskServices() *agentconsul.TaskServices {
// Interpolate with the task's environment
interpolatedServices := interpolateServices(h.taskEnv, h.services)
Expand Down
6 changes: 5 additions & 1 deletion client/allocrunner/taskrunner/task_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ func TestTaskRunner_UnregisterConsul_Retries(t *testing.T) {

consul := conf.Consul.(*consulapi.MockConsulServiceClient)
consulOps := consul.GetOps()
require.Len(t, consulOps, 6)
require.Len(t, consulOps, 8)

// Initial add
require.Equal(t, "add", consulOps[0].Op)
Expand All @@ -2006,6 +2006,10 @@ func TestTaskRunner_UnregisterConsul_Retries(t *testing.T) {
// Removing canary and non-canary entries on retry
require.Equal(t, "remove", consulOps[4].Op)
require.Equal(t, "remove", consulOps[5].Op)

// Removing canary and non-canary entries on stop
require.Equal(t, "remove", consulOps[1].Op)
require.Equal(t, "remove", consulOps[2].Op)
}

// testWaitForTaskToStart waits for the task to be running or fails the test
Expand Down
8 changes: 8 additions & 0 deletions client/storage/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package storage

type Volume struct {
}

type VolumeDriver interface {
RequiresHostAttachment(v *Volume) bool
}
16 changes: 16 additions & 0 deletions client/storage/plugin_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package storage

import hclog "github.com/hashicorp/go-hclog"

type ManagerConfig struct {
SocketDir string
}

type PluginManager struct {
cfg *ManagerConfig
logger hclog.Logger
}

func (p *PluginManager) Run() error {
return nil
}

0 comments on commit 036f939

Please sign in to comment.