From 97dbf99d3f29082c060ff8a61790277b3fd19f09 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 30 Jul 2018 16:37:26 -0700 Subject: [PATCH] call handle.Network() instead of storing it --- client/allocrunner/taskrunner/task_runner.go | 1 + client/allocrunnerv2/taskrunner/lifecycle.go | 6 +++--- client/allocrunnerv2/taskrunner/task_runner.go | 8 +++----- .../allocrunnerv2/taskrunner/task_runner_getters.go | 13 +++---------- .../allocrunnerv2/taskrunner/task_runner_hooks.go | 3 ++- 5 files changed, 12 insertions(+), 19 deletions(-) diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 9a4f6d360..fb14943b9 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -115,6 +115,7 @@ type TaskRunner struct { // updateCh is used to receive updated versions of the allocation updateCh chan *structs.Allocation + // handle is returned when Starting or Opening a driver handle driver.DriverHandle handleLock sync.Mutex diff --git a/client/allocrunnerv2/taskrunner/lifecycle.go b/client/allocrunnerv2/taskrunner/lifecycle.go index db4bc4d6e..9de87f528 100644 --- a/client/allocrunnerv2/taskrunner/lifecycle.go +++ b/client/allocrunnerv2/taskrunner/lifecycle.go @@ -9,7 +9,7 @@ import ( func (tr *TaskRunner) Restart(ctx context.Context, event *structs.TaskEvent, failure bool) error { // Grab the handle - handle, _ := tr.getDriverHandle() + handle := tr.getDriverHandle() // Check it is running if handle == nil { @@ -41,7 +41,7 @@ func (tr *TaskRunner) Restart(ctx context.Context, event *structs.TaskEvent, fai func (tr *TaskRunner) Signal(event *structs.TaskEvent, s os.Signal) error { // Grab the handle - handle, _ := tr.getDriverHandle() + handle := tr.getDriverHandle() // Check it is running if handle == nil { @@ -57,7 +57,7 @@ func (tr *TaskRunner) Signal(event *structs.TaskEvent, s os.Signal) error { func (tr *TaskRunner) Kill(ctx context.Context, event *structs.TaskEvent) error { // Grab the handle - handle, _ := tr.getDriverHandle() + handle := tr.getDriverHandle() // Check if the handle is running if handle == nil { diff --git a/client/allocrunnerv2/taskrunner/task_runner.go b/client/allocrunnerv2/taskrunner/task_runner.go index 7cafde9b8..d9705711f 100644 --- a/client/allocrunnerv2/taskrunner/task_runner.go +++ b/client/allocrunnerv2/taskrunner/task_runner.go @@ -20,7 +20,6 @@ import ( "github.com/hashicorp/nomad/client/driver" "github.com/hashicorp/nomad/client/driver/env" clientstate "github.com/hashicorp/nomad/client/state" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/vaultclient" "github.com/hashicorp/nomad/nomad/structs" "github.com/ugorji/go/codec" @@ -102,8 +101,7 @@ type TaskRunner struct { // driver is the driver for the task. driver driver.Driver - handle driver.DriverHandle // the handle to the running driver - driverNet *cstructs.DriverNetwork // driver network if one exists + handle driver.DriverHandle // the handle to the running driver handleLock sync.Mutex // task is the task being run @@ -286,7 +284,7 @@ MAIN: } // Grab the handle - handle, _ = tr.getDriverHandle() + handle = tr.getDriverHandle() select { case waitRes := <-handle.WaitCh(): @@ -376,7 +374,7 @@ func (tr *TaskRunner) runDriver() error { } // Store the driver handle and associated metadata - tr.setDriverHandle(sresp.Handle, sresp.Network) + tr.setDriverHandle(sresp.Handle) // Emit an event that we started tr.SetState(structs.TaskStateRunning, structs.NewTaskEvent(structs.TaskStarted)) diff --git a/client/allocrunnerv2/taskrunner/task_runner_getters.go b/client/allocrunnerv2/taskrunner/task_runner_getters.go index 8a0b6a9e7..013f55849 100644 --- a/client/allocrunnerv2/taskrunner/task_runner_getters.go +++ b/client/allocrunnerv2/taskrunner/task_runner_getters.go @@ -2,7 +2,6 @@ package taskrunner import ( "github.com/hashicorp/nomad/client/driver" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/nomad/structs" ) @@ -36,26 +35,20 @@ func (tr *TaskRunner) setVaultToken(token string) { tr.vaultToken = token } -// getDriverHandle returns the DriverHandle and associated driver metadata (at -// this point just the network) if it exists. -func (tr *TaskRunner) getDriverHandle() (driver.DriverHandle, *cstructs.DriverNetwork) { +func (tr *TaskRunner) getDriverHandle() driver.DriverHandle { tr.handleLock.Lock() defer tr.handleLock.Unlock() - return tr.handle, tr.driverNet + return tr.handle } -func (tr *TaskRunner) setDriverHandle(handle driver.DriverHandle, net *cstructs.DriverNetwork) { +func (tr *TaskRunner) setDriverHandle(handle driver.DriverHandle) { tr.handleLock.Lock() defer tr.handleLock.Unlock() tr.handle = handle - tr.driverNet = net } -// clearDriverHandle clears the driver handle and associated driver metadata -// (driver network). func (tr *TaskRunner) clearDriverHandle() { tr.handleLock.Lock() defer tr.handleLock.Unlock() tr.handle = nil - tr.driverNet = nil } diff --git a/client/allocrunnerv2/taskrunner/task_runner_hooks.go b/client/allocrunnerv2/taskrunner/task_runner_hooks.go index 802831e9d..ac44c1451 100644 --- a/client/allocrunnerv2/taskrunner/task_runner_hooks.go +++ b/client/allocrunnerv2/taskrunner/task_runner_hooks.go @@ -171,7 +171,8 @@ func (tr *TaskRunner) poststart() error { }() } - handle, net := tr.getDriverHandle() + handle := tr.getDriverHandle() + net := handle.Network() var merr multierror.Error for _, hook := range tr.runnerHooks {