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

NET-4135 - Fix NodeMeta filtering Catalog List Services API #18322

Merged
merged 40 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cef8e3d
logs for debugging
absolutelightning Jul 28, 2023
79fdf19
Init
absolutelightning Jul 28, 2023
91efd2c
Merge branch 'main' into NET-4135
absolutelightning Jul 28, 2023
4fa91a6
white spaces fix
absolutelightning Jul 28, 2023
92ed62d
added change log
absolutelightning Jul 28, 2023
1a53944
Fix tests
absolutelightning Jul 28, 2023
c7c5876
fix typo
absolutelightning Jul 28, 2023
db1039a
using queryoptionfilter to populate args.filter
absolutelightning Jul 29, 2023
e475fa6
Merge branch 'main' of ssh://github.com/hashicorp/consul into NET-4135
absolutelightning Sep 15, 2023
dc4f9d1
Merge branch 'main' of ssh://github.com/hashicorp/consul into NET-4135
absolutelightning Sep 16, 2023
ecc8dbf
tests
absolutelightning Sep 16, 2023
b8dfd87
fix test
absolutelightning Sep 16, 2023
174f8b0
fix tests
absolutelightning Sep 16, 2023
6d1cee7
fix tests
absolutelightning Sep 16, 2023
8b21150
fix tests
absolutelightning Sep 16, 2023
6d8d396
fix tests
absolutelightning Sep 16, 2023
f63c251
fix variable name
absolutelightning Sep 18, 2023
7f6c537
fix tests
absolutelightning Sep 18, 2023
6c08f9c
fix tests
absolutelightning Sep 18, 2023
44f366b
fix tests
absolutelightning Sep 18, 2023
8c12cbb
Merge branch 'main' into NET-4135
absolutelightning Sep 18, 2023
961eeef
Update .changelog/18322.txt
absolutelightning Sep 18, 2023
5af1b1c
fix change log
absolutelightning Sep 18, 2023
1763589
address nits
absolutelightning Sep 18, 2023
f475356
removed unused line
absolutelightning Sep 18, 2023
86b1e28
Merge branch 'main' into NET-4135
absolutelightning Sep 18, 2023
aaf6e54
Merge branch 'main' into NET-4135
absolutelightning Sep 19, 2023
4ed56cc
doing join only when filter has nodemeta
absolutelightning Sep 20, 2023
eaed33c
Merge branch 'NET-4135' of ssh://github.com/hashicorp/consul into NET…
absolutelightning Sep 20, 2023
5d366e8
Merge branch 'main' into NET-4135
absolutelightning Sep 20, 2023
7a95115
fix tests
absolutelightning Sep 20, 2023
96421c2
Merge branch 'NET-4135' of ssh://github.com/hashicorp/consul into NET…
absolutelightning Sep 20, 2023
94e73db
fix tests
absolutelightning Sep 20, 2023
b7aa37e
Update agent/consul/catalog_endpoint.go
absolutelightning Sep 26, 2023
89ed772
fix tests
absolutelightning Sep 26, 2023
b25d920
Merge branch 'main' into NET-4135
absolutelightning Sep 26, 2023
1141444
removed unwanted code
absolutelightning Sep 26, 2023
af363ff
Merge branch 'NET-4135' of ssh://github.com/hashicorp/consul into NET…
absolutelightning Sep 26, 2023
ebcf3c3
Merge branch 'main' into NET-4135
absolutelightning Sep 27, 2023
04aae7e
Merge branch 'main' into NET-4135
absolutelightning Oct 8, 2023
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
3 changes: 3 additions & 0 deletions .changelog/18322.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
catalog api: fixes a bug with catalog api where filter query parameter was not working correctly for the `/v1/catalog/services` endpoint
```
2 changes: 1 addition & 1 deletion agent/consul/catalog_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func (c *Catalog) ListServices(args *structs.DCSpecificRequest, reply *structs.I
if len(args.NodeMetaFilters) > 0 {
reply.Index, serviceNodes, err = state.ServicesByNodeMeta(ws, args.NodeMetaFilters, &args.EnterpriseMeta, args.PeerName)
} else {
reply.Index, serviceNodes, err = state.Services(ws, &args.EnterpriseMeta, args.PeerName)
reply.Index, serviceNodes, err = state.Services(ws, &args.EnterpriseMeta, args.PeerName, args.Filter != "")
}
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion agent/consul/state/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ func terminatingGatewayVirtualIPsSupported(tx ReadTxn, ws memdb.WatchSet) (bool,
}

// Services returns all services along with a list of associated tags.
func (s *Store) Services(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta, peerName string) (uint64, []*structs.ServiceNode, error) {
func (s *Store) Services(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta, peerName string, joinServiceNodes bool) (uint64, structs.ServiceNodes, error) {
tx := s.db.Txn(false)
defer tx.Abort()

Expand All @@ -1236,6 +1236,13 @@ func (s *Store) Services(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta, peerNam
for service := services.Next(); service != nil; service = services.Next() {
result = append(result, service.(*structs.ServiceNode))
}
if joinServiceNodes {
parsedResult, err := parseServiceNodes(tx, ws, result, entMeta, peerName)
if err != nil {
return 0, nil, fmt.Errorf("failed querying and parsing services :%s", err)
}
return idx, parsedResult, nil
}
return idx, result, nil
}

Expand Down
6 changes: 3 additions & 3 deletions agent/consul/state/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ func TestStateStore_Services(t *testing.T) {

// Listing with no results returns an empty list.
ws := memdb.NewWatchSet()
idx, services, err := s.Services(ws, nil, "")
idx, services, err := s.Services(ws, nil, "", false)
if err != nil {
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -2223,7 +2223,7 @@ func TestStateStore_Services(t *testing.T) {

// Pull all the services.
ws = memdb.NewWatchSet()
idx, services, err = s.Services(ws, nil, "")
idx, services, err = s.Services(ws, nil, "", false)
if err != nil {
t.Fatalf("err: %s", err)
}
Expand All @@ -2232,7 +2232,7 @@ func TestStateStore_Services(t *testing.T) {
}

// Verify the result.
expected := []*structs.ServiceNode{
expected := structs.ServiceNodes{
ns1Dogs.ToServiceNode("node1"),
ns1.ToServiceNode("node1"),
ns2.ToServiceNode("node2"),
Expand Down
67 changes: 67 additions & 0 deletions api/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,73 @@ func TestAPI_CatalogServices_NodeMetaFilter(t *testing.T) {
})
}

func TestAPI_CatalogServices_FilterExpr_NodeMeta(t *testing.T) {
t.Parallel()
meta := map[string]string{"somekey": "somevalue", "synthetic": "true"}
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
conf.NodeMeta = meta
})
defer s.Stop()

catalog := c.Catalog()
// Make sure we get the service back when filtering by filter expression
retry.Run(t, func(r *retry.R) {
absolutelightning marked this conversation as resolved.
Show resolved Hide resolved
services, meta, err := catalog.Services(&QueryOptions{Filter: "NodeMeta[\"synthetic\"] == true and NodeMeta[\"somekey\"] == somevalue"})
if err != nil {
r.Fatal(err)
}

if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}
if len(services) == 0 {
r.Fatalf("Bad: %v", services)
}
})
retry.Run(t, func(r *retry.R) {
services, meta, err := catalog.Services(&QueryOptions{Filter: "NodeMeta.synthetic == true"})
if err != nil {
r.Fatal(err)
}

if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}

if len(services) == 0 {
r.Fatalf("Bad: %v", services)
}
})
retry.Run(t, func(r *retry.R) {
services, meta, err := catalog.Services(&QueryOptions{Filter: "NodeMeta.somekey == somevalue"})
if err != nil {
r.Fatal(err)
}

if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}

if len(services) == 0 {
r.Fatalf("Bad: %v", services)
}
})
retry.Run(t, func(r *retry.R) {
services, meta, err := catalog.Services(&QueryOptions{Filter: "NodeMeta.nope == nope"})
if err != nil {
r.Fatal(err)
}

if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}

if len(services) != 0 {
r.Fatalf("Bad: %v", services)
}
})
}

func TestAPI_CatalogService(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
Expand Down