Skip to content

Conversation

@mtardy
Copy link
Member

@mtardy mtardy commented Sep 16, 2024

Fixes #1053

In the latest version of go, a change was made to the generation of Alias types by default. From the release notes:

By default, go/types now produces Alias type nodes for type aliases.
This behavior can be controlled by the GODEBUG gotypesalias flag. Its
default has changed from 0 in Go 1.22 to 1 in Go 1.23.

This provoked a panic in the localNamedToSchema function when processing any type alias becaused it was expecting only a *types.Named and not a *types.Alias. This can be fixed by using an anonymous interface to more broadly "ask" if the object supports the Obj() function instead of asking it to be a specific type. Types *types.Named and *types.Alias share this method so it can be used directly.

Note that it would be better (and would have made this easier to debug) to retrieve the "ok" second return value and print some error on failure to cast but this is a minimal working patch.

For example you can reproduce a panic, switching the project to use Go 1.23 (and thus gotypesalias to 1), putting the following file in repro/repro.go:

// +groupName=repro.io package repro import (	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type Repro struct {	metav1.TypeMeta `json:",inline"`	metav1.ObjectMeta `json:"metadata"`	Reproducer StringAlias `json:"reproducer"` } type StringAlias = string 

Then run:

go run ./cmd/controller-gen/ crd paths=./repro 

You should see something similar to:

panic: interface conversion: types.Type is *types.Alias, not *types.Named goroutine 1 [running]: sigs.k8s.io/controller-tools/pkg/crd.localNamedToSchema(0x4001315f50, 0x40003918e0)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:258 +0x3bc sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x4001315f50, {0xa391e8, 0x40003918e0})	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:197 +0xd0 sigs.k8s.io/controller-tools/pkg/crd.structToSchema(0x40009ee5f8, 0x400000e300)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:436 +0x7d8 sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40009ee5f8, {0xa391b8, 0x400000e300})	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:207 +0x90 sigs.k8s.io/controller-tools/pkg/crd.infoToSchema(0x40000505f8)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:125 +0xcc sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedSchemaFor(0x400009a120, {0x4000207b80, {0x40002c2540, 0x5}})	/home/mtardy.linux/controller-tools/pkg/crd/parser.go:193 +0x1e8 sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedFlattenedSchemaFor(0x400009a120, {0x4000207b80, {0x40002c2540, 0x5}})	/home/mtardy.linux/controller-tools/pkg/crd/parser.go:205 +0x9c sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedCRDFor(0x400009a120, {{0x4000185a06, 0x8}, {0x40002c2540, 0x5}}, 0x0)	/home/mtardy.linux/controller-tools/pkg/crd/spec.go:93 +0x3d8 sigs.k8s.io/controller-tools/pkg/crd.Generator.Generate({0x0, 0x0, 0x0, {0x0, 0x0, 0x0}, 0x0, {0x0, 0x0}, {0x0, ...}, ...}, ...)	/home/mtardy.linux/controller-tools/pkg/crd/gen.go:182 +0x464 sigs.k8s.io/controller-tools/pkg/genall.(*Runtime).Run(0x400015cbd0)	/home/mtardy.linux/controller-tools/pkg/genall/genall.go:272 +0x21c main.main.func1(0x40002a2200?, {0x40002d9ad0?, 0x4?, 0x8e519c?})	/home/mtardy.linux/controller-tools/cmd/controller-gen/main.go:176 +0x64 github.com/spf13/cobra.(*Command).execute(0x40002aec08, {0x4000136090, 0x3, 0x3})	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:985 +0x834 github.com/spf13/cobra.(*Command).ExecuteC(0x40002aec08)	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1117 +0x344 github.com/spf13/cobra.(*Command).Execute(...)	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1041 main.main()	/home/mtardy.linux/controller-tools/cmd/controller-gen/main.go:200 +0x290 exit status 2 
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 16, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @mtardy!

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
Copy link
Contributor

Hi @mtardy. 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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 16, 2024
@mtardy mtardy changed the title 🐛 pkg/crd: fix type casting panic with new default *types.Alias 🐛 pkg/crd: fix type casting panic with new default *types.Alias with Go 1.23 Sep 16, 2024
@mtardy mtardy changed the title 🐛 pkg/crd: fix type casting panic with new default *types.Alias with Go 1.23 🐛 pkg/crd: fix type casting panic with new default *types.Alias with Go 1.23 Sep 16, 2024
// NB(directxman12): if there are dot imports, this might be an external reference,
// so use typechecking info to get the actual object
typeNameInfo := typeInfo.(*types.Named).Obj()
typeNameInfo := typeInfo.(interface{ Obj() *types.TypeName }).Obj()

Choose a reason for hiding this comment

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

wow this is a nice one 😲

sayboras added a commit to sayboras/cilium that referenced this pull request Sep 20, 2024
This is the fastest way to test changes while waiting for the below upstream PR, just a note as double replacement will not work well in golang. Relates: kubernetes-sigs/controller-tools#1061 Signed-off-by: Tam Mach <tam.mach@cilium.io>
@k8s-ci-robot
Copy link
Contributor

