Skip to content

Commit

Permalink
Support lists of structs containing refs and ClearResolvedReferences (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
RedbackThomson authored Apr 17, 2023
1 parent c9e7c7b commit 4be9ee7
Show file tree
Hide file tree
Showing 9 changed files with 645 additions and 373 deletions.
10 changes: 5 additions & 5 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ var (
"GoCodeIncompleteLateInitialization": func(r *ackmodel.CRD, resVarName string, indentLevel int) string {
return code.IncompleteLateInitialization(r.Config(), r, resVarName, indentLevel)
},
"GoCodeReferencesValidation": func(r *ackmodel.CRD, sourceVarName string, indentLevel int) string {
return code.ReferenceFieldsValidation(r, sourceVarName, indentLevel)
},
"GoCodeContainsReferences": func(r *ackmodel.CRD, sourceVarName string) string {
return code.ReferenceFieldsPresent(r, sourceVarName)
"GoCodeReferencesValidation": func(f *ackmodel.Field, sourceVarName string, indentLevel int) string {
return code.ReferenceFieldsValidation(f, sourceVarName, indentLevel)
},
"CheckNilFieldPath": func(f *ackmodel.Field, sourceVarName string) string {
return code.CheckNilFieldPath(f, sourceVarName)
Expand All @@ -196,6 +193,9 @@ var (
"GoCodeResolveReference": func(f *ackmodel.Field, sourceVarName string, indentLevel int) string {
return code.ResolveReferencesForField(f, sourceVarName, indentLevel)
},
"GoCodeClearResolvedReferences": func(f *ackmodel.Field, targetVarName string, indentLevel int) string {
return code.ClearResolvedReferencesForField(f, targetVarName, indentLevel)
},
}
)

Expand Down
8 changes: 6 additions & 2 deletions pkg/generate/ack/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ func (frm *fakeRM) LateInitialize(context.Context, acktypes.AWSResource) (acktyp
return nil, nil
}

func (frm *fakeRM) ResolveReferences(context.Context, rtclient.Reader, acktypes.AWSResource) (acktypes.AWSResource, error) {
return nil, nil
func (frm *fakeRM) ResolveReferences(context.Context, rtclient.Reader, acktypes.AWSResource) (acktypes.AWSResource, bool, error) {
return nil, false, nil
}

func (frm *fakeRM) ClearResolvedReferences(acktypes.AWSResource) acktypes.AWSResource {
return nil
}

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

0 comments on commit 4be9ee7

Please sign in to comment.