2015-09-06 22:34:28 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-02-21 18:58:04 +00:00
|
|
|
"fmt"
|
2022-03-09 01:54:17 +00:00
|
|
|
"net/http"
|
2015-09-06 22:34:28 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2017-09-15 00:24:51 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2019-04-01 14:21:03 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-02-08 04:31:23 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2022-03-09 01:54:17 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state/paginator"
|
2015-09-06 22:34:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Alloc endpoint is used for manipulating allocations
|
|
|
|
type Alloc struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
2022-12-01 15:05:15 +00:00
|
|
|
ctx *RPCContext
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
func NewAllocEndpoint(srv *Server, ctx *RPCContext) *Alloc {
|
|
|
|
return &Alloc{srv: srv, ctx: ctx, logger: srv.logger.Named("alloc")}
|
2015-09-06 22:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list the allocations in the system
|
|
|
|
func (a *Alloc) List(args *structs.AllocListRequest, reply *structs.AllocListResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(a.ctx, args)
|
2015-09-06 22:34:28 +00:00
|
|
|
if done, err := a.srv.forward("Alloc.List", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2015-09-06 22:34:28 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "list"}, time.Now())
|
|
|
|
|
2022-03-09 01:54:17 +00:00
|
|
|
namespace := args.RequestNamespace()
|
2020-11-05 17:26:33 +00:00
|
|
|
|
2017-09-15 00:24:51 +00:00
|
|
|
// Check namespace read-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := a.srv.ResolveACL(args)
|
2022-07-06 20:17:16 +00:00
|
|
|
if err != nil {
|
2017-09-15 00:24:51 +00:00
|
|
|
return err
|
2022-07-06 20:17:16 +00:00
|
|
|
}
|
|
|
|
if !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadJob) {
|
2017-09-15 00:24:51 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-07-06 20:17:16 +00:00
|
|
|
allow := aclObj.AllowNsOpFunc(acl.NamespaceCapabilityReadJob)
|
2017-09-15 00:24:51 +00:00
|
|
|
|
2015-10-29 02:25:39 +00:00
|
|
|
// Setup the blocking query
|
2022-03-09 01:54:17 +00:00
|
|
|
sort := state.SortOption(args.Reverse)
|
2015-10-29 02:25:39 +00:00
|
|
|
opts := blockingOptions{
|
2015-10-29 21:47:39 +00:00
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2017-02-08 04:31:23 +00:00
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
2022-03-09 01:54:17 +00:00
|
|
|
// Scan all the allocations
|
2017-02-08 04:31:23 +00:00
|
|
|
var err error
|
2015-12-24 10:46:59 +00:00
|
|
|
var iter memdb.ResultIterator
|
2022-03-09 01:54:17 +00:00
|
|
|
var opts paginator.StructsTokenizerOptions
|
2020-06-17 13:11:08 +00:00
|
|
|
|
2020-11-05 17:26:33 +00:00
|
|
|
// get list of accessible namespaces
|
2022-03-09 01:54:17 +00:00
|
|
|
allowableNamespaces, err := allowedNSes(aclObj, state, allow)
|
2020-11-05 17:26:33 +00:00
|
|
|
if err == structs.ErrPermissionDenied {
|
2022-03-09 01:54:17 +00:00
|
|
|
// return empty allocation if token is not authorized for any
|
2020-11-05 17:26:33 +00:00
|
|
|
// namespace, matching other endpoints
|
2022-03-09 01:54:17 +00:00
|
|
|
reply.Allocations = make([]*structs.AllocListStub, 0)
|
2020-11-05 17:26:33 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2022-03-09 01:54:17 +00:00
|
|
|
if prefix := args.QueryOptions.Prefix; prefix != "" {
|
2022-03-12 00:44:52 +00:00
|
|
|
iter, err = state.AllocsByIDPrefix(ws, namespace, prefix, sort)
|
2022-03-09 01:54:17 +00:00
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithID: true,
|
|
|
|
}
|
|
|
|
} else if namespace != structs.AllNamespacesSentinel {
|
|
|
|
iter, err = state.AllocsByNamespaceOrdered(ws, namespace, sort)
|
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithCreateIndex: true,
|
|
|
|
WithID: true,
|
|
|
|
}
|
2020-11-05 17:26:33 +00:00
|
|
|
} else {
|
2022-03-09 01:54:17 +00:00
|
|
|
iter, err = state.Allocs(ws, sort)
|
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithCreateIndex: true,
|
|
|
|
WithID: true,
|
|
|
|
}
|
2020-11-05 17:26:33 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:54:17 +00:00
|
|
|
tokenizer := paginator.NewStructsTokenizer(iter, opts)
|
|
|
|
filters := []paginator.Filter{
|
|
|
|
paginator.NamespaceFilter{
|
|
|
|
AllowableNamespaces: allowableNamespaces,
|
|
|
|
},
|
2020-11-05 17:26:33 +00:00
|
|
|
}
|
2022-03-09 01:54:17 +00:00
|
|
|
|
|
|
|
var stubs []*structs.AllocListStub
|
|
|
|
paginator, err := paginator.NewPaginator(iter, tokenizer, filters, args.QueryOptions,
|
|
|
|
func(raw interface{}) error {
|
|
|
|
allocation := raw.(*structs.Allocation)
|
|
|
|
stubs = append(stubs, allocation.Stub(args.Fields))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to create result paginator: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextToken, err := paginator.Page()
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to read result page: %v", err)
|
2020-11-05 17:26:33 +00:00
|
|
|
}
|
2022-03-09 01:54:17 +00:00
|
|
|
|
|
|
|
reply.QueryMeta.NextToken = nextToken
|
|
|
|
reply.Allocations = stubs
|
2020-11-05 17:26:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:54:17 +00:00
|
|
|
// Use the last index that affected the allocs table
|
2020-11-05 17:26:33 +00:00
|
|
|
index, err := state.Index("allocs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return a.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
2015-09-06 22:46:45 +00:00
|
|
|
// GetAlloc is used to lookup a particular allocation
|
|
|
|
func (a *Alloc) GetAlloc(args *structs.AllocSpecificRequest,
|
|
|
|
reply *structs.SingleAllocResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(a.ctx, args)
|
2015-09-06 22:46:45 +00:00
|
|
|
if done, err := a.srv.forward("Alloc.GetAlloc", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2015-09-06 22:46:45 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "get_alloc"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace read-job permissions before performing blocking query.
|
|
|
|
allowNsOp := acl.NamespaceValidator(acl.NamespaceCapabilityReadJob)
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := a.srv.ResolveClientOrACL(args)
|
2019-10-01 20:06:24 +00:00
|
|
|
if err != nil {
|
2023-01-25 19:33:06 +00:00
|
|
|
return err
|
2017-09-15 00:43:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 23:04:53 +00:00
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2017-02-08 04:31:23 +00:00
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
2015-10-29 23:04:53 +00:00
|
|
|
// Lookup the allocation
|
2017-02-08 04:31:23 +00:00
|
|
|
out, err := state.AllocByID(ws, args.AllocID)
|
2015-12-22 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-29 23:04:53 +00:00
|
|
|
}
|
2015-09-06 22:46:45 +00:00
|
|
|
|
2015-10-29 23:04:53 +00:00
|
|
|
// Setup the output
|
2015-10-30 15:27:47 +00:00
|
|
|
reply.Alloc = out
|
2015-10-29 23:04:53 +00:00
|
|
|
if out != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
// Re-check namespace in case it differs from request.
|
|
|
|
if !allowNsOp(aclObj, out.Namespace) {
|
|
|
|
return structs.NewErrUnknownAllocation(args.AllocID)
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:04:53 +00:00
|
|
|
reply.Index = out.ModifyIndex
|
|
|
|
} else {
|
2017-01-10 21:25:52 +00:00
|
|
|
// Use the last index that affected the allocs table
|
2017-02-08 04:31:23 +00:00
|
|
|
index, err := state.Index("allocs")
|
2015-10-29 23:04:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
}
|
2015-09-06 22:46:45 +00:00
|
|
|
|
2015-10-29 23:04:53 +00:00
|
|
|
// Set the query response
|
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return a.srv.blockingRPC(&opts)
|
2015-09-06 22:46:45 +00:00
|
|
|
}
|
2016-02-01 21:57:35 +00:00
|
|
|
|
|
|
|
// GetAllocs is used to lookup a set of allocations
|
|
|
|
func (a *Alloc) GetAllocs(args *structs.AllocsGetRequest,
|
|
|
|
reply *structs.AllocsGetResponse) error {
|
2022-02-05 01:35:20 +00:00
|
|
|
|
|
|
|
// Ensure the connection was initiated by a client if TLS is used.
|
|
|
|
err := validateTLSCertificateLevel(a.srv, a.ctx, tlsCertificateLevelClient)
|
|
|
|
if err != nil {
|
2016-02-01 21:57:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:35:20 +00:00
|
|
|
if done, err := a.srv.forward("Alloc.GetAllocs", args, args, reply); done {
|
|
|
|
return err
|
2022-02-02 20:03:18 +00:00
|
|
|
}
|
2022-02-05 01:35:20 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "get_allocs"}, time.Now())
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
allocs := make([]*structs.Allocation, len(args.AllocIDs))
|
|
|
|
|
2017-01-10 21:25:52 +00:00
|
|
|
// Setup the blocking query. We wait for at least one of the requested
|
|
|
|
// allocations to be above the min query index. This guarantees that the
|
|
|
|
// server has received that index.
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2017-02-08 04:31:23 +00:00
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
2017-01-10 21:25:52 +00:00
|
|
|
// Lookup the allocation
|
|
|
|
thresholdMet := false
|
|
|
|
maxIndex := uint64(0)
|
|
|
|
for i, alloc := range args.AllocIDs {
|
2017-02-08 04:31:23 +00:00
|
|
|
out, err := state.AllocByID(ws, alloc)
|
2017-01-10 21:25:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
// We don't have the alloc yet
|
|
|
|
thresholdMet = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the pointer
|
|
|
|
allocs[i] = out
|
|
|
|
|
|
|
|
// Check if we have passed the minimum index
|
|
|
|
if out.ModifyIndex > args.QueryOptions.MinQueryIndex {
|
|
|
|
thresholdMet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxIndex < out.ModifyIndex {
|
|
|
|
maxIndex = out.ModifyIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the output
|
|
|
|
if thresholdMet {
|
|
|
|
reply.Allocs = allocs
|
|
|
|
reply.Index = maxIndex
|
|
|
|
} else {
|
|
|
|
// Use the last index that affected the nodes table
|
2017-02-08 04:31:23 +00:00
|
|
|
index, err := state.Index("allocs")
|
2017-01-10 21:25:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return a.srv.blockingRPC(&opts)
|
2016-02-01 21:57:35 +00:00
|
|
|
}
|
2018-02-21 18:58:04 +00:00
|
|
|
|
2019-04-01 14:21:03 +00:00
|
|
|
// Stop is used to stop an allocation and migrate it to another node.
|
|
|
|
func (a *Alloc) Stop(args *structs.AllocStopRequest, reply *structs.AllocStopResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := a.srv.Authenticate(a.ctx, args)
|
2019-04-01 14:21:03 +00:00
|
|
|
if done, err := a.srv.forward("Alloc.Stop", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-04-01 14:21:03 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "stop"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(a.srv.State(), args.AllocID)
|
|
|
|
if err != nil {
|
2019-04-01 14:21:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check for namespace alloc-lifecycle permissions.
|
|
|
|
allowNsOp := acl.NamespaceValidator(acl.NamespaceCapabilityAllocLifecycle)
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := a.srv.ResolveACL(args)
|
2019-04-01 14:21:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if !allowNsOp(aclObj, alloc.Namespace) {
|
|
|
|
return structs.ErrPermissionDenied
|
2019-06-20 20:52:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:50:35 +00:00
|
|
|
now := time.Now().UTC().UnixNano()
|
2019-04-01 14:21:03 +00:00
|
|
|
eval := &structs.Evaluation{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: alloc.Namespace,
|
|
|
|
Priority: alloc.Job.Priority,
|
|
|
|
Type: alloc.Job.Type,
|
|
|
|
TriggeredBy: structs.EvalTriggerAllocStop,
|
|
|
|
JobID: alloc.Job.ID,
|
|
|
|
JobModifyIndex: alloc.Job.ModifyIndex,
|
|
|
|
Status: structs.EvalStatusPending,
|
2019-08-07 16:50:35 +00:00
|
|
|
CreateTime: now,
|
|
|
|
ModifyTime: now,
|
2019-04-01 14:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transitionReq := &structs.AllocUpdateDesiredTransitionRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
Allocs: map[string]*structs.DesiredTransition{
|
|
|
|
args.AllocID: {
|
2022-08-17 16:26:34 +00:00
|
|
|
Migrate: pointer.Of(true),
|
|
|
|
NoShutdownDelay: pointer.Of(args.NoShutdownDelay),
|
2019-04-01 14:21:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit this update via Raft
|
|
|
|
_, index, err := a.srv.raftApply(structs.AllocUpdateDesiredTransitionRequestType, transitionReq)
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Error("AllocUpdateDesiredTransitionRequest failed", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the response
|
|
|
|
reply.Index = index
|
|
|
|
reply.EvalID = eval.ID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
// UpdateDesiredTransition is used to update the desired transitions of an
|
2018-02-21 18:58:04 +00:00
|
|
|
// allocation.
|
2018-02-23 01:38:44 +00:00
|
|
|
func (a *Alloc) UpdateDesiredTransition(args *structs.AllocUpdateDesiredTransitionRequest, reply *structs.GenericResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(a.ctx, args)
|
2018-02-23 01:38:44 +00:00
|
|
|
if done, err := a.srv.forward("Alloc.UpdateDesiredTransition", args, args, reply); done {
|
2018-02-21 18:58:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "update_desired_transition"}, time.Now())
|
2018-02-21 18:58:04 +00:00
|
|
|
|
|
|
|
// Check that it is a management token.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2018-02-21 18:58:04 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure at least a single alloc
|
|
|
|
if len(args.Allocs) == 0 {
|
|
|
|
return fmt.Errorf("must update at least one allocation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit this update via Raft
|
2018-02-23 01:38:44 +00:00
|
|
|
_, index, err := a.srv.raftApply(structs.AllocUpdateDesiredTransitionRequestType, args)
|
2018-02-21 18:58:04 +00:00
|
|
|
if err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
a.logger.Error("AllocUpdateDesiredTransitionRequest failed", "error", err)
|
2018-02-21 18:58:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the response
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-03 10:25:55 +00:00
|
|
|
|
|
|
|
// GetServiceRegistrations returns a list of service registrations which belong
|
|
|
|
// to the passed allocation ID.
|
|
|
|
func (a *Alloc) GetServiceRegistrations(
|
|
|
|
args *structs.AllocServiceRegistrationsRequest,
|
|
|
|
reply *structs.AllocServiceRegistrationsResponse) error {
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(a.ctx, args)
|
2022-03-03 10:25:55 +00:00
|
|
|
if done, err := a.srv.forward(structs.AllocServiceRegistrationsRPCMethod, args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2022-03-03 10:25:55 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "get_service_registrations"}, time.Now())
|
|
|
|
|
|
|
|
// If ACLs are enabled, ensure the caller has the read-job namespace
|
|
|
|
// capability.
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := a.srv.ResolveACL(args)
|
2022-03-03 10:25:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil {
|
|
|
|
if !aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityReadJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the blocking query.
|
|
|
|
return a.srv.blockingRPC(&blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, stateStore *state.StateStore) error {
|
|
|
|
|
|
|
|
// Read the allocation to ensure its namespace matches the request
|
|
|
|
// args.
|
|
|
|
alloc, err := stateStore.AllocByID(ws, args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against the alloc not-existing or that the namespace does
|
|
|
|
// not match the request arguments.
|
|
|
|
if alloc == nil || alloc.Namespace != args.RequestNamespace() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the state query to get an iterator.
|
|
|
|
iter, err := stateStore.GetServiceRegistrationsByAllocID(ws, args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up our output after we have checked the error.
|
|
|
|
services := make([]*structs.ServiceRegistration, 0)
|
|
|
|
|
|
|
|
// Iterate the iterator, appending all service registrations
|
|
|
|
// returned to the reply.
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
services = append(services, raw.(*structs.ServiceRegistration))
|
|
|
|
}
|
|
|
|
reply.Services = services
|
|
|
|
|
|
|
|
// Use the index table to populate the query meta as we have no way
|
|
|
|
// of tracking the max index on deletes.
|
|
|
|
return a.srv.setReplyQueryMeta(stateStore, state.TableServiceRegistrations, &reply.QueryMeta)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|