From 3b35b04eddd44b22b9b5426165c5e9be11557b20 Mon Sep 17 00:00:00 2001 From: Sean Banko Date: Thu, 6 Feb 2025 13:12:47 -0800 Subject: [PATCH 1/2] fix: use upstream path in modify response Signed-off-by: Sean Banko --- injectproxy/routes.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/injectproxy/routes.go b/injectproxy/routes.go index 61a2a89b..fb673d05 100644 --- a/injectproxy/routes.go +++ b/injectproxy/routes.go @@ -389,8 +389,8 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, o r.mux = mux r.modifiers = map[string]func(*http.Response) error{ - "/api/v1/rules": modifyAPIResponse(r.filterRules), - "/api/v1/alerts": modifyAPIResponse(r.filterAlerts), + upstream.Path + "/api/v1/rules": modifyAPIResponse(r.filterRules), + upstream.Path + "/api/v1/alerts": modifyAPIResponse(r.filterAlerts), } proxy.ModifyResponse = r.ModifyResponse proxy.ErrorHandler = r.errorHandler From 734ea731062ecc6a6706e8f09aa2d3d7aba6b48e Mon Sep 17 00:00:00 2001 From: Sean Banko Date: Thu, 6 Feb 2025 14:56:54 -0800 Subject: [PATCH 2/2] add test case Signed-off-by: Sean Banko --- injectproxy/rules_test.go | 22 ++- ...tch_namespace_ns1_non_root_upstream.golden | 127 ++++++++++++++++++ 2 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 injectproxy/testdata/rules_match_namespace_ns1_non_root_upstream.golden diff --git a/injectproxy/rules_test.go b/injectproxy/rules_test.go index 8c56b607..6702a3fc 100644 --- a/injectproxy/rules_test.go +++ b/injectproxy/rules_test.go @@ -439,10 +439,11 @@ func validAlerts() http.Handler { func TestRules(t *testing.T) { for _, tc := range []struct { - labelv []string - upstream http.Handler - reqHeaders http.Header - opts []Option + labelv []string + upstream http.Handler + upstreamPathPrefix string + reqHeaders http.Header + opts []Option expCode int golden string @@ -557,10 +558,23 @@ func TestRules(t *testing.T) { expCode: http.StatusOK, golden: "rules_with_active_alerts.golden", }, + { + labelv: []string{"ns1"}, + upstream: validRules(), + upstreamPathPrefix: "/prometheus", + + expCode: http.StatusOK, + golden: "rules_match_namespace_ns1_non_root_upstream.golden", + }, } { t.Run(fmt.Sprintf("%s=%s", proxyLabel, tc.labelv), func(t *testing.T) { m := newMockUpstream(tc.upstream) defer m.Close() + + if tc.upstreamPathPrefix != "" { + m.url.Path = tc.upstreamPathPrefix + m.url.Path + } + r, err := NewRoutes( m.url, proxyLabel, diff --git a/injectproxy/testdata/rules_match_namespace_ns1_non_root_upstream.golden b/injectproxy/testdata/rules_match_namespace_ns1_non_root_upstream.golden new file mode 100644 index 00000000..cbb43610 --- /dev/null +++ b/injectproxy/testdata/rules_match_namespace_ns1_non_root_upstream.golden @@ -0,0 +1,127 @@ +{ + "status": "success", + "data": { + "groups": [ + { + "name": "group1", + "file": "testdata/rules1.yml", + "rules": [ + { + "name": "metric1", + "query": "0", + "labels": { + "namespace": "ns1" + }, + "health": "ok", + "evaluationTime": 0.000214303, + "lastEvaluation": "2024-04-29T14:23:52.403557247+02:00", + "type": "recording" + }, + { + "name": "metric2", + "query": "1", + "labels": { + "namespace": "ns1", + "operation": "create" + }, + "health": "ok", + "evaluationTime": 0.000214, + "lastEvaluation": "2024-04-29T14:23:53.403557247+02:00", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "update" + }, + "health": "ok", + "evaluationTime": 0.000214, + "lastEvaluation": "2024-04-29T14:23:54.403557247+02:00", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "delete" + }, + "health": "ok", + "evaluationTime": 0.000214, + "lastEvaluation": "2024-04-29T14:23:53.603557247+02:00", + "type": "recording" + }, + { + "state": "firing", + "name": "Alert1", + "query": "metric1{namespace=\"ns1\"} == 0", + "duration": 0, + "keepFiringFor": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert1", + "namespace": "ns1" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "evaluationTime": 0.000214, + "lastEvaluation": "2024-04-29T14:23:53.803557247+02:00", + "type": "alerting" + }, + { + "state": "firing", + "name": "Alert2", + "query": "metric2{namespace=\"ns1\"} == 0", + "duration": 0, + "keepFiringFor": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "update" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "delete" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "evaluationTime": 0.000214, + "lastEvaluation": "2024-04-29T14:23:53.903557247+02:00", + "type": "alerting" + } + ], + "interval": 10 + } + ] + } +} \ No newline at end of file