github.com/hernad/nomad@v1.6.112/nomad/structs/alloc.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package structs
     5  
     6  const (
     7  	// AllocServiceRegistrationsRPCMethod is the RPC method for listing all
     8  	// service registrations assigned to a specific allocation.
     9  	//
    10  	// Args: AllocServiceRegistrationsRequest
    11  	// Reply: AllocServiceRegistrationsResponse
    12  	AllocServiceRegistrationsRPCMethod = "Alloc.GetServiceRegistrations"
    13  )
    14  
    15  // AllocServiceRegistrationsRequest is the request object used to list all
    16  // service registrations belonging to the specified Allocation.ID.
    17  type AllocServiceRegistrationsRequest struct {
    18  	AllocID string
    19  	QueryOptions
    20  }
    21  
    22  // AllocServiceRegistrationsResponse is the response object when performing a
    23  // listing of services belonging to an allocation.
    24  type AllocServiceRegistrationsResponse struct {
    25  	Services []*ServiceRegistration
    26  	QueryMeta
    27  }
    28  
    29  // ServiceProviderNamespace returns the namespace within which the allocations
    30  // services should be registered. This takes into account the different
    31  // providers that can provide service registrations. In the event no services
    32  // are found, the function will return the Consul namespace which allows hooks
    33  // to work as they did before this feature.
    34  //
    35  // It currently assumes that all services within an allocation use the same
    36  // provider and therefore the same namespace.
    37  func (a *Allocation) ServiceProviderNamespace() string {
    38  	tg := a.Job.LookupTaskGroup(a.TaskGroup)
    39  
    40  	if len(tg.Services) > 0 {
    41  		switch tg.Services[0].Provider {
    42  		case ServiceProviderNomad:
    43  			return a.Job.Namespace
    44  		default:
    45  			return tg.Consul.GetNamespace()
    46  		}
    47  	}
    48  
    49  	for _, task := range tg.Tasks {
    50  		if len(task.Services) > 0 {
    51  			switch task.Services[0].Provider {
    52  			case ServiceProviderNomad:
    53  				return a.Job.Namespace
    54  			default:
    55  				return tg.Consul.GetNamespace()
    56  			}
    57  		}
    58  	}
    59  
    60  	return tg.Consul.GetNamespace()
    61  }
    62  
    63  type AllocInfo struct {
    64  	AllocID string
    65  
    66  	// Group in which the service belongs for a group-level service, or the
    67  	// group in which task belongs for a task-level service.
    68  	Group string
    69  
    70  	// Task in which the service belongs for task-level service. Will be empty
    71  	// for a group-level service.
    72  	Task string
    73  
    74  	// JobID provides additional context for providers regarding which job
    75  	// caused this registration.
    76  	JobID string
    77  
    78  	// NomadNamespace provides additional context for providers regarding which
    79  	// nomad namespace caused this registration.
    80  	Namespace string
    81  }