github.com/adityamillind98/nomad@v0.11.8/command/agent/consul/structs.go (about) 1 package consul 2 3 import ( 4 "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces" 5 "github.com/hashicorp/nomad/client/taskenv" 6 "github.com/hashicorp/nomad/nomad/mock" 7 "github.com/hashicorp/nomad/nomad/structs" 8 "github.com/hashicorp/nomad/plugins/drivers" 9 ) 10 11 // WorkloadServices describes services defined in either a Task or TaskGroup 12 // that need to be syncronized with Consul 13 type WorkloadServices struct { 14 AllocID string 15 16 // Name of the task and task group the services are defined for. For 17 // group based services, Task will be empty 18 Task string 19 Group string 20 21 // Canary indicates whether or not the allocation is a canary 22 Canary bool 23 24 // Restarter allows restarting the task or task group depending on the 25 // check_restart stanzas. 26 Restarter WorkloadRestarter 27 28 // Services and checks to register for the task. 29 Services []*structs.Service 30 31 // Networks from the task's resources stanza. 32 Networks structs.Networks 33 34 // DriverExec is the script executor for the task's driver. 35 // For group services this is nil and script execution is managed by 36 // a tasklet in the taskrunner script_check_hook 37 DriverExec interfaces.ScriptExecutor 38 39 // DriverNetwork is the network specified by the driver and may be nil. 40 DriverNetwork *drivers.DriverNetwork 41 } 42 43 func BuildAllocServices(node *structs.Node, alloc *structs.Allocation, restarter WorkloadRestarter) *WorkloadServices { 44 45 //TODO(schmichael) only support one network for now 46 net := alloc.AllocatedResources.Shared.Networks[0] 47 48 tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup) 49 50 ws := &WorkloadServices{ 51 AllocID: alloc.ID, 52 Group: alloc.TaskGroup, 53 Services: taskenv.InterpolateServices(taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region).Build(), tg.Services), 54 Networks: alloc.AllocatedResources.Shared.Networks, 55 56 //TODO(schmichael) there's probably a better way than hacking driver network 57 DriverNetwork: &drivers.DriverNetwork{ 58 AutoAdvertise: true, 59 IP: net.IP, 60 // Copy PortLabels from group network 61 PortMap: net.PortLabels(), 62 }, 63 64 Restarter: restarter, 65 DriverExec: nil, 66 } 67 68 if alloc.DeploymentStatus != nil { 69 ws.Canary = alloc.DeploymentStatus.Canary 70 } 71 72 return ws 73 } 74 75 // Copy method for easing tests 76 func (ws *WorkloadServices) Copy() *WorkloadServices { 77 newTS := new(WorkloadServices) 78 *newTS = *ws 79 80 // Deep copy Services 81 newTS.Services = make([]*structs.Service, len(ws.Services)) 82 for i := range ws.Services { 83 newTS.Services[i] = ws.Services[i].Copy() 84 } 85 return newTS 86 } 87 88 func (ws *WorkloadServices) Name() string { 89 if ws.Task != "" { 90 return ws.Task 91 } 92 93 return "group-" + ws.Group 94 }