From 916a40bb9e3dce674ddefce94e44ef5374ebf0b0 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Fri, 4 Jan 2019 18:01:35 -0500 Subject: [PATCH] move cstructs.DeviceNetwork to drivers pkg --- .../allocrunner/interfaces/task_lifecycle.go | 2 +- .../allocrunner/taskrunner/driver_handle.go | 6 +- client/allocrunner/taskrunner/service_hook.go | 4 +- client/allocrunner/taskrunner/state/state.go | 3 +- client/allocrunner/taskrunner/task_runner.go | 2 +- client/state/08types.go | 3 +- client/structs/structs.go | 57 ----------------- client/taskenv/env.go | 10 +-- client/taskenv/env_test.go | 10 +-- command/agent/consul/client.go | 4 +- command/agent/consul/structs.go | 6 +- command/agent/consul/unit_test.go | 27 ++++---- drivers/docker/driver.go | 4 +- drivers/docker/handle.go | 4 +- drivers/exec/driver.go | 2 +- drivers/java/driver.go | 2 +- drivers/lxc/driver.go | 2 +- drivers/mock/driver.go | 4 +- drivers/qemu/driver.go | 6 +- drivers/rawexec/driver.go | 2 +- drivers/rkt/driver.go | 8 +-- plugins/drivers/client.go | 8 +-- plugins/drivers/driver.go | 61 ++++++++++++++++++- plugins/drivers/testutils/testing.go | 4 +- plugins/drivers/testutils/testing_test.go | 5 +- 25 files changed, 121 insertions(+), 125 deletions(-) diff --git a/client/allocrunner/interfaces/task_lifecycle.go b/client/allocrunner/interfaces/task_lifecycle.go index 61454f7c8..f8d50f7a5 100644 --- a/client/allocrunner/interfaces/task_lifecycle.go +++ b/client/allocrunner/interfaces/task_lifecycle.go @@ -96,7 +96,7 @@ type TaskPoststartRequest struct { DriverExec interfaces.ScriptExecutor // Network info (may be nil) - DriverNetwork *cstructs.DriverNetwork + DriverNetwork *drivers.DriverNetwork // TaskEnv is the task's environment TaskEnv *taskenv.TaskEnv diff --git a/client/allocrunner/taskrunner/driver_handle.go b/client/allocrunner/taskrunner/driver_handle.go index 0d3c07658..ed4762f77 100644 --- a/client/allocrunner/taskrunner/driver_handle.go +++ b/client/allocrunner/taskrunner/driver_handle.go @@ -10,7 +10,7 @@ import ( ) // 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{ driver: driver, 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 type DriverHandle struct { driver drivers.DriverPlugin - net *cstructs.DriverNetwork + net *drivers.DriverNetwork task *structs.Task 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 } -func (h *DriverHandle) Network() *cstructs.DriverNetwork { +func (h *DriverHandle) Network() *drivers.DriverNetwork { return h.net } diff --git a/client/allocrunner/taskrunner/service_hook.go b/client/allocrunner/taskrunner/service_hook.go index 0c1d7368a..3c749f1e3 100644 --- a/client/allocrunner/taskrunner/service_hook.go +++ b/client/allocrunner/taskrunner/service_hook.go @@ -10,10 +10,10 @@ import ( "github.com/hashicorp/nomad/client/allocrunner/interfaces" tinterfaces "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces" "github.com/hashicorp/nomad/client/consul" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/taskenv" agentconsul "github.com/hashicorp/nomad/command/agent/consul" "github.com/hashicorp/nomad/nomad/structs" + "github.com/hashicorp/nomad/plugins/drivers" ) type serviceHookConfig struct { @@ -37,7 +37,7 @@ type serviceHook struct { // The following fields may be updated delay time.Duration driverExec tinterfaces.ScriptExecutor - driverNet *cstructs.DriverNetwork + driverNet *drivers.DriverNetwork canary bool services []*structs.Service networks structs.Networks diff --git a/client/allocrunner/taskrunner/state/state.go b/client/allocrunner/taskrunner/state/state.go index ba4879eb3..f5e6dfba3 100644 --- a/client/allocrunner/taskrunner/state/state.go +++ b/client/allocrunner/taskrunner/state/state.go @@ -1,7 +1,6 @@ package state import ( - "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/plugins/drivers" ) @@ -13,7 +12,7 @@ type LocalState struct { // DriverNetwork is the network information returned by the task // driver's Start method - DriverNetwork *structs.DriverNetwork + DriverNetwork *drivers.DriverNetwork // TaskHandle is the handle used to reattach to the task during recovery TaskHandle *drivers.TaskHandle diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index e120f0ca5..2f9cc54af 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -781,7 +781,7 @@ func (tr *TaskRunner) Restore() error { // restoreHandle ensures a TaskHandle is valid by calling Driver.RecoverTask // and sets the driver handle. If the TaskHandle is not valid, DestroyTask is // 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 if taskHandle.Config == nil { return true diff --git a/client/state/08types.go b/client/state/08types.go index 758e7ef6e..ce0478213 100644 --- a/client/state/08types.go +++ b/client/state/08types.go @@ -2,7 +2,6 @@ package state import ( "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/plugins/drivers" ) @@ -36,7 +35,7 @@ type taskRunnerState08 struct { ArtifactDownloaded bool TaskDirBuilt bool PayloadRendered bool - DriverNetwork *cstructs.DriverNetwork + DriverNetwork *drivers.DriverNetwork // Created Resources are no longer used. //CreatedResources *driver.CreatedResources } diff --git a/client/structs/structs.go b/client/structs/structs.go index 160c3f727..f57c1d67c 100644 --- a/client/structs/structs.go +++ b/client/structs/structs.go @@ -3,10 +3,7 @@ package structs //go:generate codecgen -d 102 -o structs.generated.go structs.go import ( - "crypto/md5" "errors" - "io" - "strconv" "time" "github.com/hashicorp/nomad/client/stats" @@ -272,60 +269,6 @@ func joinStringSet(s1, s2 []string) []string { 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 // Check interface type HealthCheckRequest struct{} diff --git a/client/taskenv/env.go b/client/taskenv/env.go index 33dac7855..411cef3dc 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -8,10 +8,10 @@ import ( "strings" "sync" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/helper" hargs "github.com/hashicorp/nomad/helper/args" "github.com/hashicorp/nomad/nomad/structs" + "github.com/hashicorp/nomad/plugins/drivers" "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 // was defined). - driverNetwork *cstructs.DriverNetwork + driverNetwork *drivers.DriverNetwork // network resources from the task; must be lazily turned into env vars // 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. -func (b *Builder) SetDriverNetwork(n *cstructs.DriverNetwork) *Builder { +func (b *Builder) SetDriverNetwork(n *drivers.DriverNetwork) *Builder { ncopy := n.Copy() b.mu.Lock() b.driverNetwork = ncopy @@ -682,7 +682,7 @@ func (b *Builder) SetDriverNetwork(n *cstructs.DriverNetwork) *Builder { // // Task: NOMAD_TASK_{IP,PORT,ADDR}__