call handle.Network() instead of storing it

This commit is contained in:
Michael Schurter
2018-07-30 16:37:26 -07:00
parent cea2b879e4
commit 97dbf99d3f
5 changed files with 12 additions and 19 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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))

View File

@@ -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
}

View File

@@ -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 {