Skip to content

fix: use upstream path in modify response #269

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 2 additions & 2 deletions injectproxy/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use https://pkg.go.dev/net/url#JoinPath instead?

It might be the right time to use a proper struct for modifies instead of an opaque map[string]func.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use url.JoinPath.

I agree the map for modifies isn't ideal. Do you think it's better to have a separate PR for the refactor to keep scope limited? Glad to open a follow-up.

upstream.Path + "/api/v1/alerts": modifyAPIResponse(r.filterAlerts),
}
proxy.ModifyResponse = r.ModifyResponse
proxy.ErrorHandler = r.errorHandler
Expand Down
22 changes: 18 additions & 4 deletions injectproxy/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}