Skip to content

Commit 78927ab

Browse files
authored
Bugfix/compare attribute definitions (#59)
Issue: aws-controllers-k8s/community#1599 Description of changes: - ignore default compare function of attribute definitions by update generator.yaml - implemented custom compare/delta function for attribute definitions. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 3bb079b commit 78927ab

File tree

6 files changed

+272
-5
lines changed

6 files changed

+272
-5
lines changed

apis/v1alpha1/ack-generate-metadata.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ api_directory_checksum: 553eee36730dd0637424a8d9348b37ee90eb594d
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: cf8320749a4188537376f8cec2b93e5aea7b3b52
10+
file_checksum: 8b17e0975b403cc552eba69aec5cd03a30956729
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ resources:
1515
Tags:
1616
compare:
1717
is_ignored: true
18+
AttributeDefinitions:
19+
compare:
20+
is_ignored: true
1821
exceptions:
1922
errors:
2023
404:

generator.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ resources:
1515
Tags:
1616
compare:
1717
is_ignored: true
18+
AttributeDefinitions:
19+
compare:
20+
is_ignored: true
1821
exceptions:
1922
errors:
2023
404:

pkg/resource/table/delta.go

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/table/hooks.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,15 @@ func customPreCompare(
419419
a *resource,
420420
b *resource,
421421
) {
422-
// TODO(hilalymh): customDeltaFunctions for AttributeDefinitions
422+
// customDeltaFunctions for AttributeDefinitions
423+
// see https://github.com/aws-controllers-k8s/community/issues/1599
424+
if len(a.ko.Spec.AttributeDefinitions) != len(b.ko.Spec.AttributeDefinitions) {
425+
delta.Add("Spec.AttributeDefinitions", a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions)
426+
} else if a.ko.Spec.AttributeDefinitions != nil && b.ko.Spec.AttributeDefinitions != nil {
427+
if !equalAttributeDefinitions(a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions) {
428+
delta.Add("Spec.AttributeDefinitions", a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions)
429+
}
430+
}
423431
// TODO(hilalymh): customDeltaFunctions for GlobalSecondaryIndexes
424432

425433
// See https://github.com/aws-controllers-k8s/community/issues/1595
@@ -444,3 +452,21 @@ func customPreCompare(
444452
}
445453
}
446454
}
455+
456+
func equalAttributeDefinitions(a, b []*v1alpha1.AttributeDefinition) bool {
457+
for _, aElement := range a {
458+
found := false
459+
for _, bElement := range b {
460+
if equalStrings(aElement.AttributeName, bElement.AttributeName) {
461+
found = true
462+
if !equalStrings(aElement.AttributeType, bElement.AttributeType) {
463+
return false
464+
}
465+
}
466+
}
467+
if !found {
468+
return false
469+
}
470+
}
471+
return true
472+
}

pkg/resource/table/hooks_test.go

