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

[Backport release-0.50] fix(ec2): Fix tags compare #2111

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions pkg/clients/ec2/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ec2
import (
"context"
"errors"
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -101,31 +100,3 @@ func BuildFromEC2Tags(tags []ec2types.Tag) []v1beta1.Tag {

return res
}

// CompareTags compares arrays of v1beta1.Tag and ec2types.Tag
func CompareTags(tags []v1beta1.Tag, ec2Tags []ec2types.Tag) bool {
if len(tags) != len(ec2Tags) {
return false
}

SortTags(tags, ec2Tags)

for i, t := range tags {
if t.Key != *ec2Tags[i].Key || t.Value != *ec2Tags[i].Value {
return false
}
}

return true
}

// SortTags sorts array of v1beta1.Tag and ec2types.Tag on 'Key'
func SortTags(tags []v1beta1.Tag, ec2Tags []ec2types.Tag) {
sort.Slice(tags, func(i, j int) bool {
return tags[i].Key < tags[j].Key
})

sort.Slice(ec2Tags, func(i, j int) bool {
return *ec2Tags[i].Key < *ec2Tags[j].Key
})
}
2 changes: 1 addition & 1 deletion pkg/clients/ec2/internetgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func IsIgUpToDate(p v1beta1.InternetGatewayParameters, ig ec2types.InternetGatew
// if the attachment in spec exists in ig.Attachments, compare the tags and return
for _, a := range ig.Attachments {
if aws.ToString(p.VPCID) == aws.ToString(a.VpcId) {
return CompareTagsV1Beta1(p.Tags, ig.Tags)
return CompareTags(p.Tags, ig.Tags)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/ec2/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func CreateRTPatch(in ec2types.RouteTable, target v1beta1.RouteTableParameters)
targetCopy := target.DeepCopy()
currentParams := &v1beta1.RouteTableParameters{}

SortTagsV1Beta1(target.Tags, in.Tags)
CompareTags(target.Tags, in.Tags)

if !pointer.BoolValue(target.IgnoreRoutes) {
// Add the default route for fair comparison.
Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/ec2/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ func IsSubnetUpToDate(p v1beta1.SubnetParameters, s ec2types.Subnet) bool {
if aws.ToBool(p.AssignIPv6AddressOnCreation) != aws.ToBool(s.AssignIpv6AddressOnCreation) {
return false
}
return CompareTagsV1Beta1(p.Tags, s.Tags)
return CompareTags(p.Tags, s.Tags)
}
28 changes: 28 additions & 0 deletions pkg/clients/ec2/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ func TestDiffEC2Tags(t *testing.T) {
remove: []ec2types.Tag{},
},
},
"TagsWithSameKeyValuesButDifferentOrder": {
args: args{
local: []ec2types.Tag{
{
Key: aws.String("val"),
Value: aws.String("key"),
},
{
Key: aws.String("name"),
Value: aws.String("somename"),
},
},
remote: []ec2types.Tag{
{
Key: aws.String("name"),
Value: aws.String("somename"),
},
{
Key: aws.String("val"),
Value: aws.String("key"),
},
},
},
want: want{
add: []ec2types.Tag{},
remove: []ec2types.Tag{},
},
},
"TagsWithSameKeyDifferentValuesAndSameLength": {
args: args{
local: []ec2types.Tag{
Expand Down
31 changes: 5 additions & 26 deletions pkg/clients/ec2/tags_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package ec2

import (
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

Expand Down Expand Up @@ -47,30 +45,11 @@ func GenerateEC2TagsV1Beta1(tags []svcapitypes.Tag) []ec2types.Tag {
return res
}

// CompareTags compares arrays of v1beta1.Tag and ec2type.Tag
func CompareTagsV1Beta1(tags []svcapitypes.Tag, ec2Tags []ec2types.Tag) bool {
if len(tags) != len(ec2Tags) {
// CompareTags compares arrays of v1beta1.Tag and ec2types.Tag
func CompareTags(spec []svcapitypes.Tag, current []ec2types.Tag) bool {
if len(spec) != len(current) {
return false
}

SortTagsV1Beta1(tags, ec2Tags)

for i, t := range tags {
if t.Key != *ec2Tags[i].Key || t.Value != *ec2Tags[i].Value {
return false
}
}

return true
}

// SortTags sorts array of v1beta1.Tag and ec2type.Tag on 'Key'
func SortTagsV1Beta1(tags []svcapitypes.Tag, ec2Tags []ec2types.Tag) {
sort.Slice(tags, func(i, j int) bool {
return tags[i].Key < tags[j].Key
})

sort.Slice(ec2Tags, func(i, j int) bool {
return *ec2Tags[i].Key < *ec2Tags[j].Key
})
toAdd, toRemove := DiffEC2Tags(GenerateEC2TagsV1Beta1(spec), current)
return len(toAdd) == 0 && len(toRemove) == 0
}
2 changes: 1 addition & 1 deletion pkg/clients/ec2/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func IsVpcUpToDate(spec v1beta1.VPCParameters, vpc ec2types.Vpc, attributes ec2.
return false
}

return CompareTagsV1Beta1(spec.Tags, vpc.Tags)
return CompareTags(spec.Tags, vpc.Tags)
}

// GenerateVpcObservation is used to produce v1beta1.VPCObservation from
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/ec2/natgateway/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (e *external) Observe(ctx context.Context, mgd resource.Managed) (managed.E

return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: ec2.CompareTagsV1Beta1(cr.Spec.ForProvider.Tags, observed.Tags),
ResourceUpToDate: ec2.CompareTags(cr.Spec.ForProvider.Tags, observed.Tags),
}, nil
}

Expand Down
Loading
Loading