Skip to content

Commit 48b29c4

Browse files
Update tags support for ElasticIpAddress resource (#98)
Description of changes: - Included `update_operation` in `generator.yaml` file. - Implemented update tags support for **ElasticIpAddress** resource. - Added e2e tests for the update tags. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c26d579 commit 48b29c4

File tree

12 files changed

+267
-20
lines changed

12 files changed

+267
-20
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-09-15T15:45:52Z"
2+
build_date: "2022-09-15T16:27:05Z"
33
build_hash: 1da44f4be322eea156ed23a86e33c29da5cede9c
44
go_version: go1.18.3
55
version: v0.20.1-2-g1da44f4
66
api_directory_checksum: 127aa0f51fbcdde596b8f42f93bcdf3b6dee8488
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: 92993035f8ed34335b87f5ece6405b3d6d8d61eb
10+
file_checksum: 42f9161f3b7c319d753fd54b681eecd9916d67f6
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ resources:
186186
sdk_file_end:
187187
template_path: hooks/dhcp_options/sdk_file_end.go.tpl
188188
update_operation:
189-
custom_method_name: customUpdateDhcpOptions
189+
custom_method_name: customUpdateDHCPOptions
190190
Instance:
191191
fields:
192192
HibernationOptions:
@@ -236,6 +236,10 @@ resources:
236236
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
237237
sdk_read_many_post_build_request:
238238
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
239+
sdk_file_end:
240+
template_path: hooks/elastic_ip_address/sdk_file_end.go.tpl
241+
update_operation:
242+
custom_method_name: customUpdateElasticIP
239243
InternetGateway:
240244
fields:
241245
Tags:
@@ -502,5 +506,5 @@ resources:
502506
sdk_file_end:
503507
template_path: hooks/vpc_endpoint/sdk_file_end.go.tpl
504508
update_operation:
505-
custom_method_name: customUpdateVpcEndpoint
509+
custom_method_name: customUpdateVPCEndpoint
506510

generator.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ resources:
186186
sdk_file_end:
187187
template_path: hooks/dhcp_options/sdk_file_end.go.tpl
188188
update_operation:
189-
custom_method_name: customUpdateDhcpOptions
189+
custom_method_name: customUpdateDHCPOptions
190190
Instance:
191191
fields:
192192
HibernationOptions:
@@ -236,6 +236,10 @@ resources:
236236
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
237237
sdk_read_many_post_build_request:
238238
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
239+
sdk_file_end:
240+
template_path: hooks/elastic_ip_address/sdk_file_end.go.tpl
241+
update_operation:
242+
custom_method_name: customUpdateElasticIP
239243
InternetGateway:
240244
fields:
241245
Tags:
@@ -502,5 +506,5 @@ resources:
502506
sdk_file_end:
503507
template_path: hooks/vpc_endpoint/sdk_file_end.go.tpl
504508
update_operation:
505-
custom_method_name: customUpdateVpcEndpoint
509+
custom_method_name: customUpdateVPCEndpoint
506510

pkg/resource/dhcp_options/hook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
2323
)
2424

