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

🐛 Change the type of markers.Collector.byPackage's key from string to *loader.Package #792

Merged
merged 2 commits into from Jul 12, 2023

Conversation

ntoofu
Copy link
Contributor

@ntoofu ntoofu commented Apr 8, 2023

Fix #783.

As I described in #783 (comment) , this issue is caused by using Package.ID as a key for cache of markers. This PR adds a testcase to reproduce the issue and fixes it by using Package itself instead.

@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 8, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @ntoofu!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 8, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @ntoofu. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 8, 2023
Markers might be lost in generated CRD when the package containing
markers is referenced by several packages and CRD is generated by
those packages.

For more details, see kubernetes-sigs#783.
Markers are saved per `*ast.TypeSpec`, which is specific to `Package` object.
`*ast.TypeSpec` in another `Package` object is different even when
the `Package.ID` is the same, and so collected markers cannot be reused
for another `Package` object.

Therefore, `Package` itself is used as a key for cache of markers
insetead of `Package.ID`.

For more details about the bug, see kubernetes-sigs#783.
@ntoofu ntoofu changed the title 🐛 Fix #783 🐛 Change the type of markers.Collector.byPackage's key from string to *loader.Package Apr 9, 2023
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Apr 9, 2023
@camilamacedo86
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 19, 2023
@randomvariable
Copy link
Member

@sbueringer
Copy link
Member

Thx for the extensive explanation here: #783 (comment)

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 11, 2023
@@ -31,7 +31,7 @@ import (
type Collector struct {
*Registry

byPackage map[string]map[ast.Node]MarkerValues
byPackage map[*loader.Package]map[ast.Node]MarkerValues
Copy link
Member

Choose a reason for hiding this comment

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

This stores the package based on the pointer's value, is there any other way that doesn't rely on where the memory is allocated?

Copy link
Contributor Author

@ntoofu ntoofu Jul 12, 2023

Choose a reason for hiding this comment

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

Thank you for your reviewing!

There is possibly another way, but it may be complicated.

byPackage has two keys, package ID (string) and node (ast.Node). Package ID is not a pointer's value, while node is an interface and can be a struct containing pointers. That inconsistency is the root cause of this issue.

In the problematic situation, exactly the same package is loaded several times. Among each load, the package ID is the same but nodes are different objects. Parsing markers is skipped when the same package ID is already processed. Therefore, byPackage's node keys are created only at the first time. As a result, accessing byPackage using node as a key fails from the second load of the package.

To avoid to use pointer's value as byPackage's key, I think we have to do one of the following.

  • Stop using ast.Node as a key (but I have no idea about the alternative)
  • Do not skip parsing markers even if a package with the same ID is already parsed
  • Stop loading the same package multiple times (but I have no idea why the multiple load occurs)

Copy link
Member

Choose a reason for hiding this comment

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

Yup agree. The problem is definitely that we cache the markers (with pointers) by package ID and then try to use them for another package (which has different ast nodes).

(Basically byPackage is map[string]map[ast.Node][]markerComment, where ast.Node is an interface and can be e.g. *ast.File, *ast.Field, ...)

Copy link
Member

Choose a reason for hiding this comment

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

I think it should be not worse to use *loader.Package as a map key then it already is to use the ast.Node interface (aka e.g. *ast.File) as a map key one layer deeper)

Copy link
Member

Choose a reason for hiding this comment

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

That's fair, it's a bit triggering though in review 😄

Copy link
Member

Choose a reason for hiding this comment

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

Yeah 100% :)

@vincepri
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ntoofu, vincepri

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 12, 2023
@k8s-ci-robot k8s-ci-robot merged commit a9f7560 into kubernetes-sigs:master Jul 12, 2023
6 checks passed
@sbueringer
Copy link
Member

/cherry-pick release-0.11

@k8s-infra-cherrypick-robot

@sbueringer: new pull request created: #833

In response to this:

/cherry-pick release-0.11

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sbueringer
Copy link
Member

Oh don't need the fix in release-0.11 (although no objection to merge there).

Just missed that we have a v0.12 release because we don't have a release branch for v0.12 yet

@sbueringer
Copy link
Member

I created a release-0.12 branch based on v0.12.0

/cherry-pick release-0.12

@k8s-infra-cherrypick-robot

@sbueringer: new pull request created: #834

In response to this:

I created a release-0.12 branch based on v0.12.0

/cherry-pick release-0.12

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing x-kubernetes-map-type when multiple paths= are specified
7 participants