go get 安装 k8s.io/code-generator 报错 cannot find package
通过如下方式解决:
git clone https://github.com/golang/sys.git $GOPATH/src/github.com/golang/sys
git clone https://github.com/golang/net.git $GOPATH/src/github.com/golang/net
git clone https://github.com/golang/text.git $GOPATH/src/github.com/golang/text
git clone https://github.com/golang/lint.git $GOPATH/src/github.com/golang/lint
git clone https://github.com/golang/tools.git $GOPATH/src/github.com/golang/tools
git clone https://github.com/golang/crypto.git $GOPATH/src/github.com/golang/crypto
ln -s $GOPATH/src/github.com/golang/ $GOPATH/src/golang.org/x
碰到新问题
#go get -u k8s.io/code-generator/...
package context: unrecognized import path "context"
尝试升级golang版本
#wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
#tar -C /usr/local -xzf go1.14.2.linux-amd64.tar.gz
#export PATH=/usr/local/go/bin:$PATH
执行 go version,发现还是查找老的版本
$go version
go: cannot find GOROOT directory: /opt/golang/go1.5.3
原因可能是GOROOT这个环境变量的问题,尝试修改
$export GOROOT=/usr/local/go
$go version
go version go1.14.2 linux/amd64
再此执行 go get 成功
$ go get -u k8s.io/code-generator/...
最后执行完整代码
# 代码生成的工作目录,也就是我们的项目路径
$ ROOT_PACKAGE="github.com/resouer/k8s-controller-custom-resource"
# API Group
$ CUSTOM_RESOURCE_NAME="samplecrd"
# API Version
$ CUSTOM_RESOURCE_VERSION="v1"
# 安装k8s.io/code-generator
$ go get -u k8s.io/code-generator/...
$ cd $GOPATH/src/k8s.io/code-generator
# 执行代码自动生成,其中pkg/client是生成目标目录,pkg/apis是类型定义目录
$ ./generate-groups.sh all "$ROOT_PACKAGE/pkg/client" "$ROOT_PACKAGE/pkg/apis" "$CUSTOM_RESOURCE_NAME:$CUSTOM_RESOURCE_VERSION"