Skip to content

Commit 9a814bd

Browse files
Adds / Updates test cases for client wrr
Signed-off-by: anurag.ag <[email protected]>
1 parent 8fd98df commit 9a814bd

File tree

14 files changed

+651
-37
lines changed

14 files changed

+651
-37
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Envoy Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
package gatewayapi
7+
8+
import (
9+
"testing"
10+
"time"
11+
12+
"github.com/stretchr/testify/require"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
15+
16+
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
17+
"github.com/envoyproxy/gateway/internal/ir"
18+
)
19+
20+
func TestBuildLoadBalancer_ClientSideWeightedRoundRobin(t *testing.T) {
21+
cswrr := &egv1a1.ClientSideWeightedRoundRobin{
22+
EnableOOBLoadReport: ptrBool(true),
23+
OOBReportingPeriod: ptrDuration("5s"),
24+
BlackoutPeriod: ptrDuration("10s"),
25+
WeightExpirationPeriod: ptrDuration("3m"),
26+
WeightUpdatePeriod: ptrDuration("1s"),
27+
ErrorUtilizationPenalty: ptrFloat32(1.5),
28+
MetricNamesForComputingUtilization: []string{"named_metrics.foo", "cpu_utilization"},
29+
}
30+
31+
policy := &egv1a1.ClusterSettings{
32+
LoadBalancer: &egv1a1.LoadBalancer{
33+
Type: egv1a1.ClientSideWeightedRoundRobinLoadBalancerType,
34+
ClientSideWeightedRoundRobin: cswrr,
35+
},
36+
}
37+
38+
lb, err := buildLoadBalancer(policy)
39+
require.NoError(t, err)
40+
require.NotNil(t, lb)
41+
require.NotNil(t, lb.ClientSideWeightedRoundRobin)
42+
43+
got := lb.ClientSideWeightedRoundRobin
44+
require.Equal(t, cswrr.EnableOOBLoadReport, got.EnableOOBLoadReport)
45+
require.Equal(t, toMetaV1Duration(t, "5s"), got.OOBReportingPeriod)
46+
require.Equal(t, toMetaV1Duration(t, "10s"), got.BlackoutPeriod)
47+
require.Equal(t, toMetaV1Duration(t, "3m"), got.WeightExpirationPeriod)
48+
require.Equal(t, toMetaV1Duration(t, "1s"), got.WeightUpdatePeriod)
49+
require.NotNil(t, got.ErrorUtilizationPenalty)
50+
require.InDelta(t, 1.5, *got.ErrorUtilizationPenalty, 0.0001)
51+
require.Equal(t, []string{"named_metrics.foo", "cpu_utilization"}, got.MetricNamesForComputingUtilization)
52+
}
53+
54+
func ptrBool(v bool) *bool { return &v }
55+
func ptrFloat32(v float32) *float32 { return &v }
56+
func ptrDuration(d string) *gwapiv1.Duration { v := gwapiv1.Duration(d); return &v }
57+
58+
func toMetaV1Duration(t *testing.T, d string) *metav1.Duration {
59+
dur, err := time.ParseDuration(d)
60+
require.NoError(t, err)
61+
return ir.MetaV1DurationPtr(dur)
62+
}

