Skip to content

Commit b490dc5

Browse files
committed
MGDAPI-5690 CRO Redis snapshots tagging logics update
1 parent 5450c62 commit b490dc5

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

pkg/providers/aws/aws_interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type ElastiCacheAPI interface {
7474
CreateReplicationGroup(ctx context.Context, input *elasticache.CreateReplicationGroupInput, optFns ...func(*elasticache.Options)) (*elasticache.CreateReplicationGroupOutput, error)
7575
CreateCacheSubnetGroup(ctx context.Context, input *elasticache.CreateCacheSubnetGroupInput, optFns ...func(*elasticache.Options)) (*elasticache.CreateCacheSubnetGroupOutput, error)
7676
DeleteReplicationGroup(ctx context.Context, input *elasticache.DeleteReplicationGroupInput, optFns ...func(*elasticache.Options)) (*elasticache.DeleteReplicationGroupOutput, error)
77+
ListTagsForResource(ctx context.Context, input *elasticache.ListTagsForResourceInput, optFns ...func(*elasticache.Options)) (*elasticache.ListTagsForResourceOutput, error)
7778
}
7879

7980
type S3API interface {

pkg/providers/aws/aws_mock.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ func (m *mock_ElasticacheClient) CreateReplicationGroup(ctx context.Context, inp
465465
return args.Get(0).(*elasticache.CreateReplicationGroupOutput), args.Error(1)
466466
}
467467

468+
func (m *mock_ElasticacheClient) ListTagsForResource(ctx context.Context, input *elasticache.ListTagsForResourceInput, optFns ...func(*elasticache.Options)) (*elasticache.ListTagsForResourceOutput, error) {
469+
args := m.Called(ctx, input, optFns)
470+
return args.Get(0).(*elasticache.ListTagsForResourceOutput), args.Error(1)
471+
}
472+
468473
// S3 Mock
469474
type mock_S3Client struct {
470475
mock.Mock

pkg/providers/aws/aws_wrapper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ func (r *RealElasticacheClient) CreateReplicationGroup(ctx context.Context, inpu
274274
return r.Client.CreateReplicationGroup(ctx, input, optFns...)
275275
}
276276

277+
func (r *RealElasticacheClient) ListTagsForResource(ctx context.Context, input *elasticache.ListTagsForResourceInput, optFns ...func(*elasticache.Options)) (*elasticache.ListTagsForResourceOutput, error) {
278+
return r.Client.ListTagsForResource(ctx, input, optFns...)
279+
}
280+
277281
// ---------- S3 ----------
278282
func NewS3Client(cfg aws.Config) S3API {
279283
return &RealS3Client{

pkg/providers/aws/credentials.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var (
8181
"elasticache:ModifyCacheSubnetGroup",
8282
"elasticache:DeleteCacheSubnetGroup",
8383
"elasticache:ModifyReplicationGroup",
84+
"elasticache:ListTagsForResource",
8485
"rds:DescribeDBInstances",
8586
"rds:CreateDBInstance",
8687
"rds:DeleteDBInstance",

pkg/providers/aws/provider_redis.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,24 @@ func (p *RedisProvider) TagElasticacheNode(ctx context.Context, elasticacheClien
455455
// need arn in the following format arn:aws:elasticache:us-east-1:1234567890:cluster:my-mem-cluster
456456
arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, *id.Account, *cache.CacheClusterId)
457457

458+
// Get default tags
458459
cacheTags, clusterID, err := p.getDefaultElasticacheTags(ctx, r)
459460
if err != nil {
460461
msg := "Failed to build default tags"
461462
return croType.StatusMessage(msg), errorUtil.Wrapf(err, msg)
462463
}
463464

464-
// add tags
465+
// Filter out already applied tags
466+
filteredTags, err := filterAlreadyAppliedTags(ctx, elasticacheClient, arn, cacheTags)
467+
if err != nil {
468+
msg := "Failed to filter already applied tags"
469+
return croType.StatusMessage(msg), errorUtil.Wrapf(err, msg)
470+
}
471+
472+
// Add only new/changed tags
465473
_, err = elasticacheClient.AddTagsToResource(ctx, &elasticache.AddTagsToResourceInput{
466474
ResourceName: aws.String(arn),
467-
Tags: cacheTags,
475+
Tags: filteredTags,
468476
})
469477
if err != nil {
470478
msg := "failed to add tags to aws elasticache :"
@@ -1278,3 +1286,32 @@ func validServiceUpdateStates(status string) bool {
12781286
}
12791287
return false
12801288
}
1289+
1290+
// filterAlreadyAppliedTags removes tags from `desired` that already exist (same key and value) on the resource.
1291+
func filterAlreadyAppliedTags(ctx context.Context, client ElastiCacheAPI, resourceARN string, desired []elasticachetypes.Tag) ([]elasticachetypes.Tag, error) {
1292+
// List current tags on the resource
1293+
resp, err := client.ListTagsForResource(ctx, &elasticache.ListTagsForResourceInput{
1294+
ResourceName: aws.String(resourceARN),
1295+
})
1296+
if err != nil {
1297+
return nil, err
1298+
}
1299+
1300+
// Build a map of current tags
1301+
currentTags := make(map[string]string)
1302+
for _, tag := range resp.TagList {
1303+
currentTags[*tag.Key] = *tag.Value
1304+
}
1305+
1306+
// Filter out tags that are already applied with same value
1307+
var filtered []elasticachetypes.Tag
1308+
for _, tag := range desired {
1309+
val, exists := currentTags[*tag.Key]
1310+
if !exists || val != *tag.Value {
1311+
// Either tag doesn't exist or value is different — keep it
1312+
filtered = append(filtered, tag)
1313+
}
1314+
}
1315+
1316+
return filtered, nil
1317+
}

0 commit comments

Comments
 (0)