@sayboras: changing LGTM is restricted to collaborators

In response to this:

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-sigs/prow repository.

@sbueringer
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 Sep 21, 2024
@sbueringer
Copy link
Member

@mtardy Thank you! Especially for the explanations and code to reproduce. Feel free to ping me once #1061 (comment) is resolved

@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. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 23, 2024
In the latest version of go, a change was made to the generation of Alias types by default. From the release notes: > By default, go/types now produces Alias type nodes for type aliases. > This behavior can be controlled by the GODEBUG gotypesalias flag. Its > default has changed from 0 in Go 1.22 to 1 in Go 1.23. This provoked a panic in the localNamedToSchema function when processing any type alias becaused it was expecting only a *types.Named and not a *types.Alias. This can be fixed by using an anonymous interface to more broadly "ask" if the object supports the Obj() function instead of asking it to be a specific type. Types *types.Named and *types.Alias share this method so it can be used directly. Note that it would be better (and would have made this easier to debug) to retrieve the "ok" second return value and print some error on failure to cast but this is a minimal working patch. For example you can reproduce a panic, switching the project to use Go 1.23 (and thus gotypesalias to 1), putting the following file in repro/repro.go:	// +groupName=repro.io	package repro	import (	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"	)	type Repro struct {	metav1.TypeMeta `json:",inline"`	metav1.ObjectMeta `json:"metadata"`	Reproducer StringAlias `json:"reproducer"`	}	type StringAlias = string Then run:	go run ./cmd/controller-gen/ crd paths=./repro You should see something similar to:	panic: interface conversion: types.Type is *types.Alias, not *types.Named	goroutine 1 [running]:	sigs.k8s.io/controller-tools/pkg/crd.localNamedToSchema(0x4001315f50, 0x40003918e0)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:258 +0x3bc	sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x4001315f50, {0xa391e8, 0x40003918e0})	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:197 +0xd0	sigs.k8s.io/controller-tools/pkg/crd.structToSchema(0x40009ee5f8, 0x400000e300)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:436 +0x7d8	sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40009ee5f8, {0xa391b8, 0x400000e300})	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:207 +0x90	sigs.k8s.io/controller-tools/pkg/crd.infoToSchema(0x40000505f8)	/home/mtardy.linux/controller-tools/pkg/crd/schema.go:125 +0xcc	sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedSchemaFor(0x400009a120, {0x4000207b80, {0x40002c2540, 0x5}})	/home/mtardy.linux/controller-tools/pkg/crd/parser.go:193 +0x1e8	sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedFlattenedSchemaFor(0x400009a120, {0x4000207b80, {0x40002c2540, 0x5}})	/home/mtardy.linux/controller-tools/pkg/crd/parser.go:205 +0x9c	sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedCRDFor(0x400009a120, {{0x4000185a06, 0x8}, {0x40002c2540, 0x5}}, 0x0)	/home/mtardy.linux/controller-tools/pkg/crd/spec.go:93 +0x3d8	sigs.k8s.io/controller-tools/pkg/crd.Generator.Generate({0x0, 0x0, 0x0, {0x0, 0x0, 0x0}, 0x0, {0x0, 0x0}, {0x0, ...}, ...}, ...)	/home/mtardy.linux/controller-tools/pkg/crd/gen.go:182 +0x464	sigs.k8s.io/controller-tools/pkg/genall.(*Runtime).Run(0x400015cbd0)	/home/mtardy.linux/controller-tools/pkg/genall/genall.go:272 +0x21c	main.main.func1(0x40002a2200?, {0x40002d9ad0?, 0x4?, 0x8e519c?})	/home/mtardy.linux/controller-tools/cmd/controller-gen/main.go:176 +0x64	github.com/spf13/cobra.(*Command).execute(0x40002aec08, {0x4000136090, 0x3, 0x3})	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:985 +0x834	github.com/spf13/cobra.(*Command).ExecuteC(0x40002aec08)	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1117 +0x344	github.com/spf13/cobra.(*Command).Execute(...)	/home/mtardy.linux/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1041	main.main()	/home/mtardy.linux/controller-tools/cmd/controller-gen/main.go:200 +0x290	exit status 2 Also added a test case to reproduce the error thanks to sbueringer. Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
@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 Sep 23, 2024
@mtardy mtardy requested a review from sbueringer September 23, 2024 08:11
sayboras added a commit to cilium/cilium that referenced this pull request Oct 17, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Oct 19, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Oct 27, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 1, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 1, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 5, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 7, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 15, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 15, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 25, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 27, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 28, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 28, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 28, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
sayboras added a commit to cilium/cilium that referenced this pull request Nov 28, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
github-merge-queue bot pushed a commit to cilium/cilium that referenced this pull request Nov 28, 2024
All files are re-generated again as there is change in generation tools. Relates: kubernetes/kubernetes#127271 Relates: cilium/deepequal-gen#7 Relates: kubernetes-sigs/controller-tools#1061 Relates: 1d9d51f Signed-off-by: Tam Mach <tam.mach@cilium.io>
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.

4 participants