Skip to content

Commit 81733b3

Browse files
authored
Update computeTagsDelta function (#113)
This patch updates the `computeTagsDelta` function for `db_cluster`, `db_parameter_group`, `db_subnet_group` and `db_cluster_parameter_group` The previous implementation always returned a non-empty `toAdd` array which causes the controller to make unnecessary `TagResource` API Calls. Signed-off-by: Amine Hilaly <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 7099abf commit 81733b3

File tree

7 files changed

+188
-233
lines changed

7 files changed

+188
-233
lines changed

pkg/resource/db_cluster/hooks.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2626
svcsdk "github.com/aws/aws-sdk-go/service/rds"
2727
corev1 "k8s.io/api/core/v1"
28+
29+
"github.com/aws-controllers-k8s/rds-controller/pkg/util"
2830
)
2931

3032
// NOTE(jaypipes): The below list is derived from looking at the RDS control
@@ -182,7 +184,7 @@ func (rm *resourceManager) syncTags(
182184

183185
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
184186

185-
toAdd, toDelete := computeTagsDelta(
187+
toAdd, toDelete := util.ComputeTagsDelta(
186188
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
187189
)
188190

@@ -257,48 +259,12 @@ func compareTags(
257259
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
258260
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
259261
} else if len(a.ko.Spec.Tags) > 0 {
260-
if !equalTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
262+
if !util.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
261263
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
262264
}
263265
}
264266
}
265267

266-
// equalTags returns true if two Tag arrays are equal regardless of the order
267-
// of their elements.
268-
func equalTags(
269-
a []*svcapitypes.Tag,
270-
b []*svcapitypes.Tag,
271-
) bool {
272-
added, removed := computeTagsDelta(a, b)
273-
return len(added) == 0 && len(removed) == 0
274-
}
275-
276-
// computeTagsDelta compares two Tag arrays and returns the tags to add and the
277-
// tag keys to delete
278-
func computeTagsDelta(
279-
desired []*svcapitypes.Tag,
280-
latest []*svcapitypes.Tag,
281-
) (added []*svcapitypes.Tag, removed []*string) {
282-
toDelete := []*string{}
283-
toAdd := []*svcapitypes.Tag{}
284-
285-
desiredTags := map[string]string{}
286-
for _, tag := range desired {
287-
desiredTags[*tag.Key] = *tag.Value
288-
}
289-
290-
for _, tag := range desired {
291-
toAdd = append(toAdd, tag)
292-
}
293-
for _, tag := range latest {
294-
_, ok := desiredTags[*tag.Key]
295-
if !ok {
296-
toDelete = append(toDelete, tag.Key)
297-
}
298-
}
299-
return toAdd, toDelete
300-
}
301-
302268
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag
303269
// array.
304270
func sdkTagsFromResourceTags(
@@ -314,13 +280,6 @@ func sdkTagsFromResourceTags(
314280
return tags
315281
}
316282

317-
func equalStrings(a, b *string) bool {
318-
if a == nil {
319-
return b == nil || *b == ""
320-
}
321-
return (*a == "" && b == nil) || *a == *b
322-
}
323-
324283
// function to create restoreDbClusterFromSnapshot payload and call restoreDbClusterFromSnapshot API
325284
func (rm *resourceManager) restoreDbClusterFromSnapshot(
326285
ctx context.Context,

pkg/resource/db_cluster_parameter_group/hooks.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
svcapitypes "github.com/aws-controllers-k8s/rds-controller/apis/v1alpha1"
2323
svcsdk "github.com/aws/aws-sdk-go/service/rds"
24+
25+
"github.com/aws-controllers-k8s/rds-controller/pkg/util"
2426
)
2527

2628
// customUpdate is required to fix
@@ -76,7 +78,7 @@ func (rm *resourceManager) syncTags(
7678

7779
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
7880

79-
toAdd, toDelete := computeTagsDelta(
81+
toAdd, toDelete := util.ComputeTagsDelta(
8082
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
8183
)
8284

@@ -151,48 +153,12 @@ func compareTags(
151153
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
152154
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
153155
} else if len(a.ko.Spec.Tags) > 0 {
154-
if !equalTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
156+
if !util.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
155157
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
156158
}
157159
}
158160
}
159161

160-
// equalTags returns true if two Tag arrays are equal regardless of the order
161-
// of their elements.
162-
func equalTags(
163-
a []*svcapitypes.Tag,
164-
b []*svcapitypes.Tag,
165-
) bool {
166-
added, removed := computeTagsDelta(a, b)
167-
return len(added) == 0 && len(removed) == 0
168-
}
169-
170-
// computeTagsDelta compares two Tag arrays and returns the tags to add and the
171-
// tag keys to delete
172-
func computeTagsDelta(
173-
desired []*svcapitypes.Tag,
174-
latest []*svcapitypes.Tag,
175-
) ([]*svcapitypes.Tag, []*string) {
176-
toDelete := []*string{}
177-
toAdd := []*svcapitypes.Tag{}
178-
179-
desiredTags := map[string]string{}
180-
for _, tag := range desired {
181-
desiredTags[*tag.Key] = *tag.Value
182-
}
183-
184-
for _, tag := range desired {
185-
toAdd = append(toAdd, tag)
186-
}
187-
for _, tag := range latest {
188-
_, ok := desiredTags[*tag.Key]
189-
if !ok {
190-
toDelete = append(toDelete, tag.Key)
191-
}
192-
}
193-
return toAdd, toDelete
194-
}
195-
196162
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag
197163
// array.
198164
func sdkTagsFromResourceTags(
@@ -207,10 +173,3 @@ func sdkTagsFromResourceTags(
207173
}
208174
return tags
209175
}
210-
211-
func equalStrings(a, b *string) bool {
212-
if a == nil {
213-
return b == nil || *b == ""
214-
}
215-
return (*a == "" && b == nil) || *a == *b
216-
}

pkg/resource/db_instance/hooks.go

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2727
svcsdk "github.com/aws/aws-sdk-go/service/rds"
2828
corev1 "k8s.io/api/core/v1"
29+
30+
"github.com/aws-controllers-k8s/rds-controller/pkg/util"
2931
)
3032

