Commit f71533ce authored by Trevor Pounds's avatar Trevor Pounds

Remove unnecessary describe VPC call.

The external DescribeVPCs call is unnecessary since only the VPC ID is used and it is retrieved from the EC2 metadata service.
parent 326dd7c1
...@@ -91,8 +91,6 @@ type EC2 interface { ...@@ -91,8 +91,6 @@ type EC2 interface {
AuthorizeSecurityGroupIngress(*ec2.AuthorizeSecurityGroupIngressInput) (*ec2.AuthorizeSecurityGroupIngressOutput, error) AuthorizeSecurityGroupIngress(*ec2.AuthorizeSecurityGroupIngressInput) (*ec2.AuthorizeSecurityGroupIngressOutput, error)
RevokeSecurityGroupIngress(*ec2.RevokeSecurityGroupIngressInput) (*ec2.RevokeSecurityGroupIngressOutput, error) RevokeSecurityGroupIngress(*ec2.RevokeSecurityGroupIngressInput) (*ec2.RevokeSecurityGroupIngressOutput, error)
DescribeVPCs(*ec2.DescribeVpcsInput) ([]*ec2.Vpc, error)
DescribeSubnets(*ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) DescribeSubnets(*ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error)
CreateTags(*ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) CreateTags(*ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error)
...@@ -377,15 +375,6 @@ func (s *awsSdkEC2) DeleteVolume(volumeID string) (resp *ec2.DeleteVolumeOutput, ...@@ -377,15 +375,6 @@ func (s *awsSdkEC2) DeleteVolume(volumeID string) (resp *ec2.DeleteVolumeOutput,
return s.ec2.DeleteVolume(&request) return s.ec2.DeleteVolume(&request)
} }
func (s *awsSdkEC2) DescribeVPCs(request *ec2.DescribeVpcsInput) ([]*ec2.Vpc, error) {
// VPCs are not paged
response, err := s.ec2.DescribeVpcs(request)
if err != nil {
return nil, fmt.Errorf("error listing AWS VPCs: %v", err)
}
return response.Vpcs, nil
}
func (s *awsSdkEC2) DescribeSubnets(request *ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) { func (s *awsSdkEC2) DescribeSubnets(request *ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) {
// Subnets are not paged // Subnets are not paged
response, err := s.ec2.DescribeSubnets(request) response, err := s.ec2.DescribeSubnets(request)
...@@ -1256,35 +1245,6 @@ func (self *AWSCloud) findVPCID() (string, error) { ...@@ -1256,35 +1245,6 @@ func (self *AWSCloud) findVPCID() (string, error) {
return "", fmt.Errorf("Could not find VPC ID in instance metadata") return "", fmt.Errorf("Could not find VPC ID in instance metadata")
} }
// Find the VPC which self is attached to.
func (self *AWSCloud) findVPC() (*ec2.Vpc, error) {
request := &ec2.DescribeVpcsInput{}
// find by vpcID from metadata
vpcID, err := self.findVPCID()
if err != nil {
return nil, err
}
filters := []*ec2.Filter{newEc2Filter("vpc-id", vpcID)}
// Don't bother adding the filterTags as we know this VPC is valid for this instance from findVPCID above.
// This is important as sharing a single regional VPC with multiple per-AZ clusters is a common deployment.
request.Filters = filters
vpcs, err := self.ec2.DescribeVPCs(request)
if err != nil {
glog.Error("error listing VPCs", err)
return nil, err
}
if len(vpcs) == 0 {
return nil, nil
}
if len(vpcs) == 1 {
return vpcs[0], nil
}
return nil, fmt.Errorf("Found multiple matching VPCs for vpcID = %s", vpcID)
}
// Retrieves the specified security group from the AWS API, or returns nil if not found // Retrieves the specified security group from the AWS API, or returns nil if not found
func (s *AWSCloud) findSecurityGroup(securityGroupId string) (*ec2.SecurityGroup, error) { func (s *AWSCloud) findSecurityGroup(securityGroupId string) (*ec2.SecurityGroup, error) {
describeSecurityGroupsRequest := &ec2.DescribeSecurityGroupsInput{ describeSecurityGroupsRequest := &ec2.DescribeSecurityGroupsInput{
...@@ -1572,13 +1532,13 @@ func (s *AWSCloud) createTags(request *ec2.CreateTagsInput) (*ec2.CreateTagsOutp ...@@ -1572,13 +1532,13 @@ func (s *AWSCloud) createTags(request *ec2.CreateTagsInput) (*ec2.CreateTagsOutp
} }
} }
func (s *AWSCloud) listSubnetIDsinVPC(vpc *ec2.Vpc) ([]string, error) { func (s *AWSCloud) listSubnetIDsinVPC(vpcId string) ([]string, error) {
subnetIds := []string{} subnetIds := []string{}
request := &ec2.DescribeSubnetsInput{} request := &ec2.DescribeSubnetsInput{}
filters := []*ec2.Filter{} filters := []*ec2.Filter{}
filters = append(filters, newEc2Filter("vpc-id", orEmpty(vpc.VpcId))) filters = append(filters, newEc2Filter("vpc-id", vpcId))
// Note, this will only return subnets tagged with the cluster identifier for this Kubernetes cluster. // Note, this will only return subnets tagged with the cluster identifier for this Kubernetes cluster.
// In the case where an AZ has public & private subnets per AWS best practices, the deployment should ensure // In the case where an AZ has public & private subnets per AWS best practices, the deployment should ensure
// only the public subnet (where the ELB will go) is so tagged. // only the public subnet (where the ELB will go) is so tagged.
...@@ -1629,17 +1589,13 @@ func (s *AWSCloud) EnsureTCPLoadBalancer(name, region string, publicIP net.IP, p ...@@ -1629,17 +1589,13 @@ func (s *AWSCloud) EnsureTCPLoadBalancer(name, region string, publicIP net.IP, p
return nil, err return nil, err
} }
vpc, err := s.findVPC() vpcId, err := s.findVPCID()
if err != nil { if err != nil {
glog.Error("error finding VPC", err)
return nil, err return nil, err
} }
if vpc == nil {
return nil, fmt.Errorf("Unable to find VPC")
}
// Construct list of configured subnets // Construct list of configured subnets
subnetIDs, err := s.listSubnetIDsinVPC(vpc) subnetIDs, err := s.listSubnetIDsinVPC(vpcId)
if err != nil { if err != nil {
glog.Error("error listing subnets in VPC", err) glog.Error("error listing subnets in VPC", err)
return nil, err return nil, err
...@@ -1650,7 +1606,7 @@ func (s *AWSCloud) EnsureTCPLoadBalancer(name, region string, publicIP net.IP, p ...@@ -1650,7 +1606,7 @@ func (s *AWSCloud) EnsureTCPLoadBalancer(name, region string, publicIP net.IP, p
{ {
sgName := "k8s-elb-" + name sgName := "k8s-elb-" + name
sgDescription := "Security group for Kubernetes ELB " + name sgDescription := "Security group for Kubernetes ELB " + name
securityGroupID, err = s.ensureSecurityGroup(sgName, sgDescription, orEmpty(vpc.VpcId)) securityGroupID, err = s.ensureSecurityGroup(sgName, sgDescription, vpcId)
if err != nil { if err != nil {
glog.Error("Error creating load balancer security group: ", err) glog.Error("Error creating load balancer security group: ", err)
return nil, err return nil, err
......
...@@ -383,10 +383,6 @@ func (ec2 *FakeEC2) RevokeSecurityGroupIngress(*ec2.RevokeSecurityGroupIngressIn ...@@ -383,10 +383,6 @@ func (ec2 *FakeEC2) RevokeSecurityGroupIngress(*ec2.RevokeSecurityGroupIngressIn
panic("Not implemented") panic("Not implemented")
} }
func (ec2 *FakeEC2) DescribeVPCs(*ec2.DescribeVpcsInput) ([]*ec2.Vpc, error) {
panic("Not implemented")
}
func (ec2 *FakeEC2) DescribeSubnets(request *ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) { func (ec2 *FakeEC2) DescribeSubnets(request *ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) {
ec2.DescribeSubnetsInput = request ec2.DescribeSubnetsInput = request
return ec2.Subnets, nil return ec2.Subnets, nil
...@@ -728,9 +724,6 @@ func TestSubnetIDsinVPC(t *testing.T) { ...@@ -728,9 +724,6 @@ func TestSubnetIDsinVPC(t *testing.T) {
} }
vpcID := "vpc-deadbeef" vpcID := "vpc-deadbeef"
vpc := &ec2.Vpc{
VpcId: &vpcID,
}
// test with 3 subnets from 3 different AZs // test with 3 subnets from 3 different AZs
subnets := make(map[int]map[string]string) subnets := make(map[int]map[string]string)
...@@ -745,7 +738,7 @@ func TestSubnetIDsinVPC(t *testing.T) { ...@@ -745,7 +738,7 @@ func TestSubnetIDsinVPC(t *testing.T) {
subnets[2]["az"] = "af-south-1c" subnets[2]["az"] = "af-south-1c"
awsServices.ec2.Subnets = constructSubnets(subnets) awsServices.ec2.Subnets = constructSubnets(subnets)
result, err := c.listSubnetIDsinVPC(vpc) result, err := c.listSubnetIDsinVPC(vpcID)
if err != nil { if err != nil {
t.Errorf("Error listing subnets: %v", err) t.Errorf("Error listing subnets: %v", err)
return return
...@@ -775,7 +768,7 @@ func TestSubnetIDsinVPC(t *testing.T) { ...@@ -775,7 +768,7 @@ func TestSubnetIDsinVPC(t *testing.T) {
subnets[3]["az"] = "af-south-1c" subnets[3]["az"] = "af-south-1c"
awsServices.ec2.Subnets = constructSubnets(subnets) awsServices.ec2.Subnets = constructSubnets(subnets)
result, err = c.listSubnetIDsinVPC(vpc) result, err = c.listSubnetIDsinVPC(vpcID)
if err != nil { if err != nil {
t.Errorf("Error listing subnets: %v", err) t.Errorf("Error listing subnets: %v", err)
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment