github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/serviceregistration/workload.go (about)

     1  package serviceregistration
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
     5  	"github.com/hashicorp/nomad/nomad/structs"
     6  	"github.com/hashicorp/nomad/plugins/drivers"
     7  )
     8  
     9  // WorkloadServices describes services defined in either a Task or TaskGroup
    10  // that need to be syncronized with a service registration provider.
    11  type WorkloadServices struct {
    12  	AllocInfo structs.AllocInfo
    13  
    14  	// Canary indicates whether, or not the allocation is a canary. This is
    15  	// used to build the correct tags mapping.
    16  	Canary bool
    17  
    18  	// ProviderNamespace is the provider namespace in which services will be
    19  	// registered, if the provider supports this functionality.
    20  	ProviderNamespace string
    21  
    22  	// Restarter allows restarting the task or task group depending on the
    23  	// check_restart stanzas.
    24  	Restarter WorkloadRestarter
    25  
    26  	// Services and checks to register for the task.
    27  	Services []*structs.Service
    28  
    29  	// Networks from the task's resources stanza.
    30  	// TODO: remove and use Ports
    31  	Networks structs.Networks
    32  
    33  	// NetworkStatus from alloc if network namespace is created.
    34  	// Can be nil.
    35  	NetworkStatus *structs.AllocNetworkStatus
    36  
    37  	// AllocatedPorts is the list of port mappings.
    38  	Ports structs.AllocatedPorts
    39  
    40  	// DriverExec is the script executor for the task's driver. For group
    41  	// services this is nil and script execution is managed by a tasklet in the
    42  	// taskrunner script_check_hook.
    43  	DriverExec interfaces.ScriptExecutor
    44  
    45  	// DriverNetwork is the network specified by the driver and may be nil.
    46  	DriverNetwork *drivers.DriverNetwork
    47  }
    48  
    49  // RegistrationProvider identifies the service registration provider for the
    50  // WorkloadServices.
    51  func (ws *WorkloadServices) RegistrationProvider() string {
    52  
    53  	// Protect against an empty array; it would be embarrassing to panic here.
    54  	if len(ws.Services) == 0 {
    55  		return ""
    56  	}
    57  
    58  	// Note(jrasell): a Nomad task group can only currently utilise a single
    59  	// service provider for all services included within it. In the event we
    60  	// remove this restriction, this will need to change along which a lot of
    61  	// other logic.
    62  	return ws.Services[0].Provider
    63  }
    64  
    65  // Copy method for easing tests.
    66  func (ws *WorkloadServices) Copy() *WorkloadServices {
    67  	newTS := new(WorkloadServices)
    68  	*newTS = *ws
    69  
    70  	// Deep copy Services
    71  	newTS.Services = make([]*structs.Service, len(ws.Services))
    72  	for i := range ws.Services {
    73  		newTS.Services[i] = ws.Services[i].Copy()
    74  	}
    75  	return newTS
    76  }
    77  
    78  func (ws *WorkloadServices) Name() string {
    79  	if ws.AllocInfo.Task != "" {
    80  		return ws.AllocInfo.Task
    81  	}
    82  	return "group-" + ws.AllocInfo.Group
    83  }