Skip to content

Commit 681bb78

Browse files
aws: log when multiple subnets found for the same availability zone
Signed-off-by: Alexander Yastrebov <[email protected]>
1 parent f2f28dc commit 681bb78

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

aws/adapter.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aws
33
import (
44
"errors"
55
"fmt"
6+
"sort"
67
"strings"
78
"time"
89

@@ -915,47 +916,42 @@ func (a *Adapter) FindLBSubnets(scheme string) []string {
915916
internal = true
916917
}
917918

918-
subnetsByAZ := make(map[string]*subnetDetails)
919+
subnetsByAZ := make(map[string][]*subnetDetails)
919920
for _, subnet := range a.manifest.subnets {
920921
// ignore private subnet for public LB
921922
if !internal && !subnet.public {
922923
continue
923924
}
925+
subnetsByAZ[subnet.availabilityZone] = append(subnetsByAZ[subnet.availabilityZone], subnet)
926+
}
924927

925-
existing, ok := subnetsByAZ[subnet.availabilityZone]
926-
if !ok {
927-
subnetsByAZ[subnet.availabilityZone] = subnet
928-
continue
929-
}
928+
subnetIDs := make([]string, 0, len(subnetsByAZ))
929+
for az, subnets := range subnetsByAZ {
930+
if len(subnets) > 1 {
931+
sort.Slice(subnets, func(a, b int) bool {
932+
subnetA, subnetB := subnets[a], subnets[b]
930933

931-
// prefer subnet with an elb role tag
932-
var tagName string
933-
if internal {
934-
tagName = internalELBRoleTagName
935-
} else {
936-
tagName = elbRoleTagName
937-
}
934+
// prefer subnet with an elb role tag
935+
tagName := elbRoleTagName
936+
if internal {
937+
tagName = internalELBRoleTagName
938+
}
938939

939-
_, existingHasTag := existing.tags[tagName]
940-
_, subnetHasTag := subnet.tags[tagName]
940+
_, subnetAHasTag := subnetA.tags[tagName]
941+
_, subnetBHasTag := subnetB.tags[tagName]
941942

942-
if existingHasTag != subnetHasTag {
943-
if subnetHasTag {
944-
subnetsByAZ[subnet.availabilityZone] = subnet
945-
}
946-
continue
947-
}
943+
if subnetAHasTag != subnetBHasTag {
944+
return subnetAHasTag
945+
}
948946

949-
// If we have two subnets for the same AZ we arbitrarily choose
950-
// the one that is first lexicographically.
951-
if strings.Compare(existing.id, subnet.id) > 0 {
952-
subnetsByAZ[subnet.availabilityZone] = subnet
947+
// prefer subnet id that is first lexicographically
948+
return subnetA.id < subnetB.id
949+
})
950+
951+
log.Warnf("Found multiple subnets %v for availability zone %s, using %s", subnets, az, subnets[0].id)
953952
}
954-
}
955953

956-
subnetIDs := make([]string, 0, len(subnetsByAZ))
957-
for _, subnet := range subnetsByAZ {
958-
subnetIDs = append(subnetIDs, subnet.id)
954+
subnetIDs = append(subnetIDs, subnets[0].id)
959955
}
960956

961957
return subnetIDs

aws/adapter_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,23 @@ func TestFindLBSubnets(tt *testing.T) {
704704
scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
705705
expectedSubnets: []string{"1"},
706706
},
707+
{
708+
name: "should select first lexicographically subnet when two match a single zone (regardless of details order)",
709+
subnets: []*subnetDetails{
710+
{
711+
availabilityZone: "a",
712+
public: true,
713+
id: "1",
714+
},
715+
{
716+
availabilityZone: "a",
717+
public: true,
718+
id: "2",
719+
},
720+
},
721+
scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
722+
expectedSubnets: []string{"1"},
723+
},
707724
{
708725
name: "should not use internal subnets for public LB",
709726
subnets: []*subnetDetails{
@@ -736,6 +753,34 @@ func TestFindLBSubnets(tt *testing.T) {
736753
scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
737754
expectedSubnets: []string{"2"},
738755
},
756+
{
757+
name: "should prefer tagged subnet selected first lexicographically",
758+
subnets: []*subnetDetails{
759+
{
760+
availabilityZone: "a",
761+
public: true,
762+
id: "1",
763+
},
764+
{
765+
availabilityZone: "a",
766+
public: true,
767+
id: "2",
768+
tags: map[string]string{
769+
elbRoleTagName: "",
770+
},
771+
},
772+
{
773+
availabilityZone: "a",
774+
public: true,
775+
id: "3",
776+
tags: map[string]string{
777+
elbRoleTagName: "",
778+
},
779+
},
780+
},
781+
scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
782+
expectedSubnets: []string{"2"},
783+
},
739784
{
740785
name: "should prefer tagged subnet (internal)",
741786
subnets: []*subnetDetails{

0 commit comments

Comments
 (0)