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

panic on type casting *types.Named using Go 1.23 because of new *types.Alias #1053

Closed
mtardy opened this issue Sep 12, 2024 · 6 comments · Fixed by #1061
Closed

panic on type casting *types.Named using Go 1.23 because of new *types.Alias #1053

mtardy opened this issue Sep 12, 2024 · 6 comments · Fixed by #1061
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.

Comments

@mtardy
Copy link
Contributor

mtardy commented Sep 12, 2024

Using it with Go 1.22 works and break with Go 1.23, I think it's related to this:

In the latest version of go, a change was made to the generation of Alias types. 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.

When using Go 1.23 and tools/controller-gen to generates my CRDs I get the following panic:

panic: interface conversion: types.Type is *types.Alias, not *types.Named

goroutine 1 [running]:
sigs.k8s.io/controller-tools/pkg/crd.localNamedToSchema(0x400186f028, 0x400098b340)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:269 +0x4ac
sigs.k8s.io/controller-tools/pkg/crd.mapToSchema(0x4001b0d6b0, 0x400097b410)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:346 +0x36c
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x4001b0d6b0, {0xa38628, 0x400097b410})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:203 +0x74
sigs.k8s.io/controller-tools/pkg/crd.structToSchema(0x400186f518, 0x4000975c50)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:455 +0xa3c
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x400186f518, {0xa38538, 0x4000975c50})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:207 +0x90
sigs.k8s.io/controller-tools/pkg/crd.infoToSchema(0x400186f518)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:125 +0xcc
sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedSchemaFor(0x4000116120, {0x40004829a0, {0x40005c3a50, 0xd}})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go:193 +0x1e8
sigs.k8s.io/controller-tools/pkg/crd.(*schemaContext).requestSchema(0x40016d6a80?, {0x4000351a40?, 0x8e6a4b?}, {0x40005c3a50?, 0xd?})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:108 +0xd8
sigs.k8s.io/controller-tools/pkg/crd.namedToSchema(0x40016d6a80, 0x400050ec18)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:292 +0x1bc
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40016d6a80, {0xa38598, 0x400050ec18})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:199 +0xec
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40016d6a80, {0xa385f8, 0x400050ec30})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:205 +0xb4
sigs.k8s.io/controller-tools/pkg/crd.structToSchema(0x400186fda8, 0x400050ede0)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:455 +0xa3c
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x400186fda8, {0xa38538, 0x400050ede0})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:207 +0x90
sigs.k8s.io/controller-tools/pkg/crd.infoToSchema(0x400186fda8)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:125 +0xcc
sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedSchemaFor(0x4000116120, {0x40004827c0, {0x40004f15d8, 0x11}})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go:193 +0x1e8
sigs.k8s.io/controller-tools/pkg/crd.(*schemaContext).requestSchema(0x4000351b00?, {0x0?, 0x8e6a4b?}, {0x40004f15d8?, 0x0?})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:108 +0xd8
sigs.k8s.io/controller-tools/pkg/crd.localNamedToSchema(0x40016728d0, 0x400007dae0)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:275 +0x168
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40016728d0, {0xa38568, 0x400007dae0})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:197 +0xd0
sigs.k8s.io/controller-tools/pkg/crd.structToSchema(0x40018705f8, 0x400050e708)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:455 +0xa3c
sigs.k8s.io/controller-tools/pkg/crd.typeToSchema(0x40018705f8, {0xa38538, 0x400050e708})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:207 +0x90
sigs.k8s.io/controller-tools/pkg/crd.infoToSchema(0x40018705f8)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go:125 +0xcc
sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedSchemaFor(0x4000116120, {0x40004827c0, {0x4000697540, 0xd}})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go:193 +0x1e8
sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedFlattenedSchemaFor(0x4000116120, {0x40004827c0, {0x4000697540, 0xd}})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go:205 +0x9c
sigs.k8s.io/controller-tools/pkg/crd.(*Parser).NeedCRDFor(0x4000116120, {{0x400089400e, 0x9}, {0x4000697540, 0xd}}, 0x0)
        /src/pkg/k8s/vendor/sigs.k8s.io/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, ...}, ...}, ...)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/crd/gen.go:182 +0x464
