Skip to content

Commit 4be9ee7

Browse files
Support lists of structs containing refs and ClearResolvedReferences (#435)
Implements aws-controllers-k8s/runtime#121 Description of changes: This PR abstracts out the process of iterating through all references into a separate method called `iterReferenceValues`. `iterReferenceValues` produces Go code that drills down into the spec for every ref, iterating through any slices as necessary. Once it reaches the ref object, it calls a callback which can be used by other methods for logic pertaining to accessing the refs. This change allows `ResolveReferencesForField` to access all refs within lists of structs (or even within lists of lists of structs, etc.). `iterReferenceValues` is also used to implement `ClearResolvedReferencesForField`, which simply sets the concrete value to `nil` if it detects a non-nil value in the ref field (or `len > 0` for lists of refs). This PR also removes `hasNonNilReferences`. `hasNonNilReferences` was being used to indicate whether there were any references inside the resource, which required another set of iterating through all ref fields. Instead, each `resolveReferencesFor*` returns a boolean which indicates whether it found a reference during its iteration. Below is a Gist that shows the output of running the code-generator on the current EC2 `generator.yaml`: https://gist.github.com/RedbackThomson/8e11cbbe96065a4eb812c387665c747d For testing: `ec2` and `lambda` already have a series of tests for references. Both of these tests are passing, but they were also passing before this PR. I have a branch of the `ec2-controller` with additional tests using `RouteTables`, which have lists of structs containing refs, and all of those are passing as well. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c9e7c7b commit 4be9ee7

File tree

9 files changed

+645
-373
lines changed

9 files changed

+645
-373
lines changed

pkg/generate/ack/controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,8 @@ var (
172172
"GoCodeIncompleteLateInitialization": func(r *ackmodel.CRD, resVarName string, indentLevel int) string {
173173
return code.IncompleteLateInitialization(r.Config(), r, resVarName, indentLevel)
174174
},
175-
"GoCodeReferencesValidation": func(r *ackmodel.CRD, sourceVarName string, indentLevel int) string {
176-
return code.ReferenceFieldsValidation(r, sourceVarName, indentLevel)
177-
},
178-
"GoCodeContainsReferences": func(r *ackmodel.CRD, sourceVarName string) string {
179-
return code.ReferenceFieldsPresent(r, sourceVarName)
175+
"GoCodeReferencesValidation": func(f *ackmodel.Field, sourceVarName string, indentLevel int) string {
176+
return code.ReferenceFieldsValidation(f, sourceVarName, indentLevel)
180177
},
181178
"CheckNilFieldPath": func(f *ackmodel.Field, sourceVarName string) string {
182179
return code.CheckNilFieldPath(f, sourceVarName)
@@ -196,6 +193,9 @@ var (
196193
"GoCodeResolveReference": func(f *ackmodel.Field, sourceVarName string, indentLevel int) string {
197194
return code.ResolveReferencesForField(f, sourceVarName, indentLevel)
198195
},
196+
"GoCodeClearResolvedReferences": func(f *ackmodel.Field, targetVarName string, indentLevel int) string {
197+
return code.ClearResolvedReferencesForField(f, targetVarName, indentLevel)
198+
},
199199
}
200200
)
201201

pkg/generate/ack/runtime_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ func (frm *fakeRM) LateInitialize(context.Context, acktypes.AWSResource) (acktyp
126126
return nil, nil
127127
}
128128

129-
func (frm *fakeRM) ResolveReferences(context.Context, rtclient.Reader, acktypes.AWSResource) (acktypes.AWSResource, error) {
130-
return nil, nil
129+
func (frm *fakeRM) ResolveReferences(context.Context, rtclient.Reader, acktypes.AWSResource) (acktypes.AWSResource, bool, error) {
130+
return nil, false, nil
131+
}
132+
133+
func (frm *fakeRM) ClearResolvedReferences(acktypes.AWSResource) acktypes.AWSResource {
134+
return nil
131135
}
132136

133137
func (frm *fakeRM) IsSynced(context.Context, acktypes.AWSResource) (bool, error) {

0 commit comments

Comments
 (0)