我的电脑环境为windows,安装operator-sdk需要自己编译构建二进制文件。 参考官方文档Compile and install from master 官方给的Prerequisites为:
其中mercurial和bazaar我都没装,operator-sdk也编译安装成功了。
$ go get -d github.com/operator-framework/operator-sdk # This will download the git repository and not install it
$ cd $GOPATH/src/github.com/operator-framework/operator-sdk
$ git checkout master
$ make tidy
$ make install
$ operator-sdk version
operator-sdk version: "v0.15.0-33-ga1a9cbba", commit: "a1a9cbba09cb05914704c3229bc6df3f86269a56", go version: "go1.13.7 windows/amd64"
仍然参考官方文档
$ cd $GOPATH/src/
$ export GO111MODULE=on && export GOPROXY=https://goproxy.io
$ operator-sdk new operator-demo
time="2020-02-11T16:39:13+08:00" level=info msg="Creating new Go operator 'operator-demo'."
time="2020-02-11T16:39:13+08:00" level=info msg="Created go.mod"
time="2020-02-11T16:39:13+08:00" level=info msg="Created tools.go"
time="2020-02-11T16:39:13+08:00" level=info msg="Created cmd\\manager\\main.go"
time="2020-02-11T16:39:13+08:00" level=info msg="Created build\\Dockerfile"
time="2020-02-11T16:39:13+08:00" level=info msg="Created build\\bin\\entrypoint"
time="2020-02-11T16:39:13+08:00" level=info msg="Created build\\bin\\user_setup"
time="2020-02-11T16:39:13+08:00" level=info msg="Created deploy\\service_account.yaml"
time="2020-02-11T16:39:13+08:00" level=info msg="Created deploy\\role.yaml"
time="2020-02-11T16:39:13+08:00" level=info msg="Created deploy\\role_binding.yaml"
time="2020-02-11T16:39:13+08:00" level=info msg="Created deploy\\operator.yaml"
time="2020-02-11T16:39:13+08:00" level=info msg="Created pkg\\apis\\apis.go"
time="2020-02-11T16:39:13+08:00" level=info msg="Created pkg\\controller\\controller.go"
time="2020-02-11T16:39:13+08:00" level=info msg="Created version\\version.go"
time="2020-02-11T16:39:13+08:00" level=info msg="Created .gitignore"
time="2020-02-11T16:39:13+08:00" level=info msg="Validating project"
go: finding github.com/operator-framework/operator-sdk master
go: downloading github.com/operator-framework/operator-sdk v0.15.1-0.20200210173459-a1a9cbba09cb
go: extracting github.com/operator-framework/operator-sdk v0.15.1-0.20200210173459-a1a9cbba09cb
$ cd operator-demo/
$ operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService
time="2020-02-11T16:43:47+08:00" level=info msg="Generating api version app.example.com/v1alpha1 for kind AppService."
time="2020-02-11T16:43:47+08:00" level=info msg="Created pkg\\apis\\app\\group.go"
time="2020-02-11T16:44:13+08:00" level=info msg="Created pkg\\apis\\app\\v1alpha1\\appservice_types.go"
time="2020-02-11T16:44:14+08:00" level=info msg="Created pkg\\apis\\addtoscheme_app_v1alpha1.go"
time="2020-02-11T16:44:14+08:00" level=info msg="Created pkg\\apis\\app\\v1alpha1\\register.go"
time="2020-02-11T16:44:14+08:00" level=info msg="Created pkg\\apis\\app\\v1alpha1\\doc.go"
time="2020-02-11T16:44:14+08:00" level=info msg="Created deploy\\crds\\app.example.com_v1alpha1_appservice_cr.yaml"
time="2020-02-11T16:44:14+08:00" level=info msg="Running deepcopy code-generation for Custom Resource group versions: [app:[v1alpha1], ]\n"
time="2020-02-11T16:44:50+08:00" level=info msg="Code-generation complete."
time="2020-02-11T16:44:50+08:00" level=info msg="Running CRD generator."
time="2020-02-11T16:44:55+08:00" level=info msg="CRD generation complete."
time="2020-02-11T16:44:55+08:00" level=info msg="API generation complete."
$ operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService
time="2020-02-11T16:46:02+08:00" level=info msg="Generating controller version app.example.com/v1alpha1 for kind AppService."
time="2020-02-11T16:46:02+08:00" level=info msg="Created pkg\\controller\\appservice\\appservice_controller.go"
time="2020-02-11T16:46:02+08:00" level=info msg="Created pkg\\controller\\add_appservice.go"
time="2020-02-11T16:46:02+08:00" level=info msg="Controller generation complete."
到此,operator的demo工程就建好了,并且可以打包构建镜像推到k8s里直接使用。