2018-07-20 00:40:25 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2018-11-05 20:20:45 +00:00
|
|
|
"time"
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2018-10-04 19:08:20 +00:00
|
|
|
tinterfaces "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
2018-07-20 00:40:25 +00:00
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
2018-11-30 11:18:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2018-07-20 00:40:25 +00:00
|
|
|
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-04 23:01:35 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-07-20 00:40:25 +00:00
|
|
|
)
|
|
|
|
|
2019-06-12 14:00:21 +00:00
|
|
|
var _ interfaces.TaskPoststartHook = &serviceHook{}
|
|
|
|
var _ interfaces.TaskPreKillHook = &serviceHook{}
|
|
|
|
var _ interfaces.TaskExitedHook = &serviceHook{}
|
|
|
|
var _ interfaces.TaskStopHook = &serviceHook{}
|
|
|
|
|
2018-07-20 00:40:25 +00:00
|
|
|
type serviceHookConfig struct {
|
|
|
|
alloc *structs.Allocation
|
|
|
|
task *structs.Task
|
|
|
|
consul consul.ConsulServiceAPI
|
|
|
|
|
|
|
|
// Restarter is a subset of the TaskLifecycle interface
|
2019-11-18 18:04:01 +00:00
|
|
|
restarter agentconsul.WorkloadRestarter
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceHook struct {
|
|
|
|
consul consul.ConsulServiceAPI
|
|
|
|
allocID string
|
|
|
|
taskName string
|
2019-11-18 18:04:01 +00:00
|
|
|
restarter agentconsul.WorkloadRestarter
|
2018-07-20 00:40:25 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
|
|
|
// The following fields may be updated
|
2018-11-05 20:20:45 +00:00
|
|
|
delay time.Duration
|
2018-10-04 19:08:20 +00:00
|
|
|
driverExec tinterfaces.ScriptExecutor
|
2019-01-04 23:01:35 +00:00
|
|
|
driverNet *drivers.DriverNetwork
|
2018-07-20 00:40:25 +00:00
|
|
|
canary bool
|
|
|
|
services []*structs.Service
|
|
|
|
networks structs.Networks
|
2018-11-30 11:18:39 +00:00
|
|
|
taskEnv *taskenv.TaskEnv
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Since Update() may be called concurrently with any other hook all
|
|
|
|
// hook methods must be fully serialized
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServiceHook(c serviceHookConfig) *serviceHook {
|
|
|
|
h := &serviceHook{
|
|
|
|
consul: c.consul,
|
|
|
|
allocID: c.alloc.ID,
|
|
|
|
taskName: c.task.Name,
|
|
|
|
services: c.task.Services,
|
|
|
|
restarter: c.restarter,
|
2018-11-05 20:20:45 +00:00
|
|
|
delay: c.task.ShutdownDelay,
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:25:07 +00:00
|
|
|
if res := c.alloc.AllocatedResources.Tasks[c.task.Name]; res != nil {
|
|
|
|
h.networks = res.Networks
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.alloc.DeploymentStatus != nil && c.alloc.DeploymentStatus.Canary {
|
|
|
|
h.canary = true
|
|
|
|
}
|
|
|
|
|
|
|
|
h.logger = c.logger.Named(h.Name())
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serviceHook) Name() string {
|
|
|
|
return "consul_services"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serviceHook) Poststart(ctx context.Context, req *interfaces.TaskPoststartRequest, _ *interfaces.TaskPoststartResponse) error {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
// Store the TaskEnv for interpolating now and when Updating
|
|
|
|
h.driverExec = req.DriverExec
|
|
|
|
h.driverNet = req.DriverNetwork
|
|
|
|
h.taskEnv = req.TaskEnv
|
|
|
|
|
|
|
|
// Create task services struct with request's driver metadata
|
2019-11-18 18:04:01 +00:00
|
|
|
workloadServices := h.getWorkloadServices()
|
2018-07-20 00:40:25 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
return h.consul.RegisterWorkload(workloadServices)
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *serviceHook) Update(ctx context.Context, req *interfaces.TaskUpdateRequest, _ *interfaces.TaskUpdateResponse) error {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
// Create old task services struct with request's driver metadata as it
|
|
|
|
// can't change due to Updates
|
2019-11-18 18:04:01 +00:00
|
|
|
oldWorkloadServices := h.getWorkloadServices()
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Store new updated values out of request
|
|
|
|
canary := false
|
|
|
|
if req.Alloc.DeploymentStatus != nil {
|
|
|
|
canary = req.Alloc.DeploymentStatus.Canary
|
|
|
|
}
|
|
|
|
|
|
|
|
var networks structs.Networks
|
2020-01-09 14:25:07 +00:00
|
|
|
if res := req.Alloc.AllocatedResources.Tasks[h.taskName]; res != nil {
|
|
|
|
networks = res.Networks
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task := req.Alloc.LookupTask(h.taskName)
|
|
|
|
if task == nil {
|
|
|
|
return fmt.Errorf("task %q not found in updated alloc", h.taskName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update service hook fields
|
2018-11-05 20:20:45 +00:00
|
|
|
h.delay = task.ShutdownDelay
|
2018-07-20 00:40:25 +00:00
|
|
|
h.taskEnv = req.TaskEnv
|
|
|
|
h.services = task.Services
|
|
|
|
h.networks = networks
|
|
|
|
h.canary = canary
|
|
|
|
|
|
|
|
// Create new task services struct with those new values
|
2019-11-18 18:04:01 +00:00
|
|
|
newWorkloadServices := h.getWorkloadServices()
|
2018-07-20 00:40:25 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
return h.consul.UpdateWorkload(oldWorkloadServices, newWorkloadServices)
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 23:28:13 +00:00
|
|
|
func (h *serviceHook) PreKilling(ctx context.Context, req *interfaces.TaskPreKillRequest, resp *interfaces.TaskPreKillResponse) error {
|
2018-11-05 20:20:45 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
// Deregister before killing task
|
|
|
|
h.deregister()
|
|
|
|
|
|
|
|
// If there's no shutdown delay, exit early
|
|
|
|
if h.delay == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h.logger.Debug("waiting before killing task", "shutdown_delay", h.delay)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(h.delay):
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:08:26 +00:00
|
|
|
func (h *serviceHook) Exited(context.Context, *interfaces.TaskExitedRequest, *interfaces.TaskExitedResponse) error {
|
2018-07-20 00:40:25 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2018-11-05 20:20:45 +00:00
|
|
|
h.deregister()
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-20 00:40:25 +00:00
|
|
|
|
2018-11-05 20:20:45 +00:00
|
|
|
// deregister services from Consul.
|
|
|
|
func (h *serviceHook) deregister() {
|
2019-11-18 18:04:01 +00:00
|
|
|
workloadServices := h.getWorkloadServices()
|
|
|
|
h.consul.RemoveWorkload(workloadServices)
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Canary flag may be getting flipped when the alloc is being
|
|
|
|
// destroyed, so remove both variations of the service
|
2019-11-18 18:04:01 +00:00
|
|
|
workloadServices.Canary = !workloadServices.Canary
|
|
|
|
h.consul.RemoveWorkload(workloadServices)
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:00:21 +00:00
|
|
|
func (h *serviceHook) Stop(ctx context.Context, req *interfaces.TaskStopRequest, resp *interfaces.TaskStopResponse) error {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
h.deregister()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
func (h *serviceHook) getWorkloadServices() *agentconsul.WorkloadServices {
|
2018-07-20 00:40:25 +00:00
|
|
|
// Interpolate with the task's environment
|
2019-11-18 18:04:01 +00:00
|
|
|
interpolatedServices := taskenv.InterpolateServices(h.taskEnv, h.services)
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Create task services struct with request's driver metadata
|
2019-11-18 18:04:01 +00:00
|
|
|
return &agentconsul.WorkloadServices{
|
2018-07-20 00:40:25 +00:00
|
|
|
AllocID: h.allocID,
|
2019-11-18 18:04:01 +00:00
|
|
|
Task: h.taskName,
|
2018-07-20 00:40:25 +00:00
|
|
|
Restarter: h.restarter,
|
|
|
|
Services: interpolatedServices,
|
|
|
|
DriverExec: h.driverExec,
|
|
|
|
DriverNetwork: h.driverNet,
|
|
|
|
Networks: h.networks,
|
|
|
|
Canary: h.canary,
|
|
|
|
}
|
|
|
|
}
|