Skip to content

Commit 27bacf8

Browse files
UpdateTags support for VPC (#90)
Description of changes: - Implemented updateTags support for **VPC** resource using go templates and hook code. - Added integration tests for updateTags. - Removed existing `delta_pre_compare.go.tpl` file from subnet templates as we are using `sdk_file_end.go.tpl`. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f594a03 commit 27bacf8

File tree

12 files changed

+231
-5
lines changed

12 files changed

+231
-5
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-09-07T14:24:14Z"
2+
build_date: "2022-09-08T18:33:19Z"
33
build_hash: 585f06bbd6d4cc1b738acb85901e7a009bf452c7
4-
go_version: go1.18.1
4+
go_version: go1.18.3
55
version: v0.20.0
66
api_directory_checksum: 3960da50b98c36b05635d3abbfd129ae7bb48e32
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.42.0
99
generator_config_info:
10-
file_checksum: 6c21f6d996045b6c6c7c79074147f9bbcff5a73c
10+
file_checksum: 7862e66fc124ed9cba5b17483244f5619039eb23
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ resources:
451451
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
452452
sdk_read_many_post_set_output:
453453
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
454+
sdk_file_end:
455+
template_path: hooks/vpc/sdk_file_end.go.tpl
454456
VpcEndpoint:
455457
fields:
456458
PolicyDocument:

config/crd/common/bases/services.k8s.aws_fieldexports.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ spec:
6666
description: FieldExportTarget provides the values necessary to identify
6767
the output path for a field export.
6868
properties:
69+
key:
70+
description: Key overrides the default value (`<namespace>.<FieldExport-resource-name>`)
71+
for the FieldExport target
72+
type: string
6973
kind:
7074
description: FieldExportOutputType represents all types that can
7175
be produced by a field export operation

generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ resources:
451451
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
452452
sdk_read_many_post_set_output:
453453
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
454+
sdk_file_end:
455+
template_path: hooks/vpc/sdk_file_end.go.tpl
454456
VpcEndpoint:
455457
fields:
456458
PolicyDocument:

helm/crds/services.k8s.aws_fieldexports.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ spec:
6666
description: FieldExportTarget provides the values necessary to identify
6767
the output path for a field export.
6868
properties:
69+
key:
70+
description: Key overrides the default value (`<namespace>.<FieldExport-resource-name>`)
71+
for the FieldExport target
72+
type: string
6973
kind:
7074
description: FieldExportOutputType represents all types that can
7175
be produced by a field export operation

pkg/resource/vpc/hook.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ func (rm *resourceManager) customUpdate(
267267
}
268268
}
269269

270+
if delta.DifferentAt("Spec.Tags") {
271+
if err := rm.syncTags(ctx, desired, latest); err != nil {
272+
return nil, err
273+
}
274+
}
275+
270276
rm.setStatusDefaults(ko)
271277
return &resource{ko}, nil
272278
}
@@ -281,6 +287,105 @@ func applyPrimaryCIDRBlockInCreateRequest(r *resource,
281287
}
282288
}
283289

