operator-sdk: sigsegv if GOROOT is not set

Bug Report

When running an

operator-sdk add api --api-version=hello.k8spatterns.io/v1alpha1 --kind=Hello

in a module enabled project below $GOPATH, and $GOROOT is not set, then the following errors occurs:

operator-sdk add api --api-version=hello.k8spatterns.io/v1alpha1 --kind=Hello
INFO[0000] Generating api version hello.k8spatterns.io/v1alpha1 for kind Hello.
INFO[0001] Created pkg/apis/hello/v1alpha1/hello_types.go
INFO[0001] Created pkg/apis/addtoscheme_hello_v1alpha1.go
INFO[0001] Created pkg/apis/hello/v1alpha1/register.go
INFO[0001] Created pkg/apis/hello/v1alpha1/doc.go
INFO[0001] Created deploy/crds/hello_v1alpha1_hello_cr.yaml
W0611 10:07:38.859903   39295 parse.go:224] Ignoring directory github.com/k8spatterns/hello-operator/pkg/apis: unable to import "github.com/k8spatterns/hello-operator/pkg/apis": go/build: importGo github.com/k8spatterns/hello-operator/pkg/apis: exit status 2
go: cannot find GOROOT directory: /usr/local/go

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1760595]

goroutine 1 [running]:
github.com/operator-framework/operator-sdk/vendor/k8s.io/gengo/parser.(*Builder).AddDirRecursive(0xc000962230, 0xc0006b71d0, 0x2e, 0x2, 0x2)
	src/github.com/operator-framework/operator-sdk/vendor/k8s.io/gengo/parser/parse.go:229 +0xb5
github.com/operator-framework/operator-sdk/vendor/sigs.k8s.io/controller-tools/pkg/crd/generator.(*Generator).Do(0xc00092d760, 0x0, 0x0)
	src/github.com/operator-framework/operator-sdk/vendor/sigs.k8s.io/controller-tools/pkg/crd/generator/generator.go:126 +0x3ac
github.com/operator-framework/operator-sdk/internal/pkg/scaffold.(*CRD).CustomRender(0xc0001b11e0, 0x2cca880, 0x41d87c0, 0x4f814e8, 0xc0001b11e0, 0x1)
	src/github.com/operator-framework/operator-sdk/internal/pkg/scaffold/crd.go:122 +0x868
github.com/operator-framework/operator-sdk/internal/pkg/scaffold.(*Scaffold).doRender(0xc00092dcf8, 0xc0006b7110, 0x29, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0003e4db1, 0x25, ...)
	src/github.com/operator-framework/operator-sdk/internal/pkg/scaffold/scaffold.go:228 +0x16c
github.com/operator-framework/operator-sdk/internal/pkg/scaffold.(*Scaffold).doFile(0xc00092dcf8, 0x2c4f2c0, 0xc0001b11e0, 0x0, 0x0)
	src/github.com/operator-framework/operator-sdk/internal/pkg/scaffold/scaffold.go:198 +0x230
github.com/operator-framework/operator-sdk/internal/pkg/scaffold.(*Scaffold).Execute(0xc00092dcf8, 0xc000925c68, 0xc00092dc98, 0x6, 0x6, 0x0, 0x0)
	src/github.com/operator-framework/operator-sdk/internal/pkg/scaffold/scaffold.go:163 +0x103
github.com/operator-framework/operator-sdk/cmd/operator-sdk/add.apiRun(0xc0003b8000, 0xc00000d020, 0x0, 0x2, 0x0, 0x0)
	src/github.com/operator-framework/operator-sdk/cmd/operator-sdk/add/api.go:107 +0x4d2
github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra.(*Command).execute(0xc0003b8000, 0xc00000cfc0, 0x2, 0x2, 0xc0003b8000, 0xc00000cfc0)
	src/github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra/command.go:762 +0x465
github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc000361b80, 0x2c53720, 0xc0000f1a00, 0x0)
	src/github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra/command.go:852 +0x2ec
github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra.(*Command).Execute(...)
	src/github.com/operator-framework/operator-sdk/vendor/github.com/spf13/cobra/command.go:800
main.main()
	src/github.com/operator-framework/operator-sdk/cmd/operator-sdk/main.go:80 +0x4ce

I would expect either to work out of the box (i’m using modules anyway) or a clear simple pre-run check the GOROOT needs to be set.

(ideally I would prefer to run outside of $GOPATH but this is prevented by #1457 )

Environment

  • operator-sdk version:

operator-sdk version: v0.8.1, commit: 33b3bfe10176f8647f5354516fff29dea42b6342

env | grep GO
GOPATH=/Users/roland/Development/go/workspace
GO111MODULE=on
  • go version:
go version go1.12.4 darwin/amd64
  • Kubernetes version information:
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-19T22:12:47Z", GoVersion:"go1.12.4", Compiler:"gc", Platform:"darwin/amd64"}
  • Kubernetes cluster kind: ----

  • Are you writing your operator in ansible, helm, or go?

go

Possible Solution

Pre-check with clear error message indicating what is wrong or missing.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, I could fix it by setting GOROOT. I just think that its not a nice UX to get a SIGSEGV when not setting GOROOT in my case. If there would have been an error message like ‘Please set your GOROOT to your go installation’ that would be totally fine.

I meant that I had no GOROOT set at all. I would expect an error message pointing me to set my GOROOT env var, but not a SIGSEGV. That’s all.

@rhuss There is a line in your log with go: cannot find GOROOT directory: /usr/local/go, but I agree that it isn’t nice to panic.

Unfortunately, it looks like the k8s.io/gengo library is the source of this bug, and I would hesitate to add extra checks in the operator-sdk to guard against misconfigured Go environments.

I submitted an issue upstream to see if this can be improved: https://github.com/kubernetes/gengo/issues/146