2017-07-28 21:48:15 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-09-15 23:23:13 +00:00
|
|
|
"fmt"
|
2017-08-14 13:34:31 +00:00
|
|
|
"strings"
|
2017-12-20 20:17:28 +00:00
|
|
|
"time"
|
2017-08-14 03:51:47 +00:00
|
|
|
|
2017-12-20 20:17:28 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2017-08-10 19:24:11 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2017-07-28 21:48:15 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2017-08-14 13:34:31 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-07-28 21:48:15 +00:00
|
|
|
)
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
const (
|
2017-08-14 13:34:31 +00:00
|
|
|
// truncateLimit is the maximum number of matches that will be returned for a
|
|
|
|
// prefix for a specific context
|
2017-08-04 22:18:49 +00:00
|
|
|
truncateLimit = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-09-07 23:56:15 +00:00
|
|
|
// ossContexts are the oss contexts which are searched to find matches
|
|
|
|
// for a given prefix
|
2017-10-12 01:05:27 +00:00
|
|
|
ossContexts = []structs.Context{
|
|
|
|
structs.Allocs,
|
|
|
|
structs.Jobs,
|
|
|
|
structs.Nodes,
|
|
|
|
structs.Evals,
|
|
|
|
structs.Deployments,
|
2020-03-06 15:09:10 +00:00
|
|
|
structs.Plugins,
|
|
|
|
structs.Volumes,
|
2021-02-03 19:26:57 +00:00
|
|
|
structs.ScalingPolicies,
|
2020-10-21 04:16:25 +00:00
|
|
|
structs.Namespaces,
|
2017-10-12 01:05:27 +00:00
|
|
|
}
|
2017-08-04 22:18:49 +00:00
|
|
|
)
|
2017-08-03 20:34:56 +00:00
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
// Search endpoint is used to look up matches for a given prefix and context
|
2017-08-10 19:24:11 +00:00
|
|
|
type Search struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
|
|
|
logger log.Logger
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 20:34:56 +00:00
|
|
|
// getMatches extracts matches for an iterator, and returns a list of ids for
|
|
|
|
// these matches.
|
2017-08-10 19:24:11 +00:00
|
|
|
func (s *Search) getMatches(iter memdb.ResultIterator, prefix string) ([]string, bool) {
|
2017-08-02 21:55:48 +00:00
|
|
|
var matches []string
|
|
|
|
|
2017-08-03 20:34:56 +00:00
|
|
|
for i := 0; i < truncateLimit; i++ {
|
2017-08-02 21:55:48 +00:00
|
|
|
raw := iter.Next()
|
|
|
|
if raw == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-08-04 15:08:12 +00:00
|
|
|
var id string
|
2017-08-07 14:16:24 +00:00
|
|
|
switch t := raw.(type) {
|
2017-08-14 13:34:31 +00:00
|
|
|
case *structs.Job:
|
2020-03-06 15:09:10 +00:00
|
|
|
id = t.ID
|
2017-08-14 13:34:31 +00:00
|
|
|
case *structs.Evaluation:
|
2020-03-06 15:09:10 +00:00
|
|
|
id = t.ID
|
2017-08-14 13:34:31 +00:00
|
|
|
case *structs.Allocation:
|
2020-03-06 15:09:10 +00:00
|
|
|
id = t.ID
|
2017-08-14 13:34:31 +00:00
|
|
|
case *structs.Node:
|
2020-03-06 15:09:10 +00:00
|
|
|
id = t.ID
|
2017-08-18 20:06:25 +00:00
|
|
|
case *structs.Deployment:
|
2020-03-06 15:09:10 +00:00
|
|
|
id = t.ID
|
|
|
|
case *structs.CSIPlugin:
|
|
|
|
id = t.ID
|
|
|
|
case *structs.CSIVolume:
|
|
|
|
id = t.ID
|
2021-02-03 19:26:57 +00:00
|
|
|
case *structs.ScalingPolicy:
|
|
|
|
id = t.ID
|
2020-10-21 04:16:25 +00:00
|
|
|
case *structs.Namespace:
|
|
|
|
id = t.Name
|
2017-08-04 15:08:12 +00:00
|
|
|
default:
|
2017-09-07 23:56:15 +00:00
|
|
|
matchID, ok := getEnterpriseMatch(raw)
|
|
|
|
if !ok {
|
2018-09-15 23:23:13 +00:00
|
|
|
s.logger.Error("unexpected type for resources context", "type", fmt.Sprintf("%T", t))
|
2017-09-07 23:56:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id = matchID
|
2017-08-02 21:55:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 13:34:31 +00:00
|
|
|
if !strings.HasPrefix(id, prefix) {
|
2017-08-11 13:33:12 +00:00
|
|
|
continue
|
2017-08-10 14:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:55:48 +00:00
|
|
|
matches = append(matches, id)
|
|
|
|
}
|
|
|
|
|
2017-08-07 17:23:32 +00:00
|
|
|
return matches, iter.Next() != nil
|
2017-08-02 21:55:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 20:34:56 +00:00
|
|
|
// getResourceIter takes a context and returns a memdb iterator specific to
|
|
|
|
// that context
|
2017-10-13 21:36:02 +00:00
|
|
|
func getResourceIter(context structs.Context, aclObj *acl.ACL, namespace, prefix string, ws memdb.WatchSet, state *state.StateStore) (memdb.ResultIterator, error) {
|
2017-08-03 15:59:27 +00:00
|
|
|
switch context {
|
2017-08-14 13:34:31 +00:00
|
|
|
case structs.Jobs:
|
2017-09-07 23:56:15 +00:00
|
|
|
return state.JobsByIDPrefix(ws, namespace, prefix)
|
2017-08-14 13:34:31 +00:00
|
|
|
case structs.Evals:
|
2017-09-07 23:56:15 +00:00
|
|
|
return state.EvalsByIDPrefix(ws, namespace, prefix)
|
2017-08-14 13:34:31 +00:00
|
|
|
case structs.Allocs:
|
2017-09-07 23:56:15 +00:00
|
|
|
return state.AllocsByIDPrefix(ws, namespace, prefix)
|
2017-08-14 13:34:31 +00:00
|
|
|
case structs.Nodes:
|
2017-08-03 15:59:27 +00:00
|
|
|
return state.NodesByIDPrefix(ws, prefix)
|
2017-08-18 20:06:25 +00:00
|
|
|
case structs.Deployments:
|
2017-09-07 23:56:15 +00:00
|
|
|
return state.DeploymentsByIDPrefix(ws, namespace, prefix)
|
2020-03-06 15:09:10 +00:00
|
|
|
case structs.Plugins:
|
|
|
|
return state.CSIPluginsByIDPrefix(ws, prefix)
|
2021-02-03 19:26:57 +00:00
|
|
|
case structs.ScalingPolicies:
|
|
|
|
return state.ScalingPoliciesByIDPrefix(ws, namespace, prefix)
|
2020-03-06 15:09:10 +00:00
|
|
|
case structs.Volumes:
|
|
|
|
return state.CSIVolumesByIDPrefix(ws, namespace, prefix)
|
2020-10-21 04:16:25 +00:00
|
|
|
case structs.Namespaces:
|
|
|
|
iter, err := state.NamespacesByNamePrefix(ws, prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if aclObj == nil {
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
return memdb.NewFilterIterator(iter, namespaceFilter(aclObj)), nil
|
2017-08-03 15:59:27 +00:00
|
|
|
default:
|
2017-10-13 21:36:02 +00:00
|
|
|
return getEnterpriseResourceIter(context, aclObj, namespace, prefix, ws, state)
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 04:16:25 +00:00
|
|
|
// namespaceFilter wraps a namespace iterator with a filter for removing
|
|
|
|
// namespaces the ACL can't access.
|
|
|
|
func namespaceFilter(aclObj *acl.ACL) memdb.FilterFunc {
|
|
|
|
return func(v interface{}) bool {
|
|
|
|
return !aclObj.AllowNamespace(v.(*structs.Namespace).Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:48:02 +00:00
|
|
|
// If the length of a prefix is odd, return a subset to the last even character
|
2017-08-11 13:33:12 +00:00
|
|
|
// This only applies to UUIDs, jobs are excluded
|
2017-08-14 13:34:31 +00:00
|
|
|
func roundUUIDDownIfOdd(prefix string, context structs.Context) string {
|
|
|
|
if context == structs.Jobs {
|
2017-08-10 14:48:02 +00:00
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:21:18 +00:00
|
|
|
// We ignore the count of hyphens when calculating if the prefix is even:
|
|
|
|
// E.g "e3671fa4-21"
|
|
|
|
numHyphens := strings.Count(prefix, "-")
|
|
|
|
l := len(prefix) - numHyphens
|
2017-08-11 13:33:12 +00:00
|
|
|
if l%2 == 0 {
|
2017-08-10 14:48:02 +00:00
|
|
|
return prefix
|
2017-08-08 20:29:02 +00:00
|
|
|
}
|
2017-09-13 17:28:42 +00:00
|
|
|
return prefix[:len(prefix)-1]
|
2017-08-08 20:29:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
// PrefixSearch is used to list matches for a given prefix, and returns
|
|
|
|
// matching jobs, evaluations, allocations, and/or nodes.
|
2017-10-10 22:04:23 +00:00
|
|
|
func (s *Search) PrefixSearch(args *structs.SearchRequest, reply *structs.SearchResponse) error {
|
2017-12-20 20:17:28 +00:00
|
|
|
if done, err := s.srv.forward("Search.PrefixSearch", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-20 22:52:41 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "search", "prefix_search"}, time.Now())
|
2017-12-20 20:17:28 +00:00
|
|
|
|
2017-10-12 22:16:33 +00:00
|
|
|
aclObj, err := s.srv.ResolveToken(args.AuthToken)
|
2017-10-10 22:04:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-12 01:05:27 +00:00
|
|
|
namespace := args.RequestNamespace()
|
2017-10-10 22:04:23 +00:00
|
|
|
|
2017-10-12 01:05:27 +00:00
|
|
|
// Require either node:read or namespace:read-job
|
|
|
|
if !anySearchPerms(aclObj, namespace, args.Context) {
|
|
|
|
return structs.ErrPermissionDenied
|
2017-10-10 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 13:34:31 +00:00
|
|
|
reply.Matches = make(map[structs.Context][]string)
|
|
|
|
reply.Truncations = make(map[structs.Context]bool)
|
2017-07-28 21:48:15 +00:00
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2017-08-14 13:34:31 +00:00
|
|
|
queryOpts: &structs.QueryOptions{},
|
2017-07-28 21:48:15 +00:00
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
|
2017-08-14 13:34:31 +00:00
|
|
|
iters := make(map[structs.Context]memdb.ResultIterator)
|
2017-08-03 15:59:27 +00:00
|
|
|
|
2017-10-12 01:05:27 +00:00
|
|
|
contexts := searchContexts(aclObj, namespace, args.Context)
|
2017-08-04 22:18:49 +00:00
|
|
|
|
2017-08-14 13:34:31 +00:00
|
|
|
for _, ctx := range contexts {
|
2017-10-13 21:36:02 +00:00
|
|
|
iter, err := getResourceIter(ctx, aclObj, namespace, roundUUIDDownIfOdd(args.Prefix, args.Context), ws, state)
|
2017-08-03 15:59:27 +00:00
|
|
|
if err != nil {
|
2017-08-30 17:51:05 +00:00
|
|
|
e := err.Error()
|
|
|
|
switch {
|
2017-08-18 20:27:53 +00:00
|
|
|
// Searching other contexts with job names raises an error, which in
|
|
|
|
// this case we want to ignore.
|
2017-08-30 17:51:05 +00:00
|
|
|
case strings.Contains(e, "Invalid UUID: encoding/hex"):
|
|
|
|
case strings.Contains(e, "UUID have 36 characters"):
|
2017-09-13 17:28:42 +00:00
|
|
|
case strings.Contains(e, "must be even length"):
|
|
|
|
case strings.Contains(e, "UUID should have maximum of 4"):
|
2017-08-30 17:51:05 +00:00
|
|
|
default:
|
2017-08-18 20:27:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-08-15 17:43:50 +00:00
|
|
|
} else {
|
|
|
|
iters[ctx] = iter
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:18:46 +00:00
|
|
|
// Return matches for the given prefix
|
2017-08-03 15:59:27 +00:00
|
|
|
for k, v := range iters {
|
2017-08-10 19:24:11 +00:00
|
|
|
res, isTrunc := s.getMatches(v, args.Prefix)
|
2017-08-03 15:59:27 +00:00
|
|
|
reply.Matches[k] = res
|
|
|
|
reply.Truncations[k] = isTrunc
|
2017-08-02 21:55:48 +00:00
|
|
|
}
|
2017-08-03 14:28:38 +00:00
|
|
|
|
2017-08-04 15:08:12 +00:00
|
|
|
// Set the index for the context. If the context has been specified, it
|
2017-08-07 15:07:18 +00:00
|
|
|
// will be used as the index of the response. Otherwise, the
|
|
|
|
// maximum index from all resources will be used.
|
2017-08-14 13:34:31 +00:00
|
|
|
for _, ctx := range contexts {
|
2017-10-13 21:36:02 +00:00
|
|
|
index, err := state.Index(contextToIndex(ctx))
|
2017-08-07 15:07:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if index > reply.Index {
|
|
|
|
reply.Index = index
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 15:12:14 +00:00
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
s.srv.setQueryMeta(&reply.QueryMeta)
|
2017-07-28 21:48:15 +00:00
|
|
|
return nil
|
|
|
|
}}
|
2017-08-10 19:24:11 +00:00
|
|
|
return s.srv.blockingRPC(&opts)
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|