Skip to content

Commit 4b7895a

Browse files
authored
add customprecompare to normalize policy (#75)
fixes aws-controllers-k8s/community#2421 Description of changes: - added a custom pre compare to remove the whitespaces and clean policy before delta. There is no longer a diff. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 3461f9d commit 4b7895a

File tree

8 files changed

+322
-18
lines changed

8 files changed

+322
-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: "2025-03-27T16:10:01Z"
3-
build_hash: 980cb1e4734f673d16101cf55206b84ca639ec01
2+
build_date: "2025-04-29T03:41:47Z"
3+
build_hash: ab8fa0bbefe77c9682f53251412fd8d1002ba30f
44
go_version: go1.24.1
5-
version: v0.44.0
5+
version: v0.44.0-3-gab8fa0b
66
api_directory_checksum: 2627dc306e3a83c86c04050c6c4336451459e728
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.32.6
99
generator_config_info:
10-
file_checksum: 662a51e8e4a1225d04aa0121d55827e0a9a054af
10+
file_checksum: b332aeda9a33b58316273296754ee470b9568b59
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ resources:
1212
values:
1313
- All
1414
hooks:
15+
delta_pre_compare:
16+
code: customPreCompare(delta, a, b)
1517
sdk_get_attributes_pre_set_output:
1618
template_path: hooks/queue/sdk_get_attributes_pre_set_output.go.tpl
1719
sdk_get_attributes_post_set_output:
@@ -75,6 +77,8 @@ resources:
7577
service_name: iam
7678
resource: Policy
7779
path: Spec.PolicyDocument
80+
compare:
81+
is_ignored: true
7882
ReceiveMessageWaitTimeSeconds:
7983
is_attribute: true
8084
type: string
@@ -92,6 +96,8 @@ resources:
9296
RedrivePolicy:
9397
is_attribute: true
9498
type: string
99+
compare:
100+
is_ignored: true
95101
RedriveAllowPolicy:
96102
is_attribute: true
97103
type: string

generator.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ resources:
1212
values:
1313
- All
1414
hooks:
15+
delta_pre_compare:
16+
code: customPreCompare(delta, a, b)
1517
sdk_get_attributes_pre_set_output:
1618
template_path: hooks/queue/sdk_get_attributes_pre_set_output.go.tpl
1719
sdk_get_attributes_post_set_output:
@@ -75,6 +77,8 @@ resources:
7577
service_name: iam
7678
resource: Policy
7779
path: Spec.PolicyDocument
80+
compare:
81+
is_ignored: true
7882
ReceiveMessageWaitTimeSeconds:
7983
is_attribute: true
8084
type: string
@@ -92,6 +96,8 @@ resources:
9296
RedrivePolicy:
9397
is_attribute: true
9498
type: string
99+
compare:
100+
is_ignored: true
95101
RedriveAllowPolicy:
96102
is_attribute: true
97103
type: string

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ require (
1313
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.10
1414
github.com/aws/smithy-go v1.22.2
1515
github.com/go-logr/logr v1.4.2
16+
github.com/micahhausler/aws-iam-policy v0.4.2
1617
github.com/spf13/pflag v1.0.5
18+
github.com/stretchr/testify v1.9.0
1719
k8s.io/api v0.32.1
1820
k8s.io/apimachinery v0.32.1
1921
k8s.io/client-go v0.32.1
@@ -61,6 +63,7 @@ require (
6163
github.com/modern-go/reflect2 v1.0.2 // indirect
6264
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
6365
github.com/pkg/errors v0.9.1 // indirect
66+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6467
github.com/prometheus/client_golang v1.19.1 // indirect
6568
github.com/prometheus/client_model v0.6.1 // indirect
6669
github.com/prometheus/common v0.55.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
113113
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
114114
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
115115
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
116+
github.com/micahhausler/aws-iam-policy v0.4.2 h1:HF7bERLnpqEmffV9/wTT4jZ7TbSNVk0JbpXo1Cj3up0=
117+
github.com/micahhausler/aws-iam-policy v0.4.2/go.mod h1:Ojgst9ZFn+VEEJpqtuw/LxVGqEf2+hwWBlkYWvF/XWM=
116118
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
117119
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
118120
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

pkg/resource/queue/delta.go

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

pkg/resource/queue/hooks.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
package queue
1515

1616
import (
17+
"bytes"
1718
"context"
19+
"encoding/json"
1820
"fmt"
21+
"reflect"
1922

2023
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
24+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2125
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2226
svcsdk "github.com/aws/aws-sdk-go-v2/service/sqs"
2327
"github.com/aws/aws-sdk-go/aws/arn"
28+
policy "github.com/micahhausler/aws-iam-policy/policy"
2429
)
2530

2631
// syncTags examines the Tags in the supplied Queue and calls the
@@ -143,3 +148,73 @@ func (rm *resourceManager) getQueueNameFromARN(tmpARN ackv1alpha1.AWSResourceNam
143148
}
144149
return queueARN.Resource, nil
145150
}
151+
152+
// customPreCompare is the entry point for custom comparison logic
153+
func customPreCompare(
154+
delta *ackcompare.Delta,
155+
a *resource,
156+
b *resource,
157+
) {
158+
comparePolicy(delta, a, b)
159+
compareRedrivePolicy(delta, a, b)
160+
}
161+
162+
// comparePolicy compares the Policy fields of two resources by unmarshalling
163+
// them into policy.Policy structs and using reflect.DeepEqual.
164+
func comparePolicy(
165+
delta *ackcompare.Delta,
166+
a *resource,
167+
b *resource,
168+
) {
169+
if a.ko.Spec.Policy == b.ko.Spec.Policy {
170+
// both are nil or equal
171+
return
172+
}
173+
if a.ko.Spec.Policy == nil || b.ko.Spec.Policy == nil {
174+
// one is nil and the other is not
175+
delta.Add("Spec.Policy", a.ko.Spec.Policy, b.ko.Spec.Policy)
176+
return
177+
}
178+
var policyA policy.Policy
179+
decoderA := json.NewDecoder(bytes.NewBufferString(*a.ko.Spec.Policy))
180+
decoderA.DisallowUnknownFields()
181+
errA := decoderA.Decode(&policyA)
182+
183+
var policyB policy.Policy
184+
decoderB := json.NewDecoder(bytes.NewBufferString(*b.ko.Spec.Policy))
185+
decoderB.DisallowUnknownFields()
186+
errB := decoderB.Decode(&policyB)
187+
188+
if errA != nil || errB != nil || !reflect.DeepEqual(policyA, policyB) {
189+
delta.Add("Spec.Policy", a.ko.Spec.Policy, b.ko.Spec.Policy)
190+
}
191+
}
192+
193+
// compareRedrivePolicy compares the RedrivePolicy fields of two resources by
194+
// unmarshalling them into interface{} and using reflect.DeepEqual.
195+
// since RedrivePolicy is a JSON string, we need to unmarshal it
196+
// into an interface{} and then compare the two interface{}s.
197+
func compareRedrivePolicy(
198+
delta *ackcompare.Delta,
199+
a *resource,
200+
b *resource,
201+
) {
202+
if a.ko.Spec.RedrivePolicy == b.ko.Spec.RedrivePolicy {
203+
// both are nil or equal
204+
return
205+
}
206+
if a.ko.Spec.RedrivePolicy == nil || b.ko.Spec.RedrivePolicy == nil {
207+
// one is nil and the other is not
208+
delta.Add("Spec.RedrivePolicy", a.ko.Spec.RedrivePolicy, b.ko.Spec.RedrivePolicy)
209+
return
210+
}
211+
var redriveA interface{}
212+
errA := json.Unmarshal([]byte(*a.ko.Spec.RedrivePolicy), &redriveA)
213+
214+
var redriveB interface{}
215+
errB := json.Unmarshal([]byte(*b.ko.Spec.RedrivePolicy), &redriveB)
216+
217+
if errA != nil || errB != nil || !reflect.DeepEqual(redriveA, redriveB) {
218+
delta.Add("Spec.RedrivePolicy", a.ko.Spec.RedrivePolicy, b.ko.Spec.RedrivePolicy)
219+
}
220+
}

0 commit comments

Comments
 (0)