'FATA[0004] failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v3": exit status 2

I am trying to create an operator using operator-sdk.

I have installed opeator-sdk on my mac OS.

My Environment Details :

go version go1.15.12 darwin/amd64

operator-sdk version: "v1.7.2", commit: "6db9787d4e9ff63f344e23bfa387133112bda56b", kubernetes version: "v1.19.4", go version: "go1.16.3", GOOS: "darwin", GOARCH: "amd64"

I am trying to create an operator using command -

operator-sdk init hello-operator

I have enabled GO111MODULE.

When I am trying to run opeator-sdk init , I am getting following error.

Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
Get controller runtime:
$ go get sigs.k8s.io/[email protected]
# container/list
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# crypto/internal/subtle
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# unicode/utf8
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# internal/race
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# k8s.io/apimachinery/pkg/selection
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# encoding
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# unicode/utf16
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# internal/nettrace
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# math/bits
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# runtime/internal/sys
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# internal/unsafeheader
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# unicode
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# vendor/golang.org/x/crypto/internal/subtle
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# crypto/subtle
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# vendor/golang.org/x/crypto/cryptobyte/asn1
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# golang.org/x/sys/internal/unsafeheader
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# runtime/internal/atomic
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# google.golang.org/protobuf/internal/flags
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# github.com/google/go-cmp/cmp/internal/flags
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# k8s.io/utils/integer
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# k8s.io/utils/buffer
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# internal/cpu
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# k8s.io/apimachinery/pkg/types
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# sync/atomic
compile: version "go1.15.6" does not match go tool version "go1.15.12"
# runtime/cgo
compile: version "go1.15.6" does not match go tool version "go1.15.12"
Error: failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v3": exit status 2

FATA[0003] failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v3": exit status 2

Does anybody has any idea about this?

Thanks in advance.



Solution 1:[1]

After some time spending on my go environment setup I was able to figure out. I had link go with different version.

I changed my GOROOT to -> /usr/local/opt/[email protected]/libexec and then I was able to init the opeartor.

Solution 2:[2]

Check your GOPATH folder permissions. In my case, kubebuilder had access denied and showed the same error you found.

You can also try to reinstall GO Lang in a different folder and see what happens.

Solution 3:[3]

I had same problem.

I ran go env and saw that the path permissions were incorrect.

Once I fixed them, I was able to proceed

Solution 4:[4]

upgrade go verion to go1.16.3, the same in output of operator-sdk version

Solution 5:[5]

May be you can try by running this command:

export GO111MODULE=on

Solution 6:[6]

The bellow commands show how you can run and scaffold an operator with the operator-sdk CLI tool. As of writing the latest version is v1.20.0. It covers some of the pitfalls such as setting the correct GO environment variables and installing the gcc for some OS. I tried it with Ubuntu 18.04.3 LTS (Bionic Beaver)

#golang
echo "--- Installing golang ---"

GOVERSION=1.17.9
GOTAR=go$GOVERSION.linux-amd64.tar.gz
wget https://dl.google.com/go/$GOTAR
sudo tar -xvf $GOTAR
rm $GOTAR

sudo mv go /usr/local/bin

#gcc (used by operator-sdk CLI)
echo "--- Installing gcc ---"

sudo apt update
sudo apt install -y build-essential
sudo apt-get install manpages-dev

#operator-sdk
echo "--- Installing operator-sdk ---"

curl -Lo ./operator-sdk https://github.com/operator-framework/operator-sdk/releases/download/v1.20.0/operator-sdk_linux_amd64
chmod +x ./operator-sdk
sudo mv ./operator-sdk /usr/local/bin/operator-sdk

#environment variables
export GOROOT=/usr/local/bin/go
export PATH=$GOROOT/bin:$PATH

#verify versions
go version
operator-sdk version

#scaffold and run the HelloWorld operator
sudo -s
mkdir hello-operator
chmod 777 hello-operator
cd hello-operator

operator-sdk init --domain example.com --repo github.com/example/memcached-operator

operator-sdk create api --group example --version v1alpha1 --kind HelloWorld --resource --controller

make manifests
make run

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajendra Gosavi
Solution 2 Francisco Cardoso
Solution 3 Scott Hebert
Solution 4 Nick Huang
Solution 5 Suraj Rao
Solution 6