Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement filtering of some endpoints #5579

Merged
merged 62 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
11e1411
Initial filtering implementation
mkeeler Mar 28, 2019
8a5652c
Merge branch 'master' of github.com:hashicorp/consul into filtering
mkeeler Mar 28, 2019
7db84d1
Update vendoring
mkeeler Mar 28, 2019
f17beca
Fixup go.sum
mkeeler Mar 28, 2019
43aa3de
Make the agent cache key take the filter into account
mkeeler Mar 29, 2019
f94fd7b
Merge remote-tracking branch 'origin/master' into filtering
mkeeler Mar 29, 2019
008e3ce
Pull in go-bexpr updates
mkeeler Mar 29, 2019
e312540
Implement tests to ensure the expected filter fields are correct.
mkeeler Mar 29, 2019
cec6e1f
Fix a ui endpoint test and add an /agent/checks filtering test
mkeeler Mar 29, 2019
43eec2c
Add /agent/services filtering test
mkeeler Mar 29, 2019
a2d172b
Add /catalog/nodes filter test
mkeeler Apr 1, 2019
e08274a
Implement a test for /catalog/service/:service
mkeeler Apr 1, 2019
edb77fe
Add test for /catalog/connect/:service filtering
mkeeler Apr 1, 2019
3f683f5
Add test for /catalog/node/:node filtering
mkeeler Apr 1, 2019
2d95577
Add /health/node/:node filtering test
mkeeler Apr 1, 2019
b1375da
tidy the go.sum file
mkeeler Apr 1, 2019
788dde0
Fixup comment
mkeeler Apr 1, 2019
5b6d418
Add test for /health/checks/:service filtering
mkeeler Apr 1, 2019
4665b6b
Add test for /health/service/:service filtering
mkeeler Apr 1, 2019
5a07080
Add test for /health/connect/:service filtering
mkeeler Apr 1, 2019
2c33792
Add test for /health/state/:state filtering
mkeeler Apr 1, 2019
e8ec84b
Add test for /ui/internal/nodes filtering
mkeeler Apr 1, 2019
6037b90
Add test for /ui/internal/services filtering
mkeeler Apr 1, 2019
d3af587
Add Agent.(Checks|Services)WithFilter functions
mkeeler Apr 1, 2019
1cf25c0
Add filtering to catalog API
mkeeler Apr 1, 2019
3565d9d
Get rid of the retry.Run in the new catalog API tests
mkeeler Apr 2, 2019
fd9573c
Add filtering to the health API
mkeeler Apr 2, 2019
f288c4d
Add filter to consul catalog nodes command
mkeeler Apr 2, 2019
6317e92
Implement filtering test for the Catalog RPC endpoints.
mkeeler Apr 2, 2019
88163c3
Merge remote-tracking branch 'origin/master' into filtering
mkeeler Apr 2, 2019
aef945b
Update API vendoring
mkeeler Apr 2, 2019
131d49b
Add Health RPC filtering tests
mkeeler Apr 3, 2019
2ee4145
Implement filtering tests for internal RPC endpoints
mkeeler Apr 3, 2019
46d0cc8
Update go-bexpr dependency to v0.1.0
mkeeler Apr 3, 2019
609568a
Fix accidental modification
mkeeler Apr 3, 2019
ef51dce
API endpoint filtering documentation.
mkeeler Apr 3, 2019
a3e1719
First pass at top level filtering docs
mkeeler Apr 4, 2019
c8bdc82
Add some information about selectors and values.
mkeeler Apr 5, 2019
58c222b
Add test for the returned data of the /agent/services endpoint for fi…
mkeeler Apr 5, 2019
31755e0
Update docs to be more explicit about which part of the data is getti…
mkeeler Apr 5, 2019
716807b
Fix a couple of typos
mkeeler Apr 5, 2019
bcff054
Revendor the api :(
mkeeler Apr 5, 2019
a8f0aea
Support passing the filter from stdin or a file as well.
mkeeler Apr 5, 2019
8dcd499
Update catalog nodes command docs
mkeeler Apr 5, 2019
fe7ccde
Fix typo
mkeeler Apr 5, 2019
c981ff0
Updated the structure of the API site. Still working on the filtering…
kaitlincart Apr 5, 2019
9a3cf01
Merge branch 'filtering' of github.com:hashicorp/consul into filtering
kaitlincart Apr 5, 2019
2ca5876
Remove filter parsing from endpoint we are not enabling it for yet.
mkeeler Apr 9, 2019
fafe6e7
Merge branch 'filtering' of github.com:hashicorp/consul into filtering
mkeeler Apr 9, 2019
17eb199
updating the language
kaitlincart Apr 9, 2019
30eabd6
Merge branch 'filtering' of github.com:hashicorp/consul into filtering
kaitlincart Apr 9, 2019
da76f9c
adding example sections
kaitlincart Apr 9, 2019
6933a9b
Some filtering doc updates
mkeeler Apr 11, 2019
8d93781
Some formatting updates
mkeeler Apr 11, 2019
00c298f
Minor doc update
mkeeler Apr 11, 2019
24ed818
Wording update
mkeeler Apr 11, 2019
d2e6851
Update website/source/api/catalog.html.md
rboyer Apr 15, 2019
051e3ec
Update website/source/api/features/filtering.html.md
rboyer Apr 15, 2019
e0ddb55
Ride the Filter within QueryOptions both for RPCs and in the API pakage
mkeeler Apr 15, 2019
1136dae
Merge branch 'filtering' of github.com:hashicorp/consul into filtering
mkeeler Apr 15, 2019
4ad9824
Move filter parsing into generic parse function.
mkeeler Apr 15, 2019
ae04743
Fix some tests
mkeeler Apr 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/hashicorp/consul/lib/file"
"github.com/hashicorp/consul/logger"
"github.com/hashicorp/consul/types"
bexpr "github.com/hashicorp/go-bexpr"
"github.com/hashicorp/logutils"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
Expand Down Expand Up @@ -219,6 +220,9 @@ func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request)
var token string
s.parseToken(req, &token)

