Skip to content

Commit 334b246

Browse files
authored
Fix hook code for Tags (#109)
Issue #, if available: aws-controllers-k8s/community#1493 Description of changes: * set `input.TagSpecifications` to non-nil in `updateTagSpecificationsInCreateRequest` iff there are desired tags * apply workaround to all resources to maintain `Spec.Tags` order; otherwise, unnecessary `Update` calls may be triggered By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 1cf07ed commit 334b246

File tree

25 files changed

+268
-18
lines changed

25 files changed

+268
-18
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: "2022-10-12T21:27:52Z"
2+
build_date: "2022-10-13T18:47:02Z"
33
build_hash: 5ee0ac052c54f008dff50f6f5ebb73f2cf3a0bd7
4-
go_version: go1.18.2
4+
go_version: go1.18.1
55
version: v0.20.1-4-g5ee0ac0
6-
api_directory_checksum: 3638dfc2a8e94a1ea91486172874d0b0b4e039f7
6+
api_directory_checksum: b3a2878ca8a156389214b900257c4d572ad4e3a5
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: b50a20e57aaf81d2a636ebf9a74d9693a82eeeb7
10+
file_checksum: 0260a4e58e3e1667fc456baff448cbabc2da4bf6
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ resources:
185185
operation: CreateTags
186186
path: Tags
187187
hooks:
188+
delta_pre_compare:
189+
code: compareTags(delta, a, b)
188190
sdk_create_post_build_request:
189191
template_path: hooks/dhcp_options/sdk_create_post_build_request.go.tpl
190192
sdk_file_end:
@@ -211,6 +213,8 @@ resources:
211213
operation: CreateTags
212214
path: Tags
213215
hooks:
216+
delta_pre_compare:
217+
code: compareTags(delta, a, b)
214218
sdk_create_post_build_request:
215219
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
216220
sdk_create_post_set_output:
@@ -244,6 +248,8 @@ resources:
244248
match_fields:
245249
- AllocationId
246250
hooks:
251+
delta_pre_compare:
252+
code: compareTags(delta, a, b)
247253
sdk_create_post_build_request:
248254
template_path: hooks/elastic_ip_address/sdk_create_post_build_request.go.tpl
249255
sdk_delete_post_build_request:
@@ -274,6 +280,8 @@ resources:
274280
path: Status.internetGatewayID
275281
name: ID
276282
hooks:
283+
delta_pre_compare:
284+
code: compareTags(delta, a, b)
277285
sdk_create_post_build_request:
278286
template_path: hooks/internet_gateway/sdk_create_post_build_request.go.tpl
279287
sdk_create_post_set_output:
@@ -319,6 +327,8 @@ resources:
319327
in:
320328
- available
321329
hooks:
330+
delta_pre_compare:
331+
code: compareTags(delta, a, b)
322332
sdk_create_post_build_request:
323333
template_path: hooks/nat_gateway/sdk_create_post_build_request.go.tpl
324334
sdk_file_end:
@@ -374,7 +384,7 @@ resources:
374384
path: Status.VPCEndpointID
375385
hooks:
376386
delta_pre_compare:
377-
code: customPreCompare(a, b)
387+
code: customPreCompare(delta, a, b)
378388
sdk_create_post_build_request:
379389
template_path: hooks/route_table/sdk_create_post_build_request.go.tpl
380390
sdk_create_post_set_output:
@@ -429,6 +439,8 @@ resources:
429439
GroupIds: Ids
430440
GroupNames: Names
431441
hooks:
442+
delta_pre_compare:
443+
code: compareTags(delta, a, b)
432444
sdk_create_post_build_request:
433445
template_path: hooks/security_group/sdk_create_post_build_request.go.tpl
434446
sdk_file_end:
@@ -488,6 +500,8 @@ resources:
488500
- InvalidParameterValue
489501
- InvalidCustomerOwnedIpv4PoolID.Malformed
490502
hooks:
503+
delta_pre_compare:
504+
code: compareTags(delta, a, b)
491505
sdk_create_post_build_request:
492506
template_path: hooks/subnet/sdk_create_post_build_request.go.tpl
493507
sdk_create_post_set_output:
@@ -513,6 +527,8 @@ resources:
513527
path: Status.transitGatewayID
514528
name: ID
515529
hooks:
530+
delta_pre_compare:
531+
code: compareTags(delta, a, b)
516532
sdk_create_post_build_request:
517533
template_path: hooks/transit_gateway/sdk_create_post_build_request.go.tpl
518534
sdk_file_end:
@@ -554,6 +570,8 @@ resources:
554570
path: Status.vpcID
555571
name: ID
556572
hooks:
573+
delta_pre_compare:
574+
code: compareTags(delta, a, b)
557575
sdk_create_post_build_request:
558576
template_path: hooks/vpc/sdk_create_post_build_request.go.tpl
559577
sdk_create_post_set_output:
@@ -599,6 +617,8 @@ resources:
599617
- InvalidVpcId.Malformed
600618
- InvalidServiceName
601619
hooks:
620+
delta_pre_compare:
621+
code: compareTags(delta, a, b)
602622
sdk_create_post_build_request:
603623
template_path: hooks/vpc_endpoint/sdk_create_post_build_request.go.tpl
604624
sdk_delete_post_build_request:

generator.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ resources:
185185
operation: CreateTags
186186
path: Tags
187187
hooks:
188+
delta_pre_compare:
189+
code: compareTags(delta, a, b)
188190
sdk_create_post_build_request:
189191
template_path: hooks/dhcp_options/sdk_create_post_build_request.go.tpl
190192
sdk_file_end:
@@ -211,6 +213,8 @@ resources:
211213
operation: CreateTags
212214
path: Tags
213215
hooks:
216+
delta_pre_compare:
217+
code: compareTags(delta, a, b)
214218
sdk_create_post_build_request:
215219
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
216220
sdk_create_post_set_output:
@@ -244,6 +248,8 @@ resources:
244248
match_fields:
245249
- AllocationId
246250
hooks:
251+
delta_pre_compare:
252+
code: compareTags(delta, a, b)
247253
sdk_create_post_build_request:
248254
template_path: hooks/elastic_ip_address/sdk_create_post_build_request.go.tpl
249255
sdk_delete_post_build_request:
@@ -274,6 +280,8 @@ resources:
274280
path: Status.internetGatewayID
275281
name: ID
276282
hooks:
283+
delta_pre_compare:
284+
code: compareTags(delta, a, b)
277285
sdk_create_post_build_request:
278286
template_path: hooks/internet_gateway/sdk_create_post_build_request.go.tpl
279287
sdk_create_post_set_output:
@@ -319,6 +327,8 @@ resources:
319327
in:
320328
- available
321329
hooks:
330+
delta_pre_compare:
331+
code: compareTags(delta, a, b)
322332
sdk_create_post_build_request:
323333
template_path: hooks/nat_gateway/sdk_create_post_build_request.go.tpl
324334
sdk_file_end:
@@ -374,7 +384,7 @@ resources:
374384
path: Status.VPCEndpointID
375385
hooks:
376386
delta_pre_compare:
377-
code: customPreCompare(a, b)
387+
code: customPreCompare(delta, a, b)
378388
sdk_create_post_build_request:
379389
template_path: hooks/route_table/sdk_create_post_build_request.go.tpl
380390
sdk_create_post_set_output:
@@ -429,6 +439,8 @@ resources:
429439
GroupIds: Ids
430440
GroupNames: Names
431441
hooks:
442+
delta_pre_compare:
443+
code: compareTags(delta, a, b)
432444
sdk_create_post_build_request:
433445
template_path: hooks/security_group/sdk_create_post_build_request.go.tpl
434446
sdk_file_end:
@@ -488,6 +500,8 @@ resources:
488500
- InvalidParameterValue
489501
- InvalidCustomerOwnedIpv4PoolID.Malformed
490502
hooks:
503+
delta_pre_compare:
504+
code: compareTags(delta, a, b)
491505
sdk_create_post_build_request:
492506
template_path: hooks/subnet/sdk_create_post_build_request.go.tpl
493507
sdk_create_post_set_output:
@@ -513,6 +527,8 @@ resources:
513527
path: Status.transitGatewayID
514528
name: ID
515529
hooks:
530+
delta_pre_compare:
531+
code: compareTags(delta, a, b)
516532
sdk_create_post_build_request:
517533
template_path: hooks/transit_gateway/sdk_create_post_build_request.go.tpl
518534
sdk_file_end:
@@ -554,6 +570,8 @@ resources:
554570
path: Status.vpcID
555571
name: ID
556572
hooks:
573+
delta_pre_compare:
574+
code: compareTags(delta, a, b)
557575
sdk_create_post_build_request:
558576
template_path: hooks/vpc/sdk_create_post_build_request.go.tpl
559577
sdk_create_post_set_output:
@@ -599,6 +617,8 @@ resources:
599617
- InvalidVpcId.Malformed
600618
- InvalidServiceName
601619
hooks:
620+
delta_pre_compare:
621+
code: compareTags(delta, a, b)
602622
sdk_create_post_build_request:
603623
template_path: hooks/vpc_endpoint/sdk_create_post_build_request.go.tpl
604624
sdk_delete_post_build_request:

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,29 @@ func computeTagsDelta(
147147

148148
}
149149

150+
// compareTags is a custom comparison function for comparing lists of Tag
151+
// structs where the order of the structs in the list is not important.
152+
func compareTags(
153+
delta *ackcompare.Delta,
154+
a *resource,
155+
b *resource,
156+
) {
157+
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
158+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
159+
} else if len(a.ko.Spec.Tags) > 0 {
160+
addedOrUpdated, removed := computeTagsDelta(a.ko.Spec.Tags, b.ko.Spec.Tags)
161+
if len(addedOrUpdated) != 0 || len(removed) != 0 {
162+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
163+
}
164+
}
165+
}
166+
150167
// updateTagSpecificationsInCreateRequest adds
151168
// Tags defined in the Spec to CreateDhcpOptionsInput.TagSpecification
152169
// and ensures the ResourceType is always set to 'dhcp-options'
153170
func updateTagSpecificationsInCreateRequest(r *resource,
154171
input *svcsdk.CreateDhcpOptionsInput) {
172+
input.TagSpecifications = nil
155173
desiredTagSpecs := svcsdk.TagSpecification{}
156174
if r.ko.Spec.Tags != nil {
157175
requestedTags := []*svcsdk.Tag{}
@@ -166,6 +184,6 @@ func updateTagSpecificationsInCreateRequest(r *resource,
166184
}
167185
desiredTagSpecs.SetResourceType("dhcp-options")
168186
desiredTagSpecs.SetTags(requestedTags)
187+
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
169188
}
170-
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
171189
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,29 @@ func computeTagsDelta(
147147

148148
}
149149

150+
// compareTags is a custom comparison function for comparing lists of Tag
151+
// structs where the order of the structs in the list is not important.
152+
func compareTags(
153+
delta *ackcompare.Delta,
154+
a *resource,
155+
b *resource,
156+
) {
157+
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
158+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
159+
} else if len(a.ko.Spec.Tags) > 0 {
160+
addedOrUpdated, removed := computeTagsDelta(a.ko.Spec.Tags, b.ko.Spec.Tags)
161+
if len(addedOrUpdated) != 0 || len(removed) != 0 {
162+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
163+
}
164+
}
165+
}
166+
150167
// updateTagSpecificationsInCreateRequest adds
151168
// Tags defined in the Spec to AllocateAddressInput.TagSpecification
152169
// and ensures the ResourceType is always set to 'elastic-ip'
153170
func updateTagSpecificationsInCreateRequest(r *resource,
154171
input *svcsdk.AllocateAddressInput) {
172+
input.TagSpecifications = nil
155173
desiredTagSpecs := svcsdk.TagSpecification{}
156174
if r.ko.Spec.Tags != nil {
157175
requestedTags := []*svcsdk.Tag{}
@@ -166,6 +184,6 @@ func updateTagSpecificationsInCreateRequest(r *resource,
166184
}
167185
desiredTagSpecs.SetResourceType("elastic-ip")
168186
desiredTagSpecs.SetTags(requestedTags)
187+
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
169188
}
170-
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
171189
}

