From ae5980ae26f63d4b5505c946dc1842d37733d6e1 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Wed, 12 Jul 2023 21:43:44 -0400 Subject: [PATCH 1/3] nsd: retain query params in HTTP health checks Apply the same logic as Consul service health checks when building the HTTP URL so that query params in `path` are preserved. --- client/serviceregistration/checks/client.go | 13 ++++++++--- .../serviceregistration/checks/client_test.go | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/client/serviceregistration/checks/client.go b/client/serviceregistration/checks/client.go index 4ebc6ac879ac..cdaec49d8f27 100644 --- a/client/serviceregistration/checks/client.go +++ b/client/serviceregistration/checks/client.go @@ -155,11 +155,18 @@ func (c *checker) checkHTTP(ctx context.Context, qc *QueryContext, q *Query) *st return qr } - u := (&url.URL{ + relative, err := url.Parse(q.Path) + if err != nil { + qr.Output = err.Error() + qr.Status = structs.CheckFailure + return qr + } + + base := url.URL{ Scheme: q.Protocol, Host: addr, - Path: q.Path, - }).String() + } + u := base.ResolveReference(relative).String() request, err := http.NewRequest(q.Method, u, nil) if err != nil { diff --git a/client/serviceregistration/checks/client_test.go b/client/serviceregistration/checks/client_test.go index 5a81ad0ab0f1..03df2ce227b4 100644 --- a/client/serviceregistration/checks/client_test.go +++ b/client/serviceregistration/checks/client_test.go @@ -39,6 +39,19 @@ func TestChecker_Do_HTTP(t *testing.T) { // create an http server with various responses ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // handle query param requests with string match because we want to + // test the path is set correctly instead of with escaped query params. + if strings.Contains(r.URL.Path, "query-param") { + if r.URL.RawQuery == "" { + w.WriteHeader(400) + _, _ = io.WriteString(w, "400 bad request") + } else { + w.WriteHeader(200) + _, _ = io.WriteString(w, "200 ok") + } + return + } + switch r.URL.Path { case "/fail": w.WriteHeader(500) @@ -182,6 +195,16 @@ func TestChecker_Do_HTTP(t *testing.T) { http.StatusCreated, truncate, ), + }, { + name: "query param", + qc: makeQueryContext(), + q: makeQuery(structs.Healthiness, "query-param?a=b"), + expResult: makeExpResult( + structs.Healthiness, + structs.CheckSuccess, + http.StatusOK, + "nomad: http ok", + ), }} for _, tc := range cases { From 0887531a391e27f7958b0756b0ca5302736b8917 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Thu, 13 Jul 2023 10:43:34 -0400 Subject: [PATCH 2/3] changelog: add entry for #17936 --- .changelog/17936.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/17936.txt diff --git a/.changelog/17936.txt b/.changelog/17936.txt new file mode 100644 index 000000000000..70a6f44f8960 --- /dev/null +++ b/.changelog/17936.txt @@ -0,0 +1,3 @@ +```release-note:improvement +services: Allow passing query parameters in Nomad native service discovery HTTP health checks +``` From 762a8e1a25c068f009b36143118149e23bb6c7d1 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Fri, 14 Jul 2023 17:03:19 -0400 Subject: [PATCH 3/3] changelog: update entry for #17936 --- .changelog/17936.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changelog/17936.txt b/.changelog/17936.txt index 70a6f44f8960..5861a694503c 100644 --- a/.changelog/17936.txt +++ b/.changelog/17936.txt @@ -1,3 +1,3 @@ -```release-note:improvement -services: Allow passing query parameters in Nomad native service discovery HTTP health checks +```release-note:bug +services: Fixed a bug that prevented passing query parameters in Nomad native service discovery HTTP health check paths ```