internal/gatewayapi/testdata/backendtrafficpolicy-with-loadbalancer.in.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ httpRoutes:
100100
backendRefs:
101101
- name: service-2
102102
port: 8080
103+
- apiVersion: gateway.networking.k8s.io/v1
104+
kind: HTTPRoute
105+
metadata:
106+
namespace: default
107+
name: httproute-4
108+
spec:
109+
hostnames:
110+
- gateway.envoyproxy.io
111+
parentRefs:
112+
- namespace: envoy-gateway
113+
name: gateway-2
114+
sectionName: http
115+
rules:
116+
- matches:
117+
- path:
118+
value: "/test4"
119+
backendRefs:
120+
- name: service-2
121+
port: 8080
103122
backendTrafficPolicies:
104123
- apiVersion: gateway.envoyproxy.io/v1alpha1
105124
kind: BackendTrafficPolicy
@@ -178,3 +197,22 @@ backendTrafficPolicies:
178197
type: Cookie
179198
cookie:
180199
name: "test"
200+
- apiVersion: gateway.envoyproxy.io/v1alpha1
201+
kind: BackendTrafficPolicy
202+
metadata:
203+
namespace: default
204+
name: policy-for-route4
205+
spec:
206+
targetRef:
207+
group: gateway.networking.k8s.io
208+
kind: HTTPRoute
209+
name: httproute-4
210+
loadBalancer:
211+
type: ClientSideWeightedRoundRobin
212+
clientSideWeightedRoundRobin:
213+
blackoutPeriod: 10s
214+
weightExpirationPeriod: 60s
215+
weightUpdatePeriod: 10s
216+
errorUtilizationPenalty: 1
217+
metricNamesForComputingUtilization:
218+
- "cpu_utilization"

internal/gatewayapi/testdata/backendtrafficpolicy-with-loadbalancer.out.yaml

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ backendTrafficPolicies:
8989
status: "True"
9090
type: Accepted
9191
controllerName: gateway.envoyproxy.io/gatewayclass-controller
92+
- apiVersion: gateway.envoyproxy.io/v1alpha1
93+
kind: BackendTrafficPolicy
94+
metadata:
95+
name: policy-for-route4
96+
namespace: default
97+
spec:
98+
loadBalancer:
99+
type: ClientSideWeightedRoundRobin
100+
clientSideWeightedRoundRobin:
101+
blackoutPeriod: 10s
102+
weightExpirationPeriod: 60s
103+
weightUpdatePeriod: 10s
104+
errorUtilizationPenalty: 1
105+
metricNamesForComputingUtilization:
106+
- "cpu_utilization"
107+
targetRef:
108+
group: gateway.networking.k8s.io
109+
kind: HTTPRoute
110+
name: httproute-4
111+
status:
112+
ancestors:
113+
- ancestorRef:
114+
group: gateway.networking.k8s.io
115+
kind: Gateway
116+
name: gateway-2
117+
namespace: envoy-gateway
118+
sectionName: http
119+
conditions:
120+
- lastTransitionTime: null
121+
message: Policy has been accepted.
122+
reason: Accepted
123+
status: "True"
124+
type: Accepted
125+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
92126
- apiVersion: gateway.envoyproxy.io/v1alpha1
93127
kind: BackendTrafficPolicy
94128
metadata:
@@ -150,7 +184,7 @@ backendTrafficPolicies:
150184
type: Accepted
151185
- lastTransitionTime: null
152186
message: 'This policy is being overridden by other backendTrafficPolicies
153-
for these routes: [default/httproute-1 default/httproute-2 default/httproute-3]'
187+
for these routes: [default/httproute-1 default/httproute-2 default/httproute-3 default/httproute-4]'
154188
reason: Overridden
155189
status: "True"
156190
type: Overridden
@@ -211,7 +245,7 @@ gateways:
211245
protocol: HTTP
212246
status:
213247
listeners:
214-
- attachedRoutes: 3
248+
- attachedRoutes: 4
215249
conditions:
216250
- lastTransitionTime: null
217251
message: Sending translated listener configuration to the data plane
@@ -379,6 +413,43 @@ httpRoutes:
379413
name: gateway-2
380414
namespace: envoy-gateway
381415
sectionName: http
416+
- apiVersion: gateway.networking.k8s.io/v1
417+
kind: HTTPRoute
418+
metadata:
419+
name: httproute-4
420+
namespace: default
421+
spec:
422+
hostnames:
423+
- gateway.envoyproxy.io
424+
parentRefs:
425+
- name: gateway-2
426+
namespace: envoy-gateway
427+
sectionName: http
428+
rules:
429+
- backendRefs:
430+
- name: service-2
431+
port: 8080
432+
matches:
433+
- path:
434+
value: /test4
435+
status:
436+
parents:
437+
- conditions:
438+
- lastTransitionTime: null
439+
message: Route is accepted
440+
reason: Accepted
441+
status: "True"
442+
type: Accepted
443+
- lastTransitionTime: null
444+
message: Resolved all the Object references for the Route
445+
reason: ResolvedRefs
446+
status: "True"
447+
type: ResolvedRefs
448+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
449+
parentRef:
450+
name: gateway-2
451+
namespace: envoy-gateway
452+
sectionName: http
382453
infraIR:
383454
envoy-gateway/gateway-1:
384455
proxy:
@@ -600,6 +671,44 @@ xdsIR:
600671
consistentHash:
601672
cookie:
602673
name: test
674+
- destination:
675+
metadata:
676+
kind: HTTPRoute
677+
name: httproute-4
678+
namespace: default
679+
name: httproute/default/httproute-4/rule/0
680+
settings:
681+
- addressType: IP
682+
endpoints:
683+
- host: 7.7.7.7
684+
port: 8080
685+
metadata:
686+
name: service-2
687+
namespace: default
688+
sectionName: "8080"
689+
name: httproute/default/httproute-4/rule/0/backend/0
690+
protocol: HTTP
691+
weight: 1
692+
hostname: gateway.envoyproxy.io
693+
isHTTP2: false
694+
metadata:
695+
kind: HTTPRoute
696+
name: httproute-4
697+
namespace: default
698+
name: httproute/default/httproute-4/rule/0/match/0/gateway_envoyproxy_io
699+
pathMatch:
700+
distinct: false
701+
name: ""
702+
prefix: /test4
703+
traffic:
704+
loadBalancer:
705+
clientSideWeightedRoundRobin:
706+
blackoutPeriod: 10s
707+
weightExpirationPeriod: 60s
708+
weightUpdatePeriod: 10s
709+
errorUtilizationPenalty: 1
710+
metricNamesForComputingUtilization:
711+
- "cpu_utilization"
603712
- destination:
604713
metadata:
605714
kind: HTTPRoute