+238
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,241 @@ func Test_customPreCompare(t *testing.T) {
158158
}
159159
})
160160
}
161+
162+
func Test_newResourceDelta_customDeltaFunction_AttributeDefinitions(t *testing.T) {
163+
type args struct {
164+
a *resource
165+
b *resource
166+
}
167+
tests := []struct {
168+
name string
169+
args args
170+
want bool
171+
}{
172+
{
173+
name: "both desired and latest are nil",
174+
args: args{
175+
a: &resource{
176+
ko: &v1alpha1.Table{
177+
Spec: v1alpha1.TableSpec{
178+
AttributeDefinitions: nil,
179+
},
180+
},
181+
},
182+
b: &resource{
183+
ko: &v1alpha1.Table{
184+
Spec: v1alpha1.TableSpec{
185+
AttributeDefinitions: nil,
186+
},
187+
},
188+
},
189+
},
190+
want: true,
191+
},
192+
{
193+
name: "desired is not nil",
194+
args: args{
195+
a: &resource{
196+
ko: &v1alpha1.Table{
197+
Spec: v1alpha1.TableSpec{
198+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
199+
{
200+
AttributeName: aws.String("externalId"),
201+
AttributeType: aws.String("S"),
202+
},
203+
},
204+
},
205+
},
206+
},
207+
b: &resource{
208+
ko: &v1alpha1.Table{
209+
Spec: v1alpha1.TableSpec{
210+
AttributeDefinitions: nil,
211+
},
212+
},
213+
},
214+
},
215+
want: false,
216+
},
217+
{
218+
name: "latest is not nil",
219+
args: args{
220+
a: &resource{
221+
ko: &v1alpha1.Table{
222+
Spec: v1alpha1.TableSpec{
223+
AttributeDefinitions: nil,
224+
},
225+
},
226+
},
227+
b: &resource{
228+
ko: &v1alpha1.Table{
229+
Spec: v1alpha1.TableSpec{
230+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
231+
{
232+
AttributeName: aws.String("externalId"),
233+
AttributeType: aws.String("S"),
234+
},
235+
},
236+
},
237+
},
238+
},
239+
},
240+
want: false,
241+
},
242+
{
243+
name: "desired and latest are equal",
244+
args: args{
245+
a: &resource{
246+
ko: &v1alpha1.Table{
247+
Spec: v1alpha1.TableSpec{
248+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
249+
{
250+
AttributeName: aws.String("externalId"),
251+
AttributeType: aws.String("S"),
252+
},
253+
{
254+
AttributeName: aws.String("id"),
255+
AttributeType: aws.String("S"),
256+
},
257+
},
258+
},
259+
},
260+
},
261+
b: &resource{
262+
ko: &v1alpha1.Table{
263+
Spec: v1alpha1.TableSpec{
264+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
265+
{
266+
AttributeName: aws.String("id"),
267+
AttributeType: aws.String("S"),
268+
},
269+
{
270+
AttributeName: aws.String("externalId"),
271+
AttributeType: aws.String("S"),
272+
},
273+
},
274+
},
275+
},
276+
},
277+
},
278+
want: true,
279+
},
280+
{
281+
name: "desired is updated",
282+
args: args{
283+
a: &resource{
284+
ko: &v1alpha1.Table{
285+
Spec: v1alpha1.TableSpec{
286+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
287+
{
288+
AttributeName: aws.String("externalId"),
289+
AttributeType: aws.String("N"),
290+
},
291+
{
292+
AttributeName: aws.String("id"),
293+
AttributeType: aws.String("S"),
294+
},
295+
},
296+
},
297+
},
298+
},
299+
b: &resource{
300+
ko: &v1alpha1.Table{
301+
Spec: v1alpha1.TableSpec{
302+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
303+
{
304+
AttributeName: aws.String("id"),
305+
AttributeType: aws.String("S"),
306+
},
307+
{
308+
AttributeName: aws.String("externalId"),
309+
AttributeType: aws.String("S"),
310+
},
311+
},
312+
},
313+
},
314+
},
315+
},
316+
want: false,
317+
},
318+
{
319+
name: "removed in desired",
320+
args: args{
321+
a: &resource{
322+
ko: &v1alpha1.Table{
323+
Spec: v1alpha1.TableSpec{
324+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
325+
{
326+
AttributeName: aws.String("id"),
327+
AttributeType: aws.String("S"),
328+
},
329+
},
330+
},
331+
},
332+
},
333+
b: &resource{
334+
ko: &v1alpha1.Table{
335+
Spec: v1alpha1.TableSpec{
336+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
337+
{
338+
AttributeName: aws.String("id"),
339+
AttributeType: aws.String("S"),
340+
},
341+
{
342+
AttributeName: aws.String("externalId"),
343+
AttributeType: aws.String("S"),
344+
},
345+
},
346+
},
347+
},
348+
},
349+
},
350+
want: false,
351+
},
352+
{
353+
name: "added in desired",
354+
args: args{
355+
a: &resource{
356+
ko: &v1alpha1.Table{
357+
Spec: v1alpha1.TableSpec{
358+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
359+
{
360+
AttributeName: aws.String("id"),
361+
AttributeType: aws.String("S"),
362+
},
363+
{
364+
AttributeName: aws.String("externalId"),
365+
AttributeType: aws.String("S"),
366+
},
367+
},
368+
},
369+
},
370+
},
371+
b: &resource{
372+
ko: &v1alpha1.Table{
373+
Spec: v1alpha1.TableSpec{
374+
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
375+
{
376+
AttributeName: aws.String("id"),
377+
AttributeType: aws.String("S"),
378+
},
379+
},
380+
},
381+
},
382+
},
383+
},
384+
want: false,
385+
},
386+
}
387+
388+
isEqual := func(delta *compare.Delta) bool {
389+
return len(delta.Differences) == 0
390+
}
391+
for _, tt := range tests {
392+
t.Run(tt.name, func(t *testing.T) {
393+
if delta := newResourceDelta(tt.args.a, tt.args.b); isEqual(delta) != tt.want {
394+
t.Errorf("Compare attribution defintions should be %v", tt.want)
395+
}
396+
})
397+
}
398+
}

0 commit comments

Comments
 (0)