Skip to content

Commit 1af00f7

Browse files
jorgemareysreeram77nitin-sachdev-29boruszak
authored
Fix catalog service endpoint when querying for a peer service (#22189)
* Fix catalog service endpoint when querying for a peer service * Add changelog file * Add changes to docs. Add test * Update website/content/api-docs/catalog.mdx Co-authored-by: Jeff Boruszak <[email protected]> --------- Co-authored-by: Sreeram Narayanan <[email protected]> Co-authored-by: nitin-sachdev-29 <[email protected]> Co-authored-by: Jeff Boruszak <[email protected]>
1 parent 616de64 commit 1af00f7

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

.changelog/22189.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
http: Add peer query param on catalog service API
3+
```

agent/catalog_endpoint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ func (s *HTTPHandlers) catalogServiceNodes(resp http.ResponseWriter, req *http.R
354354
return nil, nil
355355
}
356356

357+
s.parsePeerName(req, &args)
358+
357359
// Check for a tag
358360
params := req.URL.Query()
359361
if _, ok := params["tag"]; ok {

agent/catalog_endpoint_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,76 @@ func TestCatalogServiceNodes_Filter(t *testing.T) {
964964
require.Len(t, nodes, 1)
965965
}
966966

967+
func TestCatalogServiceNodes_PeerFilter(t *testing.T) {
968+
if testing.Short() {
969+
t.Skip("too slow for testing.Short")
970+
}
971+
972+
t.Parallel()
973+
a := StartTestAgent(t, TestAgent{HCL: "", Overrides: `peering = { test_allow_peer_registrations = true }`})
974+
defer a.Shutdown()
975+
976+
peerName := "test"
977+
queryPath := "/v1/catalog/service/api?filter=" + url.QueryEscape("ServiceMeta.somekey == somevalue") + peerQuerySuffix(peerName)
978+
979+
// Make sure an empty list is returned, not a nil
980+
{
981+
req, _ := http.NewRequest("GET", queryPath, nil)
982+
resp := httptest.NewRecorder()
983+
obj, err := a.srv.CatalogServiceNodes(resp, req)
984+
require.NoError(t, err)
985+
986+
assertIndex(t, resp)
987+
988+
nodes := obj.(structs.ServiceNodes)
989+
require.Empty(t, nodes)
990+
}
991+
992+
// Register node
993+
args := &structs.RegisterRequest{
994+
Datacenter: "dc1",
995+
Node: "foo",
996+
Address: "127.0.0.1",
997+
PeerName: peerName,
998+
Service: &structs.NodeService{
999+
Service: "api",
1000+
Meta: map[string]string{
1001+
"somekey": "somevalue",
1002+
},
1003+
},
1004+
}
1005+
1006+
var out struct{}
1007+
require.NoError(t, a.RPC(context.Background(), "Catalog.Register", args, &out))
1008+
1009+
// Register a second service for the node
1010+
args = &structs.RegisterRequest{
1011+
Datacenter: "dc1",
1012+
Node: "foo",
1013+
Address: "127.0.0.1",
1014+
PeerName: peerName,
1015+
Service: &structs.NodeService{
1016+
ID: "api2",
1017+
Service: "api",
1018+
Meta: map[string]string{
1019+
"somekey": "notvalue",
1020+
},
1021+
},
1022+
SkipNodeUpdate: true,
1023+
}
1024+
1025+
require.NoError(t, a.RPC(context.Background(), "Catalog.Register", args, &out))
1026+
1027+
req, _ := http.NewRequest("GET", queryPath, nil)
1028+
resp := httptest.NewRecorder()
1029+
obj, err := a.srv.CatalogServiceNodes(resp, req)
1030+
require.NoError(t, err)
1031+
assertIndex(t, resp)
1032+
1033+
nodes := obj.(structs.ServiceNodes)
1034+
require.Len(t, nodes, 1)
1035+
}
1036+
9671037
func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
9681038
if testing.Short() {
9691039
t.Skip("too slow for testing.Short")

website/content/api-docs/catalog.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ The table below shows this endpoint's support for
551551
- `filter` `(string: "")` - Specifies the expression used to filter the
552552
queries results prior to returning the data.
553553

554+
- `peer` `(string: "")` - Specifies the name of the peer that exported the service. Does not apply when no cluster peering connections exist.
555+
554556
- `merge-central-config` - Include this flag in a request for `connect-proxy` kind or `*-gateway` kind
555557
services to return a fully resolved service definition that includes merged values from the
556558
[proxy-defaults/global](/consul/docs/connect/config-entries/proxy-defaults) and

0 commit comments

Comments
 (0)