Skip to content

Commit bfe3875

Browse files
committed
update pkg/resource templates for new Delta code
Adds plumbing to the `pkg/resource/depscriptor.go.tpl` template that implements the `Delta()` method on the `github.com/aws-controllers-k8s/runtime/pkg/types.AWSResourceDescriptor` interface-implementing `resource` struct.
1 parent 8e10e86 commit bfe3875

File tree

4 files changed

+30
-48
lines changed

4 files changed

+30
-48
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{ template "boilerplate" }}
2+
3+
package {{ .CRD.Names.Snake }}
4+
5+
import (
6+
ackcompare "github.com/aws/aws-controllers-k8s/pkg/compare"
7+
)
8+
9+
// newResourceDelta returns a new `ackcompare.Delta` used to compare two
10+
// resources
11+
func newResourceDelta(
12+
a *resource,
13+
b *resource,
14+
) *ackcompare.Delta {
15+
delta := ackcompare.NewDelta()
16+
if ((a == nil && b != nil) ||
17+
(a != nil && b == nil)) {
18+
delta.Add("", a, b)
19+
return delta
20+
}
21+
{{ GoCodeCompare .CRD "delta" "a.ko" "b.ko" 1}}
22+
return delta
23+
}

templates/pkg/resource/descriptor.go.tpl

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,51 +52,10 @@ func (d *resourceDescriptor) ResourceFromRuntimeObject(
5252
}
5353
}
5454

55-
// Equal returns true if the two supplied AWSResources have the same content.
56-
// The underlying types of the two supplied AWSResources should be the same. In
57-
// other words, the Equal() method should be called with the same concrete
58-
// implementing AWSResource type
59-
func (d *resourceDescriptor) Equal(
60-
a acktypes.AWSResource,
61-
b acktypes.AWSResource,
62-
) bool {
63-
ac := a.(*resource)
64-
bc := b.(*resource)
65-
opts := []cmp.Option{cmpopts.EquateEmpty()}
66-
{{- if .CRD.CompareIgnoredFields }}
67-
opts = append(opts, cmpopts.IgnoreFields(*ac.ko,
68-
{{- range $fieldPath := .CRD.CompareIgnoredFields }}
69-
{{ printf "%q" $fieldPath }},
70-
{{- end }}
71-
))
72-
{{- end }}
73-
return cmp.Equal(ac.ko, bc.ko, opts...)
74-
}
75-
76-
// Diff returns a Reporter which provides the difference between two supplied
77-
// AWSResources. The underlying types of the two supplied AWSResources should
78-
// be the same. In other words, the Diff() method should be called with the
79-
// same concrete implementing AWSResource type
80-
func (d *resourceDescriptor) Diff(
81-
a acktypes.AWSResource,
82-
b acktypes.AWSResource,
83-
) *ackcompare.Reporter {
84-
ac := a.(*resource)
85-
bc := b.(*resource)
86-
var diffReporter ackcompare.Reporter
87-
opts := []cmp.Option{
88-
cmp.Reporter(&diffReporter),
89-
cmp.AllowUnexported(svcapitypes.{{ .CRD.Kind }}{}),
90-
}
91-
{{- if .CRD.CompareIgnoredFields }}
92-
opts = append(opts, cmpopts.IgnoreFields(*ac.ko,
93-
{{- range $fieldPath := .CRD.CompareIgnoredFields }}
94-
{{ printf "%q" $fieldPath }},
95-
{{- end }}
96-
))
97-
{{- end }}
98-
cmp.Equal(ac.ko, bc.ko, opts...)
99-
return &diffReporter
55+
// Delta returns an `ackcompare.Delta` object containing the difference between
56+
// one `AWSResource` and another.
57+
func (d *resourceDescriptor) Delta(a, b acktypes.AWSResource) *ackcompare.Delta {
58+
return newResourceDelta(a, b)
10059
}
10160

10261
// UpdateCRStatus accepts an AWSResource object and changes the Status

templates/pkg/resource/resource.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
svcapitypes "github.com/aws-controllers-k8s/{{ .ServiceIDClean }}-controller/apis/{{ .APIVersion}}"
1212
)
1313

14-
// resource implements the `aws-service-operator-k8s/pkg/types.AWSResource`
14+
// resource implements the `aws-controller-k8s/runtime/pkg/types.AWSResource`
1515
// interface
1616
type resource struct {
1717
// The Kubernetes-native CR representing the resource

templates/pkg/resource/sdk_update.go.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ func (rm *resourceManager) sdkUpdate(
33
ctx context.Context,
44
desired *resource,
55
latest *resource,
6-
diffReporter *ackcompare.Reporter,
6+
delta *ackcompare.Delta,
77
) (*resource, error) {
88
{{ $customMethod := .CRD.GetCustomImplementation .CRD.Ops.Update }}
99
{{ if $customMethod }}
10-
customResp, customRespErr := rm.{{ $customMethod }}(ctx, desired, latest, diffReporter)
10+
customResp, customRespErr := rm.{{ $customMethod }}(ctx, desired, latest, delta)
1111
if customResp != nil || customRespErr != nil {
1212
return customResp, customRespErr
1313
}

0 commit comments

Comments
 (0)