Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix scheme registration in pkg/apis/templates #142
Fix scheme registration in pkg/apis/templates #142
Changes from 1 commit
c6f3e0f
654f030
65407b5
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a scheme builder that explicitly registers all necessary registrations. Otherwise this function will still be vulnerable to failures due to reordering that changes the contents of
localSchemeBuilder
Also, we can move this to the
helpers.go
package, since this is the only thing that should depend on thesch
scheme.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define the schemeBuilder outside of a function as a global var to avoid ordering concerns:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
defaults.go
file also depends on the Scheme, as the scheme is used when creating thestructuralSchema
. I thought the separate file would prevent the assumption that the scheme only belongs to one thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually re-introduces ordering problems. One of the funcs that's included in the
schemeBuilder
(SchemeBuilder.AddToScheme
) is declared as a package level variable inregister.go
: https://github.com/open-policy-agent/frameworks/blob/master/constraint/pkg/apis/templates/v1/register.go#L39As @maxsmythe suggests, not using
localSchemeBuilder
's contents directly has a possible benefit. The difference between what I have here and writing outSchemeBuilder.AddToScheme, addDefaultingFuncs
is that iflocalSchemeBuilder
were to change in the future, we would not see those changes in the separate schemebuilder I'm making. That could be good or bad, depending on the scenario.The key idea here is to take advantage of this ordering:
init()
functions runBasically I can reliably use any package variable, but I can't rely on other
init()
funcs. To truly not rely on otherinit()
functions, I'll actually need to duplicate this line from constrainttemplate_types.go in my own schemebuilder.I see two problems that we're choosing between:
init()
function orderingAddToScheme
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My vote is for option 2 since it is explicit and doesn't rely on magic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's okay to have our own package-internal scheme,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack, SGTM
Fortunately we don't need all changes to the schema builder, just those features that we rely on (defaulting and conversion it looks like).
Also note that we are coupled to the init() functions if we are using anything whose contents may change because of the init functions (such as
localSchemeBuilder
).+1 to option 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid doing this as a package-level variable as I step back towards ordering problems. Putting all of the logic inside an
init()
function that is decoupled from otherinit()
functions allows me to take advantage of other package level variables while still isolating my schemeBuilder from side-effects.