Skip to content

Commit

Permalink
Drop duplicate instance tags, if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Karen Almog committed Oct 23, 2018
1 parent 4d9498f commit ecae784
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ type ActuatorParams struct {
AwsClientBuilder awsclient.AwsClientBuilderFuncType
}

// Scan machine tags, and return a deduped tags list
func removeDuplicatedTags(tags []*ec2.Tag) []*ec2.Tag {
m := make(map[string]bool)
result := []*ec2.Tag{}

// look for duplicates
for _, entry := range tags {
if _, value := m[*entry.Key]; !value {
m[*entry.Key] = true
result = append(result, entry)
}
}
return result
}

// NewActuator returns a new AWS Actuator
func NewActuator(params ActuatorParams) (*Actuator, error) {
actuator := &Actuator{
Expand Down Expand Up @@ -339,16 +354,16 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
return nil, err
}
// Add tags to the created machine
tagList := []*ec2.Tag{}
rawTagList := []*ec2.Tag{}
for _, tag := range machineProviderConfig.Tags {
tagList = append(tagList, &ec2.Tag{Key: aws.String(tag.Name), Value: aws.String(tag.Value)})
rawTagList = append(rawTagList, &ec2.Tag{Key: aws.String(tag.Name), Value: aws.String(tag.Value)})
}
tagList = append(tagList, []*ec2.Tag{
rawTagList = append(rawTagList, []*ec2.Tag{
{Key: aws.String("clusterid"), Value: aws.String(clusterID)},
{Key: aws.String("kubernetes.io/cluster/" + clusterID), Value: aws.String("owned")},
{Key: aws.String("Name"), Value: aws.String(machine.Name)},
}...)

tagList := removeDuplicatedTags(rawTagList)
tagInstance := &ec2.TagSpecification{
ResourceType: aws.String("instance"),
Tags: tagList,
Expand Down
47 changes: 47 additions & 0 deletions pkg/cloud/aws/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -292,3 +293,49 @@ func mockTerminateInstances(mockAWSClient *mockaws.MockClient) {
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(
&ec2.TerminateInstancesOutput{}, nil)
}

func TestRemoveDuplicatedTags(t *testing.T) {
cases := []struct {
tagList []*ec2.Tag
expected []*ec2.Tag
}{
{
// empty tags
tagList: []*ec2.Tag{},
expected: []*ec2.Tag{},
},
{
// no duplicate tags
tagList: []*ec2.Tag{
{Key: aws.String("clusterID"), Value: aws.String("test-ClusterIDValue")},
{Key: aws.String("clusterName"), Value: aws.String("test-ClusterNameValue")},
},
expected: []*ec2.Tag{
{Key: aws.String("clusterID"), Value: aws.String("test-ClusterIDValue")},
{Key: aws.String("clusterName"), Value: aws.String("test-ClusterNameValue")},
},
},
{
// multiple duplicate tags
tagList: []*ec2.Tag{
{Key: aws.String("clusterID"), Value: aws.String("test-ClusterIDValue")},
{Key: aws.String("clusterName"), Value: aws.String("test-ClusterNameValue")},
{Key: aws.String("clusterSize"), Value: aws.String("test-ClusterSizeValue")},
{Key: aws.String("clusterName"), Value: aws.String("test-ClusterNameDuplicatedValue")},
{Key: aws.String("clusterSize"), Value: aws.String("test-ClusterSizeDuplicatedValue")},
},
expected: []*ec2.Tag{
{Key: aws.String("clusterID"), Value: aws.String("test-ClusterIDValue")},
{Key: aws.String("clusterName"), Value: aws.String("test-ClusterNameValue")},
{Key: aws.String("clusterSize"), Value: aws.String("test-ClusterSizeValue")},
},
},
}

for i, c := range cases {
actual := removeDuplicatedTags(c.tagList)
if !reflect.DeepEqual(c.expected, actual) {
t.Errorf("test #%d: expected %+v, got %+v", i, c.expected, actual)
}
}
}

0 comments on commit ecae784

Please sign in to comment.