3133
// NOTE(jaypipes): The below list is derived from looking at the RDS control
@@ -426,7 +428,7 @@ func (rm *resourceManager) syncTags(
426428

427429
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
428430

429-
toAdd, toDelete := computeTagsDelta(
431+
toAdd, toDelete := util.ComputeTagsDelta(
430432
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
431433
)
432434

@@ -501,56 +503,12 @@ func compareTags(
501503
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
502504
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
503505
} else if len(a.ko.Spec.Tags) > 0 {
504-
if !equalTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
506+
if !util.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
505507
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
506508
}
507509
}
508510
}
509511

510-
// equalTags returns true if two Tag arrays are equal regardless of the order
511-
// of their elements.
512-
func equalTags(
513-
a []*svcapitypes.Tag,
514-
b []*svcapitypes.Tag,
515-
) bool {
516-
added, removed := computeTagsDelta(a, b)
517-
return len(added) == 0 && len(removed) == 0
518-
}
519-
520-
// computeTagsDelta compares two Tag arrays and returns the tags to add and the
521-
// tag keys to delete
522-
func computeTagsDelta(
523-
desired []*svcapitypes.Tag,
524-
latest []*svcapitypes.Tag,
525-
) (added []*svcapitypes.Tag, removed []*string) {
526-
toDelete := []*string{}
527-
toAdd := []*svcapitypes.Tag{}
528-
529-
desiredTags := map[string]string{}
530-
for _, tag := range desired {
531-
desiredTags[*tag.Key] = *tag.Value
532-
}
533-
534-
latestTags := map[string]string{}
535-
for _, tag := range latest {
536-
latestTags[*tag.Key] = *tag.Value
537-
}
538-
539-
for _, tag := range desired {
540-
val, ok := latestTags[*tag.Key]
541-
if !ok || val != *tag.Value {
542-
toAdd = append(toAdd, tag)
543-
}
544-
}
545-
for _, tag := range latest {
546-
_, ok := desiredTags[*tag.Key]
547-
if !ok {
548-
toDelete = append(toDelete, tag.Key)
549-
}
550-
}
551-
return toAdd, toDelete
552-
}
553-
554512
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag
555513
// array.
556514
func sdkTagsFromResourceTags(
@@ -565,10 +523,3 @@ func sdkTagsFromResourceTags(
565523
}
566524
return tags
567525
}
568-
569-
func equalStrings(a, b *string) bool {
570-
if a == nil {
571-
return b == nil || *b == ""
572-
}
573-
return (*a == "" && b == nil) || *a == *b
574-
}

pkg/resource/db_parameter_group/hooks.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
svcapitypes "github.com/aws-controllers-k8s/rds-controller/apis/v1alpha1"
2323
svcsdk "github.com/aws/aws-sdk-go/service/rds"
24+
25+
"github.com/aws-controllers-k8s/rds-controller/pkg/util"
2426
)
2527

2628
// customUpdate is required to fix
@@ -76,7 +78,7 @@ func (rm *resourceManager) syncTags(
7678

7779
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
7880

79-
toAdd, toDelete := computeTagsDelta(
81+
toAdd, toDelete := util.ComputeTagsDelta(
8082
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
8183
)
8284

@@ -151,48 +153,12 @@ func compareTags(
151153
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
152154
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
153155
} else if len(a.ko.Spec.Tags) > 0 {
154-
if !equalTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
156+
if !util.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
155157
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
156158
}
157159
}
158160
}
159161

160-
// equalTags returns true if two Tag arrays are equal regardless of the order
161-
// of their elements.
162-
func equalTags(
163-
a []*svcapitypes.Tag,
164-
b []*svcapitypes.Tag,
165-
) bool {
166-
added, removed := computeTagsDelta(a, b)
167-
return len(added) == 0 && len(removed) == 0
168-
}
169-
170-
// computeTagsDelta compares two Tag arrays and returns the tags to add and the
171-
// tag keys to delete
172-
func computeTagsDelta(
173-
desired []*svcapitypes.Tag,
174-
latest []*svcapitypes.Tag,
175-
) ([]*svcapitypes.Tag, []*string) {
176-
toDelete := []*string{}
177-
toAdd := []*svcapitypes.Tag{}
178-
179-
desiredTags := map[string]string{}
180-
for _, tag := range desired {
181-
desiredTags[*tag.Key] = *tag.Value
182-
}
183-
184-
for _, tag := range desired {
185-
toAdd = append(toAdd, tag)
186-
}
187-
for _, tag := range latest {
188-
_, ok := desiredTags[*tag.Key]
189-
if !ok {
190-
toDelete = append(toDelete, tag.Key)
191-
}
192-
}
193-
return toAdd, toDelete
194-
}
195-
196162
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag
197163
// array.
198164
func sdkTagsFromResourceTags(
@@ -207,10 +173,3 @@ func sdkTagsFromResourceTags(
207173
}
208174
return tags
209175
}
210-
211-
func equalStrings(a, b *string) bool {
212-
if a == nil {
213-
return b == nil || *b == ""
214-
}
215-
return (*a == "" && b == nil) || *a == *b
216-
}

0 commit comments

Comments
 (0)