Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nomad
Commits
b9852423
Commit
b9852423
authored
7 years ago
by
Chelsea Komlo
Committed by
GitHub
7 years ago
Browse files
Options
Download
Plain Diff
Merge pull request #3068 from hashicorp/f-add-deployments-search-api
Add deployments to search api
parents
993f4d94
9051c7ed
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
command/agent/search_endpoint_test.go
+31
-0
command/agent/search_endpoint_test.go
nomad/search_endpoint.go
+6
-1
nomad/search_endpoint.go
nomad/search_endpoint_test.go
+33
-0
nomad/search_endpoint_test.go
nomad/structs/structs.go
+9
-7
nomad/structs/structs.go
with
79 additions
and
8 deletions
+79
-8
command/agent/search_endpoint_test.go
+
31
-
0
View file @
b9852423
...
@@ -241,6 +241,37 @@ func TestHTTP_Search_Nodes(t *testing.T) {
...
@@ -241,6 +241,37 @@ func TestHTTP_Search_Nodes(t *testing.T) {
})
})
}
}
func
TestHTTP_Search_Deployments
(
t
*
testing
.
T
)
{
assert
:=
assert
.
New
(
t
)
t
.
Parallel
()
httpTest
(
t
,
nil
,
func
(
s
*
TestAgent
)
{
state
:=
s
.
Agent
.
server
.
State
()
deployment
:=
mock
.
Deployment
()
assert
.
Nil
(
state
.
UpsertDeployment
(
999
,
deployment
),
"UpsertDeployment"
)
prefix
:=
deployment
.
ID
[
:
len
(
deployment
.
ID
)
-
2
]
data
:=
structs
.
SearchRequest
{
Prefix
:
prefix
,
Context
:
structs
.
Deployments
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
"/v1/search"
,
encodeReq
(
data
))
assert
.
Nil
(
err
)
respW
:=
httptest
.
NewRecorder
()
resp
,
err
:=
s
.
Server
.
SearchRequest
(
respW
,
req
)
assert
.
Nil
(
err
)
res
:=
resp
.
(
structs
.
SearchResponse
)
assert
.
Equal
(
1
,
len
(
res
.
Matches
))
n
:=
res
.
Matches
[
structs
.
Deployments
]
assert
.
Equal
(
1
,
len
(
n
))
assert
.
Contains
(
n
,
deployment
.
ID
)
assert
.
Equal
(
"999"
,
respW
.
HeaderMap
.
Get
(
"X-Nomad-Index"
))
})
}
func
TestHTTP_Search_NoJob
(
t
*
testing
.
T
)
{
func
TestHTTP_Search_NoJob
(
t
*
testing
.
T
)
{
assert
:=
assert
.
New
(
t
)
assert
:=
assert
.
New
(
t
)
...
...
This diff is collapsed.
Click to expand it.
nomad/search_endpoint.go
+
6
-
1
View file @
b9852423
...
@@ -18,7 +18,8 @@ const (
...
@@ -18,7 +18,8 @@ const (
var
(
var
(
// allContexts are the available contexts which are searched to find matches
// allContexts are the available contexts which are searched to find matches
// for a given prefix
// for a given prefix
allContexts
=
[]
structs
.
Context
{
structs
.
Allocs
,
structs
.
Jobs
,
structs
.
Nodes
,
structs
.
Evals
}
allContexts
=
[]
structs
.
Context
{
structs
.
Allocs
,
structs
.
Jobs
,
structs
.
Nodes
,
structs
.
Evals
,
structs
.
Deployments
}
)
)
// Search endpoint is used to look up matches for a given prefix and context
// Search endpoint is used to look up matches for a given prefix and context
...
@@ -47,6 +48,8 @@ func (s *Search) getMatches(iter memdb.ResultIterator, prefix string) ([]string,
...
@@ -47,6 +48,8 @@ func (s *Search) getMatches(iter memdb.ResultIterator, prefix string) ([]string,
id
=
raw
.
(
*
structs
.
Allocation
)
.
ID
id
=
raw
.
(
*
structs
.
Allocation
)
.
ID
case
*
structs
.
Node
:
case
*
structs
.
Node
:
id
=
raw
.
(
*
structs
.
Node
)
.
ID
id
=
raw
.
(
*
structs
.
Node
)
.
ID
case
*
structs
.
Deployment
:
id
=
raw
.
(
*
structs
.
Deployment
)
.
ID
default
:
default
:
s
.
srv
.
logger
.
Printf
(
"[ERR] nomad.resources: unexpected type for resources context: %T"
,
t
)
s
.
srv
.
logger
.
Printf
(
"[ERR] nomad.resources: unexpected type for resources context: %T"
,
t
)
continue
continue
...
@@ -74,6 +77,8 @@ func getResourceIter(context structs.Context, prefix string, ws memdb.WatchSet,
...
@@ -74,6 +77,8 @@ func getResourceIter(context structs.Context, prefix string, ws memdb.WatchSet,
return
state
.
AllocsByIDPrefix
(
ws
,
prefix
)
return
state
.
AllocsByIDPrefix
(
ws
,
prefix
)
case
structs
.
Nodes
:
case
structs
.
Nodes
:
return
state
.
NodesByIDPrefix
(
ws
,
prefix
)
return
state
.
NodesByIDPrefix
(
ws
,
prefix
)
case
structs
.
Deployments
:
return
state
.
DeploymentsByIDPrefix
(
ws
,
prefix
)
default
:
default
:
return
nil
,
fmt
.
Errorf
(
"context must be one of %v; got %q"
,
allContexts
,
context
)
return
nil
,
fmt
.
Errorf
(
"context must be one of %v; got %q"
,
allContexts
,
context
)
}
}
...
...
This diff is collapsed.
Click to expand it.
nomad/search_endpoint_test.go
+
33
-
0
View file @
b9852423
...
@@ -199,6 +199,39 @@ func TestSearch_PrefixSearch_Node(t *testing.T) {
...
@@ -199,6 +199,39 @@ func TestSearch_PrefixSearch_Node(t *testing.T) {
assert
.
Equal
(
uint64
(
100
),
resp
.
Index
)
assert
.
Equal
(
uint64
(
100
),
resp
.
Index
)
}
}
func
TestSearch_PrefixSearch_Deployment
(
t
*
testing
.
T
)
{
assert
:=
assert
.
New
(
t
)
t
.
Parallel
()
s
:=
testServer
(
t
,
func
(
c
*
Config
)
{
c
.
NumSchedulers
=
0
})
defer
s
.
Shutdown
()
codec
:=
rpcClient
(
t
,
s
)
testutil
.
WaitForLeader
(
t
,
s
.
RPC
)
deployment
:=
mock
.
Deployment
()
s
.
fsm
.
State
()
.
UpsertDeployment
(
2000
,
deployment
)
prefix
:=
deployment
.
ID
[
:
len
(
deployment
.
ID
)
-
2
]
req
:=
&
structs
.
SearchRequest
{
Prefix
:
prefix
,
Context
:
structs
.
Deployments
,
}
var
resp
structs
.
SearchResponse
if
err
:=
msgpackrpc
.
CallWithCodec
(
codec
,
"Search.PrefixSearch"
,
req
,
&
resp
);
err
!=
nil
{
t
.
Fatalf
(
"err: %v"
,
err
)
}
assert
.
Equal
(
1
,
len
(
resp
.
Matches
[
structs
.
Deployments
]))
assert
.
Equal
(
deployment
.
ID
,
resp
.
Matches
[
structs
.
Deployments
][
0
])
assert
.
Equal
(
resp
.
Truncations
[
structs
.
Deployments
],
false
)
assert
.
Equal
(
uint64
(
2000
),
resp
.
Index
)
}
func
TestSearch_PrefixSearch_AllContext
(
t
*
testing
.
T
)
{
func
TestSearch_PrefixSearch_AllContext
(
t
*
testing
.
T
)
{
assert
:=
assert
.
New
(
t
)
assert
:=
assert
.
New
(
t
)
t
.
Parallel
()
t
.
Parallel
()
...
...
This diff is collapsed.
Click to expand it.
nomad/structs/structs.go
+
9
-
7
View file @
b9852423
...
@@ -23,7 +23,7 @@ import (
...
@@ -23,7 +23,7 @@ import (
"github.com/gorhill/cronexpr"
"github.com/gorhill/cronexpr"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
version
"github.com/hashicorp/go-version"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/helper/args"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/copystructure"
...
@@ -89,15 +89,17 @@ const (
...
@@ -89,15 +89,17 @@ const (
GetterModeDir
=
"dir"
GetterModeDir
=
"dir"
)
)
// Context defines the scope in which a search for Nomad object operates
// Context defines the scope in which a search for Nomad object operates, and
// is also used to query the matching index value for this context
type
Context
string
type
Context
string
const
(
const
(
Allocs
Context
=
"allocs"
Allocs
Context
=
"allocs"
Evals
Context
=
"evals"
Deployments
Context
=
"deployment"
Jobs
Context
=
"jobs"
Evals
Context
=
"evals"
Nodes
Context
=
"nodes"
Jobs
Context
=
"jobs"
All
Context
=
""
Nodes
Context
=
"nodes"
All
Context
=
""
)
)
// RPCInfo is used to describe common information about query
// RPCInfo is used to describe common information about query
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment