/
virtual-gateway-listener.ts
202 lines (177 loc) · 6.4 KB
/
virtual-gateway-listener.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
import * as cdk from '@aws-cdk/core';
import { CfnVirtualGateway } from './appmesh.generated';
import { validateHealthChecks, ConnectionPoolConfig } from './private/utils';
import {
GrpcConnectionPool,
HealthCheck,
Http2ConnectionPool,
HttpConnectionPool,
Protocol,
} from './shared-interfaces';
import { TlsCertificate, TlsCertificateConfig } from './tls-certificate';
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
import { Construct } from '@aws-cdk/core';
/**
* Represents the properties needed to define a Listeners for a VirtualGateway
*/
interface VirtualGatewayListenerCommonOptions {
/**
* Port to listen for connections on
*
* @default - 8080
*/
readonly port?: number
/**
* The health check information for the listener
*
* @default - no healthcheck
*/
readonly healthCheck?: HealthCheck;
/**
* Represents the configuration for enabling TLS on a listener
*
* @default - none
*/
readonly tlsCertificate?: TlsCertificate;
}
/**
* Represents the properties needed to define HTTP Listeners for a VirtualGateway
*/
export interface HttpGatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* Connection pool for http listeners
*
* @default - None
*/
readonly connectionPool?: HttpConnectionPool;
}
/**
* Represents the properties needed to define HTTP2 Listeners for a VirtualGateway
*/
export interface Http2GatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* Connection pool for http listeners
*
* @default - None
*/
readonly connectionPool?: Http2ConnectionPool;
}
/**
* Represents the properties needed to define GRPC Listeners for a VirtualGateway
*/
export interface GrpcGatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* Connection pool for http listeners
*
* @default - None
*/
readonly connectionPool?: GrpcConnectionPool;
}
/**
* Properties for a VirtualGateway listener
*/
export interface VirtualGatewayListenerConfig {
/**
* Single listener config for a VirtualGateway
*/
readonly listener: CfnVirtualGateway.VirtualGatewayListenerProperty;
}
/**
* Represents the properties needed to define listeners for a VirtualGateway
*/
export abstract class VirtualGatewayListener {
/**
* Returns an HTTP Listener for a VirtualGateway
*/
public static http(options: HttpGatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.HTTP, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}
/**
* Returns an HTTP2 Listener for a VirtualGateway
*/
public static http2(options: Http2GatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.HTTP2, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}
/**
* Returns a GRPC Listener for a VirtualGateway
*/
public static grpc(options: GrpcGatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.GRPC, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}
/**
* Called when the GatewayListener type is initialized. Can be used to enforce
* mutual exclusivity
*/
public abstract bind(scope: Construct): VirtualGatewayListenerConfig;
}
/**
* Represents the properties needed to define an HTTP Listener for a VirtualGateway
*/
class VirtualGatewayListenerImpl extends VirtualGatewayListener {
constructor(private readonly protocol: Protocol,
private readonly healthCheck: HealthCheck | undefined,
private readonly port: number = 8080,
private readonly tlsCertificate: TlsCertificate | undefined,
private readonly connectionPool: ConnectionPoolConfig | undefined) {
super();
}
/**
* Called when the GatewayListener type is initialized. Can be used to enforce
* mutual exclusivity
*/
public bind(scope: Construct): VirtualGatewayListenerConfig {
const tlsConfig = this.tlsCertificate?.bind(scope);
return {
listener: {
portMapping: {
port: this.port,
protocol: this.protocol,
},
healthCheck: this.healthCheck ? renderHealthCheck(this.healthCheck, this.protocol, this.port): undefined,
tls: tlsConfig ? renderTls(tlsConfig) : undefined,
connectionPool: this.connectionPool ? renderConnectionPool(this.connectionPool, this.protocol) : undefined,
},
};
}
}
/**
* Renders the TLS config for a listener
*/
function renderTls(tlsCertificateConfig: TlsCertificateConfig): CfnVirtualGateway.VirtualGatewayListenerTlsProperty {
return {
certificate: tlsCertificateConfig.tlsCertificate,
mode: tlsCertificateConfig.tlsMode.toString(),
};
}
function renderHealthCheck(hc: HealthCheck, listenerProtocol: Protocol,
listenerPort: number): CfnVirtualGateway.VirtualGatewayHealthCheckPolicyProperty {
if (hc.protocol === Protocol.TCP) {
throw new Error('TCP health checks are not permitted for gateway listeners');
}
if (hc.protocol === Protocol.GRPC && hc.path) {
throw new Error('The path property cannot be set with Protocol.GRPC');
}
const protocol = hc.protocol? hc.protocol : listenerProtocol;
const healthCheck: CfnVirtualGateway.VirtualGatewayHealthCheckPolicyProperty = {
healthyThreshold: hc.healthyThreshold || 2,
intervalMillis: (hc.interval || cdk.Duration.seconds(5)).toMilliseconds(), // min
path: hc.path || ((protocol === Protocol.HTTP || protocol === Protocol.HTTP2) ? '/' : undefined),
port: hc.port || listenerPort,
protocol: hc.protocol || listenerProtocol,
timeoutMillis: (hc.timeout || cdk.Duration.seconds(2)).toMilliseconds(),
unhealthyThreshold: hc.unhealthyThreshold || 2,
};
validateHealthChecks(healthCheck);
return healthCheck;
}
function renderConnectionPool(connectionPool: ConnectionPoolConfig, listenerProtocol: Protocol):
CfnVirtualGateway.VirtualGatewayConnectionPoolProperty {
return ({
[listenerProtocol]: {
maxRequests: connectionPool?.maxRequests !== undefined ? connectionPool.maxRequests : undefined,
maxConnections: connectionPool?.maxConnections !== undefined ? connectionPool.maxConnections : undefined,
maxPendingRequests: connectionPool?.maxPendingRequests !== undefined ? connectionPool.maxPendingRequests : undefined,
},
});
}