sigs.k8s.io/controller-tools/pkg/genall.(*Runtime).Run(0x40000cb0e0)
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/pkg/genall/genall.go:272 +0x21c
main.main.func1(0x4000272200?, {0x40002c1b30?, 0x4?, 0x8e4bdc?})
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go:176 +0x64
github.com/spf13/cobra.(*Command).execute(0x4000282c08, {0x4000032110, 0x3, 0x3})
        /src/pkg/k8s/vendor/github.com/spf13/cobra/command.go:985 +0x834
github.com/spf13/cobra.(*Command).ExecuteC(0x4000282c08)
        /src/pkg/k8s/vendor/github.com/spf13/cobra/command.go:1117 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
        /src/pkg/k8s/vendor/github.com/spf13/cobra/command.go:1041
main.main()
        /src/pkg/k8s/vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go:200 +0x290
exit status 2

You can use the project I'm working on for repro https://github.com/cilium/tetragon/actions/runs/10808612794/job/29981972633?pr=2800. Basically clone the project with that branch and run make in pkg/k8s.

@sbueringer
Copy link
Member

Thx for reporting. That sounds pretty bad

@sbueringer
Copy link
Member

If someone has time to take a look at this feel free to go ahead / open a PR

/help

@k8s-ci-robot
Copy link
Contributor

@sbueringer:
This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

  • Why are we solving this issue?
  • To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
  • Does this issue have zero to low barrier of entry?
  • How can the assignee reach out to you for help?

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.

In response to this:

If someone has time to take a look at this feel free to go ahead / open a PR

/help

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 the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label Sep 12, 2024
@sbueringer
Copy link
Member

@mtardy You are compiling controller-gen yourself, right?

Does the published binary work? (https://github.com/kubernetes-sigs/controller-tools/releases/tag/v0.16.2)

@mtardy
Copy link
Contributor Author

mtardy commented Sep 12, 2024

@mtardy You are compiling controller-gen yourself, right?

Does the published binary work? (https://github.com/kubernetes-sigs/controller-tools/releases/tag/v0.16.2)

yep I have it vendored and use it. I would assume the published binary to work since it's built with 1.22

go 1.22.0

I can try however :).

Thx for reporting. That sounds pretty bad

I hope I'm not too alarming here if the issue is based on my config (I saw #492). But the fact that this thing only breaks when I bump the Go version in the go.mod directive, and that Golang has an entry in the 1.23 changelog related to this makes me think it can be a general issue.

@mtardy
Copy link
Contributor Author

mtardy commented Sep 16, 2024

Okay I have a minimal reproducer and a fix, I'll post a PR just after (EDIT: here), here's how to reproduce the issue:

  1. Create a a repro directory mkdir repro and put repro.go inside of it with:

    // +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
  2. Bump the go directive to Go 1.23.0 in go.mod:

    diff --git a/go.mod b/go.mod
    index 790a5a11..c9e19176 100644
    --- a/go.mod
    +++ b/go.mod
    @@ -1,6 +1,6 @@
     module sigs.k8s.io/controller-tools
    
    -go 1.22.0
    + go 1.23.0
  3. Run controller-gen with:

    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/[email protected]/command.go:985 +0x834
github.com/spf13/cobra.(*Command).ExecuteC(0x40002aec08)
        /home/mtardy.linux/go/pkg/mod/github.com/spf13/[email protected]/command.go:1117 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
        /home/mtardy.linux/go/pkg/mod/github.com/spf13/[email protected]/command.go:1041
main.main()
        /home/mtardy.linux/controller-tools/cmd/controller-gen/main.go:200 +0x290
exit status 2

@mtardy mtardy changed the title panic on interface conversion using Go 1.23 panic on type casting *types.Named using Go 1.23 because of new *types.Alias Sep 16, 2024
mtardy added a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
mtardy added a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
mtardy added a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
mtardy added a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
mtardy added a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
jrfastab pushed a commit to cilium/tetragon that referenced this issue Sep 30, 2024
This directive is not needed as we don't use any of the customization
implemented in the Cilium fork, we need this for kubernetes-sigs/controller-tools#1053.

Signed-off-by: Mahe Tardy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.
Projects
None yet
3 participants