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 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