var filterExpression string
s.parseFilter(req, &filterExpression)

services := s.agent.State.Services()
if err := s.agent.filterServices(token, &services); err != nil {
return nil, err
Expand All @@ -238,7 +242,12 @@ func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request)
agentSvcs[id] = &agentService
}

return agentSvcs, nil
filter, err := bexpr.CreateFilter(filterExpression, nil, agentSvcs)
if err != nil {
return nil, err
}

return filter.Execute(agentSvcs)
}

// GET /v1/agent/service/:service_id
Expand Down Expand Up @@ -403,6 +412,13 @@ func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (i
var token string
s.parseToken(req, &token)

var filterExpression string
s.parseFilter(req, &filterExpression)
filter, err := bexpr.CreateFilter(filterExpression, nil, nil)
if err != nil {
return nil, err
}

checks := s.agent.State.Checks()
if err := s.agent.filterChecks(token, &checks); err != nil {
return nil, err
Expand All @@ -417,14 +433,17 @@ func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (i
}
}

return checks, nil
return filter.Execute(checks)
}

func (s *HTTPServer) AgentMembers(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
// Fetch the ACL token, if any.
var token string
s.parseToken(req, &token)

var filterExpression string
s.parseFilter(req, &filterExpression)

// Check if the WAN is being queried
wan := false
if other := req.URL.Query().Get("wan"); other != "" {
Expand Down
4 changes: 4 additions & 0 deletions agent/catalog_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (s *HTTPServer) CatalogNodes(resp http.ResponseWriter, req *http.Request) (
// Setup the request
args := structs.DCSpecificRequest{}
s.parseSource(req, &args.Source)
s.parseFilter(req, &args.Filter)
args.NodeMetaFilters = s.parseMetaFilter(req)
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_nodes"}, 1,
Expand Down Expand Up @@ -180,6 +181,7 @@ func (s *HTTPServer) catalogServiceNodes(resp http.ResponseWriter, req *http.Req
// Set default DC
args := structs.ServiceSpecificRequest{Connect: connect}
s.parseSource(req, &args.Source)
s.parseFilter(req, &args.Filter)
args.NodeMetaFilters = s.parseMetaFilter(req)
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
Expand Down Expand Up @@ -261,6 +263,8 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req
return nil, nil
}

s.parseFilter(req, &args.Filter)

// Pull out the node name
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/catalog/node/")
if args.Node == "" {
Expand Down
50 changes: 48 additions & 2 deletions agent/consul/catalog_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/ipaddr"
"github.com/hashicorp/consul/types"
bexpr "github.com/hashicorp/go-bexpr"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-uuid"
)
Expand Down Expand Up @@ -219,6 +220,11 @@ func (c *Catalog) ListNodes(args *structs.DCSpecificRequest, reply *structs.Inde
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
if err != nil {
return err
}

return c.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -239,6 +245,13 @@ func (c *Catalog) ListNodes(args *structs.DCSpecificRequest, reply *structs.Inde
if err := c.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.Nodes)
if err != nil {
return err
}
reply.Nodes = raw.(structs.Nodes)

return c.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
})
}
Expand Down Expand Up @@ -327,7 +340,12 @@ func (c *Catalog) ServiceNodes(args *structs.ServiceSpecificRequest, reply *stru
}
}

err := c.srv.blockingQuery(
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.ServiceNodes)
if err != nil {
return err
}

err = c.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
func(ws memdb.WatchSet, state *state.Store) error {
Expand All @@ -346,9 +364,19 @@ func (c *Catalog) ServiceNodes(args *structs.ServiceSpecificRequest, reply *stru
}
reply.ServiceNodes = filtered
}

if err := c.srv.filterACL(args.Token, reply); err != nil {
return err
}

// This is safe to do even when the filter is nil - its just a no-op then
raw, err := filter.Execute(reply.ServiceNodes)
if err != nil {
return err
}

