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): tag-only-change triggers update + add RemoveTag call #1966

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
6 changes: 6 additions & 0 deletions pkg/clients/database/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type MockRDSClient struct {
MockModify func(context.Context, *rds.ModifyDBInstanceInput, []func(*rds.Options)) (*rds.ModifyDBInstanceOutput, error)
MockDelete func(context.Context, *rds.DeleteDBInstanceInput, []func(*rds.Options)) (*rds.DeleteDBInstanceOutput, error)
MockAddTags func(context.Context, *rds.AddTagsToResourceInput, []func(*rds.Options)) (*rds.AddTagsToResourceOutput, error)
MockRemoveTags func(context.Context, *rds.RemoveTagsFromResourceInput, []func(*rds.Options)) (*rds.RemoveTagsFromResourceOutput, error)
}

// DescribeDBInstances finds RDS Instance by name
Expand Down Expand Up @@ -73,3 +74,8 @@ func (m *MockRDSClient) DeleteDBInstance(ctx context.Context, i *rds.DeleteDBIns
func (m *MockRDSClient) AddTagsToResource(ctx context.Context, i *rds.AddTagsToResourceInput, opts ...func(*rds.Options)) (*rds.AddTagsToResourceOutput, error) {
return m.MockAddTags(ctx, i, opts)
}

// RemoveTagsFromResource removes tags from RDS Instance.
func (m *MockRDSClient) RemoveTagsFromResource(ctx context.Context, i *rds.RemoveTagsFromResourceInput, opts ...func(*rds.Options)) (*rds.RemoveTagsFromResourceOutput, error) {
return m.MockRemoveTags(ctx, i, opts)
}
80 changes: 58 additions & 22 deletions pkg/clients/database/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package rds
import (
"context"
"encoding/json"
"slices"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -59,6 +59,7 @@ type Client interface {
ModifyDBInstance(context.Context, *rds.ModifyDBInstanceInput, ...func(*rds.Options)) (*rds.ModifyDBInstanceOutput, error)
DeleteDBInstance(context.Context, *rds.DeleteDBInstanceInput, ...func(*rds.Options)) (*rds.DeleteDBInstanceOutput, error)
AddTagsToResource(context.Context, *rds.AddTagsToResourceInput, ...func(*rds.Options)) (*rds.AddTagsToResourceOutput, error)
RemoveTagsFromResource(context.Context, *rds.RemoveTagsFromResourceInput, ...func(*rds.Options)) (*rds.RemoveTagsFromResourceOutput, error)
}

// NewClient creates new RDS RDSClient with provided AWS Configurations/Credentials
Expand Down Expand Up @@ -320,18 +321,11 @@ func GenerateRestoreRDSInstanceToPointInTimeInput(name string, p *v1beta1.RDSIns
// CreatePatch creates a *v1beta1.RDSInstanceParameters that has only the changed
// values between the target *v1beta1.RDSInstanceParameters and the current
// *rds.DBInstance
func CreatePatch(in *rdstypes.DBInstance, spec *v1beta1.RDSInstanceParameters) (*v1beta1.RDSInstanceParameters, error) { //nolint:gocyclo
func CreatePatch(in *rdstypes.DBInstance, spec *v1beta1.RDSInstanceParameters) (*v1beta1.RDSInstanceParameters, error) {
target := spec.DeepCopy()
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, ""),
})
}

// AvailabilityZone parameters is not allowed for MultiAZ deployments.
// So set this to nil if that is the case to avoid unnecessary diffs.
if ptr.Deref(target.MultiAZ, false) {
Expand Down Expand Up @@ -367,9 +361,6 @@ func CreatePatch(in *rdstypes.DBInstance, spec *v1beta1.RDSInstanceParameters) (
}
}

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

jsonPatch, err := jsonpatch.CreateJSONPatch(currentParams, target)
if err != nil {
return nil, err
Expand All @@ -381,10 +372,6 @@ func CreatePatch(in *rdstypes.DBInstance, spec *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 Expand Up @@ -707,14 +694,18 @@ func lateInitializeOptionGroupName(inOptionGroupName *string, members []rdstypes
}

// IsUpToDate checks whether there is a change in any of the modifiable fields.
func IsUpToDate(ctx context.Context, kube client.Client, r *v1beta1.RDSInstance, db rdstypes.DBInstance) (bool, string, error) {
func IsUpToDate(ctx context.Context, kube client.Client, r *v1beta1.RDSInstance, db rdstypes.DBInstance) (bool, string, []rdstypes.Tag, []string, error) { //nolint:gocyclo

addTags := []rdstypes.Tag{}
removeTags := []string{}

_, pwdChanged, err := GetPassword(ctx, kube, r.Spec.ForProvider.MasterPasswordSecretRef, r.Spec.WriteConnectionSecretToReference)
if err != nil {
return false, "", err
return false, "", addTags, removeTags, err
}
patch, err := CreatePatch(&db, &r.Spec.ForProvider)
if err != nil {
return false, "", err
return false, "", addTags, removeTags, err
}
diff := cmp.Diff(&v1beta1.RDSInstanceParameters{}, patch, cmpopts.EquateEmpty(),
cmpopts.IgnoreTypes(&xpv1.Reference{}, &xpv1.Selector{}, []xpv1.Reference{}),
Expand All @@ -735,6 +726,9 @@ func IsUpToDate(ctx context.Context, kube client.Client, r *v1beta1.RDSInstance,
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "CloudwatchLogsExportConfiguration"),
)

addTags, removeTags = DiffTags(r.Spec.ForProvider.Tags, db.TagList)
tagsChanged := len(addTags) != 0 || len(removeTags) != 0

engineVersionChanged := !isEngineVersionUpToDate(r, db)

optionGroupChanged := !isOptionGroupUpToDate(r, db)
Expand All @@ -746,13 +740,17 @@ func IsUpToDate(ctx context.Context, kube client.Client, r *v1beta1.RDSInstance,
cloudwatchLogsExportChanged = !areSameElements(r.Spec.ForProvider.EnableCloudwatchLogsExports, db.EnabledCloudwatchLogsExports)
}

if diff == "" && !pwdChanged && !engineVersionChanged && !optionGroupChanged && !cloudwatchLogsExportChanged {
return true, "", nil
if diff == "" && !pwdChanged && !engineVersionChanged && !optionGroupChanged && !cloudwatchLogsExportChanged && !tagsChanged {
return true, "", addTags, removeTags, nil
}

diff = "Found observed difference in rds\n" + diff

return false, diff, nil
if tagsChanged {
diff += fmt.Sprintf("\nadd %d tag(s) and remove %d tag(s)", len(addTags), len(removeTags))
}

return false, diff, addTags, removeTags, nil
}

func isEngineVersionUpToDate(cr *v1beta1.RDSInstance, db rdstypes.DBInstance) bool {
Expand Down Expand Up @@ -891,3 +889,41 @@ func areSameElements(a1, a2 []string) bool {

return true
}

// DiffTags between spec and current
func DiffTags(spec []v1beta1.Tag, current []rdstypes.Tag) (addTags []rdstypes.Tag, removeTags []string) {
currentMap := make(map[string]string, len(current))
for _, t := range current {
currentMap[pointer.StringValue(t.Key)] = pointer.StringValue(t.Value)
}

specMap := make(map[string]string, len(spec))
for _, t := range spec {
key := t.Key
val := t.Value
specMap[key] = t.Value

if currentVal, exists := currentMap[key]; exists {
if currentVal != val {
addTags = append(addTags, rdstypes.Tag{
Key: pointer.ToOrNilIfZeroValue(key),
Value: pointer.ToOrNilIfZeroValue(val),
})
}
} else {
addTags = append(addTags, rdstypes.Tag{
Key: pointer.ToOrNilIfZeroValue(key),
Value: pointer.ToOrNilIfZeroValue(val),
})
}
}

for _, t := range current {
key := pointer.StringValue(t.Key)
if _, exists := specMap[key]; !exists {
removeTags = append(removeTags, key)
}
}

return addTags, removeTags
}
140 changes: 70 additions & 70 deletions pkg/clients/database/rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,75 +148,6 @@ 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{},
},
},
"IgnoreDifferentAvailabilityZoneForMultiAZ": {
args: args{
db: &rdstypes.DBInstance{
Expand Down Expand Up @@ -527,12 +458,81 @@ func TestIsUpToDate(t *testing.T) {
},
want: true,
},
"SameTags": {
args: args{
db: rdstypes.DBInstance{
TagList: []rdstypes.Tag{
{Key: ptr.To("tag1")},
{Key: ptr.To("tag2")},
{Key: ptr.To("tag3")},
},
},
r: v1beta1.RDSInstance{
Spec: v1beta1.RDSInstanceSpec{
ForProvider: v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag1"},
{Key: "tag2"},
{Key: "tag3"},
},
},
},
},
},
want: true,
},
"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")},
},
},
r: v1beta1.RDSInstance{
Spec: v1beta1.RDSInstanceSpec{
ForProvider: v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag3", Value: "val"},
{Key: "tag2", Value: "val"},
{Key: "tag1", Value: "val"},
},
},
},
},
},
want: true,
},
"DifferentTags": {
args: args{
db: rdstypes.DBInstance{
TagList: []rdstypes.Tag{
{Key: ptr.To("tag1")},
{Key: ptr.To("tag2")},
{Key: ptr.To("tag3")},
},
},
r: v1beta1.RDSInstance{
Spec: v1beta1.RDSInstanceSpec{
ForProvider: v1beta1.RDSInstanceParameters{
Tags: []v1beta1.Tag{
{Key: "tag1"},
{Key: "tag5"},
{Key: "tag6"},
},
},
},
},
},
want: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
got, _, _ := IsUpToDate(ctx, tc.args.kube, &tc.args.r, tc.args.db)
got, _, _, _, _ := IsUpToDate(ctx, tc.args.kube, &tc.args.r, tc.args.db)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
Expand Down
Loading
Loading