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

fix(database): Sort tags before compare #1895

Merged
merged 1 commit into from
Oct 12, 2023
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
16 changes: 16 additions & 0 deletions pkg/clients/database/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package rds
import (
"context"
"encoding/json"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -35,6 +36,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane-contrib/provider-aws/apis/database/v1beta1"
Expand Down Expand Up @@ -320,6 +322,13 @@ func CreatePatch(in *rdstypes.DBInstance, target *v1beta1.RDSInstanceParameters)
currentParams := &v1beta1.RDSInstanceParameters{}
LateInitialize(currentParams, in)

for _, t := range in.TagList {
currentParams.Tags = append(currentParams.Tags, v1beta1.Tag{
Key: ptr.Deref(t.Key, ""),
Value: ptr.Deref(t.Value, ""),
})
}

// Don't attempt to scale down storage if autoscaling is enabled,
// and the current storage is larger than what was once
// requested. We still want to allow the user to manually scale
Expand All @@ -346,6 +355,9 @@ func CreatePatch(in *rdstypes.DBInstance, target *v1beta1.RDSInstanceParameters)
}
}

slices.SortFunc(currentParams.Tags, compareTags)
slices.SortFunc(target.Tags, compareTags)

jsonPatch, err := awsclients.CreateJSONPatch(currentParams, target)
if err != nil {
return nil, err
Expand All @@ -357,6 +369,10 @@ func CreatePatch(in *rdstypes.DBInstance, target *v1beta1.RDSInstanceParameters)
return patch, nil
}

func compareTags(a, b v1beta1.Tag) int {
return strings.Compare(a.Key, b.Key)
}

// GenerateModifyDBInstanceInput from RDSInstanceSpec
func GenerateModifyDBInstanceInput(name string, p *v1beta1.RDSInstanceParameters, db *rdstypes.DBInstance) *rds.ModifyDBInstanceInput {
// NOTE(muvaf): MasterUserPassword is not used here. So, password is set once
Expand Down
69 changes: 69 additions & 0 deletions pkg/clients/database/rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,75 @@ func TestCreatePatch(t *testing.T) {
},
},
},
"DifferentTags": {
args: args{
db: &rdstypes.DBInstance{
TagList: []rdstypes.Tag{
{Key: ptr.To("tag1")},
{Key: ptr.To("tag2")},
{Key: ptr.To("tag3")},
},
},
p: &v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag1"},
{Key: "tag5"},
{Key: "tag6"},
},
},
},
want: want{
patch: &v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag1"},
{Key: "tag5"},
{Key: "tag6"},
},
},
},
},
"SameTags": {
args: args{
db: &rdstypes.DBInstance{
TagList: []rdstypes.Tag{
{Key: ptr.To("tag1")},
{Key: ptr.To("tag2")},
{Key: ptr.To("tag3")},
},
},
p: &v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag1"},
{Key: "tag2"},
{Key: "tag3"},
},
},
},
want: want{
patch: &v1beta1.RDSInstanceParameters{},
},
},
"SameTagsDifferentOrder": {
args: args{
db: &rdstypes.DBInstance{
TagList: []rdstypes.Tag{
{Key: ptr.To("tag1"), Value: ptr.To("val")},
{Key: ptr.To("tag2"), Value: ptr.To("val")},
{Key: ptr.To("tag3"), Value: ptr.To("val")},
},
},
p: &v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag3", Value: "val"},
{Key: "tag2", Value: "val"},
{Key: "tag1", Value: "val"},
},
},
},
want: want{
patch: &v1beta1.RDSInstanceParameters{},
},
},
}

for name, tc := range cases {
Expand Down
Loading