Skip to content

Commit 68d8ede

Browse files
committed
tags
1 parent 7cf5db0 commit 68d8ede

File tree

10 files changed

+179
-8
lines changed

10 files changed

+179
-8
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2025-04-07T06:55:50Z"
3-
build_hash: bf1bf427cd755bcbc37414eaea9e1d66465bc422
2+
build_date: "2025-04-07T20:33:31Z"
3+
build_hash: 66ad2d04c2b520425a0682aae523d45cad967248
44
go_version: go1.24.1
5-
version: v0.44.0-2-gbf1bf42-dirty
5+
version: v0.44.0-2-g66ad2d0
66
api_directory_checksum: f1f3604161839adf828e8ecdbca457e930c7efa9
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.32.6
99
generator_config_info:
10-
file_checksum: a81c543fa4a387b579e8f479bc5be3c3d8a0bd47
10+
file_checksum: aecdd17e1a8362d83aee855cffce7d2d5e39eaf2
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,15 @@ resources:
236236
input_fields:
237237
ApiKey: Id
238238
hooks:
239+
delta_pre_compare:
240+
code: compareTags(delta, a, b)
239241
sdk_update_post_build_request:
240242
template_path: hooks/api_key/sdk_update_post_build_request.go.tpl
241243
sdk_read_one_post_request:
242244
template_path: hooks/api_key/sdk_read_one_post_request.go.tpl
243245
sdk_create_post_request:
244246
template_path: hooks/api_key/sdk_update_post_request.go.tpl
245247
sdk_update_post_request:
246-
template_path: hooks/api_key/sdk_update_post_request.go.tpl
248+
template_path: hooks/api_key/sdk_update_post_request.go.tpl
249+
sdk_read_one_post_set_output:
250+
template_path: hooks/api_key/sdk_read_one_post_set_output.go.tpl

generator.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,15 @@ resources:
236236
input_fields:
237237
ApiKey: Id
238238
hooks:
239+
delta_pre_compare:
240+
code: compareTags(delta, a, b)
239241
sdk_update_post_build_request:
240242
template_path: hooks/api_key/sdk_update_post_build_request.go.tpl
241243
sdk_read_one_post_request:
242244
template_path: hooks/api_key/sdk_read_one_post_request.go.tpl
243245
sdk_create_post_request:
244246
template_path: hooks/api_key/sdk_update_post_request.go.tpl
245247
sdk_update_post_request:
246-
template_path: hooks/api_key/sdk_update_post_request.go.tpl
248+
template_path: hooks/api_key/sdk_update_post_request.go.tpl
249+
sdk_read_one_post_set_output:
250+
template_path: hooks/api_key/sdk_read_one_post_set_output.go.tpl

pkg/resource/api_key/delta.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/api_key/hooks.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
package api_key
1515

