Skip to content

Commit cb06d8a

Browse files
committed
Code review
1 parent b82f666 commit cb06d8a

File tree

6 files changed

+57
-61
lines changed

6 files changed

+57
-61
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type Location struct {
4141
Rewrites []string
4242
Includes []Include
4343
GRPC bool
44-
Redirects bool
4544
}
4645

4746
// Header defines an HTTP header to be passed to the proxied server.

internal/mode/static/nginx/config/policies/clientsettings/generator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func (g Generator) GenerateForServer(pols []policies.Policy, _ http.Server) poli
5151
return generate(pols)
5252
}
5353

54-
// GenerateForServer generates policy configuration for a normal location block.
54+
// GenerateForLocation generates policy configuration for a normal location block.
5555
func (g Generator) GenerateForLocation(pols []policies.Policy, _ http.Location) policies.GenerateResultFiles {
5656
return generate(pols)
5757
}
5858

59-
// GenerateForServer generates policy configuration for a normal location block.
59+
// GenerateForInternalLocation generates policy configuration for an internal location block.
6060
func (g Generator) GenerateForInternalLocation(pols []policies.Policy) policies.GenerateResultFiles {
6161
return generate(pols)
6262
}

internal/mode/static/nginx/config/policies/clientsettings/generator_test.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,28 @@ func TestGenerate(t *testing.T) {
149149
},
150150
}
151151

