Skip to content

Commit

Permalink
Return a VPC only if it's available or pending. (#255)
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri authored and k8s-ci-robot committed Oct 15, 2018
1 parent ca9682c commit 2a2fe06
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/cloud/aws/services/ec2/bastion.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *Service) ReconcileBastion(clusterName, keyName string, status *v1alpha1
if keyName == "" {
keyName = defaultSSHKeyName
}

spec := s.getDefaultBastion(clusterName, status.Region, status.Network, keyName)

// Describe bastion instance, if any.
Expand Down
7 changes: 7 additions & 0 deletions pkg/cloud/aws/services/ec2/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func (s *Service) filterInstanceStates(states ...string) *ec2.Filter {
}
}

func (s *Service) filterVPCStates(states ...string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("state"),
Values: aws.StringSlice(states),
}
}

// Add additional cluster tag filters, to match on our tags
func (s *Service) addFilterTags(clusterName string, filters []*ec2.Filter) []*ec2.Filter {
filters = append(filters, s.filterCluster(clusterName))
Expand Down
12 changes: 11 additions & 1 deletion pkg/cloud/aws/services/ec2/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (s *Service) deleteVPC(v *v1alpha1.VPC) error {
}

func (s *Service) describeVPC(clusterName string, id string) (*v1alpha1.VPC, error) {
input := &ec2.DescribeVpcsInput{}
input := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
s.filterVPCStates(ec2.VpcStatePending, ec2.VpcStateAvailable),
},
}

if id == "" {
// Try to find a previously created and tagged VPC
Expand All @@ -124,6 +128,12 @@ func (s *Service) describeVPC(clusterName string, id string) (*v1alpha1.VPC, err
return nil, NewConflict(errors.Errorf("found more than one vpc with supplied filters. Please clean up extra VPCs: %s", out.GoString()))
}

switch *out.Vpcs[0].State {
case ec2.VpcStateAvailable, ec2.VpcStatePending:
default:
return nil, NewNotFound(errors.Errorf("could not find available or pending vpc"))
}

return &v1alpha1.VPC{
ID: *out.Vpcs[0].VpcId,
CidrBlock: *out.Vpcs[0].CidrBlock,
Expand Down
14 changes: 14 additions & 0 deletions pkg/cloud/aws/services/ec2/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ func TestReconcileVPC(t *testing.T) {
VpcIds: []*string{
aws.String("vpc-exists"),
},
Filters: []*ec2.Filter{
{
Name: aws.String("state"),
Values: aws.StringSlice([]string{ec2.VpcStatePending, ec2.VpcStateAvailable}),
},
},
})).
Return(&ec2.DescribeVpcsOutput{
Vpcs: []*ec2.Vpc{
{
State: aws.String("available"),
VpcId: aws.String("vpc-exists"),
CidrBlock: aws.String("10.0.0.0/8"),
},
Expand All @@ -65,13 +72,20 @@ func TestReconcileVPC(t *testing.T) {
VpcIds: []*string{
aws.String("vpc-new"),
},
Filters: []*ec2.Filter{
{
Name: aws.String("state"),
Values: aws.StringSlice([]string{ec2.VpcStatePending, ec2.VpcStateAvailable}),
},
},
})).
Return(&ec2.DescribeVpcsOutput{}, nil)

m.EXPECT().
CreateVpc(gomock.AssignableToTypeOf(&ec2.CreateVpcInput{})).
Return(&ec2.CreateVpcOutput{
Vpc: &ec2.Vpc{
State: aws.String("available"),
VpcId: aws.String("vpc-new"),
CidrBlock: aws.String("10.1.0.0/16"),
},
Expand Down

0 comments on commit 2a2fe06

Please sign in to comment.