1616
import (
17+
"context"
1718
"fmt"
1819
"strings"
1920

2021
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
22+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2123
"github.com/aws/aws-sdk-go-v2/aws"
2224
svcsdk "github.com/aws/aws-sdk-go-v2/service/apigateway"
2325
svcsdktypes "github.com/aws/aws-sdk-go-v2/service/apigateway/types"
@@ -43,6 +45,9 @@ func updateApiKeyInput(desired *resource, input *svcsdk.UpdateApiKeyInput, delta
4345
patchSet.Replace("/customerId", desiredSpec.CustomerID)
4446
}
4547

48+
// Tags are managed through separate TagResource/UntagResource APIs,
49+
// not through patch operations in UpdateApiKey
50+
4651
// Handle StageKeys with add/remove operations
4752
if delta.DifferentAt("Spec.StageKeys") && desiredSpec.StageKeys != nil {
4853
// Convert StageKey objects to strings in the format "restApiId/stageName"
@@ -65,3 +70,127 @@ func updateApiKeyInput(desired *resource, input *svcsdk.UpdateApiKeyInput, delta
6570
patchOps := patchSet.GetPatchOperations()
6671
input.PatchOperations = append(patchOps, stageKeyPatches...)
6772
}
73+
74+
// syncTags keeps tags in sync by calling TagResource and UntagResource APIs
75+
func (rm *resourceManager) syncTags(
76+
ctx context.Context,
77+
desired *resource,
78+
latest *resource,
79+
) (err error) {
80+
rlog := ackrtlog.FromContext(ctx)
81+
exit := rlog.Trace("rm.syncTags")
82+
defer func() {
83+
exit(err)
84+
}()
85+
86+
if latest.ko.Status.ACKResourceMetadata == nil || latest.ko.Status.ACKResourceMetadata.ARN == nil {
87+
return fmt.Errorf("resource ARN is nil")
88+
}
89+
90+
resourceARN := aws.String(string(*latest.ko.Status.ACKResourceMetadata.ARN))
91+
92+
desiredTagsMap := desired.ko.Spec.Tags
93+
latestTagsMap := latest.ko.Spec.Tags
94+
95+
desiredTags, _ := convertToOrderedACKTags(desiredTagsMap)
96+
latestTags, _ := convertToOrderedACKTags(latestTagsMap)
97+
98+
added, updated, removed := ackcompare.GetTagsDifference(latestTags, desiredTags)
99+
100+
// Combine added and updated tags
101+
toAdd := make(map[string]string)
102+
for k, v := range added {
103+
toAdd[k] = v
104+
}
105+
for k, v := range updated {
106+
toAdd[k] = v
107+
}
108+
109+
var toRemoveTagKeys []string
110+
for k := range removed {
111+
toRemoveTagKeys = append(toRemoveTagKeys, k)
112+
}
113+
114+
// Remove tags using UntagResource API:
115+
if len(toRemoveTagKeys) > 0 {
116+
rlog.Debug("removing tags from resource", "tags", toRemoveTagKeys)
117+
_, err = rm.sdkapi.UntagResource(
118+
ctx,
119+
&svcsdk.UntagResourceInput{
120+
ResourceArn: resourceARN,
121+
TagKeys: toRemoveTagKeys,
122+
},
123+
)
124+
rm.metrics.RecordAPICall("UPDATE", "UntagResource", err)
125+
if err != nil {
126+
return err
127+
}
128+
}
129+
130+
// Add tags using TagResource API
131+
if len(toAdd) > 0 {
132+
rlog.Debug("adding tags to resource", "tags", toAdd)
133+
_, err = rm.sdkapi.TagResource(
134+
ctx,
135+
&svcsdk.TagResourceInput{
136+
ResourceArn: resourceARN,
137+
Tags: toAdd,
138+
},
139+
)
140+
rm.metrics.RecordAPICall("UPDATE", "TagResource", err)
141+
if err != nil {
142+
return err
143+
}
144+
}
145+
146+
return nil
147+
}
148+
149+
func compareTags(
150+
delta *ackcompare.Delta,
151+
a *resource,
152+
b *resource,
153+
) {
154+
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
155+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
156+
} else if len(a.ko.Spec.Tags) > 0 {
157+
// Convert map[string]*string to acktags.Tags for GetTagsDifference
158+
aTagsConverted, _ := convertToOrderedACKTags(a.ko.Spec.Tags)
159+
bTagsConverted, _ := convertToOrderedACKTags(b.ko.Spec.Tags)
160+
161+
added, updated, removed := ackcompare.GetTagsDifference(bTagsConverted, aTagsConverted)
162+
if len(added) != 0 || len(updated) != 0 || len(removed) != 0 {
163+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
164+
}
165+
}
166+
}
167+
168+
// fetchCurrentTags returns the current tags for the resource
169+
// using the GetTags API: GET /tags/resource_arn
170+
func (rm *resourceManager) fetchCurrentTags(
171+
ctx context.Context,
172+
resourceARN *string,
173+
) (map[string]string, error) {
174+
output, err := rm.sdkapi.GetTags(
175+
ctx,
176+
&svcsdk.GetTagsInput{
177+
ResourceArn: resourceARN,
178+
},
179+
)
180+
181+
if err != nil {
182+
return nil, err
183+
}
184+
185+
return output.Tags, nil
186+
}
187+
188+
// CustomResourcesDifference helps return differences in custom resources
189+
func (rm *resourceManager) CustomResourcesDifference(
190+
a *resource,
191+
b *resource,
192+
) *ackcompare.Delta {
193+
delta := ackcompare.NewDelta()
194+
compareTags(delta, a, b)
195+
return delta
196+
}

pkg/resource/api_key/sdk.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/integration/sdk.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/rest_api/sdk.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// fetch tags
3+
if r.ko.Status.ID != nil {
4+
resourceARN := string(*r.ko.Status.ACKResourceMetadata.ARN)
5+
tags, err := rm.fetchCurrentTags(ctx, &resourceARN)
6+
if err != nil {
7+
return nil, err
8+
}
9+
r.ko.Spec.Tags = aws.StringMap(tags)
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Handle tag updates separately through TagResource/UntagResource APIs
2+
if delta.DifferentAt("Spec.Tags") {
3+
err = rm.syncTags(ctx, desired, latest)
4+
if err != nil {
5+
return nil, err
6+
}
7+
}
8+
9+
// If only tags were different, return early
10+
if !delta.DifferentExcept("Spec.Tags") {
11+
return desired, nil
12+
}

0 commit comments

Comments
 (0)