internal/ir/xds_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,15 @@ func TestValidateLoadBalancer(t *testing.T) {
13371337
},
13381338
want: ErrLoadBalancerInvalid,
13391339
},
1340+
{
1341+
name: "client side wrr set",
1342+
input: LoadBalancer{
1343+
ClientSideWeightedRoundRobin: &ClientSideWeightedRoundRobin{
1344+
EnableOOBLoadReport: ptr.To(false),
1345+
MetricNamesForComputingUtilization: []string{"named_metrics.foo"},
1346+
},
1347+
},
1348+
},
13401349
}
13411350
for i := range tests {
13421351
test := tests[i]

internal/xds/translator/cluster.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ func buildXdsCluster(args *xdsClusterArgs) (*buildClusterResult, error) {
399399
}},
400400
}
401401
case args.loadBalancer.ClientSideWeightedRoundRobin != nil:
402-
cluster.LbPolicy = clusterv3.Cluster_CLUSTER_PROVIDED
403402
cswrr := &cswrrv3.ClientSideWeightedRoundRobin{}
404403
if v := args.loadBalancer.ClientSideWeightedRoundRobin; v != nil {
405404
if v.EnableOOBLoadReport != nil {

internal/xds/translator/cluster_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ func TestBuildCluster_WithClientSideWeightedRoundRobin(t *testing.T) {
176176
cluster := result.cluster
177177
require.NotNil(t, cluster)
178178

179-
// LbPolicy should be CLUSTER_PROVIDED when using typed LoadBalancingPolicy
180-
require.Equal(t, clusterv3.Cluster_CLUSTER_PROVIDED, cluster.LbPolicy)
181179
require.NotNil(t, cluster.LoadBalancingPolicy)
182180
require.Len(t, cluster.LoadBalancingPolicy.Policies, 1)
183181

internal/xds/translator/testdata/in/xds-ir/load-balancer.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,40 @@ http:
202202
- host: "1.2.3.4"
203203
port: 50000
204204
name: "fourteenth-route-dest/backend/0"
205+
- name: "fifteenth-route"
206+
hostname: "*"
207+
traffic:
208+
loadBalancer:
209+
clientSideWeightedRoundRobin:
210+
blackoutPeriod: 30s
211+
weightExpirationPeriod: 60s
212+
weightUpdatePeriod: 10s
213+
errorUtilizationPenalty: 1
214+
metricNamesForComputingUtilization:
215+
- "cpu_utilization"
216+
destination:
217+
name: "fifteenth-route-dest"
218+
settings:
219+
- endpoints:
220+
- host: "1.2.3.4"
221+
port: 50000
222+
name: "fifteenth-route-dest/backend/0"
223+
- name: "sixteenth-route"
224+
hostname: "*"
225+
traffic:
226+
loadBalancer:
227+
clientSideWeightedRoundRobin:
228+
enableOobLoadReport: true
229+
oobReportingPeriod: 20s
230+
blackoutPeriod: 30s
231+
weightExpirationPeriod: 60s
232+
weightUpdatePeriod: 10s
233+
metricNamesForComputingUtilization:
234+
- "named_metric.foo"
235+
destination:
236+
name: "sixteenth-route-dest"
237+
settings:
238+
- endpoints:
239+
- host: "1.2.3.4"
240+
port: 50000
241+
name: "sixteenth-route-dest/backend/0"

internal/xds/translator/testdata/out/xds-ir/load-balancer.clusters.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,58 @@
343343
name: fourteenth-route-dest
344344
perConnectionBufferLimitBytes: 32768
345345
type: EDS
346+
- circuitBreakers:
347+
thresholds:
348+
- maxRetries: 1024
349+
commonLbConfig: {}
350+
connectTimeout: 10s
351+
dnsLookupFamily: V4_PREFERRED
352+
edsClusterConfig:
353+
edsConfig:
354+
ads: {}
355+
resourceApiVersion: V3
356+
serviceName: fifteenth-route-dest
357+
ignoreHealthOnHostRemoval: true
358+
loadBalancingPolicy:
359+
policies:
360+
- typedExtensionConfig:
361+
name: envoy.load_balancing_policies.client_side_weighted_round_robin
362+
typedConfig:
363+
'@type': type.googleapis.com/envoy.extensions.load_balancing_policies.client_side_weighted_round_robin.v3.ClientSideWeightedRoundRobin
364+
blackoutPeriod: 30s
365+
errorUtilizationPenalty: 1
366+
metricNamesForComputingUtilization:
367+
- cpu_utilization
368+
weightExpirationPeriod: 60s
369+
weightUpdatePeriod: 10s
370+
name: fifteenth-route-dest
371+
perConnectionBufferLimitBytes: 32768
372+
type: EDS
373+
- circuitBreakers:
374+
thresholds:
375+
- maxRetries: 1024
376+
commonLbConfig: {}
377+
connectTimeout: 10s
378+
dnsLookupFamily: V4_PREFERRED
379+
edsClusterConfig:
380+
edsConfig:
381+
ads: {}
382+
resourceApiVersion: V3
383+
serviceName: sixteenth-route-dest
384+
ignoreHealthOnHostRemoval: true
385+
loadBalancingPolicy:
386+
policies:
387+
- typedExtensionConfig:
388+
name: envoy.load_balancing_policies.client_side_weighted_round_robin
389+
typedConfig:
390+
'@type': type.googleapis.com/envoy.extensions.load_balancing_policies.client_side_weighted_round_robin.v3.ClientSideWeightedRoundRobin
391+
blackoutPeriod: 30s
392+
enableOobLoadReport: true
393+
metricNamesForComputingUtilization:
394+
- named_metric.foo
395+
oobReportingPeriod: 20s
396+
weightExpirationPeriod: 60s
397+
weightUpdatePeriod: 10s
398+
name: sixteenth-route-dest
399+
perConnectionBufferLimitBytes: 32768
400+
type: EDS

0 commit comments

Comments
 (0)