Skip to content

Commit 0f99f03

Browse files
author
Kate Osborn
committed
Extract function for adding includes to locations
1 parent d83bbb4 commit 0f99f03

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

internal/mode/static/nginx/config/servers.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,19 @@ func createLocations(server *dataplane.VirtualServer, serverID int) ([]http.Loca
240240
for matchRuleIdx, r := range rule.MatchRules {
241241
buildLocations := extLocations
242242

243-
includes := createIncludes(r.Additions)
244-
245243
if len(rule.MatchRules) != 1 || !isPathOnlyMatch(r.Match) {
246244
intLocation, match := initializeInternalLocation(pathRuleIdx, matchRuleIdx, r.Match)
247-
intLocation.Includes = includes
248245
buildLocations = []http.Location{intLocation}
249246
matches = append(matches, match)
250-
} else {
251-
for i := range extLocations {
252-
extLocations[i].Includes = includes
253-
}
254247
}
255248

249+
includes := createIncludes(r.Additions)
250+
251+
// buildLocations will either contain the extLocations OR the intLocation.
252+
// If it contains the intLocation, the extLocations will be added to the final locations after we loop
253+
// through all the MatchRules.
254+
// It is safe to modify the buildLocations here by adding includes and filters.
255+
buildLocations = updateLocationsForIncludes(buildLocations, includes)
256256
buildLocations = updateLocationsForFilters(r.Filters, buildLocations, r, server.Port, rule.Path, rule.GRPC)
257257
locs = append(locs, buildLocations...)
258258
}
@@ -281,6 +281,14 @@ func createLocations(server *dataplane.VirtualServer, serverID int) ([]http.Loca
281281
return locs, matchPairs, grpc
282282
}
283283

284+
func updateLocationsForIncludes(locations []http.Location, includes []http.Include) []http.Location {
285+
for i := range locations {
286+
locations[i].Includes = includes
287+
}
288+
289+
return locations
290+
}
291+
284292
// pathAndTypeMap contains a map of paths and any path types defined for that path
285293
// for example, {/foo: {exact: {}, prefix: {}}}
286294
type pathAndTypeMap map[string]map[dataplane.PathType]struct{}

internal/mode/static/nginx/config/servers_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,30 @@ func TestCreateServers(t *testing.T) {
584584
GRPC: true,
585585
},
586586
{
587-
Path: "/addition",
587+
Path: "/addition-path-only-match",
588588
PathType: dataplane.PathTypeExact,
589589
MatchRules: []dataplane.MatchRule{
590590
{
591591
Match: dataplane.Match{},
592592
BackendGroup: fooGroup,
593+
Additions: []*dataplane.Addition{
594+
{
595+
Bytes: []byte("path-only-match-addition"),
596+
Identifier: "path-only-match-addition",
597+
},
598+
},
599+
},
600+
},
601+
},
602+
{
603+
Path: "/addition-header-match",
604+
PathType: dataplane.PathTypeExact,
605+
MatchRules: []dataplane.MatchRule{
606+
{
607+
Match: dataplane.Match{
608+
Method: helpers.GetPointer("GET"),
609+
},
610+
BackendGroup: fooGroup,
593611
Additions: []*dataplane.Addition{
594612
{
595613
Bytes: []byte("match-addition"),
@@ -684,6 +702,12 @@ func TestCreateServers(t *testing.T) {
684702
Any: false,
685703
},
686704
},
705+
"1_17": {
706+
{
707+
Method: "GET",
708+
RedirectPath: "@rule17-route0",
709+
},
710+
},
687711
}
688712

689713
allExpMatchPair := make(httpMatchPairs)
@@ -973,7 +997,17 @@ func TestCreateServers(t *testing.T) {
973997
ProxySetHeaders: grpcBaseHeaders,
974998
},
975999
{
976-
Path: "= /addition",
1000+
Path: "= /addition-path-only-match",
1001+
ProxyPass: "http://test_foo_80$request_uri",
1002+
ProxySetHeaders: baseHeaders,
1003+
Includes: []http.Include{
1004+
{
1005+
Filename: includesFolder + "/path-only-match-addition.conf",
1006+
},
1007+
},
1008+
},
1009+
{
1010+
Path: "@rule17-route0",
9771011
ProxyPass: "http://test_foo_80$request_uri",
9781012
ProxySetHeaders: httpBaseHeaders,
9791013
Includes: []http.Include{
@@ -982,6 +1016,10 @@ func TestCreateServers(t *testing.T) {
9821016
},
9831017
},
9841018
},
1019+
{
1020+
Path: "= /addition-header-match",
1021+
HTTPMatchKey: ssl + "1_17",
1022+
},
9851023
}
9861024
}
9871025

0 commit comments

Comments
 (0)