pkg/resource/instance/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/instance/hooks.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,29 @@ func computeTagsDelta(
160160

161161
}
162162

163+
// compareTags is a custom comparison function for comparing lists of Tag
164+
// structs where the order of the structs in the list is not important.
165+
func compareTags(
166+
delta *ackcompare.Delta,
167+
a *resource,
168+
b *resource,
169+
) {
170+
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
171+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
172+
} else if len(a.ko.Spec.Tags) > 0 {
173+
addedOrUpdated, removed := computeTagsDelta(a.ko.Spec.Tags, b.ko.Spec.Tags)
174+
if len(addedOrUpdated) != 0 || len(removed) != 0 {
175+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
176+
}
177+
}
178+
}
179+
163180
// updateTagSpecificationsInCreateRequest adds
164181
// Tags defined in the Spec to RunInstancesInput.TagSpecification
165182
// and ensures the ResourceType is always set to 'instance'
166183
func updateTagSpecificationsInCreateRequest(r *resource,
167184
input *svcsdk.RunInstancesInput) {
185+
input.TagSpecifications = nil
168186
desiredTagSpecs := svcsdk.TagSpecification{}
169187
if r.ko.Spec.Tags != nil {
170188
instanceTags := []*svcsdk.Tag{}
@@ -179,6 +197,6 @@ func updateTagSpecificationsInCreateRequest(r *resource,
179197
}
180198
desiredTagSpecs.SetResourceType("instance")
181199
desiredTagSpecs.SetTags(instanceTags)
200+
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
182201
}
183-
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
184202
}

pkg/resource/internet_gateway/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.

0 commit comments

Comments
 (0)