From b016b2b5b0e2d86a362139bf5e1cffd9f2b50c63 Mon Sep 17 00:00:00 2001 From: Nick Ethier Date: Wed, 10 Oct 2018 20:07:52 -0400 Subject: [PATCH] plugin/driver: add Copy funcs --- plugins/drivers/driver.go | 32 ++++++++++++++++++++++++++++++++ plugins/drivers/task_handle.go | 7 +++++++ plugins/drivers/utils.go | 15 --------------- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/plugins/drivers/driver.go b/plugins/drivers/driver.go index 7d503f451..f64d3dd5b 100644 --- a/plugins/drivers/driver.go +++ b/plugins/drivers/driver.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/nomad/client/allocdir" cstructs "github.com/hashicorp/nomad/client/structs" + "github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/base" "github.com/hashicorp/nomad/plugins/shared/hclspec" @@ -110,6 +111,17 @@ type TaskConfig struct { StderrPath string } +func (tc *TaskConfig) Copy() *TaskConfig { + if tc == nil { + return nil + } + c := new(TaskConfig) + *c = *tc + c.Env = helper.CopyMapStringString(c.Env) + c.Resources = tc.Resources.Copy() + return c +} + func (tc *TaskConfig) EnvList() []string { l := make([]string, 0, len(tc.Env)) for k, v := range tc.Env { @@ -161,6 +173,20 @@ type Resources struct { LinuxResources *LinuxResources } +func (r *Resources) Copy() *Resources { + if r == nil { + return nil + } + res := new(Resources) + if r.NomadResources != nil { + res.NomadResources = r.NomadResources.Copy() + } + if r.LinuxResources != nil { + res.LinuxResources = r.LinuxResources.Copy() + } + return res +} + type LinuxResources struct { CPUPeriod int64 CPUQuota int64 @@ -171,6 +197,12 @@ type LinuxResources struct { CpusetMems string } +func (r *LinuxResources) Copy() *LinuxResources { + res := new(LinuxResources) + *res = *r + return res +} + type DeviceConfig struct { TaskPath string HostPath string diff --git a/plugins/drivers/task_handle.go b/plugins/drivers/task_handle.go index 745ddea07..e71288952 100644 --- a/plugins/drivers/task_handle.go +++ b/plugins/drivers/task_handle.go @@ -27,3 +27,10 @@ func (h *TaskHandle) GetDriverState(v interface{}) error { return base.MsgPackDecode(h.driverState, v) } + +func (h *TaskHandle) Copy() *TaskHandle { + handle := new(TaskHandle) + *handle = *h + handle.Config = h.Config.Copy() + return handle +} diff --git a/plugins/drivers/utils.go b/plugins/drivers/utils.go index ba629abd5..ddf97fb6b 100644 --- a/plugins/drivers/utils.go +++ b/plugins/drivers/utils.go @@ -4,8 +4,6 @@ import ( "time" "github.com/golang/protobuf/ptypes" - "github.com/hashicorp/nomad/client/allocdir" - "github.com/hashicorp/nomad/client/driver/env" cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/drivers/proto" @@ -47,19 +45,6 @@ func healthStateFromProto(pb proto.FingerprintResponse_HealthState) HealthState return HealthStateUndetected } -// NewTaskConfig builds a TaskConfig from a Task struct -func NewTaskConfig(task *structs.Task, taskDir *allocdir.TaskDir, env *env.TaskEnv) *TaskConfig { - return &TaskConfig{ - Name: task.Name, - Resources: &Resources{ - NomadResources: task.Resources, - }, - Env: env.Map(), - User: task.User, - AllocDir: taskDir.AllocDir, - } -} - func taskConfigFromProto(pb *proto.TaskConfig) *TaskConfig { if pb == nil { return &TaskConfig{}