@@ -30,13 +30,6 @@ var _standardInstanceFamilies = strset.New("a", "c", "d", "h", "i", "m", "r", "t
30
30
var _knownInstanceFamilies = strset .Union (_standardInstanceFamilies , strset .New ("p" , "g" , "inf" , "x" , "f" , "mac" ))
31
31
32
32
const (
33
- _elasticIPsQuotaCode = "L-0263D0A3"
34
- _internetGatewayQuotaCode = "L-A4707A72"
35
- _natGatewayQuotaCode = "L-FE5A380F"
36
- _vpcQuotaCode = "L-F678F1CE"
37
- _securityGroupsQuotaCode = "L-E79EC296"
38
- _securityGroupRulesQuotaCode = "L-0EA8095F"
39
-
40
33
// 11 inbound rules
41
34
_baseInboundRulesForNodeGroup = 11
42
35
_inboundRulesPerAZ = 8
@@ -159,181 +152,167 @@ func (c *Client) VerifyInstanceQuota(instances []InstanceTypeRequests) error {
159
152
return nil
160
153
}
161
154
162
- func (c * Client ) VerifyNetworkQuotas (
163
- requiredInternetGateways int ,
164
- natGatewayRequired bool ,
165
- highlyAvailableNATGateway bool ,
166
- requiredVPCs int ,
167
- availabilityZones strset.Set ,
168
- numNodeGroups int ,
169
- longestCIDRWhiteList int ) error {
170
- quotaCodeToValueMap := map [string ]int {
171
- _elasticIPsQuotaCode : 0 , // elastic IP quota code
172
- _internetGatewayQuotaCode : 0 , // internet gw quota code
173
- _natGatewayQuotaCode : 0 , // nat gw quota code
174
- _vpcQuotaCode : 0 , // vpc quota code
175
- _securityGroupsQuotaCode : 0 , // security groups quota code
176
- _securityGroupRulesQuotaCode : 0 , // security group rules quota code
177
- }
178
-
179
- err := c .ServiceQuotas ().ListServiceQuotasPages (
180
- & servicequotas.ListServiceQuotasInput {
181
- ServiceCode : aws .String ("ec2" ),
182
- },
183
- func (page * servicequotas.ListServiceQuotasOutput , lastPage bool ) bool {
184
- if page == nil {
185
- return false
186
- }
187
- for _ , quota := range page .Quotas {
188
- if quota == nil || quota .QuotaCode == nil || quota .Value == nil {
189
- continue
190
- }
191
- if _ , ok := quotaCodeToValueMap [* quota .QuotaCode ]; ok {
192
- quotaCodeToValueMap [* quota .QuotaCode ] = int (* quota .Value )
155
+ func (c * Client ) ListServiceQuotas (quotaCodes []string , serviceCodes []string ) (map [string ]int , error ) {
156
+ desiredQuotaCodes := strset .New (quotaCodes ... )
157
+ quotaCodeToValueMap := map [string ]int {}
158
+
159
+ for _ , serviceCode := range serviceCodes {
160
+ err := c .ServiceQuotas ().ListServiceQuotasPages (
161
+ & servicequotas.ListServiceQuotasInput {
162
+ ServiceCode : aws .String (serviceCode ),
163
+ },
164
+ func (page * servicequotas.ListServiceQuotasOutput , lastPage bool ) bool {
165
+ if page == nil {
193
166
return false
194
167
}
195
- }
196
- return true
197
- },
198
- )
199
- if err != nil {
200
- return errors .WithStack (err )
168
+ for _ , quota := range page .Quotas {
169
+ if quota == nil || quota .QuotaCode == nil || quota .Value == nil {
170
+ continue
171
+ }
172
+ if desiredQuotaCodes .Has (* quota .QuotaCode ) {
173
+ quotaCodeToValueMap [* quota .QuotaCode ] = int (* quota .Value )
174
+ }
175
+ }
176
+ return true
177
+ },
178
+ )
179
+ if err != nil {
180
+ return nil , errors .Wrap (err , serviceCode )
181
+ }
201
182
}
202
183
203
- err = c .ServiceQuotas ().ListServiceQuotasPages (
204
- & servicequotas.ListServiceQuotasInput {
205
- ServiceCode : aws .String ("vpc" ),
206
- },
207
- func (page * servicequotas.ListServiceQuotasOutput , lastPage bool ) bool {
208
- if page == nil {
209
- return false
210
- }
211
- for _ , quota := range page .Quotas {
212
- if quota == nil || quota .QuotaCode == nil || quota .Value == nil {
213
- continue
214
- }
215
- if _ , ok := quotaCodeToValueMap [* quota .QuotaCode ]; ok {
216
- quotaCodeToValueMap [* quota .QuotaCode ] = int (* quota .Value )
217
- }
218
- }
219
- return true
220
- },
221
- )
184
+ return quotaCodeToValueMap , nil
185
+ }
186
+
187
+ func (c * Client ) VerifyInternetGatewayQuota (internetGatewayQuota int , requiredInternetGateways int ) error {
188
+ internetGatewaysInUse , err := c .ListInternetGateways ()
222
189
if err != nil {
223
- return errors . WithStack ( err )
190
+ return err
224
191
}
225
192
226
- // check internet GW quota
227
- if requiredInternetGateways > 0 {
228
- internetGatewaysInUse , err := c .ListInternetGateways ()
229
- if err != nil {
230
- return err
231
- }
232
- if quotaCodeToValueMap [_internetGatewayQuotaCode ]- len (internetGatewaysInUse )- requiredInternetGateways < 0 {
233
- additionalQuotaRequired := len (internetGatewaysInUse ) + requiredInternetGateways - quotaCodeToValueMap [_internetGatewayQuotaCode ]
234
- return ErrorInternetGatewayLimitExceeded (quotaCodeToValueMap [_internetGatewayQuotaCode ], additionalQuotaRequired , c .Region )
235
- }
193
+ additionalQuotaRequired := len (internetGatewaysInUse ) + requiredInternetGateways - internetGatewayQuota
194
+
195
+ if additionalQuotaRequired > 0 {
196
+ return ErrorInternetGatewayLimitExceeded (internetGatewayQuota , additionalQuotaRequired , c .Region )
236
197
}
198
+ return nil
199
+ }
237
200
238
- if natGatewayRequired {
239
- // get NAT GW in use per selected AZ
240
- natGateways , err := c .DescribeNATGateways ()
241
- if err != nil {
242
- return err
243
- }
244
- subnets , err := c .DescribeSubnets ()
245
- if err != nil {
246
- return err
201
+ func (c * Client ) VerifyNATGatewayQuota (natGatewayQuota int , availabilityZones strset.Set , highlyAvailableNATGateway bool ) error {
202
+ // get NAT GW in use per selected AZ
203
+ natGateways , err := c .DescribeNATGateways ()
204
+ if err != nil {
205
+ return err
206
+ }
207
+ subnets , err := c .DescribeSubnets ()
208
+ if err != nil {
209
+ return err
210
+ }
211
+ azToGatewaysInUse := map [string ]int {}
212
+ for _ , natGateway := range natGateways {
213
+ if natGateway .SubnetId == nil {
214
+ continue
247
215
}
248
- azToGatewaysInUse := map [string ]int {}
249
- for _ , natGateway := range natGateways {
250
- if natGateway .SubnetId == nil {
216
+ for _ , subnet := range subnets {
217
+ if subnet .SubnetId == nil || subnet .AvailabilityZone == nil {
251
218
continue
252
219
}
253
- for _ , subnet := range subnets {
254
- if subnet .SubnetId == nil || subnet .AvailabilityZone == nil {
255
- continue
256
- }
257
- if ! availabilityZones .Has (* subnet .AvailabilityZone ) {
258
- continue
259
- }
260
- if * subnet .SubnetId == * natGateway .SubnetId {
261
- azToGatewaysInUse [* subnet .AvailabilityZone ]++
262
- }
220
+ if ! availabilityZones .Has (* subnet .AvailabilityZone ) {
221
+ continue
263
222
}
264
- }
265
- // check NAT GW quota
266
- numOfExhaustedNATGatewayAZs := 0
267
- azsWithQuotaDeficit := []string {}
268
- for az , numActiveGatewaysOnAZ := range azToGatewaysInUse {
269
- // -1 comes from the NAT gateway we require per AZ
270
- azDeficit := quotaCodeToValueMap [_natGatewayQuotaCode ] - numActiveGatewaysOnAZ - 1
271
- if azDeficit < 0 {
272
- numOfExhaustedNATGatewayAZs ++
273
- azsWithQuotaDeficit = append (azsWithQuotaDeficit , az )
223
+ if * subnet .SubnetId == * natGateway .SubnetId {
224
+ azToGatewaysInUse [* subnet .AvailabilityZone ]++
274
225
}
275
226
}
276
- if (highlyAvailableNATGateway && numOfExhaustedNATGatewayAZs > 0 ) || (! highlyAvailableNATGateway && numOfExhaustedNATGatewayAZs == len (availabilityZones )) {
277
- return ErrorNATGatewayLimitExceeded (quotaCodeToValueMap [_natGatewayQuotaCode ], 1 , azsWithQuotaDeficit , c .Region )
227
+ }
228
+ // check NAT GW quota
229
+ numOfExhaustedNATGatewayAZs := 0
230
+ azsWithQuotaDeficit := []string {}
231
+ for az , numActiveGatewaysOnAZ := range azToGatewaysInUse {
232
+ // -1 comes from the NAT gateway we require per AZ
233
+ azDeficit := natGatewayQuota - numActiveGatewaysOnAZ - 1
234
+ if azDeficit < 0 {
235
+ numOfExhaustedNATGatewayAZs ++
236
+ azsWithQuotaDeficit = append (azsWithQuotaDeficit , az )
278
237
}
279
238
}
239
+ if (highlyAvailableNATGateway && numOfExhaustedNATGatewayAZs > 0 ) || (! highlyAvailableNATGateway && numOfExhaustedNATGatewayAZs == len (availabilityZones )) {
240
+ return ErrorNATGatewayLimitExceeded (natGatewayQuota , 1 , azsWithQuotaDeficit , c .Region )
241
+ }
280
242
281
- // check EIP quota
282
- if natGatewayRequired {
283
- elasticIPsInUse , err := c .ListElasticIPs ()
284
- if err != nil {
285
- return err
286
- }
287
- var requiredElasticIPs int
288
- if highlyAvailableNATGateway {
289
- requiredElasticIPs = len (availabilityZones )
290
- } else {
291
- requiredElasticIPs = 1
292
- }
293
- if quotaCodeToValueMap [_elasticIPsQuotaCode ]- len (elasticIPsInUse )- requiredElasticIPs < 0 {
294
- additionalQuotaRequired := len (elasticIPsInUse ) + requiredElasticIPs - quotaCodeToValueMap [_elasticIPsQuotaCode ]
295
- return ErrorEIPLimitExceeded (quotaCodeToValueMap [_elasticIPsQuotaCode ], additionalQuotaRequired , c .Region )
296
- }
243
+ return nil
244
+ }
245
+
246
+ func (c * Client ) VerifyEIPQuota (eipQuota int , availabilityZones strset.Set , highlyAvailableNATGateway bool ) error {
247
+ elasticIPsInUse , err := c .ListElasticIPs ()
248
+ if err != nil {
249
+ return err
250
+ }
251
+ var requiredElasticIPs int
252
+ if highlyAvailableNATGateway {
253
+ requiredElasticIPs = len (availabilityZones )
254
+ } else {
255
+ requiredElasticIPs = 1
297
256
}
298
257
299
- // check VPC quota
300
- if requiredVPCs > 0 {
301
- vpcs , err := c .DescribeVpcs ()
302
- if err != nil {
303
- return err
304
- }
305
- if quotaCodeToValueMap [_vpcQuotaCode ]- len (vpcs )- requiredVPCs < 0 {
306
- additionalQuotaRequired := len (vpcs ) + requiredVPCs - quotaCodeToValueMap [_vpcQuotaCode ]
307
- return ErrorVPCLimitExceeded (quotaCodeToValueMap [_vpcQuotaCode ], additionalQuotaRequired , c .Region )
308
- }
258
+ additionalQuotaRequired := len (elasticIPsInUse ) + requiredElasticIPs - eipQuota
259
+
260
+ if additionalQuotaRequired > 0 {
261
+ return ErrorEIPLimitExceeded (eipQuota , additionalQuotaRequired , c .Region )
309
262
}
310
263
311
- // check rules quota for nodegroup SGs
312
- requiredRulesForSG := requiredRulesForNodeGroupSecurityGroup (len (availabilityZones ), longestCIDRWhiteList )
313
- if requiredRulesForSG > quotaCodeToValueMap [_securityGroupRulesQuotaCode ] {
314
- additionalQuotaRequired := requiredRulesForSG - quotaCodeToValueMap [_securityGroupRulesQuotaCode ]
315
- return ErrorSecurityGroupRulesExceeded (quotaCodeToValueMap [_securityGroupRulesQuotaCode ], additionalQuotaRequired , c .Region )
264
+ return nil
265
+ }
266
+
267
+ func (c * Client ) VerifyVPCQuota (vpcQuota int , requiredVPCs int ) error {
268
+ vpcs , err := c .DescribeVpcs ()
269
+ if err != nil {
270
+ return err
316
271
}
317
272
318
- // check rules quota for control plane SG
319
- requiredRulesForCPSG := requiredRulesForControlPlaneSecurityGroup (numNodeGroups )
320
- if requiredRulesForCPSG > quotaCodeToValueMap [_securityGroupRulesQuotaCode ] {
321
- additionalQuotaRequired := requiredRulesForCPSG - quotaCodeToValueMap [_securityGroupRulesQuotaCode ]
322
- return ErrorSecurityGroupRulesExceeded (quotaCodeToValueMap [_securityGroupRulesQuotaCode ], additionalQuotaRequired , c .Region )
273
+ additionalQuotaRequired := len (vpcs ) + requiredVPCs - vpcQuota
274
+
275
+ if additionalQuotaRequired > 0 {
276
+ return ErrorVPCLimitExceeded (vpcQuota , additionalQuotaRequired , c .Region )
323
277
}
278
+ return nil
279
+ }
324
280
325
- // check security groups quota
281
+ func ( c * Client ) VerifySecurityGroupQuota ( securifyGroupsQuota int , numNodeGroups int ) error {
326
282
requiredSecurityGroups := requiredSecurityGroups (numNodeGroups )
327
283
sgs , err := c .DescribeSecurityGroups ()
328
284
if err != nil {
329
285
return err
330
286
}
331
- if quotaCodeToValueMap [_securityGroupsQuotaCode ]- len (sgs )- requiredSecurityGroups < 0 {
332
- additionalQuotaRequired := len (sgs ) + requiredSecurityGroups - quotaCodeToValueMap [_securityGroupsQuotaCode ]
333
- return ErrorSecurityGroupLimitExceeded (quotaCodeToValueMap [_securityGroupsQuotaCode ], additionalQuotaRequired , c .Region )
334
287
288
+ additionalQuotaRequired := len (sgs ) + requiredSecurityGroups - securifyGroupsQuota
289
+
290
+ if additionalQuotaRequired > 0 {
291
+ return ErrorSecurityGroupLimitExceeded (securifyGroupsQuota , additionalQuotaRequired , c .Region )
292
+
293
+ }
294
+ return nil
295
+ }
296
+
297
+ func (c * Client ) VerifySecurityGroupRulesQuota (
298
+ securifyGroupRulesQuota int ,
299
+ availabilityZones strset.Set ,
300
+ numNodeGroups int ,
301
+ longestCIDRWhiteList int ) error {
302
+
303
+ // check rules quota for nodegroup SGs
304
+ requiredRulesForSG := requiredRulesForNodeGroupSecurityGroup (len (availabilityZones ), longestCIDRWhiteList )
305
+ if requiredRulesForSG > securifyGroupRulesQuota {
306
+ additionalQuotaRequired := requiredRulesForSG - securifyGroupRulesQuota
307
+ return ErrorSecurityGroupRulesExceeded (securifyGroupRulesQuota , additionalQuotaRequired , c .Region )
335
308
}
336
309
310
+ // check rules quota for control plane SG
311
+ requiredRulesForCPSG := requiredRulesForControlPlaneSecurityGroup (numNodeGroups )
312
+ if requiredRulesForCPSG > securifyGroupRulesQuota {
313
+ additionalQuotaRequired := requiredRulesForCPSG - securifyGroupRulesQuota
314
+ return ErrorSecurityGroupRulesExceeded (securifyGroupRulesQuota , additionalQuotaRequired , c .Region )
315
+ }
337
316
return nil
338
317
}
339
318
0 commit comments