Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a VPC only if it's available or pending #255

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed if we are filtering on the VPC states we are checking for here?

Copy link
Member Author

@vincepri vincepri Oct 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't in that case, I just added it to be explicit in case we ever change the filters

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