-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathroute-spec.ts
650 lines (569 loc) · 16.4 KB
/
route-spec.ts
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import { Construct } from 'constructs';
import { CfnRoute } from './appmesh.generated';
import { HeaderMatch } from './header-match';
import { HttpRouteMethod } from './http-route-method';
import { HttpRoutePathMatch } from './http-route-path-match';
import { validateGrpcRouteMatch, validateGrpcMatchArrayLength, validateHttpMatchArrayLength } from './private/utils';
import { QueryParameterMatch } from './query-parameter-match';
import { GrpcTimeout, HttpTimeout, Protocol, TcpTimeout } from './shared-interfaces';
import { IVirtualNode } from './virtual-node';
import * as cdk from '../../core';
/**
* Properties for the Weighted Targets in the route
*/
export interface WeightedTarget {
/**
* The VirtualNode the route points to
*/
readonly virtualNode: IVirtualNode;
/**
* The weight for the target
*
* @default 1
*/
readonly weight?: number;
}
/**
* The criterion for determining a request match for this Route
*/
export interface HttpRouteMatch {
/**
* Specifies how is the request matched based on the path part of its URL.
*
* @default - matches requests with all paths
*/
readonly path?: HttpRoutePathMatch;
/**
* Specifies the client request headers to match on. All specified headers
* must match for the route to match.
*
* @default - do not match on headers
*/
readonly headers?: HeaderMatch[];
/**
* The HTTP client request method to match on.
*
* @default - do not match on request method
*/
readonly method?: HttpRouteMethod;
/**
* The client request protocol to match on. Applicable only for HTTP2 routes.
*
* @default - do not match on HTTP2 request protocol
*/
readonly protocol?: HttpRouteProtocol;
/**
* The query parameters to match on.
* All specified query parameters must match for the route to match.
*
* @default - do not match on query parameters
*/
readonly queryParameters?: QueryParameterMatch[];
/**
* The port to match from the request.
*
* @default - do not match on port
*/
readonly port?: number;
}
/**
* Supported :scheme options for HTTP2
*/
export enum HttpRouteProtocol {
/**
* Match HTTP requests
*/
HTTP = 'http',
/**
* Match HTTPS requests
*/
HTTPS = 'https',
}
/**
* The criterion for determining a request match for this Route.
* At least one match type must be selected.
*/
export interface GrpcRouteMatch {
/**
* Create service name based gRPC route match.
*
* @default - do not match on service name
*/
readonly serviceName?: string;
/**
* Create metadata based gRPC route match.
* All specified metadata must match for the route to match.
*
* @default - do not match on metadata
*/
readonly metadata?: HeaderMatch[];
/**
* The method name to match from the request.
* If the method name is specified, service name must be also provided.
*
* @default - do not match on method name
*/
readonly methodName?: string;
/**
* The port to match from the request.
*
* @default - do not match on port
*/
readonly port?: number;
}
/**
* Base options for all route specs.
*/
export interface RouteSpecOptionsBase {
/**
* The priority for the route. When a Virtual Router has multiple routes, route match is performed in the
* order of specified value, where 0 is the highest priority, and first matched route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}
/**
* Properties specific for HTTP Based Routes
*/
export interface HttpRouteSpecOptions extends RouteSpecOptionsBase {
/**
* The criterion for determining a request match for this Route
*
* @default - matches on '/'
*/
readonly match?: HttpRouteMatch;
/**
* List of targets that traffic is routed to when a request matches the route
*/
readonly weightedTargets: WeightedTarget[];
/**
* An object that represents a http timeout
*
* @default - None
*/
readonly timeout?: HttpTimeout;
/**
* The retry policy
*
* @default - no retry policy
*/
readonly retryPolicy?: HttpRetryPolicy;
}
/**
* HTTP retry policy
*/
export interface HttpRetryPolicy {
/**
* Specify HTTP events on which to retry. You must specify at least one value
* for at least one types of retry events.
*
* @default - no retries for http events
*/
readonly httpRetryEvents?: HttpRetryEvent[];
/**
* The maximum number of retry attempts
*/
readonly retryAttempts: number;
/**
* The timeout for each retry attempt
*/
readonly retryTimeout: cdk.Duration;
/**
* TCP events on which to retry. The event occurs before any processing of a
* request has started and is encountered when the upstream is temporarily or
* permanently unavailable. You must specify at least one value for at least
* one types of retry events.
*
* @default - no retries for tcp events
*/
readonly tcpRetryEvents?: TcpRetryEvent[];
}
/**
* HTTP events on which to retry.
*/
export enum HttpRetryEvent {
/**
* HTTP status codes 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, and 511
*/
SERVER_ERROR = 'server-error',
/**
* HTTP status codes 502, 503, and 504
*/
GATEWAY_ERROR = 'gateway-error',
/**
* HTTP status code 409
*/
CLIENT_ERROR = 'client-error',
/**
* Retry on refused stream
*/
STREAM_ERROR = 'stream-error',
}
/**
* TCP events on which you may retry
*/
export enum TcpRetryEvent {
/**
* A connection error
*/
CONNECTION_ERROR = 'connection-error',
}
/**
* Properties specific for a TCP Based Routes
*/
export interface TcpRouteSpecOptions extends RouteSpecOptionsBase {
/**
* List of targets that traffic is routed to when a request matches the route
*/
readonly weightedTargets: WeightedTarget[];
/**
* An object that represents a tcp timeout
*
* @default - None
*/
readonly timeout?: TcpTimeout;
}
/**
* Properties specific for a GRPC Based Routes
*/
export interface GrpcRouteSpecOptions extends RouteSpecOptionsBase {
/**
* The criterion for determining a request match for this Route
*/
readonly match: GrpcRouteMatch;
/**
* An object that represents a grpc timeout
*
* @default - None
*/
readonly timeout?: GrpcTimeout;
/**
* List of targets that traffic is routed to when a request matches the route
*/
readonly weightedTargets: WeightedTarget[];
/**
* The retry policy
*
* @default - no retry policy
*/
readonly retryPolicy?: GrpcRetryPolicy;
}
/** gRPC retry policy */
export interface GrpcRetryPolicy extends HttpRetryPolicy {
/**
* gRPC events on which to retry. You must specify at least one value
* for at least one types of retry events.
*
* @default - no retries for gRPC events
*/
readonly grpcRetryEvents?: GrpcRetryEvent[];
}
/**
* gRPC events
*/
export enum GrpcRetryEvent {
/**
* Request was cancelled
*
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
*/
CANCELLED = 'cancelled',
/**
* The deadline was exceeded
*
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
*/
DEADLINE_EXCEEDED = 'deadline-exceeded',
/**
* Internal error
*
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
*/
INTERNAL_ERROR = 'internal',
/**
* A resource was exhausted
*
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
*/
RESOURCE_EXHAUSTED = 'resource-exhausted',
/**
* The service is unavailable
*
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
*/
UNAVAILABLE = 'unavailable',
}
/**
* All Properties for Route Specs
*/
export interface RouteSpecConfig {
/**
* The spec for an http route
*
* @default - no http spec
*/
readonly httpRouteSpec?: CfnRoute.HttpRouteProperty;
/**
* The spec for an http2 route
*
* @default - no http2 spec
*/
readonly http2RouteSpec?: CfnRoute.HttpRouteProperty;
/**
* The spec for a grpc route
*
* @default - no grpc spec
*/
readonly grpcRouteSpec?: CfnRoute.GrpcRouteProperty;
/**
* The spec for a tcp route
*
* @default - no tcp spec
*/
readonly tcpRouteSpec?: CfnRoute.TcpRouteProperty;
/**
* The priority for the route. When a Virtual Router has multiple routes, route match is performed in the
* order of specified value, where 0 is the highest priority, and first matched route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}
/**
* Used to generate specs with different protocols for a RouteSpec
*/
export abstract class RouteSpec {
/**
* Creates an HTTP Based RouteSpec
*/
public static http(options: HttpRouteSpecOptions): RouteSpec {
return new HttpRouteSpec(options, Protocol.HTTP);
}
/**
* Creates an HTTP2 Based RouteSpec
*
*/
public static http2(options: HttpRouteSpecOptions): RouteSpec {
return new HttpRouteSpec(options, Protocol.HTTP2);
}
/**
* Creates a TCP Based RouteSpec
*/
public static tcp(options: TcpRouteSpecOptions): RouteSpec {
return new TcpRouteSpec(options);
}
/**
* Creates a GRPC Based RouteSpec
*/
public static grpc(options: GrpcRouteSpecOptions): RouteSpec {
return new GrpcRouteSpec(options);
}
/**
* Called when the RouteSpec type is initialized. Can be used to enforce
* mutual exclusivity with future properties
*/
public abstract bind(scope: Construct): RouteSpecConfig;
}
class HttpRouteSpec extends RouteSpec {
public readonly priority?: number;
public readonly protocol: Protocol;
public readonly match?: HttpRouteMatch;
public readonly timeout?: HttpTimeout;
public readonly weightedTargets: WeightedTarget[];
/**
* The retry policy
*/
public readonly retryPolicy?: HttpRetryPolicy;
constructor(props: HttpRouteSpecOptions, protocol: Protocol) {
super();
this.protocol = protocol;
this.match = props.match;
this.weightedTargets = props.weightedTargets;
this.timeout = props.timeout;
this.priority = props.priority;
if (props.retryPolicy) {
const httpRetryEvents = props.retryPolicy.httpRetryEvents ?? [];
const tcpRetryEvents = props.retryPolicy.tcpRetryEvents ?? [];
if (httpRetryEvents.length + tcpRetryEvents.length === 0) {
throw new Error('You must specify one value for at least one of `httpRetryEvents` or `tcpRetryEvents`');
}
this.retryPolicy = {
...props.retryPolicy,
httpRetryEvents: httpRetryEvents.length > 0 ? httpRetryEvents : undefined,
tcpRetryEvents: tcpRetryEvents.length > 0 ? tcpRetryEvents : undefined,
};
}
}
public bind(scope: Construct): RouteSpecConfig {
const pathMatchConfig = (this.match?.path ?? HttpRoutePathMatch.startsWith('/')).bind(scope);
// Set prefix path match to '/' if none of path matches are defined.
const headers = this.match?.headers;
const queryParameters = this.match?.queryParameters;
validateHttpMatchArrayLength(headers, queryParameters);
const httpConfig: CfnRoute.HttpRouteProperty = {
action: {
weightedTargets: renderWeightedTargets(this.weightedTargets),
},
match: {
prefix: pathMatchConfig.prefixPathMatch,
path: pathMatchConfig.wholePathMatch,
headers: headers?.map(header => header.bind(scope).headerMatch),
method: this.match?.method,
scheme: this.match?.protocol,
queryParameters: queryParameters?.map(queryParameter => queryParameter.bind(scope).queryParameterMatch),
port: this.match?.port,
},
timeout: renderTimeout(this.timeout),
retryPolicy: this.retryPolicy ? renderHttpRetryPolicy(this.retryPolicy) : undefined,
};
return {
priority: this.priority,
httpRouteSpec: this.protocol === Protocol.HTTP ? httpConfig : undefined,
http2RouteSpec: this.protocol === Protocol.HTTP2 ? httpConfig : undefined,
};
}
}
class TcpRouteSpec extends RouteSpec {
/**
* The priority for the route.
*/
public readonly priority?: number;
/*
* List of targets that traffic is routed to when a request matches the route
*/
public readonly weightedTargets: WeightedTarget[];
/**
* The criteria for determining a timeout configuration
*/
public readonly timeout?: TcpTimeout;
constructor(props: TcpRouteSpecOptions) {
super();
this.weightedTargets = props.weightedTargets;
this.timeout = props.timeout;
this.priority = props.priority;
}
public bind(_scope: Construct): RouteSpecConfig {
return {
priority: this.priority,
tcpRouteSpec: {
action: {
weightedTargets: renderWeightedTargets(this.weightedTargets),
},
timeout: renderTimeout(this.timeout),
},
};
}
}
class GrpcRouteSpec extends RouteSpec {
/**
* The priority for the route.
*/
public readonly priority?: number;
public readonly weightedTargets: WeightedTarget[];
public readonly match: GrpcRouteMatch;
public readonly timeout?: GrpcTimeout;
/**
* The retry policy.
*/
public readonly retryPolicy?: GrpcRetryPolicy;
constructor(props: GrpcRouteSpecOptions) {
super();
this.weightedTargets = props.weightedTargets;
this.match = props.match;
this.timeout = props.timeout;
this.priority = props.priority;
if (props.retryPolicy) {
const grpcRetryEvents = props.retryPolicy.grpcRetryEvents ?? [];
const httpRetryEvents = props.retryPolicy.httpRetryEvents ?? [];
const tcpRetryEvents = props.retryPolicy.tcpRetryEvents ?? [];
if (grpcRetryEvents.length + httpRetryEvents.length + tcpRetryEvents.length === 0) {
throw new Error('You must specify one value for at least one of `grpcRetryEvents`, `httpRetryEvents` or `tcpRetryEvents`');
}
this.retryPolicy = {
...props.retryPolicy,
grpcRetryEvents: grpcRetryEvents.length > 0 ? grpcRetryEvents : undefined,
httpRetryEvents: httpRetryEvents.length > 0 ? httpRetryEvents : undefined,
tcpRetryEvents: tcpRetryEvents.length > 0 ? tcpRetryEvents : undefined,
};
}
}
public bind(scope: Construct): RouteSpecConfig {
const serviceName = this.match.serviceName;
const methodName = this.match.methodName;
const metadata = this.match.metadata;
const port = this.match.port;
validateGrpcRouteMatch(this.match);
validateGrpcMatchArrayLength(metadata);
if (methodName && !serviceName) {
throw new Error('If you specify a method name, you must also specify a service name');
}
return {
priority: this.priority,
grpcRouteSpec: {
action: {
weightedTargets: renderWeightedTargets(this.weightedTargets),
},
match: {
serviceName: serviceName,
methodName: methodName,
metadata: metadata?.map(singleMetadata => singleMetadata.bind(scope).headerMatch),
port: port,
},
timeout: renderTimeout(this.timeout),
retryPolicy: this.retryPolicy ? renderGrpcRetryPolicy(this.retryPolicy) : undefined,
},
};
}
}
/**
* Utility method to add weighted route targets to an existing route
*/
function renderWeightedTargets(weightedTargets: WeightedTarget[]): CfnRoute.WeightedTargetProperty[] {
const renderedTargets: CfnRoute.WeightedTargetProperty[] = [];
for (const t of weightedTargets) {
renderedTargets.push({
virtualNode: t.virtualNode.virtualNodeName,
weight: t.weight == undefined ? 1 : t.weight,
});
}
return renderedTargets;
}
/**
* Utility method to construct a route timeout object
*/
function renderTimeout(timeout?: HttpTimeout): CfnRoute.HttpTimeoutProperty | undefined {
return timeout
? {
idle: timeout?.idle !== undefined
? {
unit: 'ms',
value: timeout?.idle.toMilliseconds(),
}
: undefined,
perRequest: timeout?.perRequest !== undefined
? {
unit: 'ms',
value: timeout?.perRequest.toMilliseconds(),
}
: undefined,
}
: undefined;
}
function renderHttpRetryPolicy(retryPolicy: HttpRetryPolicy): CfnRoute.HttpRetryPolicyProperty {
return {
maxRetries: retryPolicy.retryAttempts,
perRetryTimeout: {
unit: 'ms',
value: retryPolicy.retryTimeout.toMilliseconds(),
},
httpRetryEvents: retryPolicy.httpRetryEvents,
tcpRetryEvents: retryPolicy.tcpRetryEvents,
};
}
function renderGrpcRetryPolicy(retryPolicy: GrpcRetryPolicy): CfnRoute.GrpcRetryPolicyProperty {
return {
...renderHttpRetryPolicy(retryPolicy),
grpcRetryEvents: retryPolicy.grpcRetryEvents,
};
}