Skip to content

Commit

Permalink
Add env.Builder.UpdateTask for alloc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed May 23, 2017
1 parent 59b3cca commit 72a24ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
10 changes: 9 additions & 1 deletion client/alloc_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ func TestAllocRunner_Destroy(t *testing.T) {
ctestutil.ExecCompatible(t)
upd, ar := testAllocRunner(false)

// Shrink chroot
ar.config.ChrootEnv = map[string]string{
"/bin": "/bin",
"/lib": "/lib",
"/lib32": "/lib32",
"/lib64": "/lib64",
}

// Ensure task takes some time
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
task.Config["command"] = "/bin/sleep"
Expand Down Expand Up @@ -269,7 +277,7 @@ func TestAllocRunner_Destroy(t *testing.T) {

return nil
}); err != nil {
return false, fmt.Errorf("state not destroyed")
return false, fmt.Errorf("state not destroyed: %v", err)
}

// Check the alloc directory was cleaned
Expand Down
23 changes: 17 additions & 6 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@ type Builder struct {
// NewBuilder creates a new task environment builder.
func NewBuilder(node *structs.Node, alloc *structs.Allocation, task *structs.Task, region string) *Builder {
b := &Builder{
region: region,
envvars: make(map[string]string),
nodeAttrs: make(map[string]string),
otherPorts: make(map[string]string),
mu: &sync.RWMutex{},
region: region,
mu: &sync.RWMutex{},
}
return b.setTask(task).setAlloc(alloc).setNode(node)
}
Expand Down Expand Up @@ -344,14 +341,26 @@ func (b *Builder) Build() *TaskEnv {
return NewTaskEnv(cleanedEnv, nodeAttrs)
}

// Update task updates the environment based on a new alloc and task.
func (b *Builder) UpdateTask(alloc *structs.Allocation, task *structs.Task) *Builder {
b.mu.Lock()
defer b.mu.Unlock()
return b.setTask(task).setAlloc(alloc)
}

// setTask is called from NewBuilder to populate task related environment
// variables.
func (b *Builder) setTask(task *structs.Task) *Builder {
b.taskName = task.Name
b.envvars = make(map[string]string, len(task.Env))
for k, v := range task.Env {
b.envvars[k] = v
}
if task.Resources != nil {
if task.Resources == nil {
b.memLimit = 0
b.cpuLimit = 0
b.networks = []*structs.NetworkResource{}
} else {
b.memLimit = task.Resources.MemoryMB
b.cpuLimit = task.Resources.CPU
// Copy networks to prevent sharing
Expand Down Expand Up @@ -380,6 +389,7 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
}

// Add ports from other tasks
b.otherPorts = make(map[string]string, len(alloc.TaskResources)*2)
for taskName, resources := range alloc.TaskResources {
if taskName == b.taskName {
continue
Expand All @@ -398,6 +408,7 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {

// setNode is called from NewBuilder to populate node attributes.
func (b *Builder) setNode(n *structs.Node) *Builder {
b.nodeAttrs = make(map[string]string, 4+len(n.Attributes)+len(n.Meta))
b.nodeAttrs[nodeIdKey] = n.ID
b.nodeAttrs[nodeNameKey] = n.Name
b.nodeAttrs[nodeClassKey] = n.NodeClass
Expand Down
8 changes: 6 additions & 2 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,9 @@ func (r *TaskRunner) handleUpdate(update *structs.Allocation) error {
// Merge in the task resources
updatedTask.Resources = update.TaskResources[updatedTask.Name]

// Update the task's environment
r.envBuilder.UpdateTask(update, updatedTask)

var mErr multierror.Error
r.handleLock.Lock()
if r.handle != nil {
Expand All @@ -1518,7 +1521,8 @@ func (r *TaskRunner) handleUpdate(update *structs.Allocation) error {
mErr.Errors = append(mErr.Errors, fmt.Errorf("updating task resources failed: %v", err))
}

if err := r.updateServices(drv, r.handle, r.task, updatedTask, update); err != nil {
// Update services in Consul
if err := r.updateServices(drv, r.handle, r.task, updatedTask); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("error updating services and checks in Consul: %v", err))
}
}
Expand All @@ -1536,7 +1540,7 @@ func (r *TaskRunner) handleUpdate(update *structs.Allocation) error {
}

// updateServices and checks with Consul.
func (r *TaskRunner) updateServices(d driver.Driver, h driver.ScriptExecutor, old, new *structs.Task, newAlloc *structs.Allocation) error {
func (r *TaskRunner) updateServices(d driver.Driver, h driver.ScriptExecutor, old, new *structs.Task) error {
var exec driver.ScriptExecutor
if d.Abilities().Exec {
// Allow set the script executor if the driver supports it
Expand Down

0 comments on commit 72a24ae

Please sign in to comment.