diff --git a/pkg/cloudprovider/providers/aws/aws.go b/pkg/cloudprovider/providers/aws/aws.go index 31faa871d6..f0c221b774 100644 --- a/pkg/cloudprovider/providers/aws/aws.go +++ b/pkg/cloudprovider/providers/aws/aws.go @@ -3653,7 +3653,11 @@ func findSecurityGroupForInstance(instance *ec2.Instance, taggedSecurityGroups m // We create instances with one SG // If users create multiple SGs, they must tag one of them as being k8s owned if len(tagged) != 1 { - return nil, fmt.Errorf("Multiple tagged security groups found for instance %s; ensure only the k8s security group is tagged", instanceID) + taggedGroups := "" + for _, v := range tagged { + taggedGroups += fmt.Sprintf("%s(%s) ", *v.GroupId, *v.GroupName) + } + return nil, fmt.Errorf("Multiple tagged security groups found for instance %s; ensure only the k8s security group is tagged; the tagged groups were %v", instanceID, taggedGroups) } return tagged[0], nil } diff --git a/pkg/cloudprovider/providers/aws/aws_test.go b/pkg/cloudprovider/providers/aws/aws_test.go index 5e9601730b..b5368e0fb1 100644 --- a/pkg/cloudprovider/providers/aws/aws_test.go +++ b/pkg/cloudprovider/providers/aws/aws_test.go @@ -1302,6 +1302,29 @@ func TestEnsureLoadBalancerHealthCheck(t *testing.T) { }) } +func TestFindSecurityGroupForInstance(t *testing.T) { + groups := map[string]*ec2.SecurityGroup{"sg123": {GroupId: aws.String("sg123")}} + id, err := findSecurityGroupForInstance(&ec2.Instance{SecurityGroups: []*ec2.GroupIdentifier{{GroupId: aws.String("sg123"), GroupName: aws.String("my_group")}}}, groups) + if err != nil { + t.Error() + } + assert.Equal(t, *id.GroupId, "sg123") + assert.Equal(t, *id.GroupName, "my_group") +} + +func TestFindSecurityGroupForInstanceMultipleTagged(t *testing.T) { + groups := map[string]*ec2.SecurityGroup{"sg123": {GroupId: aws.String("sg123")}} + _, err := findSecurityGroupForInstance(&ec2.Instance{ + SecurityGroups: []*ec2.GroupIdentifier{ + {GroupId: aws.String("sg123"), GroupName: aws.String("my_group")}, + {GroupId: aws.String("sg123"), GroupName: aws.String("another_group")}, + }, + }, groups) + require.Error(t, err) + assert.Contains(t, err.Error(), "sg123(my_group)") + assert.Contains(t, err.Error(), "sg123(another_group)") +} + func newMockedFakeAWSServices(id string) *FakeAWSServices { s := NewFakeAWSServices(id) s.ec2 = &MockedFakeEC2{FakeEC2Impl: s.ec2.(*FakeEC2Impl)}