reply.ServiceNodes = raw.(structs.ServiceNodes)

return c.srv.sortNodesByDistanceFrom(args.Source, reply.ServiceNodes)
})

Expand Down Expand Up @@ -400,6 +428,12 @@ func (c *Catalog) NodeServices(args *structs.NodeSpecificRequest, reply *structs
return fmt.Errorf("Must provide node")
}

var filterType map[string]*structs.NodeService
filter, err := bexpr.CreateFilter(args.Filter, nil, filterType)
if err != nil {
return err
}

return c.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -410,6 +444,18 @@ func (c *Catalog) NodeServices(args *structs.NodeSpecificRequest, reply *structs
}

reply.Index, reply.NodeServices = index, services
return c.srv.filterACL(args.Token, reply)
if err := c.srv.filterACL(args.Token, reply); err != nil {
return err
}

if reply.NodeServices != nil {
raw, err := filter.Execute(reply.NodeServices.Services)
if err != nil {
return err
}
reply.NodeServices.Services = raw.(map[string]*structs.NodeService)
}

return nil
})
}
56 changes: 54 additions & 2 deletions agent/consul/health_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/armon/go-metrics"
"github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/agent/structs"
bexpr "github.com/hashicorp/go-bexpr"
"github.com/hashicorp/go-memdb"
)

Expand All @@ -22,6 +23,11 @@ func (h *Health) ChecksInState(args *structs.ChecksInStateRequest,
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
if err != nil {
return err
}

return h.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -41,6 +47,13 @@ func (h *Health) ChecksInState(args *structs.ChecksInStateRequest,
if err := h.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.HealthChecks)
if err != nil {
return err
}
reply.HealthChecks = raw.(structs.HealthChecks)

return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
})
}
Expand All @@ -52,6 +65,11 @@ func (h *Health) NodeChecks(args *structs.NodeSpecificRequest,
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
if err != nil {
return err
}

return h.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -61,7 +79,16 @@ func (h *Health) NodeChecks(args *structs.NodeSpecificRequest,
return err
}
reply.Index, reply.HealthChecks = index, checks
return h.srv.filterACL(args.Token, reply)
if err := h.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.HealthChecks)
if err != nil {
return err
}
reply.HealthChecks = raw.(structs.HealthChecks)
return nil
})
}

Expand All @@ -78,6 +105,11 @@ func (h *Health) ServiceChecks(args *structs.ServiceSpecificRequest,
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
if err != nil {
return err
}

return h.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -97,6 +129,13 @@ func (h *Health) ServiceChecks(args *structs.ServiceSpecificRequest,
if err := h.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.HealthChecks)
if err != nil {
return err
}
reply.HealthChecks = raw.(structs.HealthChecks)

return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
})
}
Expand Down Expand Up @@ -138,7 +177,12 @@ func (h *Health) ServiceNodes(args *structs.ServiceSpecificRequest, reply *struc
}
}

err := h.srv.blockingQuery(
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
if err != nil {
return err
}

err = h.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
func(ws memdb.WatchSet, state *state.Store) error {
Expand All @@ -151,9 +195,17 @@ func (h *Health) ServiceNodes(args *structs.ServiceSpecificRequest, reply *struc
if len(args.NodeMetaFilters) > 0 {
reply.Nodes = nodeMetaFilter(args.NodeMetaFilters, reply.Nodes)
}

if err := h.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.Nodes)
if err != nil {
return err
}
reply.Nodes = raw.(structs.CheckServiceNodes)

return h.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
})

Expand Down
52 changes: 51 additions & 1 deletion agent/consul/internal_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/agent/structs"
bexpr "github.com/hashicorp/go-bexpr"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/serf/serf"
Expand Down Expand Up @@ -46,6 +47,11 @@ func (m *Internal) NodeDump(args *structs.DCSpecificRequest,
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Dump)
if err != nil {
return err
}

return m.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
Expand All @@ -56,7 +62,51 @@ func (m *Internal) NodeDump(args *structs.DCSpecificRequest,
}

reply.Index, reply.Dump = index, dump
return m.srv.filterACL(args.Token, reply)
if err := m.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.Dump)
if err != nil {
return err
}

reply.Dump = raw.(structs.NodeDump)
return nil
})
}

func (m *Internal) ServiceDump(args *structs.DCSpecificRequest, reply *structs.IndexedCheckServiceNodes) error {
if done, err := m.srv.forward("Internal.ServiceDump", args, args, reply); done {
return err
}

filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
if err != nil {
return err
}

return m.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
func(ws memdb.WatchSet, state *state.Store) error {
index, nodes, err := state.ServiceDump(ws)
if err != nil {
return err
}

reply.Index, reply.Nodes = index, nodes
if err := m.srv.filterACL(args.Token, reply); err != nil {
return err
}

raw, err := filter.Execute(reply.Nodes)
if err != nil {
return err
}

reply.Nodes = raw.(structs.CheckServiceNodes)
return nil
})
}

Expand Down
Loading