Arbitrarily chose first (lexicographically) subnet in AZ on AWS.

When there is more than one subnet for an AZ on AWS choose arbitrarily
chose the first one lexicographically for consistency.
This commit is contained in:
Matt Landis 2017-06-29 16:27:05 -07:00
parent f189d7f72e
commit 10794e7f6e
2 changed files with 30 additions and 7 deletions

View File

@ -2467,8 +2467,15 @@ func (c *Cloud) findELBSubnets(internalELB bool) ([]string, error) {
continue continue
} }
// TODO: Should this be an error? // If we have two subnets for the same AZ we arbitrarily choose the one that is first lexicographically.
glog.Warningf("Found multiple subnets in AZ %q; making arbitrary choice between subnets %q and %q", az, *existing.SubnetId, *subnet.SubnetId) // TODO: Should this be an error.
if strings.Compare(*existing.SubnetId, *subnet.SubnetId) > 0 {
glog.Warningf("Found multiple subnets in AZ %q; choosing %q between subnets %q and %q", az, *subnet.SubnetId, *existing.SubnetId, *subnet.SubnetId)
subnetsByAZ[az] = subnet
continue
}
glog.Warningf("Found multiple subnets in AZ %q; choosing %q between subnets %q and %q", az, *existing.SubnetId, *existing.SubnetId, *subnet.SubnetId)
continue continue
} }

View File

@ -792,12 +792,18 @@ func TestSubnetIDsinVPC(t *testing.T) {
} }
} }
// test with 4 subnets from 3 different AZs // Test with 5 subnets from 3 different AZs.
// add duplicate az subnet // Add 2 duplicate AZ subnets lexicographically chosen one is the middle element in array to
// check that we both choose the correct entry when it comes after and before another element
// in the same AZ.
subnets[3] = make(map[string]string) subnets[3] = make(map[string]string)
subnets[3]["id"] = "subnet-c0000002" subnets[3]["id"] = "subnet-c0000000"
subnets[3]["az"] = "af-south-1c" subnets[3]["az"] = "af-south-1c"
subnets[4] = make(map[string]string)
subnets[4]["id"] = "subnet-c0000002"
subnets[4]["az"] = "af-south-1c"
awsServices.ec2.Subnets = constructSubnets(subnets) awsServices.ec2.Subnets = constructSubnets(subnets)
routeTables["subnet-c0000000"] = true
routeTables["subnet-c0000002"] = true routeTables["subnet-c0000002"] = true
awsServices.ec2.RouteTables = constructRouteTables(routeTables) awsServices.ec2.RouteTables = constructRouteTables(routeTables)
@ -812,6 +818,16 @@ func TestSubnetIDsinVPC(t *testing.T) {
return return
} }
expected := []*string{aws.String("subnet-a0000001"), aws.String("subnet-b0000001"), aws.String("subnet-c0000000")}
for _, s := range result {
if !contains(expected, s) {
t.Errorf("Unexpected subnet '%s' found", s)
return
}
}
delete(routeTables, "subnet-c0000002")
// test with 6 subnets from 3 different AZs // test with 6 subnets from 3 different AZs
// with 3 private subnets // with 3 private subnets
subnets[4] = make(map[string]string) subnets[4] = make(map[string]string)
@ -825,7 +841,7 @@ func TestSubnetIDsinVPC(t *testing.T) {
routeTables["subnet-a0000001"] = false routeTables["subnet-a0000001"] = false
routeTables["subnet-b0000001"] = false routeTables["subnet-b0000001"] = false
routeTables["subnet-c0000001"] = false routeTables["subnet-c0000001"] = false
routeTables["subnet-c0000002"] = true routeTables["subnet-c0000000"] = true
routeTables["subnet-d0000001"] = true routeTables["subnet-d0000001"] = true
routeTables["subnet-d0000002"] = true routeTables["subnet-d0000002"] = true
awsServices.ec2.RouteTables = constructRouteTables(routeTables) awsServices.ec2.RouteTables = constructRouteTables(routeTables)
@ -840,7 +856,7 @@ func TestSubnetIDsinVPC(t *testing.T) {
return return
} }
expected := []*string{aws.String("subnet-c0000002"), aws.String("subnet-d0000001"), aws.String("subnet-d0000002")} expected = []*string{aws.String("subnet-c0000000"), aws.String("subnet-d0000001"), aws.String("subnet-d0000002")}
for _, s := range result { for _, s := range result {
if !contains(expected, s) { if !contains(expected, s) {
t.Errorf("Unexpected subnet '%s' found", s) t.Errorf("Unexpected subnet '%s' found", s)