-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ip-addresses.ts
312 lines (268 loc) · 7.42 KB
/
ip-addresses.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
import { Fn, Token } from '../../core';
import { calculateCidrSplits } from './cidr-splits';
import { NetworkBuilder } from './network-util';
import { SubnetConfiguration } from './vpc';
/**
* An abstract Provider of IpAddresses
*/
export class IpAddresses {
/**
* Used to provide local Ip Address Management services for your VPC
*
* VPC Cidr is supplied at creation and subnets are calculated locally
*
*/
public static cidr(cidrBlock: string): IIpAddresses {
return new Cidr(cidrBlock);
}
/**
* Used to provide centralized Ip Address Management services for your VPC
*
* Uses VPC Cidr allocations from AWS IPAM
*
* @see https://docs.aws.amazon.com/vpc/latest/ipam/what-it-is-ipam.html
*/
public static awsIpamAllocation(props: AwsIpamProps): IIpAddresses {
return new AwsIpam(props);
}
private constructor() { }
}
/**
* Implementations for ip address management
*/
export interface IIpAddresses {
/**
* Called by the VPC to retrieve VPC options from the Ipam
*
* Don't call this directly, the VPC will call it automatically.
*/
allocateVpcCidr(): VpcIpamOptions;
/**
* Called by the VPC to retrieve Subnet options from the Ipam
*
* Don't call this directly, the VPC will call it automatically.
*/
allocateSubnetsCidr(input: AllocateCidrRequest): SubnetIpamOptions;
}
/**
* Cidr Allocated Vpc
*/
export interface VpcIpamOptions {
/**
* Cidr Block for Vpc
*
* @default - Only required when Ipam has concrete allocation available for static Vpc
*/
readonly cidrBlock?: string;
/**
* Cidr Mask for Vpc
*
* @default - Only required when using AWS Ipam
*/
readonly ipv4NetmaskLength?: number;
/**
* ipv4 IPAM Pool Id
*
* @default - Only required when using AWS Ipam
*/
readonly ipv4IpamPoolId?: string;
}
/**
* Subnet requested for allocation
*/
export interface RequestedSubnet {
/**
* The availability zone for the subnet
*/
readonly availabilityZone: string;
/**
* Specify configuration parameters for a single subnet group in a VPC
*/
readonly configuration: SubnetConfiguration;
/**
* Id for the Subnet construct
*/
readonly subnetConstructId: string;
}
/**
* An instance of a Subnet requested for allocation
*/
interface IRequestedSubnetInstance {
/**
* Index location of Subnet requested for allocation
*/
readonly index: number,
/**
* Subnet requested for allocation
*/
readonly requestedSubnet: RequestedSubnet
}
/**
* Request for subnets Cidr to be allocated for a Vpc
*/
export interface AllocateCidrRequest {
/**
* The IPv4 CIDR block for this Vpc
*/
readonly vpcCidr: string;
/**
* The Subnets to be allocated
*/
readonly requestedSubnets: RequestedSubnet[];
}
/**
* Cidr Allocated Subnets
*/
export interface SubnetIpamOptions {
/**
* Cidr Allocations for Subnets
*/
readonly allocatedSubnets: AllocatedSubnet[];
}
/**
* Cidr Allocated Subnet
*/
export interface AllocatedSubnet {
/**
* Cidr Allocations for a Subnet
*/
readonly cidr: string;
}
/**
* Configuration for AwsIpam
*/
export interface AwsIpamProps {
/**
* Netmask length for Vpc
*/
readonly ipv4NetmaskLength: number;
/**
* Ipam Pool Id for ipv4 allocation
*/
readonly ipv4IpamPoolId: string; // todo: should be a type
/**
* Default length for Subnet ipv4 Network mask
*
* Specify this option only if you do not specify all Subnets using SubnetConfiguration with a cidrMask
*
* @default - Default ipv4 Subnet Mask for subnets in Vpc
*
*/
readonly defaultSubnetIpv4NetmaskLength?: number;
}
/**
* Implements integration with Amazon VPC IP Address Manager (IPAM).
*
* See the package-level documentation of this package for an overview
* of the various dimensions in which you can configure your VPC.
*
* For example:
*
* ```ts
* new ec2.Vpc(stack, 'TheVPC', {
* ipAddresses: IpAddresses.awsIpamAllocation({
* ipv4IpamPoolId: pool.ref,
* ipv4NetmaskLength: 18,
* defaultSubnetIpv4NetmaskLength: 24
* })
* });
* ```
*
*/
class AwsIpam implements IIpAddresses {
constructor(private readonly props: AwsIpamProps) {
}
/**
* Allocates Vpc Cidr. called when creating a Vpc using AwsIpam.
*/
allocateVpcCidr(): VpcIpamOptions {
return {
ipv4NetmaskLength: this.props.ipv4NetmaskLength,
ipv4IpamPoolId: this.props.ipv4IpamPoolId,
};
}
/**
* Allocates Subnets Cidrs. Called by VPC when creating subnets.
*/
allocateSubnetsCidr(input: AllocateCidrRequest): SubnetIpamOptions {
const cidrSplit = calculateCidrSplits(this.props.ipv4NetmaskLength, input.requestedSubnets.map((mask => {
if ((mask.configuration.cidrMask === undefined) && (this.props.defaultSubnetIpv4NetmaskLength=== undefined) ) {
throw new Error('If you have not set a cidr for all subnets in this case you must set a defaultCidrMask in AwsIpam Options');
}
const cidrMask = mask.configuration.cidrMask ?? this.props.defaultSubnetIpv4NetmaskLength;
if (cidrMask === undefined) {
throw new Error('Should not have happened, but satisfies the type checker');
}
return cidrMask;
})));
const allocatedSubnets: AllocatedSubnet[] = cidrSplit.map(subnet => {
return {
cidr: Fn.select(subnet.index, Fn.cidr(input.vpcCidr, subnet.count, `${32-subnet.netmask}`)),
};
});
return {
allocatedSubnets: allocatedSubnets,
};
}
}
/**
* Implements static Ip assignment locally.
*
* See the package-level documentation of this package for an overview
* of the various dimensions in which you can configure your VPC.
*
* For example:
*
* ```ts
* new ec2.Vpc(stack, 'TheVPC', {
* ipAddresses: ec2.IpAddresses.cidr('10.0.1.0/20')
* });
* ```
*
*/
class Cidr implements IIpAddresses {
private readonly networkBuilder: NetworkBuilder;
constructor(private readonly cidrBlock: string) {
if (Token.isUnresolved(cidrBlock)) {
throw new Error('\'cidr\' property must be a concrete CIDR string, got a Token (we need to parse it for automatic subdivision)');
}
this.networkBuilder = new NetworkBuilder(this.cidrBlock);
}
/**
* Allocates Vpc Cidr. called when creating a Vpc using IpAddresses.cidr.
*/
allocateVpcCidr(): VpcIpamOptions {
return {
cidrBlock: this.networkBuilder.networkCidr.cidr,
};
}
/**
* Allocates Subnets Cidrs. Called by VPC when creating subnets.
*/
allocateSubnetsCidr(input: AllocateCidrRequest): SubnetIpamOptions {
const allocatedSubnets: AllocatedSubnet[] = [];
const subnetsWithoutDefinedCidr: IRequestedSubnetInstance[] = [];
//default: Available IP space is evenly divided across subnets if no cidr is given.
input.requestedSubnets.forEach((requestedSubnet, index) => {
if (requestedSubnet.configuration.cidrMask === undefined) {
subnetsWithoutDefinedCidr.push({
index,
requestedSubnet,
});
} else {
allocatedSubnets.push({
cidr: this.networkBuilder.addSubnet(requestedSubnet.configuration.cidrMask),
});
}
});
const cidrMaskForRemaining = this.networkBuilder.maskForRemainingSubnets(subnetsWithoutDefinedCidr.length);
subnetsWithoutDefinedCidr.forEach((subnet)=> {
allocatedSubnets.splice(subnet.index, 0, {
cidr: this.networkBuilder.addSubnet(cidrMaskForRemaining),
});
});
return {
allocatedSubnets: allocatedSubnets,
};
}
}