152-
for _, test := range tests {
153-
t.Run(test.name, func(t *testing.T) {
154-
g := NewWithT(t)
152+
g := NewWithT(t)
153+
154+
checkResults := func(resFiles policies.GenerateResultFiles, expStrings []string) {
155+
g.Expect(resFiles).To(HaveLen(1))
156+
157+
for _, str := range expStrings {
158+
g.Expect(string(resFiles[0].Content)).To(ContainSubstring(str))
159+
}
160+
}
155161

162+
for _, test := range tests {
163+
t.Run(test.name, func(_ *testing.T) {
156164
generator := clientsettings.NewGenerator()
157165

158166
resFiles := generator.GenerateForServer([]policies.Policy{test.policy}, http.Server{})
159-
g.Expect(resFiles).To(HaveLen(1))
160-
161-
for _, str := range test.expStrings {
162-
g.Expect(string(resFiles[0].Content)).To(ContainSubstring(str))
163-
}
167+
checkResults(resFiles, test.expStrings)
164168

165169
resFiles = generator.GenerateForLocation([]policies.Policy{test.policy}, http.Location{})
166-
g.Expect(resFiles).To(HaveLen(1))
167-
168-
for _, str := range test.expStrings {
169-
g.Expect(string(resFiles[0].Content)).To(ContainSubstring(str))
170-
}
170+
checkResults(resFiles, test.expStrings)
171171

172172
resFiles = generator.GenerateForInternalLocation([]policies.Policy{test.policy})
173-
g.Expect(resFiles).To(HaveLen(1))
174-
175-
for _, str := range test.expStrings {
176-
g.Expect(string(resFiles[0].Content)).To(ContainSubstring(str))
177-
}
173+
checkResults(resFiles, test.expStrings)
178174
})
179175
}
180176
}

internal/mode/static/nginx/config/policies/observability/generator.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func NewGenerator(telemetry dataplane.Telemetry) *Generator {
7373
}
7474

7575
// GenerateForServer generates policy configuration for a normal location block.
76+
// For a normal location, all directives are applied.
77+
// When the configuration involves a normal location redirecting to an internal location,
78+
// only otel_trace and otel_trace_context are applied to the normal location.
7679
func (g Generator) GenerateForLocation(pols []policies.Policy, location http.Location) policies.GenerateResultFiles {
7780
buildTemplate := func(
7881
tmplate *template.Template,
@@ -111,6 +114,8 @@ func (g Generator) GenerateForLocation(pols []policies.Policy, location http.Loc
111114
}
112115

113116
// GenerateForInternalLocation generates policy configuration for an internal location block.
117+
// otel_span_attr and otel_span_name are set in the internal location, with otel_trace and otel_trace_context
118+
// being specified in the external location that redirects to the internal location.
114119
func (g Generator) GenerateForInternalLocation(pols []policies.Policy) policies.GenerateResultFiles {
115120
for _, pol := range pols {
116121
obs, ok := pol.(*ngfAPI.ObservabilityPolicy)

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

+34-39
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,15 @@ func createServers(
144144
servers := make([]http.Server, 0, len(httpServers)+len(sslServers))
145145
finalMatchPairs := make(httpMatchPairs)
146146

147-
for serverID, s := range httpServers {
147+
for idx, s := range httpServers {
148+
serverID := fmt.Sprintf("%d", idx)
148149
httpServer, matchPairs := createServer(s, serverID, generator)
149150
servers = append(servers, httpServer)
150151
maps.Copy(finalMatchPairs, matchPairs)
151152
}
152153

153-
for serverID, s := range sslServers {
154+
for idx, s := range sslServers {
155+
serverID := fmt.Sprintf("SSL_%d", idx)
154156
sslServer, matchPairs := createSSLServer(s, serverID, generator)
155157
servers = append(servers, sslServer)
156158
maps.Copy(finalMatchPairs, matchPairs)
@@ -161,7 +163,7 @@ func createServers(
161163

162164
func createSSLServer(
163165
virtualServer dataplane.VirtualServer,
164-
serverIdx int,
166+
serverID string,
165167
generator policies.Generator,
166168
) (http.Server, httpMatchPairs) {
167169
if virtualServer.IsDefault {
@@ -171,7 +173,6 @@ func createSSLServer(
171173
}, nil
172174
}
173175

174-
serverID := fmt.Sprintf("SSL_%d", serverIdx)
175176
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator)
176177

177178
server := http.Server{
@@ -193,7 +194,7 @@ func createSSLServer(
193194

194195
func createServer(
195196
virtualServer dataplane.VirtualServer,
196-
serverIdx int,
197+
serverID string,
197198
generator policies.Generator,
198199
) (http.Server, httpMatchPairs) {
199200
if virtualServer.IsDefault {
@@ -203,7 +204,6 @@ func createServer(
203204
}, nil
204205
}
205206

206-
serverID := fmt.Sprintf("%d", serverIdx)
207207
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator)
208208

209209
server := http.Server{
@@ -255,16 +255,15 @@ func createLocations(
255255
}
256256

257257
extLocations := initializeExternalLocations(rule, pathsAndTypes)
258+
for i := range extLocations {
259+
extLocations[i].Includes = createIncludesFromPolicyGenerateResult(
260+
generator.GenerateForLocation(rule.Policies, extLocations[i]),
261+
)
262+
}
258263

259264
if !needsInternalLocations(rule) {
260265
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)
268267
}
269268

270269
locs = append(locs, extLocations...)
@@ -274,24 +273,22 @@ func createLocations(
274273
internalLocations := make([]http.Location, 0, len(rule.MatchRules))
275274

276275
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)
295292
}
296293

297294
httpMatchKey := serverID + "_" + strconv.Itoa(pathRuleIdx)
@@ -300,10 +297,6 @@ func createLocations(
300297
// so we don't need nginx/njs to perform unnecessary matching.
301298
// https://github.com/nginxinc/nginx-gateway-fabric/issues/662
302299
extLocations[i].HTTPMatchKey = httpMatchKey
303-
extLocations[i].Includes = createIncludesFromPolicyGenerateResult(
304-
generator.GenerateForLocation(rule.Policies, extLocations[i]),
305-
)
306-
307300
matchPairs[extLocations[i].HTTPMatchKey] = matches
308301
}
309302

@@ -436,7 +429,8 @@ func initializeInternalLocation(
436429
return createMatchLocation(path, grpc), createRouteMatch(match, path)
437430
}
438431

439-
func updateLocationForFilters(
432+
// updateLocation updates a location with any relevant configurations, like proxy_pass, filters, tls settings, etc.
433+
func updateLocation(
440434
filters dataplane.HTTPFilters,
441435
location http.Location,
442436
matchRule dataplane.MatchRule,
@@ -484,8 +478,9 @@ func updateLocationForFilters(
484478
return location
485479
}
486480

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(
489484
filters dataplane.HTTPFilters,
490485
buildLocations []http.Location,
491486
matchRule dataplane.MatchRule,
@@ -496,7 +491,7 @@ func updateLocationsForFilters(
496491
updatedLocations := make([]http.Location, len(buildLocations))
497492

498493
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)
500495
}
501496

502497
return updatedLocations

internal/mode/static/state/graph/httproute.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ func validatePathMatch(
228228
}
229229

230230
if strings.HasPrefix(*path.Value, http.InternalRoutePathPrefix) {
231-
msg := fmt.Sprintf("path cannot start with %s", http.InternalRoutePathPrefix)
231+
msg := fmt.Sprintf("path cannot start with %s. This prefix is reserved for internal use",
232+
http.InternalRoutePathPrefix)
232233
return field.ErrorList{field.Invalid(fieldPath.Child("value"), *path.Value, msg)}
233234
}
234235

0 commit comments

Comments
 (0)