Skip to content

Commit d078b74

Browse files
authored
Adds tagUpdating API for parametergroup. (#39)
Issue #, if available: Controller doesn't support customers to update tags of parameter group after creation. Description of changes: Adds API to let customers update tags of parameter group. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9c0576f commit d078b74

File tree

14 files changed

+265
-26
lines changed

14 files changed

+265
-26
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-12-08T19:08:31Z"
3-
build_hash: 12246c7da82841b351ec7a9e1f139f9338f2784b
4-
go_version: go1.17.5
5-
version: v0.21.0
2+
build_date: "2022-12-13T17:51:08Z"
3+
build_hash: 16f0e201b37a06b535370cc69e11adb934a22d33
4+
go_version: go1.19
5+
version: v0.20.1-18-g16f0e20
66
api_directory_checksum: a1e396caca4bdd1612fa7d09f0ee56f3e4976ff7
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: 24344616e89a51daae2b05f38438010f32995a4f
10+
file_checksum: c20e176b6706e0a4c8db79814d1a1672ad760c81
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ resources:
258258
sdk_read_many_post_set_output:
259259
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
260260
sdk_create_post_set_output:
261-
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
261+
template_path: hooks/parametergroup/sdk_create_post_set_output.go.tpl
262262
sdk_update_post_set_output:
263-
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
263+
template_path: hooks/parametergroup/sdk_update_post_set_output.go.tpl
264264
sdk_update_pre_build_request:
265265
template_path: hooks/parametergroup/sdk_update_pre_build_request.go.tpl
266266
renames:

apis/v1alpha1/subnet_group.go

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

apis/v1alpha1/types.go

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

generator.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ resources:
258258
sdk_read_many_post_set_output:
259259
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
260260
sdk_create_post_set_output:
261-
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
261+
template_path: hooks/parametergroup/sdk_create_post_set_output.go.tpl
262262
sdk_update_post_set_output:
263-
template_path: hooks/parametergroup/sdk_read_many_post_set_output.go.tpl
263+
template_path: hooks/parametergroup/sdk_update_post_set_output.go.tpl
264264
sdk_update_pre_build_request:
265265
template_path: hooks/parametergroup/sdk_update_pre_build_request.go.tpl
266266
renames:

helm/crds/memorydb.services.k8s.aws_subnetgroups.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ spec:
3434
type: object
3535
spec:
3636
description: "SubnetGroupSpec defines the desired state of SubnetGroup.
37-
\n Represents the output of one of the following operations: \n * CreateSubnetGroup
38-
\n * UpdateSubnetGroup \n A subnet group is a collection of subnets
37+
\n Represents the output of one of the following operations: \n - CreateSubnetGroup
38+
\n - UpdateSubnetGroup \n A subnet group is a collection of subnets
3939
(typically private) that you can designate for your clusters running
4040
in an Amazon Virtual Private Cloud (VPC) environment."
4141
properties:

helm/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ spec:
8989
- name: {{ .Values.aws.credentials.secretName }}
9090
mountPath: {{ include "aws.credentials.secret_mount_path" . }}
9191
readOnly: true
92-
{{- end }}
92+
{{- end }}
9393
securityContext:
9494
allowPrivilegeEscalation: false
9595
privileged: false

pkg/resource/parameter_group/hooks.go

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ package parameter_group
1515

1616
import (
1717
"context"
18+
1819
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
20+
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
21+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
1922

2023
svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
21-
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
24+
2225
svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
2326
)
2427

@@ -154,3 +157,123 @@ func (rm *resourceManager) resetAllParameters(
154157
}
155158
return &resource{ko}, nil
156159
}
160+
161+
// getTags gets tags from given ParameterGroup.
162+
func (rm *resourceManager) getTags(
163+
ctx context.Context,
164+
resourceARN string,
165+
) ([]*svcapitypes.Tag, error) {
166+
resp, err := rm.sdkapi.ListTagsWithContext(
167+
ctx,
168+
&svcsdk.ListTagsInput{
169+
ResourceArn: &resourceARN,
170+
},
171+
)
172+
rm.metrics.RecordAPICall("GET", "ListTags", err)
173+
if err != nil {
174+
return nil, err
175+
}
176+
tags := make([]*svcapitypes.Tag, 0, len(resp.TagList))
177+
for _, tag := range resp.TagList {
178+
tags = append(tags, &svcapitypes.Tag{
179+
Key: tag.Key,
180+
Value: tag.Value,
181+
})
182+
}
183+
return tags, nil
184+
}
185+
186+
// updateTags updates tags of given ParameterGroup to desired tags.
187+
func (rm *resourceManager) updateTags(
188+
ctx context.Context,
189+
desired *resource,
190+
latest *resource,
191+
) (err error) {
192+
rlog := ackrtlog.FromContext(ctx)
193+
exit := rlog.Trace("rm.syncTags")
194+
defer func() { exit(err) }()
195+
196+
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
197+
198+
toAdd, toDelete := computeTagsDelta(
199+
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
200+
)
201+
202+
if len(toDelete) > 0 {
203+
rlog.Debug("removing tags from parameter group", "tags", toDelete)
204+
_, err = rm.sdkapi.UntagResourceWithContext(
205+
ctx,
206+
&svcsdk.UntagResourceInput{
207+
ResourceArn: arn,
208+
TagKeys: toDelete,
209+
},
210+
)
211+
rm.metrics.RecordAPICall("UPDATE", "UntagResource", err)
212+
if err != nil {
213+
return err
214+
}
215+
}
216+
217+
if len(toAdd) > 0 {
218+
rlog.Debug("adding tags to parameter group", "tags", toAdd)
219+
_, err = rm.sdkapi.TagResourceWithContext(
220+
ctx,
221+
&svcsdk.TagResourceInput{
222+
ResourceArn: arn,
223+
Tags: sdkTagsFromResourceTags(toAdd),
224+
},
225+
)
226+
rm.metrics.RecordAPICall("UPDATE", "TagResource", err)
227+
if err != nil {
228+
return err
229+
}
230+
}
231+
232+
return nil
233+
}
234+
235+
func computeTagsDelta(
236+
desired []*svcapitypes.Tag,
237+
latest []*svcapitypes.Tag,
238+
) (added []*svcapitypes.Tag, removed []*string) {
239+
toDelete := []*string{}
240+
toAdd := []*svcapitypes.Tag{}
241+
242+
desiredTags := map[string]string{}
243+
key := ""
244+
value := ""
245+
for _, tag := range desired {
246+
if tag.Key != nil {
247+
key = *tag.Key
248+
value = ""
249+
if tag.Value != nil {
250+
value = *tag.Value
251+
}
252+
desiredTags[key] = value
253+
}
254+
}
255+
256+
for _, tag := range desired {
257+
toAdd = append(toAdd, tag)
258+
}
259+
for _, tag := range latest {
260+
_, ok := desiredTags[*tag.Key]
261+
if !ok {
262+
toDelete = append(toDelete, tag.Key)
263+
}
264+
}
265+
return toAdd, toDelete
266+
}
267+
268+
func sdkTagsFromResourceTags(
269+
rTags []*svcapitypes.Tag,
270+
) []*svcsdk.Tag {
271+
tags := make([]*svcsdk.Tag, len(rTags))
272+
for i := range rTags {
273+
tags[i] = &svcsdk.Tag{
274+
Key: rTags[i].Key,
275+
Value: rTags[i].Value,
276+
}
277+
}
278+
return tags
279+
}

pkg/resource/parameter_group/sdk.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ko, err = rm.setParameters(ctx, ko)
2+
3+
if err != nil {
4+
return nil, err
5+
}

0 commit comments

Comments
 (0)