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 schemagen canonical group determination to be more specific #3237

Merged
merged 5 commits into from
Sep 30, 2024

Conversation

rquitales
Copy link
Member

@rquitales rquitales commented Sep 30, 2024

Proposed Changes

This PR includes a major refactor of the createGroups function in the gen package and addresses a bug that could lead to map key collisions when storing groups derived from swagger paths. Previously, consumers of the gen package could encounter key collisions when different CRDs used the same key. For instance, both io.k8s.api.apps and com.testpackage.apps would result in the key apps. This PR resolves this by using the full group name (e.g., io.k8s.api.apps) as the map key instead of just the last segment. Since no such collisions exist in the standard Kubernetes GVKs, no schema changes are expected.

To support these changes, the typegen.go file has been significantly refactored. The createGroups function has been modularized for easier testing and maintainability. Additionally, the go-linq dependency has been removed in favor of idiomatic Go code, allowing for static type checking and reducing runtime type errors.

Changes Made:

  • Refactored createGroups into modular subfunctions
  • Increased test coverage of the gen package from 36.8% to 49.6% with new tests
  • Replaced github.com/ahmetb/go-linq with idiomatic Go for better maintainability and static type checking
  • Fixed map key creation to use the full group name, preventing collisions, with no schema changes required in the p-k provider

Related Issues (optional)

Fixes: #3227 and resolves pulumi/crd2pulumi#152

Running `make schema` after these changes does not result in any schema
changes. This is purely a refactor of code with no output impact.
@rquitales rquitales self-assigned this Sep 30, 2024
Copy link

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

Copy link

codecov bot commented Sep 30, 2024

Codecov Report

Attention: Patch coverage is 75.56561% with 54 lines in your changes missing coverage. Please review.

Project coverage is 42.20%. Comparing base (8a0d0c3) to head (fc8c192).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
provider/pkg/gen/typegen.go 74.88% 37 Missing and 17 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3237      +/-   ##
==========================================
+ Coverage   40.84%   42.20%   +1.35%     
==========================================
  Files          84       84              
  Lines        9876     9878       +2     
==========================================
+ Hits         4034     4169     +135     
+ Misses       5459     5325     -134     
- Partials      383      384       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Increase test coverage in package from 28% to 31%.
This commit refactors our logic to use idiomatic Go for data processing
when parsing Swagger specs. It is ensured that tests still passes, and
the generated schema has no diffs.
@rquitales rquitales force-pushed the rquitales/fix-schemagen-namespacing branch from 14ee67d to a5873e0 Compare September 30, 2024 21:06
@@ -260,7 +260,7 @@ func GVKFromRef(ref string) schema.GroupVersionKind {
gvk := schema.GroupVersionKind{
Kind: split[len(split)-1],
Version: split[len(split)-2],
Group: split[len(split)-3],
Group: strings.Join(split[:len(split)-2], "."),
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the main behavior change. We now store io.k8s.api.apps as the key in the canonicalGroup map. Previously, this was apps. This is an issue for downstream consumers of this package since io.k8s.api.apps and com.mytestpackage.apps would have collided, and resulted in incorrectly generated package names.

@@ -463,339 +463,360 @@ func makeSchemaType(prop map[string]any, canonicalGroups map[string]string) stri
// --------------------------------------------------------------------------

func createGroups(definitionsJSON map[string]any, allowHyphens bool) []GroupConfig {
Copy link
Member Author

Choose a reason for hiding this comment

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

This function was refactored by splitting it up into multiple sub-functions to make it easier to unit test each sub-action. This made it easier to track down what changes were required so that schema generation in p-k was ultimately not modified.

"strings"

"github.com/ahmetb/go-linq"
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing go-linq as a dependency and using idiomatic golang for data processing instead.

names

This commit ensures that we do not have package collisions when storing
canonical group names. Additional consideration is required to ensure
that there are no final schema changes so that we do not break v4 users.
It is not expected to have any package collisions in the p-k provider,
but is possible in the downstream crd2pulumi consure of our schemagen.
Here, users can supply any number of CRDs to typegen, so we need to be
specific when it comes to handling group names.
@rquitales rquitales force-pushed the rquitales/fix-schemagen-namespacing branch from a5873e0 to fc8c192 Compare September 30, 2024 21:42
@rquitales rquitales changed the title [WIP] Fix schemagen canonical group determination to be more specific Fix schemagen canonical group determination to be more specific Sep 30, 2024
@rquitales rquitales added the impact/no-changelog-required This issue doesn't require a CHANGELOG update label Sep 30, 2024
@rquitales rquitales marked this pull request as ready for review September 30, 2024 22:13
Copy link
Contributor

@blampe blampe left a comment

Choose a reason for hiding this comment

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

Additionally, the go-linq dependency has been removed in favor of idiomatic Go code, allowing for static type checking and reducing runtime type errors.

🙌 🙌 🙌

Amazing, thank you! Looks like the linq stuff was mostly glue and the underlying logic is essentially untouched 👍

@rquitales rquitales merged commit b7dae6b into master Sep 30, 2024
19 checks passed
@rquitales rquitales deleted the rquitales/fix-schemagen-namespacing branch September 30, 2024 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/no-changelog-required This issue doesn't require a CHANGELOG update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[schemagen] Parsing group from swagger definition is not specific enough Major issues since at least 1.5.2
2 participants