open-nomad/nomad/structs/alloc.go
James Rasell d080eed9ae
client: fixed a problem calculating a service namespace. (#13493)
When calculating a services namespace for registration, the code
assumed the first task within the task array would include a
service block. This is incorrect as it is possible only a latter
task within the array contains a service definition.

This change fixes the logic, so we correctly search for a service
definition before identifying the namespace.
2022-06-28 09:47:28 +02:00

59 lines
1.7 KiB
Go

package structs
const (
// AllocServiceRegistrationsRPCMethod is the RPC method for listing all
// service registrations assigned to a specific allocation.
//
// Args: AllocServiceRegistrationsRequest
// Reply: AllocServiceRegistrationsResponse
AllocServiceRegistrationsRPCMethod = "Alloc.GetServiceRegistrations"
)
// AllocServiceRegistrationsRequest is the request object used to list all
// service registrations belonging to the specified Allocation.ID.
type AllocServiceRegistrationsRequest struct {
AllocID string
QueryOptions
}
// AllocServiceRegistrationsResponse is the response object when performing a
// listing of services belonging to an allocation.
type AllocServiceRegistrationsResponse struct {
Services []*ServiceRegistration
QueryMeta
}
// ServiceProviderNamespace returns the namespace within which the allocations
// services should be registered. This takes into account the different
// providers that can provide service registrations. In the event no services
// are found, the function will return the Consul namespace which allows hooks
// to work as they did before this feature.
//
// It currently assumes that all services within an allocation use the same
// provider and therefore the same namespace.
func (a *Allocation) ServiceProviderNamespace() string {
tg := a.Job.LookupTaskGroup(a.TaskGroup)
if len(tg.Services) > 0 {
switch tg.Services[0].Provider {
case ServiceProviderNomad:
return a.Job.Namespace
default:
return tg.Consul.GetNamespace()
}
}
for _, task := range tg.Tasks {
if len(task.Services) > 0 {
switch task.Services[0].Provider {
case ServiceProviderNomad:
return a.Job.Namespace
default:
return tg.Consul.GetNamespace()
}
}
}
return tg.Consul.GetNamespace()
}