diff --git a/pkg/cloud/aws/services/ec2/account.go b/pkg/cloud/aws/services/ec2/account.go index 87e6aa41e1..34fcdd943c 100644 --- a/pkg/cloud/aws/services/ec2/account.go +++ b/pkg/cloud/aws/services/ec2/account.go @@ -14,6 +14,8 @@ package ec2 import ( + "sort" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/pkg/errors" ) @@ -45,5 +47,6 @@ func (s *Service) getAvailableZones() ([]string, error) { zones = append(zones, *zone.ZoneName) } + sort.Strings(zones) return zones, nil } diff --git a/pkg/cloud/aws/services/ec2/filters.go b/pkg/cloud/aws/services/ec2/filters.go index ab2056f294..5b52a5c4a1 100644 --- a/pkg/cloud/aws/services/ec2/filters.go +++ b/pkg/cloud/aws/services/ec2/filters.go @@ -88,6 +88,13 @@ func (s *Service) filterAvailable() *ec2.Filter { } } +func (s *Service) filterNATGatewayStates(states ...string) *ec2.Filter { + return &ec2.Filter{ + Name: aws.String("state"), + Values: aws.StringSlice(states), + } +} + func (s *Service) filterInstanceStates(states ...string) *ec2.Filter { return &ec2.Filter{ Name: aws.String("instance-state-name"), diff --git a/pkg/cloud/aws/services/ec2/natgateways.go b/pkg/cloud/aws/services/ec2/natgateways.go index 21ff1d5c6f..bc8e6dae78 100644 --- a/pkg/cloud/aws/services/ec2/natgateways.go +++ b/pkg/cloud/aws/services/ec2/natgateways.go @@ -94,7 +94,7 @@ func (s *Service) describeNatGatewaysBySubnet(vpcID string) (map[string]*ec2.Nat describeNatGatewayInput := &ec2.DescribeNatGatewaysInput{ Filter: []*ec2.Filter{ s.filterVpc(vpcID), - s.filterAvailable(), + s.filterNATGatewayStates(ec2.NatGatewayStatePending, ec2.NatGatewayStateAvailable), }, } @@ -143,7 +143,6 @@ func (s *Service) createNatGateway(clusterName string, subnetID string) (*ec2.Na } glog.Infof("NAT gateway %q for subnet ID %q is now available", *out.NatGateway.NatGatewayId, subnetID) - return out.NatGateway, nil } @@ -211,5 +210,5 @@ func (s *Service) getNatGatewayForSubnet(subnets v1alpha1.Subnets, sn *v1alpha1. return gws[0], nil } - return "", errors.Errorf("no nat gateways are available in availability zone %q for subnet %q", sn.AvailabilityZone, sn.ID) + return "", errors.Errorf("no nat gateways available in %q for private subnet %q, current state: %+v", sn.AvailabilityZone, sn.ID, azGateways) } diff --git a/pkg/cloud/aws/services/ec2/natgateways_test.go b/pkg/cloud/aws/services/ec2/natgateways_test.go index 5eff37bdf1..71cba4ff13 100644 --- a/pkg/cloud/aws/services/ec2/natgateways_test.go +++ b/pkg/cloud/aws/services/ec2/natgateways_test.go @@ -100,7 +100,7 @@ func TestReconcileNatGateways(t *testing.T) { }, { Name: aws.String("state"), - Values: []*string{aws.String("available")}, + Values: []*string{aws.String("pending"), aws.String("available")}, }, }, }), @@ -177,7 +177,7 @@ func TestReconcileNatGateways(t *testing.T) { }, { Name: aws.String("state"), - Values: []*string{aws.String("available")}, + Values: []*string{aws.String("pending"), aws.String("available")}, }, }, }), @@ -253,7 +253,7 @@ func TestReconcileNatGateways(t *testing.T) { }, { Name: aws.String("state"), - Values: []*string{aws.String("available")}, + Values: []*string{aws.String("pending"), aws.String("available")}, }, }, }), diff --git a/pkg/cloud/aws/services/ec2/network.go b/pkg/cloud/aws/services/ec2/network.go index ed3e57b87c..021437576c 100644 --- a/pkg/cloud/aws/services/ec2/network.go +++ b/pkg/cloud/aws/services/ec2/network.go @@ -70,14 +70,17 @@ func (s *Service) DeleteNetwork(clusterName string, network *v1alpha1.Network) ( return err } + // NAT Gateways. if err := s.deleteNatGateways(clusterName, network.Subnets, &network.VPC); err != nil { return err } + // EIPs. if err := s.releaseAddresses(clusterName); err != nil { return err } + // Internet Gateways. if err := s.deleteInternetGateways(clusterName, network); err != nil { return err } diff --git a/pkg/cloud/aws/services/ec2/routetables_test.go b/pkg/cloud/aws/services/ec2/routetables_test.go index 1dcc176e94..dfdd507ac4 100644 --- a/pkg/cloud/aws/services/ec2/routetables_test.go +++ b/pkg/cloud/aws/services/ec2/routetables_test.go @@ -140,7 +140,7 @@ func TestReconcileRouteTables(t *testing.T) { DescribeRouteTables(gomock.AssignableToTypeOf(&ec2.DescribeRouteTablesInput{})). Return(&ec2.DescribeRouteTablesOutput{}, nil) }, - err: errors.New(`no nat gateways are available in availability zone "us-east-1a"`), + err: errors.New(`no nat gateways available in "us-east-1a"`), }, }