Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop duplicate instance tags, if exists #78

Merged
merged 1 commit into from
Oct 23, 2018
Merged

Drop duplicate instance tags, if exists #78

merged 1 commit into from
Oct 23, 2018

Conversation

trawler
Copy link

@trawler trawler commented Oct 17, 2018

@openshift-ci-robot openshift-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 17, 2018
@trawler trawler requested review from enxebre and frobware October 17, 2018 16:17
@trawler trawler changed the title Drop duplicate instance tags, if exists [WIP] Drop duplicate instance tags, if exists Oct 17, 2018
@openshift-ci-robot openshift-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 17, 2018
@trawler trawler changed the title [WIP] Drop duplicate instance tags, if exists Drop duplicate instance tags, if exists Oct 17, 2018
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 17, 2018

// look for duplicates
for i := range tags {
if m[*tags[i].Key] == true {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to == true.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, I think, can be reduced to:

    ...
    keys := make(map[string]bool)
    result := []*ec2.Tag{}
    for _, entry := range tags {
        if _, value := keys[*entry.Key]; !value {
            keys[*entry.Key] = true
            result = append(result, entry)
        }
    }    
    return result
}

but untested - I just typed it out!

Please could you also create a unit test for this function as it it generic.


// look for duplicates
for i := range tags {
if m[*tags[i].Key] == true {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, I think, can be reduced to:

    ...
    keys := make(map[string]bool)
    result := []*ec2.Tag{}
    for _, entry := range tags {
        if _, value := keys[*entry.Key]; !value {
            keys[*entry.Key] = true
            result = append(result, entry)
        }
    }    
    return result
}

but untested - I just typed it out!

Please could you also create a unit test for this function as it it generic.

Copy link
Member

@enxebre enxebre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking great! can we please include a unit test for this?

@openshift-ci-robot openshift-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 19, 2018
@paulfantom
Copy link

/test e2e-aws

}

for i, c := range cases {
if len(c.deduplicatedList) != len(removeDuplicatedTags(c.tagList)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length doesn't guarantee correctness. Better to have expected and actual and use reflect.DeepEquals to verify the result.

}{
{
// no duplicate tags
tagList: []*ec2.Tag{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the empty case (i.e., empty slices).

for i, c := range cases {
actual := removeDuplicatedTags(c.tagList)
if !reflect.DeepEqual(c.expected, actual) {
t.Errorf("test case %d: output of removeDuplicatedTags is not as expected.\n", i)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more succinct to reduce all of this output to just:

t.Errorf("test #%d: expected %+v, got %+v", c.expected, actual)

as the inputs are quite small.

}
tagList = append(tagList, []*ec2.Tag{
rawTagList = append(rawTagList, []*ec2.Tag{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just put all the tags under a map and then check if the key already exists?

tagMap := make(map[string]struct{})
for _, tag := range machineProviderConfig.Tags {
 		tagMap[tag.Name] = struct{}{}
}
if _, exists := tagMap["clusterid"]; !exists {
 		tagList = append(tagList, {Key: aws.String("clusterid"), Value: aws.String(clusterID)}}
}
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I like current implementation. It prevents also from injecting duplications @frobware wdyt?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of prefer the de-dupe phase. It's easier to rationalise. I have a bunch of tags, then a last action is to de-dupe them.

@enxebre
Copy link
Member

enxebre commented Oct 23, 2018

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 23, 2018
@frobware
Copy link

/lgtm

@frobware
Copy link

/approve

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: frobware

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 23, 2018
@openshift-merge-robot openshift-merge-robot merged commit 8f8c43e into openshift:master Oct 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants