github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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  	// TODO: remove and use Ports
    33  	Networks structs.Networks
    34  
    35  	// NetworkStatus from alloc if network namespace is created
    36  	// Can be nil
    37  	NetworkStatus *structs.AllocNetworkStatus
    38  
    39  	// AllocatedPorts is the list of port mappings
    40  	Ports structs.AllocatedPorts
    41  
    42  	// DriverExec is the script executor for the task's driver.
    43  	// For group services this is nil and script execution is managed by
    44  	// a tasklet in the taskrunner script_check_hook
    45  	DriverExec interfaces.ScriptExecutor
    46  
    47  	// DriverNetwork is the network specified by the driver and may be nil.
    48  	DriverNetwork *drivers.DriverNetwork
    49  }
    50  
    51  func BuildAllocServices(node *structs.Node, alloc *structs.Allocation, restarter WorkloadRestarter) *WorkloadServices {
    52  
    53  	//TODO(schmichael) only support one network for now
    54  	net := alloc.AllocatedResources.Shared.Networks[0]
    55  
    56  	tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
    57  
    58  	ws := &WorkloadServices{
    59  		AllocID:  alloc.ID,
    60  		Group:    alloc.TaskGroup,
    61  		Services: taskenv.InterpolateServices(taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region).Build(), tg.Services),
    62  		Networks: alloc.AllocatedResources.Shared.Networks,
    63  
    64  		//TODO(schmichael) there's probably a better way than hacking driver network
    65  		DriverNetwork: &drivers.DriverNetwork{
    66  			AutoAdvertise: true,
    67  			IP:            net.IP,
    68  			// Copy PortLabels from group network
    69  			PortMap: net.PortLabels(),
    70  		},
    71  
    72  		Restarter:  restarter,
    73  		DriverExec: nil,
    74  	}
    75  
    76  	if alloc.DeploymentStatus != nil {
    77  		ws.Canary = alloc.DeploymentStatus.Canary
    78  	}
    79  
    80  	return ws
    81  }
    82  
    83  // Copy method for easing tests
    84  func (ws *WorkloadServices) Copy() *WorkloadServices {
    85  	newTS := new(WorkloadServices)
    86  	*newTS = *ws
    87  
    88  	// Deep copy Services
    89  	newTS.Services = make([]*structs.Service, len(ws.Services))
    90  	for i := range ws.Services {
    91  		newTS.Services[i] = ws.Services[i].Copy()
    92  	}
    93  	return newTS
    94  }
    95  
    96  func (ws *WorkloadServices) Name() string {
    97  	if ws.Task != "" {
    98  		return ws.Task
    99  	}
   100  
   101  	return "group-" + ws.Group
   102  }