Skip to content

Commit 7f42248

Browse files
visit1985Michael Goehler
authored and
Michael Goehler
committed
raise an error when multiple backendSGs with same vpc-id and tags exist
1 parent 879e715 commit 7f42248

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pkg/networking/backend_sg_provider.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,15 @@ func (p *defaultBackendSGProvider) getBackendSGFromEC2(ctx context.Context, sgNa
298298
},
299299
},
300300
}
301-
p.logger.V(1).Info("Queriying existing SG", "vpc-id", vpcID, "name", sgName)
301+
tags := fmt.Sprintf("%v=%v, %v=%v", tagKeyK8sCluster, p.clusterName, tagKeyResource, tagValueBackend)
302+
p.logger.V(1).Info("Querying existing SG", "vpc-id", vpcID, "tags", tags)
302303
sgs, err := p.ec2Client.DescribeSecurityGroupsAsList(ctx, req)
303304
if err != nil && !isEC2SecurityGroupNotFoundError(err) {
304305
return "", err
305306
}
307+
if len(sgs) > 1 {
308+
return "", errors.Errorf("Found multiple SGs with vpc-id %v and tags %v", vpcID, tags)
309+
}
306310
if len(sgs) > 0 {
307311
return awssdk.ToString(sgs[0].GroupId), nil
308312
}

pkg/networking/backend_sg_provider_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ func Test_defaultBackendSGProvider_Get(t *testing.T) {
114114
},
115115
want: "sg-autogen",
116116
},
117+
{
118+
name: "backend sg enabled, auto-gen, multiple SGs exist",
119+
fields: fields{
120+
describeSGCalls: []describeSecurityGroupsAsListCall{
121+
{
122+
req: &ec2sdk.DescribeSecurityGroupsInput{
123+
Filters: defaultEC2Filters,
124+
},
125+
resp: []ec2types.SecurityGroup{
126+
{
127+
GroupId: awssdk.String("sg-autogen"),
128+
},
129+
{
130+
GroupId: awssdk.String("sg-other"),
131+
},
132+
},
133+
},
134+
},
135+
ingResources: []*networking.Ingress{ing, ing1},
136+
},
137+
wantErr: errors.New("Found multiple SGs with vpc-id vpc-xxxyyy and tags elbv2.k8s.aws/cluster=testCluster, elbv2.k8s.aws/resource=backend-sg"),
138+
},
117139
{
118140
name: "backend sg enabled, auto-gen new SG",
119141
fields: fields{

0 commit comments

Comments
 (0)