move cstructs.DeviceNetwork to drivers pkg

This commit is contained in:
Mahmood Ali 2019-01-04 18:01:35 -05:00 committed by Mahmood Ali
parent 9369b123de
commit 916a40bb9e
25 changed files with 121 additions and 125 deletions

View file

@ -96,7 +96,7 @@ type TaskPoststartRequest struct {
DriverExec interfaces.ScriptExecutor DriverExec interfaces.ScriptExecutor
// Network info (may be nil) // Network info (may be nil)
DriverNetwork *cstructs.DriverNetwork DriverNetwork *drivers.DriverNetwork
// TaskEnv is the task's environment // TaskEnv is the task's environment
TaskEnv *taskenv.TaskEnv TaskEnv *taskenv.TaskEnv

View file

@ -10,7 +10,7 @@ import (
) )
// NewDriverHandle returns a handle for task operations on a specific task // NewDriverHandle returns a handle for task operations on a specific task
func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *cstructs.DriverNetwork) *DriverHandle { func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *drivers.DriverNetwork) *DriverHandle {
return &DriverHandle{ return &DriverHandle{
driver: driver, driver: driver,
net: net, net: net,
@ -23,7 +23,7 @@ func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.T
// an api to perform driver operations on the task // an api to perform driver operations on the task
type DriverHandle struct { type DriverHandle struct {
driver drivers.DriverPlugin driver drivers.DriverPlugin
net *cstructs.DriverNetwork net *drivers.DriverNetwork
task *structs.Task task *structs.Task
taskID string taskID string
} }
@ -61,6 +61,6 @@ func (h *DriverHandle) Exec(timeout time.Duration, cmd string, args []string) ([
return res.Stdout, res.ExitResult.ExitCode, res.ExitResult.Err return res.Stdout, res.ExitResult.ExitCode, res.ExitResult.Err
} }
func (h *DriverHandle) Network() *cstructs.DriverNetwork { func (h *DriverHandle) Network() *drivers.DriverNetwork {
return h.net return h.net
} }

View file

@ -10,10 +10,10 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces" "github.com/hashicorp/nomad/client/allocrunner/interfaces"
tinterfaces "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces" tinterfaces "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
"github.com/hashicorp/nomad/client/consul" "github.com/hashicorp/nomad/client/consul"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/client/taskenv"
agentconsul "github.com/hashicorp/nomad/command/agent/consul" agentconsul "github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
) )
type serviceHookConfig struct { type serviceHookConfig struct {
@ -37,7 +37,7 @@ type serviceHook struct {
// The following fields may be updated // The following fields may be updated
delay time.Duration delay time.Duration
driverExec tinterfaces.ScriptExecutor driverExec tinterfaces.ScriptExecutor
driverNet *cstructs.DriverNetwork driverNet *drivers.DriverNetwork
canary bool canary bool
services []*structs.Service services []*structs.Service
networks structs.Networks networks structs.Networks

View file

@ -1,7 +1,6 @@
package state package state
import ( import (
"github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/plugins/drivers" "github.com/hashicorp/nomad/plugins/drivers"
) )
@ -13,7 +12,7 @@ type LocalState struct {
// DriverNetwork is the network information returned by the task // DriverNetwork is the network information returned by the task
// driver's Start method // driver's Start method
DriverNetwork *structs.DriverNetwork DriverNetwork *drivers.DriverNetwork
// TaskHandle is the handle used to reattach to the task during recovery // TaskHandle is the handle used to reattach to the task during recovery
TaskHandle *drivers.TaskHandle TaskHandle *drivers.TaskHandle

View file

@ -781,7 +781,7 @@ func (tr *TaskRunner) Restore() error {
// restoreHandle ensures a TaskHandle is valid by calling Driver.RecoverTask // restoreHandle ensures a TaskHandle is valid by calling Driver.RecoverTask
// and sets the driver handle. If the TaskHandle is not valid, DestroyTask is // and sets the driver handle. If the TaskHandle is not valid, DestroyTask is
// called. // called.
func (tr *TaskRunner) restoreHandle(taskHandle *drivers.TaskHandle, net *cstructs.DriverNetwork) (success bool) { func (tr *TaskRunner) restoreHandle(taskHandle *drivers.TaskHandle, net *drivers.DriverNetwork) (success bool) {
// Ensure handle is well-formed // Ensure handle is well-formed
if taskHandle.Config == nil { if taskHandle.Config == nil {
return true return true

View file

@ -2,7 +2,6 @@ package state
import ( import (
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state" "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers" "github.com/hashicorp/nomad/plugins/drivers"
) )
@ -36,7 +35,7 @@ type taskRunnerState08 struct {
ArtifactDownloaded bool ArtifactDownloaded bool
TaskDirBuilt bool TaskDirBuilt bool
PayloadRendered bool PayloadRendered bool
DriverNetwork *cstructs.DriverNetwork DriverNetwork *drivers.DriverNetwork
// Created Resources are no longer used. // Created Resources are no longer used.
//CreatedResources *driver.CreatedResources //CreatedResources *driver.CreatedResources
} }

View file

@ -3,10 +3,7 @@ package structs
//go:generate codecgen -d 102 -o structs.generated.go structs.go //go:generate codecgen -d 102 -o structs.generated.go structs.go
import ( import (
"crypto/md5"
"errors" "errors"
"io"
"strconv"
"time" "time"
"github.com/hashicorp/nomad/client/stats" "github.com/hashicorp/nomad/client/stats"
@ -272,60 +269,6 @@ func joinStringSet(s1, s2 []string) []string {
return j return j
} }
// DriverNetwork is the network created by driver's (eg Docker's bridge
// network) during Prestart.
type DriverNetwork struct {
// PortMap can be set by drivers to replace ports in environment
// variables with driver-specific mappings.
PortMap map[string]int
// IP is the IP address for the task created by the driver.
IP string
// AutoAdvertise indicates whether the driver thinks services that
// choose to auto-advertise-addresses should use this IP instead of the
// host's. eg If a Docker network plugin is used
AutoAdvertise bool
}
// Advertise returns true if the driver suggests using the IP set. May be
// called on a nil Network in which case it returns false.
func (d *DriverNetwork) Advertise() bool {
return d != nil && d.AutoAdvertise
}
// Copy a DriverNetwork struct. If it is nil, nil is returned.
func (d *DriverNetwork) Copy() *DriverNetwork {
if d == nil {
return nil
}
pm := make(map[string]int, len(d.PortMap))
for k, v := range d.PortMap {
pm[k] = v
}
return &DriverNetwork{
PortMap: pm,
IP: d.IP,
AutoAdvertise: d.AutoAdvertise,
}
}
// Hash the contents of a DriverNetwork struct to detect changes. If it is nil,
// an empty slice is returned.
func (d *DriverNetwork) Hash() []byte {
if d == nil {
return []byte{}
}
h := md5.New()
io.WriteString(h, d.IP)
io.WriteString(h, strconv.FormatBool(d.AutoAdvertise))
for k, v := range d.PortMap {
io.WriteString(h, k)
io.WriteString(h, strconv.Itoa(v))
}
return h.Sum(nil)
}
// HealthCheckRequest is the request type for a type that fulfils the Health // HealthCheckRequest is the request type for a type that fulfils the Health
// Check interface // Check interface
type HealthCheckRequest struct{} type HealthCheckRequest struct{}

View file

@ -8,10 +8,10 @@ import (
"strings" "strings"
"sync" "sync"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
hargs "github.com/hashicorp/nomad/helper/args" hargs "github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
) )
@ -304,7 +304,7 @@ type Builder struct {
// driverNetwork is the network defined by the driver (or nil if none // driverNetwork is the network defined by the driver (or nil if none
// was defined). // was defined).
driverNetwork *cstructs.DriverNetwork driverNetwork *drivers.DriverNetwork
// network resources from the task; must be lazily turned into env vars // network resources from the task; must be lazily turned into env vars
// because portMaps and advertiseIP can change after builder creation // because portMaps and advertiseIP can change after builder creation
@ -665,7 +665,7 @@ func (b *Builder) SetSecretsDir(dir string) *Builder {
} }
// SetDriverNetwork defined by the driver. // SetDriverNetwork defined by the driver.
func (b *Builder) SetDriverNetwork(n *cstructs.DriverNetwork) *Builder { func (b *Builder) SetDriverNetwork(n *drivers.DriverNetwork) *Builder {
ncopy := n.Copy() ncopy := n.Copy()
b.mu.Lock() b.mu.Lock()
b.driverNetwork = ncopy b.driverNetwork = ncopy
@ -682,7 +682,7 @@ func (b *Builder) SetDriverNetwork(n *cstructs.DriverNetwork) *Builder {
// //
// Task: NOMAD_TASK_{IP,PORT,ADDR}_<task>_<label> # Always host values // Task: NOMAD_TASK_{IP,PORT,ADDR}_<task>_<label> # Always host values
// //
func buildNetworkEnv(envMap map[string]string, nets structs.Networks, driverNet *cstructs.DriverNetwork) { func buildNetworkEnv(envMap map[string]string, nets structs.Networks, driverNet *drivers.DriverNetwork) {
for _, n := range nets { for _, n := range nets {
for _, p := range n.ReservedPorts { for _, p := range n.ReservedPorts {
buildPortEnv(envMap, p, n.IP, driverNet) buildPortEnv(envMap, p, n.IP, driverNet)
@ -693,7 +693,7 @@ func buildNetworkEnv(envMap map[string]string, nets structs.Networks, driverNet
} }
} }
func buildPortEnv(envMap map[string]string, p structs.Port, ip string, driverNet *cstructs.DriverNetwork) { func buildPortEnv(envMap map[string]string, p structs.Port, ip string, driverNet *drivers.DriverNetwork) {
// Host IP, port, and address // Host IP, port, and address
portStr := strconv.Itoa(p.Value) portStr := strconv.Itoa(p.Value)
envMap[IpPrefix+p.Label] = ip envMap[IpPrefix+p.Label] = ip

View file

@ -11,9 +11,9 @@ import (
"github.com/hashicorp/hcl2/gohcl" "github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax" "github.com/hashicorp/hcl2/hcl/hclsyntax"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -166,7 +166,7 @@ func TestEnvironment_AsList(t *testing.T) {
"taskEnvKey": "taskEnvVal", "taskEnvKey": "taskEnvVal",
} }
env := NewBuilder(n, a, task, "global").SetDriverNetwork( env := NewBuilder(n, a, task, "global").SetDriverNetwork(
&cstructs.DriverNetwork{PortMap: map[string]int{"https": 443}}, &drivers.DriverNetwork{PortMap: map[string]int{"https": 443}},
) )
act := env.Build().List() act := env.Build().List()
@ -277,7 +277,7 @@ func TestEnvironment_AsList_Old(t *testing.T) {
}, },
} }
env := NewBuilder(n, a, task, "global").SetDriverNetwork( env := NewBuilder(n, a, task, "global").SetDriverNetwork(
&cstructs.DriverNetwork{PortMap: map[string]int{"https": 443}}, &drivers.DriverNetwork{PortMap: map[string]int{"https": 443}},
) )
act := env.Build().List() act := env.Build().List()
@ -362,7 +362,7 @@ func TestEnvironment_AllValues(t *testing.T) {
".": "c", ".": "c",
} }
env := NewBuilder(n, a, task, "global").SetDriverNetwork( env := NewBuilder(n, a, task, "global").SetDriverNetwork(
&cstructs.DriverNetwork{PortMap: map[string]int{"https": 443}}, &drivers.DriverNetwork{PortMap: map[string]int{"https": 443}},
) )
values, errs, err := env.Build().AllValues() values, errs, err := env.Build().AllValues()
@ -499,7 +499,7 @@ func TestEnvironment_Envvars(t *testing.T) {
a := mock.Alloc() a := mock.Alloc()
task := a.Job.TaskGroups[0].Tasks[0] task := a.Job.TaskGroups[0].Tasks[0]
task.Env = envMap task.Env = envMap
net := &cstructs.DriverNetwork{PortMap: portMap} net := &drivers.DriverNetwork{PortMap: portMap}
act := NewBuilder(n, a, task, "global").SetDriverNetwork(net).Build().All() act := NewBuilder(n, a, task, "global").SetDriverNetwork(net).Build().All()
for k, v := range envMap { for k, v := range envMap {
actV, ok := act[k] actV, ok := act[k]

View file

@ -13,11 +13,11 @@ import (
metrics "github.com/armon/go-metrics" metrics "github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog" log "github.com/hashicorp/go-hclog"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
) )
const ( const (
@ -1174,7 +1174,7 @@ func isOldNomadService(id string) bool {
// getAddress returns the IP and port to use for a service or check. If no port // getAddress returns the IP and port to use for a service or check. If no port
// label is specified (an empty value), zero values are returned because no // label is specified (an empty value), zero values are returned because no
// address could be resolved. // address could be resolved.
func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet *cstructs.DriverNetwork) (string, int, error) { func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet *drivers.DriverNetwork) (string, int, error) {
switch addrMode { switch addrMode {
case structs.AddressModeAuto: case structs.AddressModeAuto:
if driverNet.Advertise() { if driverNet.Advertise() {

View file

@ -2,8 +2,8 @@ package consul
import ( import (
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces" "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
) )
type TaskServices struct { type TaskServices struct {
@ -29,10 +29,10 @@ type TaskServices struct {
DriverExec interfaces.ScriptExecutor DriverExec interfaces.ScriptExecutor
// DriverNetwork is the network specified by the driver and may be nil. // DriverNetwork is the network specified by the driver and may be nil.
DriverNetwork *cstructs.DriverNetwork DriverNetwork *drivers.DriverNetwork
} }
func NewTaskServices(alloc *structs.Allocation, task *structs.Task, restarter TaskRestarter, exec interfaces.ScriptExecutor, net *cstructs.DriverNetwork) *TaskServices { func NewTaskServices(alloc *structs.Allocation, task *structs.Task, restarter TaskRestarter, exec interfaces.ScriptExecutor, net *drivers.DriverNetwork) *TaskServices {
ts := TaskServices{ ts := TaskServices{
AllocID: alloc.ID, AllocID: alloc.ID,
Name: task.Name, Name: task.Name,

View file

@ -10,11 +10,10 @@ import (
"time" "time"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper/testlog" "github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid" "github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/testutil" "github.com/hashicorp/nomad/testutil"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -1113,7 +1112,7 @@ func TestConsul_DriverNetwork_AutoUse(t *testing.T) {
}, },
} }
ctx.Task.DriverNetwork = &cstructs.DriverNetwork{ ctx.Task.DriverNetwork = &drivers.DriverNetwork{
PortMap: map[string]int{ PortMap: map[string]int{
"x": 8888, "x": 8888,
"y": 9999, "y": 9999,
@ -1217,7 +1216,7 @@ func TestConsul_DriverNetwork_NoAutoUse(t *testing.T) {
}, },
} }
ctx.Task.DriverNetwork = &cstructs.DriverNetwork{ ctx.Task.DriverNetwork = &drivers.DriverNetwork{
PortMap: map[string]int{ PortMap: map[string]int{
"x": 8888, "x": 8888,
"y": 9999, "y": 9999,
@ -1281,7 +1280,7 @@ func TestConsul_DriverNetwork_Change(t *testing.T) {
}, },
} }
ctx.Task.DriverNetwork = &cstructs.DriverNetwork{ ctx.Task.DriverNetwork = &drivers.DriverNetwork{
PortMap: map[string]int{ PortMap: map[string]int{
"x": 8888, "x": 8888,
"y": 9999, "y": 9999,
@ -1558,7 +1557,7 @@ func TestGetAddress(t *testing.T) {
Mode string Mode string
PortLabel string PortLabel string
Host map[string]int // will be converted to structs.Networks Host map[string]int // will be converted to structs.Networks
Driver *cstructs.DriverNetwork Driver *drivers.DriverNetwork
// Results // Results
ExpectedIP string ExpectedIP string
@ -1571,7 +1570,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeAuto, Mode: structs.AddressModeAuto,
PortLabel: "db", PortLabel: "db",
Host: map[string]int{"db": 12435}, Host: map[string]int{"db": 12435},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
@ -1583,7 +1582,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeHost, Mode: structs.AddressModeHost,
PortLabel: "db", PortLabel: "db",
Host: map[string]int{"db": 12345}, Host: map[string]int{"db": 12345},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
@ -1595,7 +1594,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeDriver, Mode: structs.AddressModeDriver,
PortLabel: "db", PortLabel: "db",
Host: map[string]int{"db": 12345}, Host: map[string]int{"db": 12345},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
@ -1607,7 +1606,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeAuto, Mode: structs.AddressModeAuto,
PortLabel: "db", PortLabel: "db",
Host: map[string]int{"db": 12345}, Host: map[string]int{"db": 12345},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
AutoAdvertise: true, AutoAdvertise: true,
@ -1620,7 +1619,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeDriver, Mode: structs.AddressModeDriver,
PortLabel: "7890", PortLabel: "7890",
Host: map[string]int{"db": 12345}, Host: map[string]int{"db": 12345},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
@ -1642,7 +1641,7 @@ func TestGetAddress(t *testing.T) {
Mode: structs.AddressModeDriver, Mode: structs.AddressModeDriver,
PortLabel: "bad-port-label", PortLabel: "bad-port-label",
Host: map[string]int{"db": 12345}, Host: map[string]int{"db": 12345},
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
PortMap: map[string]int{"db": 6379}, PortMap: map[string]int{"db": 6379},
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
@ -1652,7 +1651,7 @@ func TestGetAddress(t *testing.T) {
Name: "DriverZeroPort", Name: "DriverZeroPort",
Mode: structs.AddressModeDriver, Mode: structs.AddressModeDriver,
PortLabel: "0", PortLabel: "0",
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
ExpectedErr: "invalid port", ExpectedErr: "invalid port",
@ -1682,7 +1681,7 @@ func TestGetAddress(t *testing.T) {
{ {
Name: "NoPort_DriverMode", Name: "NoPort_DriverMode",
Mode: structs.AddressModeDriver, Mode: structs.AddressModeDriver,
Driver: &cstructs.DriverNetwork{ Driver: &drivers.DriverNetwork{
IP: "10.1.2.3", IP: "10.1.2.3",
}, },
ExpectedIP: "10.1.2.3", ExpectedIP: "10.1.2.3",

View file

@ -153,7 +153,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *structs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }
@ -260,7 +260,7 @@ CREATE:
// Detect container address // Detect container address
ip, autoUse := d.detectIP(container, &driverConfig) ip, autoUse := d.detectIP(container, &driverConfig)
net := &structs.DriverNetwork{ net := &drivers.DriverNetwork{
PortMap: driverConfig.PortMap, PortMap: driverConfig.PortMap,
IP: ip, IP: ip,
AutoAdvertise: autoUse, AutoAdvertise: autoUse,

View file

@ -35,7 +35,7 @@ type taskHandle struct {
doneCh chan bool doneCh chan bool
waitCh chan struct{} waitCh chan struct{}
removeContainerOnExit bool removeContainerOnExit bool
net *cstructs.DriverNetwork net *drivers.DriverNetwork
exitResult *drivers.ExitResult exitResult *drivers.ExitResult
exitResultLock sync.Mutex exitResultLock sync.Mutex
@ -52,7 +52,7 @@ type taskHandleState struct {
ReattachConfig *shared.ReattachConfig ReattachConfig *shared.ReattachConfig
ContainerID string ContainerID string
DriverNetwork *cstructs.DriverNetwork DriverNetwork *drivers.DriverNetwork
} }
func (h *taskHandle) buildState() *taskHandleState { func (h *taskHandle) buildState() *taskHandleState {

View file

@ -269,7 +269,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }

View file

@ -291,7 +291,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }

View file

@ -322,7 +322,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }

View file

@ -379,7 +379,7 @@ func newTaskHandle(cfg *drivers.TaskConfig, driverConfig *TaskConfig, logger hcl
return h return h
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
driverConfig, err := parseDriverConfig(cfg) driverConfig, err := parseDriverConfig(cfg)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -400,7 +400,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
} }
// Create the driver network // Create the driver network
net := &cstructs.DriverNetwork{ net := &drivers.DriverNetwork{
IP: driverConfig.DriverIP, IP: driverConfig.DriverIP,
AutoAdvertise: driverConfig.DriverAdvertise, AutoAdvertise: driverConfig.DriverAdvertise,
} }

View file

@ -290,7 +290,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("taskConfig with ID '%s' already started", cfg.ID) return nil, nil, fmt.Errorf("taskConfig with ID '%s' already started", cfg.ID)
} }
@ -467,9 +467,9 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
d.tasks.Set(cfg.ID, h) d.tasks.Set(cfg.ID, h)
go h.run() go h.run()
var driverNetwork *cstructs.DriverNetwork var driverNetwork *drivers.DriverNetwork
if len(driverConfig.PortMap) == 1 { if len(driverConfig.PortMap) == 1 {
driverNetwork = &cstructs.DriverNetwork{ driverNetwork = &drivers.DriverNetwork{
PortMap: driverConfig.PortMap, PortMap: driverConfig.PortMap,
} }
} }

View file

@ -297,7 +297,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }

View file

@ -402,7 +402,7 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return nil return nil
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("taskConfig with ID '%s' already started", cfg.ID) return nil, nil, fmt.Errorf("taskConfig with ID '%s' already started", cfg.ID)
} }
@ -739,7 +739,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
// - "host" means the container itself has no networking metadata // - "host" means the container itself has no networking metadata
// - "none" means no network is configured // - "none" means no network is configured
// https://coreos.com/rkt/docs/latest/networking/overview.html#no-loopback-only-networking // https://coreos.com/rkt/docs/latest/networking/overview.html#no-loopback-only-networking
var driverNetwork *cstructs.DriverNetwork var driverNetwork *drivers.DriverNetwork
if network != "host" && network != "none" { if network != "host" && network != "none" {
d.logger.Debug("retrieving network information for pod", "pod", img, "UUID", uuid, "task_name", cfg.Name) d.logger.Debug("retrieving network information for pod", "pod", img, "UUID", uuid, "task_name", cfg.Name)
driverNetwork, err = rktGetDriverNetwork(uuid, driverConfig.PortMap, d.logger) driverNetwork, err = rktGetDriverNetwork(uuid, driverConfig.PortMap, d.logger)
@ -887,7 +887,7 @@ func GetAbsolutePath(bin string) (string, error) {
return filepath.EvalSymlinks(lp) return filepath.EvalSymlinks(lp)
} }
func rktGetDriverNetwork(uuid string, driverConfigPortMap map[string]string, logger hclog.Logger) (*cstructs.DriverNetwork, error) { func rktGetDriverNetwork(uuid string, driverConfigPortMap map[string]string, logger hclog.Logger) (*drivers.DriverNetwork, error) {
deadline := time.Now().Add(networkDeadline) deadline := time.Now().Add(networkDeadline)
var lastErr error var lastErr error
try := 0 try := 0
@ -918,7 +918,7 @@ func rktGetDriverNetwork(uuid string, driverConfigPortMap map[string]string, log
if try > 1 { if try > 1 {
logger.Debug("retrieved network info for pod", "uuid", uuid, "attempt", try) logger.Debug("retrieved network info for pod", "uuid", uuid, "attempt", try)
} }
return &cstructs.DriverNetwork{ return &drivers.DriverNetwork{
PortMap: portmap, PortMap: portmap,
IP: status.Networks[0].IP.String(), IP: status.Networks[0].IP.String(),
}, nil }, nil

View file

@ -128,7 +128,7 @@ func (d *driverPluginClient) RecoverTask(h *TaskHandle) error {
// StartTask starts execution of a task with the given TaskConfig. A TaskHandle // StartTask starts execution of a task with the given TaskConfig. A TaskHandle
// is returned to the caller that can be used to recover state of the task, // is returned to the caller that can be used to recover state of the task,
// should the driver crash or exit prematurely. // should the driver crash or exit prematurely.
func (d *driverPluginClient) StartTask(c *TaskConfig) (*TaskHandle, *cstructs.DriverNetwork, error) { func (d *driverPluginClient) StartTask(c *TaskConfig) (*TaskHandle, *DriverNetwork, error) {
req := &proto.StartTaskRequest{ req := &proto.StartTaskRequest{
Task: taskConfigToProto(c), Task: taskConfigToProto(c),
} }
@ -144,9 +144,9 @@ func (d *driverPluginClient) StartTask(c *TaskConfig) (*TaskHandle, *cstructs.Dr
return nil, nil, grpcutils.HandleGrpcErr(err, d.doneCtx) return nil, nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
} }
var net *cstructs.DriverNetwork var net *DriverNetwork
if resp.NetworkOverride != nil { if resp.NetworkOverride != nil {
net = &cstructs.DriverNetwork{ net = &DriverNetwork{
PortMap: map[string]int{}, PortMap: map[string]int{},
IP: resp.NetworkOverride.Addr, IP: resp.NetworkOverride.Addr,
AutoAdvertise: resp.NetworkOverride.AutoAdvertise, AutoAdvertise: resp.NetworkOverride.AutoAdvertise,
@ -239,7 +239,7 @@ func (d *driverPluginClient) InspectTask(taskID string) (*TaskStatus, error) {
status.DriverAttributes = resp.Driver.Attributes status.DriverAttributes = resp.Driver.Attributes
} }
if resp.NetworkOverride != nil { if resp.NetworkOverride != nil {
status.NetworkOverride = &cstructs.DriverNetwork{ status.NetworkOverride = &DriverNetwork{
PortMap: map[string]int{}, PortMap: map[string]int{},
IP: resp.NetworkOverride.Addr, IP: resp.NetworkOverride.Addr,
AutoAdvertise: resp.NetworkOverride.AutoAdvertise, AutoAdvertise: resp.NetworkOverride.AutoAdvertise,

View file

@ -2,9 +2,12 @@ package drivers
import ( import (
"context" "context"
"crypto/md5"
"fmt" "fmt"
"io"
"path/filepath" "path/filepath"
"sort" "sort"
"strconv"
"time" "time"
"github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/allocdir"
@ -32,7 +35,7 @@ type DriverPlugin interface {
Fingerprint(context.Context) (<-chan *Fingerprint, error) Fingerprint(context.Context) (<-chan *Fingerprint, error)
RecoverTask(*TaskHandle) error RecoverTask(*TaskHandle) error
StartTask(*TaskConfig) (*TaskHandle, *cstructs.DriverNetwork, error) StartTask(*TaskConfig) (*TaskHandle, *DriverNetwork, error)
WaitTask(ctx context.Context, taskID string) (<-chan *ExitResult, error) WaitTask(ctx context.Context, taskID string) (<-chan *ExitResult, error)
StopTask(taskID string, timeout time.Duration, signal string) error StopTask(taskID string, timeout time.Duration, signal string) error
DestroyTask(taskID string, force bool) error DestroyTask(taskID string, force bool) error
@ -320,7 +323,7 @@ type TaskStatus struct {
CompletedAt time.Time CompletedAt time.Time
ExitResult *ExitResult ExitResult *ExitResult
DriverAttributes map[string]string DriverAttributes map[string]string
NetworkOverride *cstructs.DriverNetwork NetworkOverride *DriverNetwork
} }
type TaskEvent struct { type TaskEvent struct {
@ -340,3 +343,57 @@ type ExecTaskResult struct {
Stderr []byte Stderr []byte
ExitResult *ExitResult ExitResult *ExitResult
} }
// DriverNetwork is the network created by driver's (eg Docker's bridge
// network) during Prestart.
type DriverNetwork struct {
// PortMap can be set by drivers to replace ports in environment
// variables with driver-specific mappings.
PortMap map[string]int
// IP is the IP address for the task created by the driver.
IP string
// AutoAdvertise indicates whether the driver thinks services that
// choose to auto-advertise-addresses should use this IP instead of the
// host's. eg If a Docker network plugin is used
AutoAdvertise bool
}
// Advertise returns true if the driver suggests using the IP set. May be
// called on a nil Network in which case it returns false.
func (d *DriverNetwork) Advertise() bool {
return d != nil && d.AutoAdvertise
}
// Copy a DriverNetwork struct. If it is nil, nil is returned.
func (d *DriverNetwork) Copy() *DriverNetwork {
if d == nil {
return nil
}
pm := make(map[string]int, len(d.PortMap))
for k, v := range d.PortMap {
pm[k] = v
}
return &DriverNetwork{
PortMap: pm,
IP: d.IP,
AutoAdvertise: d.AutoAdvertise,
}
}
// Hash the contents of a DriverNetwork struct to detect changes. If it is nil,
// an empty slice is returned.
func (d *DriverNetwork) Hash() []byte {
if d == nil {
return []byte{}
}
h := md5.New()
io.WriteString(h, d.IP)
io.WriteString(h, strconv.FormatBool(d.AutoAdvertise))
for k, v := range d.PortMap {
io.WriteString(h, k)
io.WriteString(h, strconv.Itoa(v))
}
return h.Sum(nil)
}

View file

@ -194,7 +194,7 @@ type MockDriver struct {
FingerprintF func(context.Context) (<-chan *drivers.Fingerprint, error) FingerprintF func(context.Context) (<-chan *drivers.Fingerprint, error)
CapabilitiesF func() (*drivers.Capabilities, error) CapabilitiesF func() (*drivers.Capabilities, error)
RecoverTaskF func(*drivers.TaskHandle) error RecoverTaskF func(*drivers.TaskHandle) error
StartTaskF func(*drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) StartTaskF func(*drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error)
WaitTaskF func(context.Context, string) (<-chan *drivers.ExitResult, error) WaitTaskF func(context.Context, string) (<-chan *drivers.ExitResult, error)
StopTaskF func(string, time.Duration, string) error StopTaskF func(string, time.Duration, string) error
DestroyTaskF func(string, bool) error DestroyTaskF func(string, bool) error
@ -211,7 +211,7 @@ func (d *MockDriver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerpri
} }
func (d *MockDriver) Capabilities() (*drivers.Capabilities, error) { return d.CapabilitiesF() } func (d *MockDriver) Capabilities() (*drivers.Capabilities, error) { return d.CapabilitiesF() }
func (d *MockDriver) RecoverTask(h *drivers.TaskHandle) error { return d.RecoverTaskF(h) } func (d *MockDriver) RecoverTask(h *drivers.TaskHandle) error { return d.RecoverTaskF(h) }
func (d *MockDriver) StartTask(c *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { func (d *MockDriver) StartTask(c *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
return d.StartTaskF(c) return d.StartTaskF(c)
} }
func (d *MockDriver) WaitTask(ctx context.Context, id string) (<-chan *drivers.ExitResult, error) { func (d *MockDriver) WaitTask(ctx context.Context, id string) (<-chan *drivers.ExitResult, error) {

View file

@ -7,7 +7,6 @@ import (
"testing" "testing"
"time" "time"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers" "github.com/hashicorp/nomad/plugins/drivers"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs" pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
@ -21,7 +20,7 @@ var _ drivers.DriverPlugin = (*MockDriver)(nil)
func TestDriverHarness(t *testing.T) { func TestDriverHarness(t *testing.T) {
handle := &drivers.TaskHandle{Config: &drivers.TaskConfig{Name: "mock"}} handle := &drivers.TaskHandle{Config: &drivers.TaskConfig{Name: "mock"}}
d := &MockDriver{ d := &MockDriver{
StartTaskF: func(task *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { StartTaskF: func(task *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
return handle, nil, nil return handle, nil, nil
}, },
} }
@ -138,7 +137,7 @@ func TestBaseDriver_StartTask(t *testing.T) {
state := &testDriverState{Pid: 1, Log: "log"} state := &testDriverState{Pid: 1, Log: "log"}
var handle *drivers.TaskHandle var handle *drivers.TaskHandle
impl := &MockDriver{ impl := &MockDriver{
StartTaskF: func(c *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) { StartTaskF: func(c *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
handle = drivers.NewTaskHandle("test") handle = drivers.NewTaskHandle("test")
handle.Config = c handle.Config = c
handle.State = drivers.TaskStateRunning handle.State = drivers.TaskStateRunning