Skip to content

Commit 0b6afd8

Browse files
committed
Refactor mirror percentage onto pathrules
1 parent e5ca945 commit 0b6afd8

File tree

6 files changed

+98
-34
lines changed

6 files changed

+98
-34
lines changed

internal/controller/nginx/config/servers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func createLocations(
266266
var rootPathExists bool
267267
var grpcServer bool
268268

269-
mirrorPathToPercentage := extractMirrorTargetsWithPercentages(server.PathRules)
269+
//mirrorPathToPercentage := extractMirrorTargetsWithPercentages(server.PathRules)
270270

271271
for pathRuleIdx, rule := range server.PathRules {
272272
matches := make([]routeMatch, 0, len(rule.MatchRules))
@@ -279,7 +279,7 @@ func createLocations(
279279
grpcServer = true
280280
}
281281

282-
mirrorPercentage := mirrorPathToPercentage[rule.Path]
282+
//mirrorPercentage := mirrorPathToPercentage[rule.Path]
283283

284284
extLocations := initializeExternalLocations(rule, pathsAndTypes)
285285
for i := range extLocations {
@@ -298,7 +298,7 @@ func createLocations(
298298
rule.Path,
299299
rule.GRPC,
300300
keepAliveCheck,
301-
mirrorPercentage,
301+
rule.MirrorPercent,
302302
)
303303
}
304304

@@ -322,7 +322,7 @@ func createLocations(
322322
rule.Path,
323323
rule.GRPC,
324324
keepAliveCheck,
325-
mirrorPercentage,
325+
rule.MirrorPercent,
326326
)
327327

328328
internalLocations = append(internalLocations, intLocation)

internal/controller/nginx/config/servers_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func TestExecuteServers(t *testing.T) {
105105
},
106106
},
107107
},
108+
MirrorPercent: helpers.GetPointer(float64(50)),
108109
},
109110
},
110111
},
@@ -952,6 +953,7 @@ func TestCreateServers(t *testing.T) {
952953
BackendGroup: fooGroup,
953954
},
954955
},
956+
MirrorPercent: helpers.GetPointer(float64(50)),
955957
},
956958
{
957959
Path: "/mirror-filter-100-percent",
@@ -982,6 +984,7 @@ func TestCreateServers(t *testing.T) {
982984
BackendGroup: fooGroup,
983985
},
984986
},
987+
MirrorPercent: helpers.GetPointer(float64(100)),
985988
},
986989
{
987990
Path: "/mirror-filter-0-percent",
@@ -1012,6 +1015,7 @@ func TestCreateServers(t *testing.T) {
10121015
BackendGroup: fooGroup,
10131016
},
10141017
},
1018+
MirrorPercent: helpers.GetPointer(float64(0)),
10151019
},
10161020
{
10171021
Path: "/mirror-filter-duplicate-targets",
@@ -1054,6 +1058,7 @@ func TestCreateServers(t *testing.T) {
10541058
BackendGroup: fooGroup,
10551059
},
10561060
},
1061+
MirrorPercent: helpers.GetPointer(float64(50)),
10571062
},
10581063
{
10591064
Path: "/grpc/mirror",
@@ -2646,8 +2651,8 @@ func TestCreateReturnValForRedirectFilter(t *testing.T) {
26462651

26472652
modifiedHTTPRequestRedirectFilter := func(
26482653
mod func(
2649-
filter *dataplane.HTTPRequestRedirectFilter,
2650-
) *dataplane.HTTPRequestRedirectFilter,
2654+
filter *dataplane.HTTPRequestRedirectFilter,
2655+
) *dataplane.HTTPRequestRedirectFilter,
26512656
) *dataplane.HTTPRequestRedirectFilter {
26522657
return mod(createBasicHTTPRequestRedirectFilter())
26532658
}

internal/controller/nginx/config/split_clients.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ func createRequestMirrorSplitClients(servers []dataplane.VirtualServer) []http.S
3939
var splitClients []http.SplitClient
4040

4141
for _, server := range servers {
42-
mirrorPathToPercentage := extractMirrorTargetsWithPercentages(server.PathRules)
43-
44-
for path, percentage := range mirrorPathToPercentage {
45-
if percentage != nil && *percentage != 100 {
42+
for _, pathRule := range server.PathRules {
43+
if pathRule.MirrorPercent != nil && *pathRule.MirrorPercent != 100 {
4644
splitClient := http.SplitClient{
4745
// this has to be something unique and able to be accessed from the server block
48-
VariableName: convertSplitClientVariableName(fmt.Sprintf("%s_%.2f", path, *percentage)),
46+
VariableName: convertSplitClientVariableName(fmt.Sprintf("%s_%.2f", pathRule.Path, *pathRule.MirrorPercent)),
4947
Distributions: []http.SplitClientDistribution{
5048
{
51-
Percent: fmt.Sprintf("%.2f", *percentage),
52-
Value: path,
49+
Percent: fmt.Sprintf("%.2f", *pathRule.MirrorPercent),
50+
Value: pathRule.Path,
5351
},
5452
{
5553
Percent: "*",
@@ -61,6 +59,28 @@ func createRequestMirrorSplitClients(servers []dataplane.VirtualServer) []http.S
6159
splitClients = append(splitClients, splitClient)
6260
}
6361
}
62+
//mirrorPathToPercentage := extractMirrorTargetsWithPercentages(server.PathRules)
63+
//
64+
//for path, percentage := range mirrorPathToPercentage {
65+
// if percentage != nil && *percentage != 100 {
66+
// splitClient := http.SplitClient{
67+
// // this has to be something unique and able to be accessed from the server block
68+
// VariableName: convertSplitClientVariableName(fmt.Sprintf("%s_%.2f", path, *percentage)),
69+
// Distributions: []http.SplitClientDistribution{
70+
// {
71+
// Percent: fmt.Sprintf("%.2f", *percentage),
72+
// Value: path,
73+
// },
74+
// {
75+
// Percent: "*",
76+
// Value: "\"\"",
77+
// },
78+
// },
79+
// }
80+
//
81+
// splitClients = append(splitClients, splitClient)
82+
// }
83+
//}
6484
}
6585

6686
return splitClients

internal/controller/nginx/config/split_clients_test.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,16 @@ func TestExecuteSplitClients(t *testing.T) {
133133
},
134134
},
135135
{
136-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
136+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
137+
MirrorPercent: helpers.GetPointer(float64(25)),
137138
},
138139
{
139-
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route1-0",
140+
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route1-0",
141+
MirrorPercent: helpers.GetPointer(float64(50)),
140142
},
141143
{
142-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-1",
144+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-1",
145+
MirrorPercent: helpers.GetPointer(float64(25)),
143146
},
144147
{
145148
Path: "/mirror-edge-case-percentages",
@@ -175,13 +178,16 @@ func TestExecuteSplitClients(t *testing.T) {
175178
},
176179
},
177180
{
178-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route2-0",
181+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route2-0",
182+
MirrorPercent: helpers.GetPointer(float64(0)),
179183
},
180184
{
181-
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route2-0",
185+
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route2-0",
186+
MirrorPercent: helpers.GetPointer(float64(99.999)),
182187
},
183188
{
184-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route2-1",
189+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route2-1",
190+
MirrorPercent: helpers.GetPointer(float64(0.001)),
185191
},
186192
},
187193
},
@@ -241,7 +247,8 @@ func TestExecuteSplitClients(t *testing.T) {
241247
},
242248
},
243249
{
244-
Path: http.InternalMirrorRoutePathPrefix + "-my-same-backend-test/route1-0",
250+
Path: http.InternalMirrorRoutePathPrefix + "-my-same-backend-test/route1-0",
251+
MirrorPercent: helpers.GetPointer(float64(50)),
245252
},
246253
},
247254
},
@@ -297,7 +304,8 @@ func TestExecuteSplitClients(t *testing.T) {
297304
},
298305
},
299306
{
300-
Path: http.InternalMirrorRoutePathPrefix + "-my-backend-test/route1-0",
307+
Path: http.InternalMirrorRoutePathPrefix + "-my-backend-test/route1-0",
308+
MirrorPercent: helpers.GetPointer(float64(25)),
301309
},
302310
},
303311
},
@@ -321,7 +329,8 @@ func TestExecuteSplitClients(t *testing.T) {
321329
},
322330
},
323331
{
324-
Path: http.InternalMirrorRoutePathPrefix + "-my-ssl-backend-test/route1-0",
332+
Path: http.InternalMirrorRoutePathPrefix + "-my-ssl-backend-test/route1-0",
333+
MirrorPercent: helpers.GetPointer(float64(50)),
325334
},
326335
},
327336
},
@@ -410,13 +419,16 @@ func TestCreateRequestMirrorSplitClients(t *testing.T) {
410419
},
411420
},
412421
{
413-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
422+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
423+
MirrorPercent: helpers.GetPointer(float64(25)),
414424
},
415425
{
416-
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route1-0",
426+
Path: http.InternalMirrorRoutePathPrefix + "-my-tea-backend-test/route1-0",
427+
MirrorPercent: helpers.GetPointer(float64(50)),
417428
},
418429
{
419-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-1",
430+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-1",
431+
MirrorPercent: helpers.GetPointer(float64(25)),
420432
},
421433
},
422434
},
@@ -438,7 +450,8 @@ func TestCreateRequestMirrorSplitClients(t *testing.T) {
438450
},
439451
},
440452
{
441-
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
453+
Path: http.InternalMirrorRoutePathPrefix + "-my-coffee-backend-test/route1-0",
454+
MirrorPercent: helpers.GetPointer(float64(30)),
442455
},
443456
},
444457
},

