Skip to content

Commit 0a1a724

Browse files
committed
handle subnets with multiple cluster tags
1 parent f845ff5 commit 0a1a724

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

pkg/networking/subnet_resolver.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,20 @@ func (r *defaultSubnetsResolver) checkSubnetHasClusterTag(subnet *ec2sdk.Subnet)
325325
func (r *defaultSubnetsResolver) checkSubnetIsNotTaggedForOtherClusters(subnet *ec2sdk.Subnet) bool {
326326
clusterResourceTagPrefix := "kubernetes.io/cluster"
327327
clusterResourceTagKey := fmt.Sprintf("kubernetes.io/cluster/%s", r.clusterName)
328+
hasClusterResourceTagPrefix := false
328329
for _, tag := range subnet.Tags {
329330
tagKey := awssdk.StringValue(tag.Key)
330331
if tagKey == clusterResourceTagKey {
331332
return true
332333
}
333334
if strings.HasPrefix(tagKey, clusterResourceTagPrefix) {
334-
return false
335+
// If the cluster tag is for a different cluster, keep track of it and exclude
336+
// the subnet if no matching tag found for the current cluster.
337+
hasClusterResourceTagPrefix = true
335338
}
336339
}
340+
if hasClusterResourceTagPrefix {
341+
return false
342+
}
337343
return true
338344
}

pkg/networking/subnet_resolver_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,70 @@ func Test_defaultSubnetsResolver_ResolveViaDiscovery(t *testing.T) {
548548
},
549549
},
550550
},
551+
{
552+
name: "subnets with multiple cluster tags",
553+
fields: fields{
554+
vpcID: "vpc-1",
555+
clusterName: "kube-cluster",
556+
describeSubnetsAsListCalls: []describeSubnetsAsListCall{
557+
{
558+
input: &ec2sdk.DescribeSubnetsInput{
559+
Filters: []*ec2sdk.Filter{
560+
{
561+
Name: awssdk.String("tag:kubernetes.io/role/elb"),
562+
Values: awssdk.StringSlice([]string{"", "1"}),
563+
},
564+
{
565+
Name: awssdk.String("vpc-id"),
566+
Values: awssdk.StringSlice([]string{"vpc-1"}),
567+
},
568+
},
569+
},
570+
output: []*ec2sdk.Subnet{
571+
{
572+
SubnetId: awssdk.String("subnet-1"),
573+
AvailabilityZone: awssdk.String("us-west-2a"),
574+
VpcId: awssdk.String("vpc-1"),
575+
Tags: []*ec2sdk.Tag{
576+
{
577+
Key: awssdk.String("kubernetes.io/cluster/some-other-cluster"),
578+
Value: awssdk.String("owned"),
579+
},
580+
{
581+
Key: awssdk.String("kubernetes.io/cluster/kube-cluster"),
582+
Value: awssdk.String("shared"),
583+
},
584+
},
585+
},
586+
},
587+
},
588+
},
589+
},
590+
args: args{
591+
opts: []SubnetsResolveOption{
592+
WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeNetwork),
593+
WithSubnetsResolveLBScheme(elbv2model.LoadBalancerSchemeInternetFacing),
594+
},
595+
},
596+
want: []*ec2sdk.Subnet{
597+
{
598+
SubnetId: awssdk.String("subnet-1"),
599+
AvailabilityZone: awssdk.String("us-west-2a"),
600+
VpcId: awssdk.String("vpc-1"),
601+
Tags: []*ec2sdk.Tag{
602+
{
603+
Key: awssdk.String("kubernetes.io/cluster/some-other-cluster"),
604+
Value: awssdk.String("owned"),
605+
},
606+
{
607+
Key: awssdk.String("kubernetes.io/cluster/kube-cluster"),
608+
Value: awssdk.String("shared"),
609+
},
610+
},
611+
},
612+
},
613+
614+
},
551615
}
552616

553617
for _, tt := range tests {

0 commit comments

Comments
 (0)