@@ -144,13 +144,15 @@ func createServers(
144
144
servers := make ([]http.Server , 0 , len (httpServers )+ len (sslServers ))
145
145
finalMatchPairs := make (httpMatchPairs )
146
146
147
- for serverID , s := range httpServers {
147
+ for idx , s := range httpServers {
148
+ serverID := fmt .Sprintf ("%d" , idx )
148
149
httpServer , matchPairs := createServer (s , serverID , generator )
149
150
servers = append (servers , httpServer )
150
151
maps .Copy (finalMatchPairs , matchPairs )
151
152
}
152
153
153
- for serverID , s := range sslServers {
154
+ for idx , s := range sslServers {
155
+ serverID := fmt .Sprintf ("SSL_%d" , idx )
154
156
sslServer , matchPairs := createSSLServer (s , serverID , generator )
155
157
servers = append (servers , sslServer )
156
158
maps .Copy (finalMatchPairs , matchPairs )
@@ -161,7 +163,7 @@ func createServers(
161
163
162
164
func createSSLServer (
163
165
virtualServer dataplane.VirtualServer ,
164
- serverIdx int ,
166
+ serverID string ,
165
167
generator policies.Generator ,
166
168
) (http.Server , httpMatchPairs ) {
167
169
if virtualServer .IsDefault {
@@ -171,7 +173,6 @@ func createSSLServer(
171
173
}, nil
172
174
}
173
175
174
- serverID := fmt .Sprintf ("SSL_%d" , serverIdx )
175
176
locs , matchPairs , grpc := createLocations (& virtualServer , serverID , generator )
176
177
177
178
server := http.Server {
@@ -193,7 +194,7 @@ func createSSLServer(
193
194
194
195
func createServer (
195
196
virtualServer dataplane.VirtualServer ,
196
- serverIdx int ,
197
+ serverID string ,
197
198
generator policies.Generator ,
198
199
) (http.Server , httpMatchPairs ) {
199
200
if virtualServer .IsDefault {
@@ -203,7 +204,6 @@ func createServer(
203
204
}, nil
204
205
}
205
206
206
- serverID := fmt .Sprintf ("%d" , serverIdx )
207
207
locs , matchPairs , grpc := createLocations (& virtualServer , serverID , generator )
208
208
209
209
server := http.Server {
@@ -255,16 +255,15 @@ func createLocations(
255
255
}
256
256
257
257
extLocations := initializeExternalLocations (rule , pathsAndTypes )
258
+ for i := range extLocations {
259
+ extLocations [i ].Includes = createIncludesFromPolicyGenerateResult (
260
+ generator .GenerateForLocation (rule .Policies , extLocations [i ]),
261
+ )
262
+ }
258
263
259
264
if ! needsInternalLocations (rule ) {
260
265
for _ , r := range rule .MatchRules {
261
- extLocations = updateLocationsForFilters (r .Filters , extLocations , r , server .Port , rule .Path , rule .GRPC )
262
- }
263
-
264
- for i := range extLocations {
265
- extLocations [i ].Includes = createIncludesFromPolicyGenerateResult (
266
- generator .GenerateForLocation (rule .Policies , extLocations [i ]),
267
- )
266
+ extLocations = updateLocations (r .Filters , extLocations , r , server .Port , rule .Path , rule .GRPC )
268
267
}
269
268
270
269
locs = append (locs , extLocations ... )
@@ -274,24 +273,22 @@ func createLocations(
274
273
internalLocations := make ([]http.Location , 0 , len (rule .MatchRules ))
275
274
276
275
for matchRuleIdx , r := range rule .MatchRules {
277
- if len (rule .MatchRules ) != 1 || ! isPathOnlyMatch (r .Match ) {
278
- intLocation , match := initializeInternalLocation (pathRuleIdx , matchRuleIdx , r .Match , grpc )
279
- intLocation .Includes = createIncludesFromPolicyGenerateResult (
280
- generator .GenerateForInternalLocation (rule .Policies ),
281
- )
282
-
283
- intLocation = updateLocationForFilters (
284
- r .Filters ,
285
- intLocation ,
286
- r ,
287
- server .Port ,
288
- rule .Path ,
289
- rule .GRPC ,
290
- )
291
-
292
- internalLocations = append (internalLocations , intLocation )
293
- matches = append (matches , match )
294
- }
276
+ intLocation , match := initializeInternalLocation (pathRuleIdx , matchRuleIdx , r .Match , grpc )
277
+ intLocation .Includes = createIncludesFromPolicyGenerateResult (
278
+ generator .GenerateForInternalLocation (rule .Policies ),
279
+ )
280
+
281
+ intLocation = updateLocation (
282
+ r .Filters ,
283
+ intLocation ,
284
+ r ,
285
+ server .Port ,
286
+ rule .Path ,
287
+ rule .GRPC ,
288
+ )
289
+
290
+ internalLocations = append (internalLocations , intLocation )
291
+ matches = append (matches , match )
295
292
}
296
293
297
294
httpMatchKey := serverID + "_" + strconv .Itoa (pathRuleIdx )
@@ -300,10 +297,6 @@ func createLocations(
300
297
// so we don't need nginx/njs to perform unnecessary matching.
301
298
// https://github.com/nginxinc/nginx-gateway-fabric/issues/662
302
299
extLocations [i ].HTTPMatchKey = httpMatchKey
303
- extLocations [i ].Includes = createIncludesFromPolicyGenerateResult (
304
- generator .GenerateForLocation (rule .Policies , extLocations [i ]),
305
- )
306
-
307
300
matchPairs [extLocations [i ].HTTPMatchKey ] = matches
308
301
}
309
302
@@ -436,7 +429,8 @@ func initializeInternalLocation(
436
429
return createMatchLocation (path , grpc ), createRouteMatch (match , path )
437
430
}
438
431
439
- func updateLocationForFilters (
432
+ // updateLocation updates a location with any relevant configurations, like proxy_pass, filters, tls settings, etc.
433
+ func updateLocation (
440
434
filters dataplane.HTTPFilters ,
441
435
location http.Location ,
442
436
matchRule dataplane.MatchRule ,
@@ -484,8 +478,9 @@ func updateLocationForFilters(
484
478
return location
485
479
}
486
480
487
- // updateLocationsForFilters updates the existing locations with any relevant filters.
488
- func updateLocationsForFilters (
481
+ // updateLocations updates the existing locations with any relevant configurations, like proxy_pass,
482
+ // filters, tls settings, etc.
483
+ func updateLocations (
489
484
filters dataplane.HTTPFilters ,
490
485
buildLocations []http.Location ,
491
486
matchRule dataplane.MatchRule ,
@@ -496,7 +491,7 @@ func updateLocationsForFilters(
496
491
updatedLocations := make ([]http.Location , len (buildLocations ))
497
492
498
493
for i , loc := range buildLocations {
499
- updatedLocations [i ] = updateLocationForFilters (filters , loc , matchRule , listenerPort , path , grpc )
494
+ updatedLocations [i ] = updateLocation (filters , loc , matchRule , listenerPort , path , grpc )
500
495
}
501
496
502
497
return updatedLocations
0 commit comments