25-
func (rm *resourceManager) customUpdateDhcpOptions(
25+
func (rm *resourceManager) customUpdateDHCPOptions(
2626
ctx context.Context,
2727
desired *resource,
2828
latest *resource,
2929
delta *ackcompare.Delta,
3030
) (updated *resource, err error) {
3131
rlog := ackrtlog.FromContext(ctx)
32-
exit := rlog.Trace("rm.customUpdateDhcpOptions")
32+
exit := rlog.Trace("rm.customUpdateDHCPOptions")
3333
defer exit(err)
3434

3535
// Merge in the information we read from the API call above to the copy of
@@ -65,7 +65,7 @@ func (rm *resourceManager) syncTags(
6565
)
6666

6767
if len(toDelete) > 0 {
68-
rlog.Debug("removing tags from DhcpOptions resource", "tags", toDelete)
68+
rlog.Debug("removing tags from DHCPOptions resource", "tags", toDelete)
6969
_, err = rm.sdkapi.DeleteTagsWithContext(
7070
ctx,
7171
&svcsdk.DeleteTagsInput{
@@ -81,7 +81,7 @@ func (rm *resourceManager) syncTags(
8181
}
8282

8383
if len(toAdd) > 0 {
84-
rlog.Debug("adding tags to DhcpOptions resource", "tags", toAdd)
84+
rlog.Debug("adding tags to DHCPOptions resource", "tags", toAdd)
8585
_, err = rm.sdkapi.CreateTagsWithContext(
8686
ctx,
8787
&svcsdk.CreateTagsInput{

pkg/resource/dhcp_options/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/elastic_ip_address/hook.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,137 @@
1414
package elastic_ip_address
1515

1616
import (
17+
"context"
18+
19+
svcapitypes "github.com/aws-controllers-k8s/ec2-controller/apis/v1alpha1"
20+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
21+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
1722
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
1823
)
1924

25+
func (rm *resourceManager) customUpdateElasticIP(
26+
ctx context.Context,
27+
desired *resource,
28+
latest *resource,
29+
delta *ackcompare.Delta,
30+
) (updated *resource, err error) {
31+
rlog := ackrtlog.FromContext(ctx)
32+
exit := rlog.Trace("rm.customUpdateElasticIP")
33+
defer exit(err)
34+
35+
// Merge in the information we read from the API call above to the copy of
36+
// the original Kubernetes object we passed to the function
37+
ko := desired.ko.DeepCopy()
38+
39+
if delta.DifferentAt("Spec.Tags") {
40+
if err := rm.syncTags(ctx, desired, latest); err != nil {
41+
return nil, err
42+
}
43+
}
44+
45+
rm.setStatusDefaults(ko)
46+
return &resource{ko}, nil
47+
}
48+
49+
// syncTags used to keep tags in sync by calling Create and Delete API's
50+
func (rm *resourceManager) syncTags(
51+
ctx context.Context,
52+
desired *resource,
53+
latest *resource,
54+
) (err error) {
55+
rlog := ackrtlog.FromContext(ctx)
56+
exit := rlog.Trace("rm.syncTags")
57+
defer func(err error) {
58+
exit(err)
59+
}(err)
60+
61+
resourceId := []*string{latest.ko.Status.AllocationID}
62+
63+
toAdd, toDelete := computeTagsDelta(
64+
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
65+
)
66+
67+
if len(toDelete) > 0 {
68+
rlog.Debug("removing tags from elasticip resource", "tags", toDelete)
69+
_, err = rm.sdkapi.DeleteTagsWithContext(
70+
ctx,
71+
&svcsdk.DeleteTagsInput{
72+
Resources: resourceId,
73+
Tags: rm.sdkTags(toDelete),
74+
},
75+
)
76+
rm.metrics.RecordAPICall("UPDATE", "DeleteTags", err)
77+
if err != nil {
78+
return err
79+
}
80+
81+
}
82+
83+
if len(toAdd) > 0 {
84+
rlog.Debug("adding tags to elasticip resource", "tags", toAdd)
85+
_, err = rm.sdkapi.CreateTagsWithContext(
86+
ctx,
87+
&svcsdk.CreateTagsInput{
88+
Resources: resourceId,
89+
Tags: rm.sdkTags(toAdd),
90+
},
91+
)
92+
rm.metrics.RecordAPICall("UPDATE", "CreateTags", err)
93+
if err != nil {
94+
return err
95+
}
96+
}
97+
98+
return nil
99+
}
100+
101+
// sdkTags converts *svcapitypes.Tag array to a *svcsdk.Tag array
102+
func (rm *resourceManager) sdkTags(
103+
tags []*svcapitypes.Tag,
104+
) (sdktags []*svcsdk.Tag) {
105+
106+
for _, i := range tags {
107+
sdktag := rm.newTag(*i)
108+
sdktags = append(sdktags, sdktag)
109+
}
110+
111+
return sdktags
112+
}
113+
114+
// computeTagsDelta returns tags to be added and removed from the resource
115+
func computeTagsDelta(
116+
desired []*svcapitypes.Tag,
117+
latest []*svcapitypes.Tag,
118+
) (toAdd []*svcapitypes.Tag, toDelete []*svcapitypes.Tag) {
119+
120+
desiredTags := map[string]string{}
121+
for _, tag := range desired {
122+
desiredTags[*tag.Key] = *tag.Value
123+
}
124+
125+
latestTags := map[string]string{}
126+
for _, tag := range latest {
127+
latestTags[*tag.Key] = *tag.Value
128+
}
129+
130+
for _, tag := range desired {
131+
val, ok := latestTags[*tag.Key]
132+
if !ok || val != *tag.Value {
133+
toAdd = append(toAdd, tag)
134+
}
135+
}
136+
137+
for _, tag := range latest {
138+
_, ok := desiredTags[*tag.Key]
139+
if !ok {
140+
toDelete = append(toDelete, tag)
141+
}
142+
}
143+
144+
return toAdd, toDelete
145+
146+
}
147+
20148
// updateTagSpecificationsInCreateRequest adds
21149
// Tags defined in the Spec to AllocateAddressInput.TagSpecification
22150
// and ensures the ResourceType is always set to 'elastic-ip'

pkg/resource/elastic_ip_address/sdk.go

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

pkg/resource/vpc_endpoint/hooks.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func addIDToDeleteRequest(r *resource,
3535
return nil
3636
}
3737

38-
func (rm *resourceManager) customUpdateVpcEndpoint(
38+
func (rm *resourceManager) customUpdateVPCEndpoint(
3939
ctx context.Context,
4040
desired *resource,
4141
latest *resource,
4242
delta *ackcompare.Delta,
4343
) (updated *resource, err error) {
4444
rlog := ackrtlog.FromContext(ctx)
45-
exit := rlog.Trace("rm.customUpdateVpcEndpoint")
45+
exit := rlog.Trace("rm.customUpdateVPCEndpoint")
4646
defer exit(err)
4747

4848
// Merge in the information we read from the API call above to the copy of
@@ -78,7 +78,7 @@ func (rm *resourceManager) syncTags(
7878
)
7979

8080
if len(toDelete) > 0 {
81-
rlog.Debug("removing tags from vpcEndpoint resource", "tags", toDelete)
81+
rlog.Debug("removing tags from VPCEndpoint resource", "tags", toDelete)
8282
_, err = rm.sdkapi.DeleteTagsWithContext(
8383
ctx,
8484
&svcsdk.DeleteTagsInput{
@@ -94,7 +94,7 @@ func (rm *resourceManager) syncTags(
9494
}
9595

9696
if len(toAdd) > 0 {
97-
rlog.Debug("adding tags to vpcEndpoint resource", "tags", toAdd)
97+
rlog.Debug("adding tags to VPCEndpoint resource", "tags", toAdd)
9898
_, err = rm.sdkapi.CreateTagsWithContext(
9999
ctx,
100100
&svcsdk.CreateTagsInput{

pkg/resource/vpc_endpoint/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: 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 ElasticIP */}}
5+
{{- range $specFieldName, $specField := $CRD.Config.Resources.ElasticIPAddress.Fields }}
6+
{{- if $specField.From }}
7+
{{- $operationName := $specField.From.Operation }}
8+
{{- $operation := (index $SDKAPI.API.Operations $operationName) -}}
9+
{{- range $eipRefName, $eipMemberRefs := $operation.InputRef.Shape.MemberRefs -}}
10+
{{- if eq $eipRefName "Tags" }}
11+
{{- $eipRef := $eipMemberRefs.Shape.MemberRef }}
12+
{{- $eipRefName = "Tag" }}
13+
func (rm *resourceManager) new{{ $eipRefName }}(
14+
c svcapitypes.{{ $eipRefName }},
15+
) *svcsdk.{{ $eipRefName }} {
16+
res := &svcsdk.{{ $eipRefName }}{}
17+
{{ GoCodeSetSDKForStruct $CRD "" "res" $eipRef "" "c" 1 }}
18+
return res
19+
}
20+
{{- end }}
21+
{{- end }}
22+
{{- end }}
23+
{{- end }}

0 commit comments

Comments
 (0)