Skip to content
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

Merged

Conversation

julianKatz
Copy link
Contributor

PR #138 introduced a bug: the type conversion functions required by the
Scheme for use in the ToVersionless functions of the v1, v1beta1,
v1alpha1 packages were missing. This manifested itself as an error in
Gatekeeper:

"unable to convert template: converting (v1beta1.ConstraintTemplate) to
(templates.ConstraintTemplate): unknown conversion"

This problem was due to the ordering of init() functions. init
functions are called according to the lexicographic order of their
containing files. zz_generated.conversion.go registers the conversion
functions with the scheme, but did so after the init() function
previously held in defaults.go due to the aforementioned ordering. This
left the Scheme used in ToVersionless without the conversion functions
it was expected to have.

Rather than hack this ordering to ensure a correctly populated
localSchemeBuilder, this PR makes a schemeBuilder with the explicit
purpose of using it in the Scheme that's used in ToVersionless. This
decouples the scheme from the ordering of init() funcs, resolving the
issue.

This PR also adds unit tests for each of the ToVersionless funcs,
ensuring such a problem won't happen again.

Signed-off-by: juliankatz juliankatz@google.com

PR open-policy-agent#138 introduced a bug: the type conversion functions required by the
Scheme for use in the ToVersionless functions of the v1, v1beta1,
v1alpha1 packages were missing.  This manifested itself as an error in
Gatekeeper:

"unable to convert template: converting (v1beta1.ConstraintTemplate) to
(templates.ConstraintTemplate): unknown conversion"

This problem was due to the ordering of init() functions.  init
functions are called according to the lexicographic order of their
containing files. zz_generated.conversion.go registers the conversion
functions with the scheme, but did so after the init() function
previously held in defaults.go due to the aforementioned ordering.  This
left the Scheme used in ToVersionless without the conversion functions
it was expected to have.

Rather than hack this ordering to ensure a correctly populated
`localSchemeBuilder`, this PR makes a schemeBuilder with the explicit
purpose of using it in the Scheme that's used in ToVersionless.  This
decouples the scheme from the ordering of init() funcs, resolving the
issue.

This PR also adds unit tests for each of the ToVersionless funcs,
ensuring such a problem won't happen again.

Signed-off-by: juliankatz <juliankatz@google.com>
@codecov-commenter
Copy link

Codecov Report

Merging #142 (c6f3e0f) into master (7a19e4e) will increase coverage by 0.50%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #142      +/-   ##
==========================================
+ Coverage   41.51%   42.02%   +0.50%     
==========================================
  Files          52       55       +3     
  Lines        3122     3127       +5     
==========================================
+ Hits         1296     1314      +18     
+ Misses       1435     1419      -16     
- Partials      391      394       +3     
Flag Coverage Δ
unittests 42.02% <ø> (+0.50%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...agent/frameworks/constraint/pkg/schema/fixtures.go 0.00% <0.00%> (ø)
...s/constraint/pkg/apis/templates/v1alpha1/scheme.go 33.33% <0.00%> (ø)
...meworks/constraint/pkg/apis/templates/v1/scheme.go 33.33% <0.00%> (ø)
...ks/constraint/pkg/apis/templates/v1beta1/scheme.go 33.33% <0.00%> (ø)
...works/constraint/pkg/apis/templates/v1/defaults.go 55.55% <0.00%> (+18.71%) ⬆️
.../constraint/pkg/apis/templates/v1beta1/defaults.go 55.55% <0.00%> (+18.71%) ⬆️
...constraint/pkg/apis/templates/v1alpha1/defaults.go 55.55% <0.00%> (+18.71%) ⬆️
...eworks/constraint/pkg/apis/templates/v1/helpers.go 66.66% <0.00%> (+66.66%) ⬆️
...s/constraint/pkg/apis/templates/v1beta1/helpers.go 66.66% <0.00%> (+66.66%) ⬆️
.../constraint/pkg/apis/templates/v1alpha1/helpers.go 66.66% <0.00%> (+66.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a19e4e...c6f3e0f. Read the comment docs.

Copy link
Member

@sozercan sozercan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! LGTM

// registered with the localSchemeBuilder by the time this init() function
// runs. We sidestep this problem by adding RegisterConversions here.
schemeBuilder := runtime.NewSchemeBuilder(localSchemeBuilder...)
schemeBuilder.Register(RegisterConversions)
Copy link
Contributor

@maxsmythe maxsmythe Oct 5, 2021

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 the sch scheme.

Copy link
Member

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:

var schemeBuilder = runtime.NewSchemeBuilder(fn1, fn2, fn3, ...)

Copy link
Contributor Author

@julianKatz julianKatz Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can move this to the helpers.go package, since this is the only thing that should depend on the sch scheme.

The defaults.go file also depends on the Scheme, as the scheme is used when creating the structuralSchema. I thought the separate file would prevent the assumption that the scheme only belongs to one thing.

Copy link
Contributor Author

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:

var schemeBuilder = runtime.NewSchemeBuilder(fn1, fn2, fn3, ...)

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 in register.go: https://github.com/open-policy-agent/frameworks/blob/master/constraint/pkg/apis/templates/v1/register.go#L39

As @maxsmythe suggests, not using localSchemeBuilder's contents directly has a possible benefit. The difference between what I have here and writing out SchemeBuilder.AddToScheme, addDefaultingFuncs is that if localSchemeBuilder 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:

  1. All package variables instantiated
  2. All package init() functions run

Basically I can reliably use any package variable, but I can't rely on other init() funcs. To truly not rely on other init() 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:

  1. Relying on init() function ordering
  2. maintaining a separate list of items that should be registered to the scheme from the one that external packages will receive when using our package's external AddToScheme function.

Copy link
Member

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.

Copy link
Member

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,

var toVersionlessSchemeBuilder = runtime.NewSchemeBuilder(...)

Copy link
Contributor

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 the structuralSchema. I thought the separate file would prevent the assumption that the scheme only belongs to one thing.

ack, SGTM

is that if localSchemeBuilder 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.

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

Copy link
Contributor Author

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,

var toVersionlessSchemeBuilder = runtime.NewSchemeBuilder(...)

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 other init() functions allows me to take advantage of other package level variables while still isolating my schemeBuilder from side-effects.

Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after changes

… fix-versionless-schema

Signed-off-by: juliankatz <juliankatz@google.com>
constraint/pkg/apis/templates/v1/scheme.go Outdated Show resolved Hide resolved
constraint/pkg/apis/templates/v1/scheme.go Show resolved Hide resolved
Signed-off-by: juliankatz <juliankatz@google.com>
@julianKatz julianKatz merged commit a8579b6 into open-policy-agent:master Oct 12, 2021
@julianKatz julianKatz deleted the fix-versionless-schema branch October 12, 2021 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants