/
route.go
570 lines (495 loc) · 17.1 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package translator
import (
"errors"
"strings"
"time"
"github.com/envoyproxy/gateway/internal/utils/protocov"
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
previoushost "github.com/envoyproxy/go-control-plane/envoy/extensions/retry/host/previous_hosts/v3"
matcherv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/envoyproxy/gateway/internal/ir"
)
const (
retryDefaultRetryOn = "connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes"
retryDefaultRetriableStatusCode = 503
retryDefaultNumRetries = 2
)
func buildXdsRoute(httpRoute *ir.HTTPRoute) (*routev3.Route, error) {
router := &routev3.Route{
Name: httpRoute.Name,
Match: buildXdsRouteMatch(httpRoute.PathMatch, httpRoute.HeaderMatches, httpRoute.QueryParamMatches),
}
if len(httpRoute.AddRequestHeaders) > 0 {
router.RequestHeadersToAdd = buildXdsAddedHeaders(httpRoute.AddRequestHeaders)
}
if len(httpRoute.RemoveRequestHeaders) > 0 {
router.RequestHeadersToRemove = httpRoute.RemoveRequestHeaders
}
if len(httpRoute.AddResponseHeaders) > 0 {
router.ResponseHeadersToAdd = buildXdsAddedHeaders(httpRoute.AddResponseHeaders)
}
if len(httpRoute.RemoveResponseHeaders) > 0 {
router.ResponseHeadersToRemove = httpRoute.RemoveResponseHeaders
}
switch {
case httpRoute.DirectResponse != nil:
router.Action = &routev3.Route_DirectResponse{DirectResponse: buildXdsDirectResponseAction(httpRoute.DirectResponse)}
case httpRoute.Redirect != nil:
router.Action = &routev3.Route_Redirect{Redirect: buildXdsRedirectAction(httpRoute)}
case httpRoute.URLRewrite != nil:
routeAction := buildXdsURLRewriteAction(httpRoute.Destination.Name, httpRoute.URLRewrite, httpRoute.PathMatch)
if httpRoute.Mirrors != nil {
routeAction.RequestMirrorPolicies = buildXdsRequestMirrorPolicies(httpRoute.Mirrors)
}
if !httpRoute.IsHTTP2 {
// Allow websocket upgrades for HTTP 1.1
// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
routeAction.UpgradeConfigs = []*routev3.RouteAction_UpgradeConfig{
{
UpgradeType: "websocket",
},
}
}
router.Action = &routev3.Route_Route{Route: routeAction}
default:
var routeAction *routev3.RouteAction
if httpRoute.BackendWeights.Invalid != 0 {
// If there are invalid backends then a weighted cluster is required for the route
routeAction = buildXdsWeightedRouteAction(httpRoute)
} else {
routeAction = buildXdsRouteAction(httpRoute)
}
if httpRoute.Mirrors != nil {
routeAction.RequestMirrorPolicies = buildXdsRequestMirrorPolicies(httpRoute.Mirrors)
}
if !httpRoute.IsHTTP2 {
// Allow websocket upgrades for HTTP 1.1
// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
routeAction.UpgradeConfigs = []*routev3.RouteAction_UpgradeConfig{
{
UpgradeType: "websocket",
},
}
}
router.Action = &routev3.Route_Route{Route: routeAction}
}
// Hash Policy
if router.GetRoute() != nil {
router.GetRoute().HashPolicy = buildHashPolicy(httpRoute)
}
// Timeouts
if router.GetRoute() != nil && httpRoute.Timeout != nil && httpRoute.Timeout.HTTP != nil &&
httpRoute.Timeout.HTTP.RequestTimeout != nil {
router.GetRoute().Timeout = durationpb.New(httpRoute.Timeout.HTTP.RequestTimeout.Duration)
}
// Retries
if router.GetRoute() != nil && httpRoute.Retry != nil {
if rp, err := buildRetryPolicy(httpRoute); err == nil {
router.GetRoute().RetryPolicy = rp
} else {
return nil, err
}
}
// Add per route filter configs to the route, if needed.
if err := patchRouteWithPerRouteConfig(router, httpRoute); err != nil {
return nil, err
}
return router, nil
}
func buildXdsRouteMatch(pathMatch *ir.StringMatch, headerMatches []*ir.StringMatch, queryParamMatches []*ir.StringMatch) *routev3.RouteMatch {
outMatch := &routev3.RouteMatch{}
// Add a prefix match to '/' if no matches are specified
if pathMatch == nil {
// Setup default path specifier. It may be overwritten by :host:.
outMatch.PathSpecifier = &routev3.RouteMatch_Prefix{
Prefix: "/",
}
} else {
// Path match
//nolint:gocritic
if pathMatch.Exact != nil {
outMatch.PathSpecifier = &routev3.RouteMatch_Path{
Path: *pathMatch.Exact,
}
} else if pathMatch.Prefix != nil {
if *pathMatch.Prefix == "/" {
outMatch.PathSpecifier = &routev3.RouteMatch_Prefix{
Prefix: "/",
}
} else {
// Remove trailing /
trimmedPrefix := strings.TrimSuffix(*pathMatch.Prefix, "/")
outMatch.PathSpecifier = &routev3.RouteMatch_PathSeparatedPrefix{
PathSeparatedPrefix: trimmedPrefix,
}
}
} else if pathMatch.SafeRegex != nil {
outMatch.PathSpecifier = &routev3.RouteMatch_SafeRegex{
SafeRegex: &matcherv3.RegexMatcher{
Regex: *pathMatch.SafeRegex,
},
}
}
}
// Header matches
for _, headerMatch := range headerMatches {
stringMatcher := buildXdsStringMatcher(headerMatch)
headerMatcher := &routev3.HeaderMatcher{
Name: headerMatch.Name,
HeaderMatchSpecifier: &routev3.HeaderMatcher_StringMatch{
StringMatch: stringMatcher,
},
}
outMatch.Headers = append(outMatch.Headers, headerMatcher)
}
// Query param matches
for _, queryParamMatch := range queryParamMatches {
stringMatcher := buildXdsStringMatcher(queryParamMatch)
queryParamMatcher := &routev3.QueryParameterMatcher{
Name: queryParamMatch.Name,
QueryParameterMatchSpecifier: &routev3.QueryParameterMatcher_StringMatch{
StringMatch: stringMatcher,
},
}
outMatch.QueryParameters = append(outMatch.QueryParameters, queryParamMatcher)
}
return outMatch
}
func buildXdsStringMatcher(irMatch *ir.StringMatch) *matcherv3.StringMatcher {
stringMatcher := new(matcherv3.StringMatcher)
//nolint:gocritic
if irMatch.Exact != nil {
stringMatcher = &matcherv3.StringMatcher{
MatchPattern: &matcherv3.StringMatcher_Exact{
Exact: *irMatch.Exact,
},
}
} else if irMatch.Prefix != nil {
stringMatcher = &matcherv3.StringMatcher{
MatchPattern: &matcherv3.StringMatcher_Prefix{
Prefix: *irMatch.Prefix,
},
}
} else if irMatch.Suffix != nil {
stringMatcher = &matcherv3.StringMatcher{
MatchPattern: &matcherv3.StringMatcher_Suffix{
Suffix: *irMatch.Suffix,
},
}
} else if irMatch.SafeRegex != nil {
stringMatcher = &matcherv3.StringMatcher{
MatchPattern: &matcherv3.StringMatcher_SafeRegex{
SafeRegex: &matcherv3.RegexMatcher{
Regex: *irMatch.SafeRegex,
},
},
}
}
return stringMatcher
}
func buildXdsRouteAction(httpRoute *ir.HTTPRoute) *routev3.RouteAction {
return &routev3.RouteAction{
ClusterSpecifier: &routev3.RouteAction_Cluster{
Cluster: httpRoute.Destination.Name,
},
IdleTimeout: idleTimeout(httpRoute),
}
}
func buildXdsWeightedRouteAction(httpRoute *ir.HTTPRoute) *routev3.RouteAction {
clusters := []*routev3.WeightedCluster_ClusterWeight{
{
Name: "invalid-backend-cluster",
Weight: &wrapperspb.UInt32Value{Value: httpRoute.BackendWeights.Invalid},
},
}
if httpRoute.BackendWeights.Valid > 0 {
validCluster := &routev3.WeightedCluster_ClusterWeight{
Name: httpRoute.Destination.Name,
Weight: &wrapperspb.UInt32Value{Value: httpRoute.BackendWeights.Valid},
}
clusters = append(clusters, validCluster)
}
return &routev3.RouteAction{
// Intentionally route to a non-existent cluster and return a 500 error when it is not found
ClusterNotFoundResponseCode: routev3.RouteAction_INTERNAL_SERVER_ERROR,
ClusterSpecifier: &routev3.RouteAction_WeightedClusters{
WeightedClusters: &routev3.WeightedCluster{
Clusters: clusters,
},
},
IdleTimeout: idleTimeout(httpRoute),
}
}
func idleTimeout(httpRoute *ir.HTTPRoute) *durationpb.Duration {
if httpRoute.Timeout != nil && httpRoute.Timeout.HTTP != nil {
if httpRoute.Timeout.HTTP.RequestTimeout != nil {
timeout := time.Hour // Default to 1 hour
// Ensure is not less than the request timeout
if timeout < httpRoute.Timeout.HTTP.RequestTimeout.Duration {
timeout = httpRoute.Timeout.HTTP.RequestTimeout.Duration
}
// Disable idle timeout when request timeout is disabled
if httpRoute.Timeout.HTTP.RequestTimeout.Duration == 0 {
timeout = 0
}
return durationpb.New(timeout)
}
}
return nil
}
func buildXdsRedirectAction(httpRoute *ir.HTTPRoute) *routev3.RedirectAction {
var (
redirection = httpRoute.Redirect
routeAction = &routev3.RedirectAction{}
)
if redirection.Scheme != nil {
routeAction.SchemeRewriteSpecifier = &routev3.RedirectAction_SchemeRedirect{
SchemeRedirect: *redirection.Scheme,
}
}
if redirection.Path != nil {
if redirection.Path.FullReplace != nil {
routeAction.PathRewriteSpecifier = &routev3.RedirectAction_PathRedirect{
PathRedirect: *redirection.Path.FullReplace,
}
} else if redirection.Path.PrefixMatchReplace != nil {
if useRegexRewriteForPrefixMatchReplace(httpRoute.PathMatch, *redirection.Path.PrefixMatchReplace) {
routeAction.PathRewriteSpecifier = &routev3.RedirectAction_RegexRewrite{
RegexRewrite: prefix2RegexRewrite(*httpRoute.PathMatch.Prefix),
}
} else {
routeAction.PathRewriteSpecifier = &routev3.RedirectAction_PrefixRewrite{
PrefixRewrite: *redirection.Path.PrefixMatchReplace,
}
}
}
}
if redirection.Hostname != nil {
routeAction.HostRedirect = *redirection.Hostname
}
if redirection.Port != nil {
routeAction.PortRedirect = *redirection.Port
}
if redirection.StatusCode != nil {
if *redirection.StatusCode == 302 {
routeAction.ResponseCode = routev3.RedirectAction_FOUND
} // no need to check for 301 since Envoy will use 301 as the default if the field is not configured
}
return routeAction
}
// useRegexRewriteForPrefixMatchReplace checks if the regex rewrite should be used for prefix match replace
// due to the issue with Envoy not handling the case of "//" when the replace string is "/".
// See: https://github.com/envoyproxy/envoy/issues/26055
func useRegexRewriteForPrefixMatchReplace(pathMatch *ir.StringMatch, prefixMatchReplace string) bool {
return pathMatch != nil &&
pathMatch.Prefix != nil &&
(prefixMatchReplace == "" || prefixMatchReplace == "/")
}
func prefix2RegexRewrite(prefix string) *matcherv3.RegexMatchAndSubstitute {
return &matcherv3.RegexMatchAndSubstitute{
Pattern: &matcherv3.RegexMatcher{
Regex: "^" + prefix + `\/*`,
},
Substitution: "/",
}
}
func buildXdsURLRewriteAction(destName string, urlRewrite *ir.URLRewrite, pathMatch *ir.StringMatch) *routev3.RouteAction {
routeAction := &routev3.RouteAction{
ClusterSpecifier: &routev3.RouteAction_Cluster{
Cluster: destName,
},
}
if urlRewrite.Path != nil {
if urlRewrite.Path.FullReplace != nil {
routeAction.RegexRewrite = &matcherv3.RegexMatchAndSubstitute{
Pattern: &matcherv3.RegexMatcher{
Regex: "/.+",
},
Substitution: *urlRewrite.Path.FullReplace,
}
} else if urlRewrite.Path.PrefixMatchReplace != nil {
// Circumvent the case of "//" when the replace string is "/"
// An empty replace string does not seem to solve the issue so we are using
// a regex match and replace instead
// Remove this workaround once https://github.com/envoyproxy/envoy/issues/26055 is fixed
if useRegexRewriteForPrefixMatchReplace(pathMatch, *urlRewrite.Path.PrefixMatchReplace) {
routeAction.RegexRewrite = prefix2RegexRewrite(*pathMatch.Prefix)
} else {
routeAction.PrefixRewrite = *urlRewrite.Path.PrefixMatchReplace
}
}
}
if urlRewrite.Hostname != nil {
routeAction.HostRewriteSpecifier = &routev3.RouteAction_HostRewriteLiteral{
HostRewriteLiteral: *urlRewrite.Hostname,
}
routeAction.AppendXForwardedHost = true
}
return routeAction
}
func buildXdsDirectResponseAction(res *ir.DirectResponse) *routev3.DirectResponseAction {
routeAction := &routev3.DirectResponseAction{Status: res.StatusCode}
if res.Body != nil {
routeAction.Body = &corev3.DataSource{
Specifier: &corev3.DataSource_InlineString{
InlineString: *res.Body,
},
}
}
return routeAction
}
func buildXdsRequestMirrorPolicies(mirrorDestinations []*ir.RouteDestination) []*routev3.RouteAction_RequestMirrorPolicy {
var mirrorPolicies []*routev3.RouteAction_RequestMirrorPolicy
for _, mirrorDest := range mirrorDestinations {
mirrorPolicies = append(mirrorPolicies, &routev3.RouteAction_RequestMirrorPolicy{
Cluster: mirrorDest.Name,
})
}
return mirrorPolicies
}
func buildXdsAddedHeaders(headersToAdd []ir.AddHeader) []*corev3.HeaderValueOption {
headerValueOptions := make([]*corev3.HeaderValueOption, len(headersToAdd))
for i, header := range headersToAdd {
var appendAction corev3.HeaderValueOption_HeaderAppendAction
if header.Append {
appendAction = corev3.HeaderValueOption_APPEND_IF_EXISTS_OR_ADD
} else {
appendAction = corev3.HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD
}
headerValueOptions[i] = &corev3.HeaderValueOption{
Header: &corev3.HeaderValue{
Key: header.Name,
Value: header.Value,
},
AppendAction: appendAction,
}
// Allow empty headers to be set, but don't add the config to do so unless necessary
if header.Value == "" {
headerValueOptions[i].KeepEmptyValue = true
}
}
return headerValueOptions
}
func buildHashPolicy(httpRoute *ir.HTTPRoute) []*routev3.RouteAction_HashPolicy {
// Return early
if httpRoute == nil || httpRoute.LoadBalancer == nil || httpRoute.LoadBalancer.ConsistentHash == nil {
return nil
}
if httpRoute.LoadBalancer.ConsistentHash.SourceIP != nil && *httpRoute.LoadBalancer.ConsistentHash.SourceIP {
hashPolicy := &routev3.RouteAction_HashPolicy{
PolicySpecifier: &routev3.RouteAction_HashPolicy_ConnectionProperties_{
ConnectionProperties: &routev3.RouteAction_HashPolicy_ConnectionProperties{
SourceIp: true,
},
},
}
return []*routev3.RouteAction_HashPolicy{hashPolicy}
}
return nil
}
func buildRetryPolicy(route *ir.HTTPRoute) (*routev3.RetryPolicy, error) {
if route.Retry != nil {
rr := route.Retry
rp := &routev3.RetryPolicy{
RetryOn: retryDefaultRetryOn,
RetriableStatusCodes: []uint32{retryDefaultRetriableStatusCode},
NumRetries: &wrapperspb.UInt32Value{Value: retryDefaultNumRetries},
RetryHostPredicate: []*routev3.RetryPolicy_RetryHostPredicate{
{
Name: "envoy.retry_host_predicates.previous_hosts",
ConfigType: &routev3.RetryPolicy_RetryHostPredicate_TypedConfig{
TypedConfig: protocov.ToAny(&previoushost.PreviousHostsPredicate{}),
},
},
},
HostSelectionRetryMaxAttempts: 5,
}
if rr.NumRetries != nil {
rp.NumRetries = &wrapperspb.UInt32Value{Value: *rr.NumRetries}
}
if rr.RetryOn != nil {
if rr.RetryOn.Triggers != nil && len(rr.RetryOn.Triggers) > 0 {
if ro, err := buildRetryOn(rr.RetryOn.Triggers); err == nil {
rp.RetryOn = ro
} else {
return nil, err
}
}
if rr.RetryOn.HTTPStatusCodes != nil && len(rr.RetryOn.HTTPStatusCodes) > 0 {
rp.RetriableStatusCodes = buildRetryStatusCodes(rr.RetryOn.HTTPStatusCodes)
}
}
if rr.PerRetry != nil {
if rr.PerRetry.Timeout != nil {
rp.PerTryTimeout = durationpb.New(rr.PerRetry.Timeout.Duration)
}
if rr.PerRetry.BackOff != nil {
bbo := false
rbo := &routev3.RetryPolicy_RetryBackOff{}
if rr.PerRetry.BackOff.BaseInterval != nil {
rbo.BaseInterval = durationpb.New(rr.PerRetry.BackOff.BaseInterval.Duration)
bbo = true
}
if rr.PerRetry.BackOff.MaxInterval != nil {
rbo.MaxInterval = durationpb.New(rr.PerRetry.BackOff.MaxInterval.Duration)
bbo = true
}
if bbo {
rp.RetryBackOff = rbo
}
}
}
return rp, nil
}
return nil, nil
}
func buildRetryStatusCodes(codes []ir.HTTPStatus) []uint32 {
ret := make([]uint32, len(codes))
for i, c := range codes {
ret[i] = uint32(c)
}
return ret
}
// buildRetryOn concatenates triggers to a comma-delimited string.
// An error is returned if a trigger is not in the list of supported values (not likely, due to prior validations).
func buildRetryOn(triggers []ir.TriggerEnum) (string, error) {
if len(triggers) == 0 {
return "", nil
}
lookup := map[ir.TriggerEnum]string{
ir.Error5XX: "5xx",
ir.GatewayError: "gateway-error",
ir.Reset: "reset",
ir.ConnectFailure: "connect-failure",
ir.Retriable4XX: "retriable-4xx",
ir.RefusedStream: "refused-stream",
ir.RetriableStatusCodes: "retriable-status-codes",
ir.Cancelled: "cancelled",
ir.DeadlineExceeded: "deadline-exceeded",
ir.Internal: "internal",
ir.ResourceExhausted: "resource-exhausted",
ir.Unavailable: "unavailable",
}
var b strings.Builder
if t, found := lookup[triggers[0]]; found {
b.WriteString(t)
} else {
return "", errors.New("unsupported RetryOn trigger")
}
for _, v := range triggers[1:] {
if t, found := lookup[v]; found {
b.WriteString(",")
b.WriteString(t)
} else {
return "", errors.New("unsupported RetryOn trigger")
}
}
return b.String(), nil
}