290+
// syncTags used to keep tags in sync by calling Create and Delete API's
291+
func (rm *resourceManager) syncTags(
292+
ctx context.Context,
293+
desired *resource,
294+
latest *resource,
295+
) (err error) {
296+
rlog := ackrtlog.FromContext(ctx)
297+
exit := rlog.Trace("rm.syncTags")
298+
defer func(err error) {
299+
exit(err)
300+
}(err)
301+
302+
resourceId := []*string{latest.ko.Status.VPCID}
303+
304+
toAdd, toDelete := computeTagsDelta(
305+
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
306+
)
307+
308+
if len(toDelete) > 0 {
309+
rlog.Debug("removing tags from parameter group", "tags", toDelete)
310+
_, err = rm.sdkapi.DeleteTagsWithContext(
311+
ctx,
312+
&svcsdk.DeleteTagsInput{
313+
Resources: resourceId,
314+
Tags: rm.sdkTags(toDelete),
315+
},
316+
)
317+
rm.metrics.RecordAPICall("UPDATE", "DeleteTags", err)
318+
if err != nil {
319+
return err
320+
}
321+
322+
}
323+
324+
if len(toAdd) > 0 {
325+
rlog.Debug("adding tags to parameter group", "tags", toAdd)
326+
_, err = rm.sdkapi.CreateTagsWithContext(
327+
ctx,
328+
&svcsdk.CreateTagsInput{
329+
Resources: resourceId,
330+
Tags: rm.sdkTags(toAdd),
331+
},
332+
)
333+
rm.metrics.RecordAPICall("UPDATE", "CreateTags", err)
334+
if err != nil {
335+
return err
336+
}
337+
}
338+
339+
return nil
340+
}
341+
342+
// sdkTags converts *svcapitypes.Tag array to a *svcsdk.Tag array
343+
func (rm *resourceManager) sdkTags(
344+
tags []*svcapitypes.Tag,
345+
) (sdktags []*svcsdk.Tag) {
346+
347+
for _, i := range tags {
348+
sdktag := rm.newTag(*i)
349+
sdktags = append(sdktags, sdktag)
350+
}
351+
352+
return sdktags
353+
}
354+
355+
// computeTagsDelta returns tags to be added and removed from the resource
356+
func computeTagsDelta(
357+
desired []*svcapitypes.Tag,
358+
latest []*svcapitypes.Tag,
359+
) (toAdd []*svcapitypes.Tag, toDelete []*svcapitypes.Tag) {
360+
361+
desiredTags := map[string]string{}
362+
for _, tag := range desired {
363+
desiredTags[*tag.Key] = *tag.Value
364+
}
365+
366+
latestTags := map[string]string{}
367+
for _, tag := range latest {
368+
latestTags[*tag.Key] = *tag.Value
369+
}
370+
371+
for _, tag := range desired {
372+
val, ok := latestTags[*tag.Key]
373+
if !ok || val != *tag.Value {
374+
toAdd = append(toAdd, tag)
375+
}
376+
}
377+
378+
for _, tag := range latest {
379+
_, ok := desiredTags[*tag.Key]
380+
if !ok {
381+
toDelete = append(toDelete, tag)
382+
}
383+
}
384+
385+
return toAdd, toDelete
386+
387+
}
388+
284389
// updateTagSpecificationsInCreateRequest adds
285390
// Tags defined in the Spec to CreateVpcInput.TagSpecification
286391
// and ensures the ResourceType is always set to 'vpc'

pkg/resource/vpc/sdk.go

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

templates/hooks/subnet/delta_pre_compare.go.tpl

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{ $CRD := .CRD }}
2+
{{ $SDKAPI := .SDKAPI }}
3+
4+
{{/* Generate helper methods for VPC */}}
5+
{{- range $specFieldName, $specField := $CRD.Config.Resources.Vpc.Fields }}
6+
{{- if $specField.From }}
7+
{{- $operationName := $specField.From.Operation }}
8+
{{- $operation := (index $SDKAPI.API.Operations $operationName) -}}
9+
{{- range $vpcRefName, $vpcMemberRefs := $operation.InputRef.Shape.MemberRefs -}}
10+
{{- if eq $vpcRefName "Tags" }}
11+
{{- $vpcRef := $vpcMemberRefs.Shape.MemberRef }}
12+
{{- $vpcRefName = "Tag" }}
13+
func (rm *resourceManager) new{{ $vpcRefName }}(
14+
c svcapitypes.{{ $vpcRefName }},
15+
) *svcsdk.{{ $vpcRefName }} {
16+
res := &svcsdk.{{ $vpcRefName }}{}
17+
{{ GoCodeSetSDKForStruct $CRD "" "res" $vpcRef "" "c" 1 }}
18+
return res
19+
}
20+
{{- end }}
21+
{{- end }}
22+
{{- end }}
23+
{{- end }}

test/e2e/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def simple_vpc(request):
7474
replacements["ENABLE_DNS_SUPPORT"] = data['enable_dns_support']
7575
if 'enable_dns_hostnames' in data:
7676
replacements["ENABLE_DNS_HOSTNAMES"] = data['enable_dns_hostnames']
77+
if 'tag_key' in data:
78+
replacements["TAG_KEY"] = data['tag_key']
79+
if 'tag_value' in data:
80+
replacements["TAG_VALUE"] = data['tag_value']
7781

7882
# Load VPC CR
7983
resource_data = load_ec2_resource(

0 commit comments

Comments
 (0)