Skip to content

Commit b97f1a1

Browse files
Fix weighted cluster generation in RouteAction when URLRewrite filter is applied (#7027)
* Fix weighted cluster generation in RouteAction when URLRewrite filter is applied Signed-off-by: Sekar Saravanan <[email protected]> * XDS translator test added - Fix weighted cluster generation in RouteAction when URLRewrite filter is applied Signed-off-by: Sekar Saravanan <[email protected]> --------- Signed-off-by: Sekar Saravanan <[email protected]>
1 parent 2b1f351 commit b97f1a1

6 files changed

+275
-6
lines changed

internal/xds/translator/route.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func buildXdsRoute(httpRoute *ir.HTTPRoute, httpListener *ir.HTTPListener) (*rou
7171
case httpRoute.Redirect != nil:
7272
router.Action = &routev3.Route_Redirect{Redirect: buildXdsRedirectAction(httpRoute)}
7373
case httpRoute.URLRewrite != nil:
74-
routeAction := buildXdsURLRewriteAction(httpRoute.Destination.Name, httpRoute.URLRewrite, httpRoute.PathMatch)
74+
routeAction := buildXdsURLRewriteAction(httpRoute, httpRoute.URLRewrite, httpRoute.PathMatch)
7575
routeAction.IdleTimeout = idleTimeout(httpRoute)
7676
if httpRoute.Mirrors != nil {
7777
routeAction.RequestMirrorPolicies = buildXdsRequestMirrorPolicies(httpRoute.Mirrors)
@@ -446,11 +446,18 @@ func prefix2RegexRewrite(prefix string) *matcherv3.RegexMatchAndSubstitute {
446446
}
447447
}
448448

449-
func buildXdsURLRewriteAction(destName string, urlRewrite *ir.URLRewrite, pathMatch *ir.StringMatch) *routev3.RouteAction {
450-
routeAction := &routev3.RouteAction{
451-
ClusterSpecifier: &routev3.RouteAction_Cluster{
452-
Cluster: destName,
453-
},
449+
func buildXdsURLRewriteAction(route *ir.HTTPRoute, urlRewrite *ir.URLRewrite, pathMatch *ir.StringMatch) *routev3.RouteAction {
450+
backendWeights := route.Destination.ToBackendWeights()
451+
// only use weighted cluster when there are invalid weights
452+
var routeAction *routev3.RouteAction
453+
if route.NeedsClusterPerSetting() || backendWeights.Invalid != 0 {
454+
routeAction = buildXdsWeightedRouteAction(backendWeights, route.Destination.Settings)
455+
} else {
456+
routeAction = &routev3.RouteAction{
457+
ClusterSpecifier: &routev3.RouteAction_Cluster{
458+
Cluster: backendWeights.Name,
459+
},
460+
}
454461
}
455462

456463
if urlRewrite.Path != nil {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
http:
2+
- name: "first-listener"
3+
address: "::"
4+
hostnames:
5+
- '*'
6+
path:
7+
escapedSlashesAction: UnescapeAndRedirect
8+
mergeSlashes: true
9+
port: 10080
10+
routes:
11+
- destination:
12+
name: "first-route-dest"
13+
settings:
14+
- addressType: IP
15+
endpoints:
16+
- host: 1.1.1.1
17+
port: 8080
18+
name: "first-route-dest/backend/0"
19+
filters:
20+
addRequestHeaders:
21+
- append: false
22+
name: add-header-3
23+
value:
24+
- some-value
25+
protocol: HTTP
26+
weight: 1
27+
hostname: '*'
28+
name: "first-route"
29+
pathMatch:
30+
prefix: /foo
31+
urlRewrite:
32+
path:
33+
prefixMatchReplace: /
34+
- destination:
35+
name: "second-route-dest"
36+
settings:
37+
- addressType: IP
38+
endpoints:
39+
- host: 2.2.2.2
40+
port: 8080
41+
name: "second-route-dest/backend/0"
42+
filters:
43+
addRequestHeaders:
44+
- append: true
45+
name: add-header-1
46+
value:
47+
- some-value
48+
- append: true
49+
name: add-header-2
50+
value:
51+
- some-value
52+
protocol: HTTP
53+
weight: 8
54+
- addressType: IP
55+
endpoints:
56+
- host: 3.3.3.3
57+
port: 8080
58+
filters: {}
59+
protocol: HTTP
60+
weight: 2
61+
name: "second-route-dest/backend/1"
62+
hostname: '*'
63+
name: "second-route"
64+
pathMatch:
65+
prefix: /bar
66+
urlRewrite:
67+
path:
68+
prefixMatchReplace: /
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
- circuitBreakers:
2+
thresholds:
3+
- maxRetries: 1024
4+
commonLbConfig: {}
5+
connectTimeout: 10s
6+
dnsLookupFamily: V4_PREFERRED
7+
edsClusterConfig:
8+
edsConfig:
9+
ads: {}
10+
resourceApiVersion: V3
11+
serviceName: first-route-dest/backend/0
12+
ignoreHealthOnHostRemoval: true
13+
lbPolicy: LEAST_REQUEST
14+
loadBalancingPolicy:
15+
policies:
16+
- typedExtensionConfig:
17+
name: envoy.load_balancing_policies.least_request
18+
typedConfig:
19+
'@type': type.googleapis.com/envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest
20+
localityLbConfig:
21+
localityWeightedLbConfig: {}
22+
name: first-route-dest/backend/0
23+
perConnectionBufferLimitBytes: 32768
24+
type: EDS
25+
- circuitBreakers:
26+
thresholds:
27+
- maxRetries: 1024
28+
commonLbConfig: {}
29+
connectTimeout: 10s
30+
dnsLookupFamily: V4_PREFERRED
31+
edsClusterConfig:
32+
edsConfig:
33+
ads: {}
34+
resourceApiVersion: V3
35+
serviceName: second-route-dest/backend/0
36+
ignoreHealthOnHostRemoval: true
37+
lbPolicy: LEAST_REQUEST
38+
loadBalancingPolicy:
39+
policies:
40+
- typedExtensionConfig:
41+
name: envoy.load_balancing_policies.least_request
42+
typedConfig:
43+
'@type': type.googleapis.com/envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest
44+
localityLbConfig:
45+
localityWeightedLbConfig: {}
46+
name: second-route-dest/backend/0
47+
perConnectionBufferLimitBytes: 32768
48+
type: EDS
49+
- circuitBreakers:
50+
thresholds:
51+
- maxRetries: 1024
52+
commonLbConfig: {}
53+
connectTimeout: 10s
54+
dnsLookupFamily: V4_PREFERRED
55+
edsClusterConfig:
56+
edsConfig:
57+
ads: {}
58+
resourceApiVersion: V3
59+
serviceName: second-route-dest/backend/1
60+
ignoreHealthOnHostRemoval: true
61+
lbPolicy: LEAST_REQUEST
62+
loadBalancingPolicy:
63+
policies:
64+
- typedExtensionConfig:
65+
name: envoy.load_balancing_policies.least_request
66+
typedConfig:
67+
'@type': type.googleapis.com/envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest
68+
localityLbConfig:
69+
localityWeightedLbConfig: {}
70+
name: second-route-dest/backend/1
71+
perConnectionBufferLimitBytes: 32768
72+
type: EDS
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- clusterName: first-route-dest/backend/0
2+
endpoints:
3+
- lbEndpoints:
4+
- endpoint:
5+
address:
6+
socketAddress:
7+
address: 1.1.1.1
8+
portValue: 8080
9+
loadBalancingWeight: 1
10+
loadBalancingWeight: 1
11+
locality:
12+
region: first-route-dest/backend/0
13+
- clusterName: second-route-dest/backend/0
14+
endpoints:
15+
- lbEndpoints:
16+
- endpoint:
17+
address:
18+
socketAddress:
19+
address: 2.2.2.2
20+
portValue: 8080
21+
loadBalancingWeight: 1
22+
loadBalancingWeight: 8
23+
locality:
24+
region: second-route-dest/backend/0
25+
- clusterName: second-route-dest/backend/1
26+
endpoints:
27+
- lbEndpoints:
28+
- endpoint:
29+
address:
30+
socketAddress:
31+
address: 3.3.3.3
32+
portValue: 8080
33+
loadBalancingWeight: 1
34+
loadBalancingWeight: 2
35+
locality:
36+
region: second-route-dest/backend/1
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- address:
2+
socketAddress:
3+
address: '::'
4+
portValue: 10080
5+
defaultFilterChain:
6+
filters:
7+
- name: envoy.filters.network.http_connection_manager
8+
typedConfig:
9+
'@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
10+
commonHttpProtocolOptions:
11+
headersWithUnderscoresAction: REJECT_REQUEST
12+
http2ProtocolOptions:
13+
initialConnectionWindowSize: 1048576
14+
initialStreamWindowSize: 65536
15+
maxConcurrentStreams: 100
16+
httpFilters:
17+
- name: envoy.filters.http.router
18+
typedConfig:
19+
'@type': type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
20+
suppressEnvoyHeaders: true
21+
mergeSlashes: true
22+
normalizePath: true
23+
pathWithEscapedSlashesAction: UNESCAPE_AND_REDIRECT
24+
rds:
25+
configSource:
26+
ads: {}
27+
resourceApiVersion: V3
28+
routeConfigName: first-listener
29+
serverHeaderTransformation: PASS_THROUGH
30+
statPrefix: http-10080
31+
useRemoteAddress: true
32+
name: first-listener
33+
maxConnectionsToAcceptPerSocketEvent: 1
34+
name: first-listener
35+
perConnectionBufferLimitBytes: 32768
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
- ignorePortInHostMatching: true
2+
name: first-listener
3+
virtualHosts:
4+
- domains:
5+
- '*'
6+
name: first-listener/*
7+
routes:
8+
- match:
9+
pathSeparatedPrefix: /foo
10+
name: first-route
11+
route:
12+
clusterNotFoundResponseCode: INTERNAL_SERVER_ERROR
13+
regexRewrite:
14+
pattern:
15+
regex: ^/foo\/*
16+
substitution: /
17+
upgradeConfigs:
18+
- upgradeType: websocket
19+
weightedClusters:
20+
clusters:
21+
- name: first-route-dest/backend/0
22+
requestHeadersToAdd:
23+
- appendAction: OVERWRITE_IF_EXISTS_OR_ADD
24+
header:
25+
key: add-header-3
26+
value: some-value
27+
weight: 1
28+
- match:
29+
pathSeparatedPrefix: /bar
30+
name: second-route
31+
route:
32+
clusterNotFoundResponseCode: INTERNAL_SERVER_ERROR
33+
regexRewrite:
34+
pattern:
35+
regex: ^/bar\/*
36+
substitution: /
37+
upgradeConfigs:
38+
- upgradeType: websocket
39+
weightedClusters:
40+
clusters:
41+
- name: second-route-dest/backend/0
42+
requestHeadersToAdd:
43+
- header:
44+
key: add-header-1
45+
value: some-value
46+
- header:
47+
key: add-header-2
48+
value: some-value
49+
weight: 8
50+
- name: second-route-dest/backend/1
51+
weight: 2

0 commit comments

Comments
 (0)