Skip to content

Commit 89ffac3

Browse files
committed
Add some feedback
1 parent 5db0c52 commit 89ffac3

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

internal/controller/nginx/config/servers.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,22 @@ type rewriteConfig struct {
225225
}
226226

227227
// extractMirrorTargetsWithPercentages extracts mirror targets and their percentages from path rules.
228-
func extractMirrorTargetsWithPercentages(pathRules []dataplane.PathRule) map[string]float64 {
229-
mirrorTargets := make(map[string]float64)
228+
func extractMirrorTargetsWithPercentages(pathRules []dataplane.PathRule) map[string]*float64 {
229+
mirrorTargets := make(map[string]*float64)
230230

231231
for _, rule := range pathRules {
232232
for _, matchRule := range rule.MatchRules {
233233
for _, mirrorFilter := range matchRule.Filters.RequestMirrors {
234234
if mirrorFilter.Target != nil {
235235
if mirrorFilter.Percent == nil {
236-
mirrorTargets[*mirrorFilter.Target] = 100.0
236+
mirrorTargets[*mirrorFilter.Target] = helpers.GetPointer(100.0)
237237
continue
238238
}
239239

240-
percentage := *mirrorFilter.Percent
240+
percentage := mirrorFilter.Percent
241241

242-
if _, exists := mirrorTargets[*mirrorFilter.Target]; !exists || percentage > mirrorTargets[*mirrorFilter.Target] {
242+
if _, exists := mirrorTargets[*mirrorFilter.Target]; !exists ||
243+
*percentage > *mirrorTargets[*mirrorFilter.Target] {
243244
mirrorTargets[*mirrorFilter.Target] = percentage // set a higher percentage if it exists
244245
}
245246
}
@@ -278,12 +279,7 @@ func createLocations(
278279
grpcServer = true
279280
}
280281

281-
mirrorPercentage, exists := mirrorPathToPercentage[rule.Path]
282-
if !exists {
283-
// need a way to differentiate between no mirror filter and a mirror filter with 0 percent set, and
284-
// I don't want to pass an extra boolean around.
285-
mirrorPercentage = -1
286-
}
282+
mirrorPercentage := mirrorPathToPercentage[rule.Path]
287283

288284
extLocations := initializeExternalLocations(rule, pathsAndTypes)
289285
for i := range extLocations {
@@ -464,7 +460,7 @@ func updateLocation(
464460
path string,
465461
grpc bool,
466462
keepAliveCheck keepAliveChecker,
467-
mirrorPercentage float64,
463+
mirrorPercentage *float64,
468464
) http.Location {
469465
if filters.InvalidFilter != nil {
470466
location.Return = &http.Return{Code: http.StatusInternalServerError}
@@ -533,7 +529,7 @@ func updateLocationMirrorFilters(
533529
location http.Location,
534530
mirrorFilters []*dataplane.HTTPRequestMirrorFilter,
535531
path string,
536-
mirrorPercentage float64,
532+
mirrorPercentage *float64,
537533
) http.Location {
538534
for _, filter := range mirrorFilters {
539535
if filter.Target != nil {
@@ -547,9 +543,9 @@ func updateLocationMirrorFilters(
547543

548544
// if the mirrorPercentage is 100.0 or negative (we set it to negative when there is no mirror filter because 0.0
549545
// is valid), the split clients variable is not generated, and we want to let all the traffic get mirrored.
550-
if mirrorPercentage != 100.0 && mirrorPercentage >= 0.0 {
546+
if mirrorPercentage != nil && *mirrorPercentage != 100.0 {
551547
location.MirrorSplitClientsVariableName = convertSplitClientVariableName(
552-
fmt.Sprintf("%s_%.2f", path, mirrorPercentage),
548+
fmt.Sprintf("%s_%.2f", path, *mirrorPercentage),
553549
)
554550
}
555551

@@ -599,7 +595,7 @@ func updateLocations(
599595
path string,
600596
grpc bool,
601597
keepAliveCheck keepAliveChecker,
602-
mirrorPercentage float64,
598+
mirrorPercentage *float64,
603599
) []http.Location {
604600
updatedLocations := make([]http.Location, len(buildLocations))
605601

@@ -1070,12 +1066,12 @@ func getConnectionHeader(keepAliveCheck keepAliveChecker, backends []dataplane.B
10701066

10711067
// deduplicateStrings removes duplicate strings from a slice while preserving order.
10721068
func deduplicateStrings(content []string) []string {
1073-
seen := make(map[string]bool)
1069+
seen := make(map[string]struct{})
10741070
result := make([]string, 0, len(content))
10751071

10761072
for _, str := range content {
1077-
if !seen[str] {
1078-
seen[str] = true
1073+
if _, exists := seen[str]; !exists {
1074+
seen[str] = struct{}{}
10791075
result = append(result, str)
10801076
}
10811077
}

internal/controller/nginx/config/split_clients.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ func createRequestMirrorSplitClients(servers []dataplane.VirtualServer) []http.S
4242
mirrorPathToPercentage := extractMirrorTargetsWithPercentages(server.PathRules)
4343

4444
for _, pathRule := range server.PathRules {
45-
mirrorPercentage, exists := mirrorPathToPercentage[pathRule.Path]
45+
mirrorPercentage := mirrorPathToPercentage[pathRule.Path]
4646

47-
if mirrorPercentage != 100 && exists {
47+
if mirrorPercentage != nil && *mirrorPercentage != 100 {
4848
splitClient := http.SplitClient{
49-
// this has to be something unique and able to be accessed from the server side
50-
VariableName: convertSplitClientVariableName(fmt.Sprintf("%s_%.2f", pathRule.Path, mirrorPercentage)),
49+
// this has to be something unique and able to be accessed from the server block
50+
VariableName: convertSplitClientVariableName(fmt.Sprintf("%s_%.2f", pathRule.Path, *mirrorPercentage)),
5151
Distributions: []http.SplitClientDistribution{
5252
{
53-
Percent: fmt.Sprintf("%.2f", mirrorPercentage),
53+
Percent: fmt.Sprintf("%.2f", *mirrorPercentage),
5454
Value: pathRule.Path,
5555
},
5656
{
@@ -79,12 +79,12 @@ func convertSplitClientVariableName(name string) string {
7979
}
8080

8181
func removeDuplicateSplitClients(splitClients []http.SplitClient) []http.SplitClient {
82-
seen := make(map[string]bool)
82+
seen := make(map[string]struct{})
8383
result := make([]http.SplitClient, 0, len(splitClients))
8484

8585
for _, client := range splitClients {
86-
if !seen[client.VariableName] {
87-
seen[client.VariableName] = true
86+
if _, exists := seen[client.VariableName]; !exists {
87+
seen[client.VariableName] = struct{}{}
8888
result = append(result, client)
8989
}
9090
}

internal/controller/nginx/config/split_clients_template.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ split_clients $request_id ${{ $sc.VariableName }} {
66
{{- range $d := $sc.Distributions }}
77
{{- if eq $d.Percent "0.00" }}
88
# {{ $d.Percent }}% {{ $d.Value }};
9-
{{- else if eq $d.Percent "*" }}
10-
* {{ $d.Value }};
11-
{{- else }}
9+
{{- else if eq $d.Percent "*" }}
10+
* {{ $d.Value }};
11+
{{- else }}
1212
{{ $d.Percent }}% {{ $d.Value }};
1313
{{- end }}
1414
{{- end }}

internal/controller/state/dataplane/convert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func convertHTTPRequestMirrorFilter(
9595
result.Percent = helpers.GetPointer(float64(100))
9696
}
9797

98+
if *result.Percent > 100.0 {
99+
result.Percent = helpers.GetPointer(100.0)
100+
}
101+
98102
return result
99103
}
100104

internal/controller/state/dataplane/convert_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,25 @@ func TestConvertHTTPMirrorFilter(t *testing.T) {
361361
},
362362
name: "fraction denominator not specified",
363363
},
364+
{
365+
filter: &v1.HTTPRequestMirrorFilter{
366+
BackendRef: v1.BackendObjectReference{
367+
Name: "backend",
368+
Namespace: helpers.GetPointer[v1.Namespace]("namespace"),
369+
},
370+
Fraction: &v1.Fraction{
371+
Numerator: 300,
372+
Denominator: helpers.GetPointer(int32(1)),
373+
},
374+
},
375+
expected: &HTTPRequestMirrorFilter{
376+
Name: helpers.GetPointer("backend"),
377+
Namespace: helpers.GetPointer("namespace"),
378+
Target: helpers.GetPointer("/_ngf-internal-mirror-namespace/backend-test/route1-0"),
379+
Percent: helpers.GetPointer(float64(100)),
380+
},
381+
name: "fraction result over 100",
382+
},
364383
{
365384
filter: &v1.HTTPRequestMirrorFilter{
366385
BackendRef: v1.BackendObjectReference{

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GW_SERVICE_TYPE = NodePort## Service type to use for the gateway
1212
NGF_VERSION ?= edge## NGF version to be tested
1313
PULL_POLICY = Never## Pull policy for the images
1414
NGINX_CONF_DIR = internal/controller/nginx/conf
15-
SUPPORTED_EXTENDED_FEATURES = HTTPRouteQueryParamMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteSchemeRedirect,HTTPRouteHostRewrite,HTTPRoutePathRewrite,GatewayPort8080,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,HTTPRoutePathRedirect,GatewayHTTPListenerIsolation,GatewayInfrastructurePropagation,HTTPRouteRequestMirror,HTTPRouteRequestMultipleMirrors,HTTPRouteBackendProtocolWebSocket,HTTPRouteRequestPercentageMirror
15+
SUPPORTED_EXTENDED_FEATURES = HTTPRouteQueryParamMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteSchemeRedirect,HTTPRouteHostRewrite,HTTPRoutePathRewrite,GatewayPort8080,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,HTTPRoutePathRedirect,GatewayHTTPListenerIsolation,GatewayInfrastructurePropagation,HTTPRouteRequestMirror,HTTPRouteRequestMultipleMirrors,HTTPRouteRequestPercentageMirror,HTTPRouteBackendProtocolWebSocket
1616
STANDARD_CONFORMANCE_PROFILES = GATEWAY-HTTP,GATEWAY-GRPC
1717
EXPERIMENTAL_CONFORMANCE_PROFILES = GATEWAY-TLS
1818
CONFORMANCE_PROFILES = $(STANDARD_CONFORMANCE_PROFILES) # by default we use the standard conformance profiles. If experimental is enabled we override this and add the experimental profiles.

0 commit comments

Comments
 (0)