@@ -183,7 +183,7 @@ func TestReconcileNatGateways(t *testing.T) {
183183 },
184184 },
185185 {
186- name : "two public & 1 private subnet, and one NAT gateway exists" ,
186+ name : "two public & 1 private subnet, and one NAT gateway exists, should not create additional NAT gateway " ,
187187 input : []infrav1.SubnetSpec {
188188 {
189189 ID : "subnet-1" ,
@@ -224,10 +224,74 @@ func TestReconcileNatGateways(t *testing.T) {
224224 {
225225 NatGatewayId : aws .String ("gateway" ),
226226 SubnetId : aws .String ("subnet-1" ),
227+ Tags : []types.Tag {
228+ {
229+ Key : aws .String ("sigs.k8s.io/cluster-api-provider-aws/role" ),
230+ Value : aws .String ("common" ),
231+ },
232+ {
233+ Key : aws .String ("Name" ),
234+ Value : aws .String ("test-cluster-nat" ),
235+ },
236+ {
237+ Key : aws .String ("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster" ),
238+ Value : aws .String ("owned" ),
239+ },
240+ },
227241 },
228242 },
229243 }, nil )
230244
245+ // Should not create any new NAT gateways because subnet-3 (public subnet in us-east-1b) has no private subnets
246+ m .DescribeAddresses (context .TODO (), gomock .Any ()).Times (0 )
247+ m .AllocateAddress (context .TODO (), gomock .Any ()).Times (0 )
248+ m .CreateNatGateway (context .TODO (), gomock .Any ()).Times (0 )
249+ },
250+ },
251+ {
252+ name : "multiple AZs with private subnets, should create one NAT gateway per AZ" ,
253+ input : []infrav1.SubnetSpec {
254+ {
255+ ID : "subnet-1" ,
256+ AvailabilityZone : "us-east-1a" ,
257+ CidrBlock : "10.0.10.0/24" ,
258+ IsPublic : true ,
259+ },
260+ {
261+ ID : "subnet-2" ,
262+ AvailabilityZone : "us-east-1a" ,
263+ CidrBlock : "10.0.12.0/24" ,
264+ IsPublic : false ,
265+ },
266+ {
267+ ID : "subnet-3" ,
268+ AvailabilityZone : "us-east-1b" ,
269+ CidrBlock : "10.0.13.0/24" ,
270+ IsPublic : true ,
271+ },
272+ {
273+ ID : "subnet-4" ,
274+ AvailabilityZone : "us-east-1b" ,
275+ CidrBlock : "10.0.14.0/24" ,
276+ IsPublic : false ,
277+ },
278+ },
279+ expect : func (m * mocks.MockEC2APIMockRecorder ) {
280+ m .DescribeNatGateways (context .TODO (),
281+ gomock .Eq (& ec2.DescribeNatGatewaysInput {
282+ Filter : []types.Filter {
283+ {
284+ Name : aws .String ("vpc-id" ),
285+ Values : []string {subnetsVPCID },
286+ },
287+ {
288+ Name : aws .String ("state" ),
289+ Values : []string {"pending" , "available" },
290+ },
291+ },
292+ }),
293+ gomock .Any ()).Return (& ec2.DescribeNatGatewaysOutput {}, nil )
294+
231295 m .DescribeAddresses (context .TODO (), gomock .Any ()).
232296 Return (& ec2.DescribeAddressesOutput {}, nil )
233297
@@ -254,51 +318,46 @@ func TestReconcileNatGateways(t *testing.T) {
254318 },
255319 }).Return (& ec2.AllocateAddressOutput {
256320 AllocationId : aws .String (ElasticIPAllocationID ),
257- }, nil )
321+ }, nil ). Times ( 2 )
258322
259- m .CreateNatGateway (context .TODO (), & ec2.CreateNatGatewayInput {
260- AllocationId : aws .String (ElasticIPAllocationID ),
261- SubnetId : aws .String ("subnet-3" ),
262- TagSpecifications : []types.TagSpecification {
263- {
264- ResourceType : types .ResourceTypeNatgateway ,
265- Tags : []types.Tag {
266- {
267- Key : aws .String ("Name" ),
268- Value : aws .String ("test-cluster-nat" ),
269- },
270- {
271- Key : aws .String ("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster" ),
272- Value : aws .String ("owned" ),
273- },
274- {
275- Key : aws .String ("sigs.k8s.io/cluster-api-provider-aws/role" ),
276- Value : aws .String ("common" ),
277- },
278- },
323+ // Should create NAT gateways for both AZs since both have private subnets
324+ m .CreateNatGateway (context .TODO (), gomock .Any ()).
325+ Return (& ec2.CreateNatGatewayOutput {
326+ NatGateway : & types.NatGateway {
327+ NatGatewayId : aws .String ("natgateway-1" ),
328+ SubnetId : aws .String ("subnet-1" ),
279329 },
280- },
281- }).Return (& ec2.CreateNatGatewayOutput {
282- NatGateway : & types.NatGateway {
283- NatGatewayId : aws .String ("natgateway" ),
284- SubnetId : aws .String ("subnet-3" ),
285- },
286- }, nil )
330+ }, nil )
287331
288- m .DescribeNatGateways (gomock .Any (), & ec2.DescribeNatGatewaysInput {
289- NatGatewayIds : []string {"natgateway" },
290- }, gomock .Any ()).Return (& ec2.DescribeNatGatewaysOutput {
291- NatGateways : []types.NatGateway {
292- {
293- State : types .NatGatewayStateAvailable ,
294- NatGatewayId : aws .String ("natgateway" ),
332+ m .CreateNatGateway (context .TODO (), gomock .Any ()).
333+ Return (& ec2.CreateNatGatewayOutput {
334+ NatGateway : & types.NatGateway {
335+ NatGatewayId : aws .String ("natgateway-2" ),
295336 SubnetId : aws .String ("subnet-3" ),
296337 },
297- },
298- }, nil )
338+ }, nil )
339+
340+ m .DescribeNatGateways (gomock .Any (), gomock .Any (), gomock .Any ()).
341+ Return (& ec2.DescribeNatGatewaysOutput {
342+ NatGateways : []types.NatGateway {
343+ {
344+ State : types .NatGatewayStateAvailable ,
345+ NatGatewayId : aws .String ("natgateway-1" ),
346+ SubnetId : aws .String ("subnet-1" ),
347+ },
348+ },
349+ }, nil )
299350
300- m .CreateTags (context .TODO (), gomock .AssignableToTypeOf (& ec2.CreateTagsInput {})).
301- Return (nil , nil ).Times (1 )
351+ m .DescribeNatGateways (gomock .Any (), gomock .Any (), gomock .Any ()).
352+ Return (& ec2.DescribeNatGatewaysOutput {
353+ NatGateways : []types.NatGateway {
354+ {
355+ State : types .NatGatewayStateAvailable ,
356+ NatGatewayId : aws .String ("natgateway-2" ),
357+ SubnetId : aws .String ("subnet-3" ),
358+ },
359+ },
360+ }, nil )
302361 },
303362 },
304363 {
0 commit comments