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
|
2020-03-03 23:30:22 +00:00
|
|
|
func (ws *WorkloadServices) Copy() *WorkloadServices {
|
2019-11-18 18:04:01 +00:00
|
|
|
newTS := new(WorkloadServices)
|
2020-03-03 23:30:22 +00:00
|
|
|
*newTS = *ws
|
2018-04-23 23:34:53 +00:00
|
|
|
|
|
|
|
// Deep copy Services
|
2020-03-03 23:30:22 +00:00
|
|
|
newTS.Services = make([]*structs.Service, len(ws.Services))
|
|
|
|
for i := range ws.Services {
|
|
|
|
newTS.Services[i] = ws.Services[i].Copy()
|
2018-04-23 23:34:53 +00:00
|
|
|
}
|
|
|
|
return newTS
|
|
|
|
}
|
2019-11-18 18:04:01 +00:00
|
|
|
|
2020-03-03 23:30:22 +00:00
|
|
|
func (ws *WorkloadServices) Name() string {
|
|
|
|
if ws.Task != "" {
|
|
|
|
return ws.Task
|
2019-11-18 18:04:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 23:30:22 +00:00
|
|
|
return "group-" + ws.Group
|
2019-11-18 18:04:01 +00:00
|
|
|
}
|