2017-07-28 21:48:15 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2017-08-14 13:34:31 +00:00
|
|
|
"strings"
|
2017-08-14 03:51:47 +00:00
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
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
|
|
|
|
ossContexts = []structs.Context{structs.Allocs, structs.Jobs, structs.Nodes,
|
2017-08-18 20:06:25 +00:00
|
|
|
structs.Evals, structs.Deployments}
|
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 {
|
2017-07-28 21:48:15 +00:00
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
id = raw.(*structs.Job).ID
|
|
|
|
case *structs.Evaluation:
|
|
|
|
id = raw.(*structs.Evaluation).ID
|
|
|
|
case *structs.Allocation:
|
|
|
|
id = raw.(*structs.Allocation).ID
|
|
|
|
case *structs.Node:
|
|
|
|
id = raw.(*structs.Node).ID
|
2017-08-18 20:06:25 +00:00
|
|
|
case *structs.Deployment:
|
|
|
|
id = raw.(*structs.Deployment).ID
|
2017-08-04 15:08:12 +00:00
|
|
|
default:
|
2017-09-07 23:56:15 +00:00
|
|
|
matchID, ok := getEnterpriseMatch(raw)
|
|
|
|
if !ok {
|
|
|
|
s.srv.logger.Printf("[ERR] nomad.resources: unexpected type for resources context: %T", t)
|
|
|
|
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-09-07 23:56:15 +00:00
|
|
|
func getResourceIter(context structs.Context, 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)
|
2017-08-03 15:59:27 +00:00
|
|
|
default:
|
2017-09-07 23:56:15 +00:00
|
|
|
return getEnterpriseResourceIter(context, namespace, prefix, ws, state)
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-14 13:34:31 +00:00
|
|
|
func (s *Search) PrefixSearch(args *structs.SearchRequest,
|
|
|
|
reply *structs.SearchResponse) error {
|
|
|
|
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-08-04 22:18:49 +00:00
|
|
|
contexts := allContexts
|
2017-08-14 13:34:31 +00:00
|
|
|
if args.Context != structs.All {
|
|
|
|
contexts = []structs.Context{args.Context}
|
2017-08-04 22:18:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 13:34:31 +00:00
|
|
|
for _, ctx := range contexts {
|
2017-09-07 23:56:15 +00:00
|
|
|
iter, err := getResourceIter(ctx, args.RequestNamespace(), roundUUIDDownIfOdd(args.Prefix, args.Context), ws, state)
|
2017-08-15 17:43:50 +00:00
|
|
|
|
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 {
|
|
|
|
index, err := state.Index(string(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
|
|
|
}
|