kubernetes: `k8s.io/code-generator` scripts do not work from repos that use new Go modules

When invoking generate-groups.sh from a repo that uses the new Golang v1.11 modules, none of the codegens work as they assume that the repo layout has the code in $GOPATH/src/k8s.io/code-generator and that dependencies have been downloaded for this project (not doable using go mod easily).

Steps to reproduce:

$ docker run -it --rm golang:1.11rc1 /bin/bash
root@5f5497fb59c9:/go# mkdir /foo && cd /foo && go mod init foo
go: creating new go.mod: module foo

root@5f5497fb59c9:/foo# go get -u k8s.io/code-generator
go: finding k8s.io/code-generator latest
go: downloading k8s.io/code-generator v0.0.0-20180806210827-caff7734d7c0

root@5f5497fb59c9:/foo# find / -name 'generate-groups.sh'
/go/pkg/mod/k8s.io/code-generator@v0.0.0-20180806210827-caff7734d7c0/generate-groups.sh

root@5f5497fb59c9:/foo# /go/pkg/mod/k8s.io/code-generator@v0.0.0-20180806210827-caff7734d7c0/generate-groups.sh all foo foo bar
bash: /go/pkg/mod/k8s.io/code-generator@v0.0.0-20180806210827-caff7734d7c0/generate-groups.sh: Permission denied

Changing the executable flag doesn’t help as the script goes into that directory and runs go install which in this case cannot complete due to this module not having installable dependencies with go mod:

root@5f5497fb59c9:/foo# chmod +x /go/pkg/mod/k8s.io/code-generator@v0.0.0-20180806210827-caff7734d7c0/generate-groups.sh

root@5f5497fb59c9:/foo# /go/pkg/mod/k8s.io/code-generator@v0.0.0-20180806210827-caff7734d7c0/generate-groups.sh all foo foo bar
cmd/defaulter-gen/main.go:48:2: cannot find package "github.com/golang/glog" in any of:
        /usr/local/go/src/github.com/golang/glog (from $GOROOT)
        /go/src/github.com/golang/glog (from $GOPATH)
cmd/defaulter-gen/main.go:49:2: cannot find package "github.com/spf13/pflag" in any of:
        /usr/local/go/src/github.com/spf13/pflag (from $GOROOT)
        /go/src/github.com/spf13/pflag (from $GOPATH)
cmd/defaulter-gen/main.go:50:2: cannot find package "k8s.io/gengo/args" in any of:
        /usr/local/go/src/k8s.io/gengo/args (from $GOROOT)
        /go/src/k8s.io/gengo/args (from $GOPATH)
cmd/defaulter-gen/main.go:51:2: cannot find package "k8s.io/gengo/examples/defaulter-gen/generators" in any of:
        /usr/local/go/src/k8s.io/gengo/examples/defaulter-gen/generators (from $GOROOT)
        /go/src/k8s.io/gengo/examples/defaulter-gen/generators (from $GOPATH)
cmd/deepcopy-gen/main.go:52:2: cannot find package "k8s.io/gengo/examples/deepcopy-gen/generators" in any of:
        /usr/local/go/src/k8s.io/gengo/examples/deepcopy-gen/generators (from $GOROOT)
        /go/src/k8s.io/gengo/examples/deepcopy-gen/generators (from $GOPATH)

It seems that this can get further (until full go modules support) by adding a go.mod to the base of this repo and making the script executable but even then, the pathing is broken in these:

root@26e1c7ae81c1:/foo# cd ..                                      
root@26e1c7ae81c1:/# git clone https://github.com/kubernetes/code-generator.git
Cloning into 'code-generator'...                                            
...     

root@26e1c7ae81c1:/# cd code-generator/

root@26e1c7ae81c1:/code-generator# go mod init                           
go: creating new go.mod: module k8s.io/code-generator                        
go: copying requirements from Godeps/Godeps.json

root@26e1c7ae81c1:/code-generator# cd /foo                             
root@26e1c7ae81c1:/foo# chmod +x /code-generator/generate-groups.sh

root@26e1c7ae81c1:/foo# /code-generator/generate-groups.sh all foo foo bar
go: finding github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2
...
go: downloading golang.org/x/tools v0.0.0-20170428054726-2382e3994d48
Generating deepcopy funcs
F0817 20:44:40.912251     845 main.go:81] Error: Failed making a parser: unable to add directory "-O": unable to import "-O": go/build: importGo -O: exit status 2
flag provided but not defined: -O
usage: go list [-f format] [-json] [-m] [list flags] [build flags] [packages]
Run 'go help list' for details.

/kind bug /sig api-machinery

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@nikhita has the way to use code-generator changed at all? does it support go modules? I see apimachinery already does