internal/controller/state/dataplane/configuration.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,20 @@ type pathAndType struct {
462462
}
463463

464464
type hostPathRules struct {
465-
rulesPerHost map[string]map[pathAndType]PathRule
466-
listenersForHost map[string]*graph.Listener
467-
httpsListeners []*graph.Listener
468-
port int32
469-
listenersExist bool
465+
rulesPerHost map[string]map[pathAndType]PathRule
466+
listenersForHost map[string]*graph.Listener
467+
httpsListeners []*graph.Listener
468+
port int32
469+
listenersExist bool
470+
mirrorTargetToPercentage map[string]*float64
470471
}
471472

472473
func newHostPathRules() *hostPathRules {
473474
return &hostPathRules{
474-
rulesPerHost: make(map[string]map[pathAndType]PathRule),
475-
listenersForHost: make(map[string]*graph.Listener),
476-
httpsListeners: make([]*graph.Listener, 0),
475+
rulesPerHost: make(map[string]map[pathAndType]PathRule),
476+
listenersForHost: make(map[string]*graph.Listener),
477+
httpsListeners: make([]*graph.Listener, 0),
478+
mirrorTargetToPercentage: make(map[string]*float64),
477479
}
478480
}
479481

@@ -553,6 +555,25 @@ func (hpr *hostPathRules) upsertRoute(
553555
pols := buildPolicies(gateway, route.Policies)
554556

555557
for _, h := range hostnames {
558+
559+
if filters.RequestMirrors != nil {
560+
for _, m := range filters.RequestMirrors {
561+
if m.Target != nil {
562+
if m.Percent == nil {
563+
hpr.mirrorTargetToPercentage[*m.Target] = helpers.GetPointer(100.0)
564+
continue
565+
}
566+
567+
percentage := m.Percent
568+
569+
if _, exists := hpr.mirrorTargetToPercentage[*m.Target]; !exists ||
570+
*percentage > *hpr.mirrorTargetToPercentage[*m.Target] {
571+
hpr.mirrorTargetToPercentage[*m.Target] = percentage // set a higher percentage if it exists
572+
}
573+
}
574+
}
575+
}
576+
556577
for _, m := range rule.Matches {
557578
path := getPath(m.Path)
558579

@@ -607,6 +628,8 @@ func (hpr *hostPathRules) buildServers() []VirtualServer {
607628
for _, r := range rules {
608629
sortMatchRules(r.MatchRules)
609630

631+
r.MirrorPercent = hpr.mirrorTargetToPercentage[r.Path]
632+
610633
s.PathRules = append(s.PathRules, r)
611634
}
612635

internal/controller/state/dataplane/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ type PathRule struct {
135135
Policies []policies.Policy
136136
// GRPC indicates if this is a gRPC rule
137137
GRPC bool
138+
// MirrorPercent is the percentage of requests to mirror for this PathRule. If this PathRule is not an internal
139+
// mirrored rule, this field is nil.
140+
MirrorPercent *float64
138141
}
139142

140143
// InvalidHTTPFilter is a special filter for handling the case when configured filters are invalid.

0 commit comments

Comments
 (0)