2018-04-23 23:34:53 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-10-06 01:42:15 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
2019-11-18 18:04:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2018-04-23 23:34:53 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-04 23:01:35 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-04-23 23:34:53 +00:00
|
|
|
)
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
// WorkloadServices describes services defined in either a Task or TaskGroup
|
|
|
|
// that need to be syncronized with Consul
|
|
|
|
type WorkloadServices struct {
|
2018-04-23 23:34:53 +00:00
|
|
|
AllocID string
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
// Name of the task and task group the services are defined for. For
|
|
|
|
// group based services, Task will be empty
|
|
|
|
Task string
|
|
|
|
Group string
|
2018-04-23 23:34:53 +00:00
|
|
|
|
|
|
|
// Canary indicates whether or not the allocation is a canary
|
|
|
|
Canary bool
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
// Restarter allows restarting the task or task group depending on the
|
2018-04-23 23:34:53 +00:00
|
|
|
// check_restart stanzas.
|
2019-11-18 18:04:01 +00:00
|
|
|
Restarter WorkloadRestarter
|
2018-04-23 23:34:53 +00:00
|
|
|
|
|
|
|
// Services and checks to register for the task.
|
|
|
|
Services []*structs.Service
|
|
|
|
|
|
|
|
// Networks from the task's resources stanza.
|
|
|
|
Networks structs.Networks
|
|
|
|
|
|
|
|
// DriverExec is the script executor for the task's driver.
|
2019-11-18 18:04:01 +00:00
|
|
|
// For group services this is nil and script execution is managed by
|
|
|
|
// a tasklet in the taskrunner script_check_hook
|
2018-10-04 19:08:20 +00:00
|
|
|
DriverExec interfaces.ScriptExecutor
|
2018-04-23 23:34:53 +00:00
|
|
|
|
|
|
|
// DriverNetwork is the network specified by the driver and may be nil.
|
2019-01-04 23:01:35 +00:00
|
|
|
DriverNetwork *drivers.DriverNetwork
|
2018-04-23 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
func BuildAllocServices(node *structs.Node, alloc *structs.Allocation, restarter WorkloadRestarter) *WorkloadServices {
|
|
|
|
|
|
|
|
//TODO(schmichael) only support one network for now
|
|
|
|
net := alloc.AllocatedResources.Shared.Networks[0]
|
|
|
|
|
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
|
|
|
|
ws := &WorkloadServices{
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
Group: alloc.TaskGroup,
|
|
|
|
Services: taskenv.InterpolateServices(taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region).Build(), tg.Services),
|
|
|
|
Networks: alloc.AllocatedResources.Shared.Networks,
|
|
|
|
|
|
|
|
//TODO(schmichael) there's probably a better way than hacking driver network
|
|
|
|
DriverNetwork: &drivers.DriverNetwork{
|
|
|
|
AutoAdvertise: true,
|
|
|
|
IP: net.IP,
|
|
|
|
// Copy PortLabels from group network
|
|
|
|
PortMap: net.PortLabels(),
|
|
|
|
},
|
2018-04-23 23:34:53 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
Restarter: restarter,
|
|
|
|
DriverExec: nil,
|
2018-04-23 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
if alloc.DeploymentStatus != nil {
|
|
|
|
ws.Canary = alloc.DeploymentStatus.Canary
|
2018-04-23 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
return ws
|
2018-04-23 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy method for easing tests
|
2019-11-18 18:04:01 +00:00
|
|
|
func (t *WorkloadServices) Copy() *WorkloadServices {
|
|
|
|
newTS := new(WorkloadServices)
|
2018-04-23 23:34:53 +00:00
|
|
|
*newTS = *t
|
|
|
|
|
|
|
|
// Deep copy Services
|
|
|
|
newTS.Services = make([]*structs.Service, len(t.Services))
|
|
|
|
for i := range t.Services {
|
|
|
|
newTS.Services[i] = t.Services[i].Copy()
|
|
|
|
}
|
|
|
|
return newTS
|
|
|
|
}
|
2019-11-18 18:04:01 +00:00
|
|
|
|
|
|
|
func (w *WorkloadServices) Name() string {
|
|
|
|
if w.Task != "" {
|
|
|
|
return w.Task
|
|
|
|
}
|
|
|
|
|
|
|
|
return "group-" + w.Group
|
|
|
|
}
|