勉強一下如何在Go中构建Kubernetes client-go的示例代码
系列
1. 在Mac上设置VSCode和Go环境来学习Go
2. 构建kubernetes client-go的示例代码
3. 阅读kubernetes client-go的示例代码来学习Go
4. 使用kubernetes client-go编写Pod监视器(TUI)
5. 使用kubernetes client-go编写另一个Pod监视器(TUI)
首先
我想要创建与Kubernetes相关的工具。
根据Kubernetes参考客户端库,要在Go中访问Kubernetes,需要使用client-go。
我将构建并执行使用这个库的示例。
クライアントmacOS Mojava 10.14.6
Homebrew、Visual Studio Code 、go 1.13.1、kubectl 1.15サーバ環境AWS EKS
■ 参考资料
・客户端go(GitHub)
获取示例代码
有关如何使用client-go从Kubernetes集群外部访问的示例代码已经在GitHub上发布了,我们将使用这个示例。
■示例:集群外部客户端配置
https://github.com/kubernetes/client-go/tree/master/examples/out-of-cluster-client-configuration
$ mkdir $HOME/go/src/out-of-cluster-client-configuration
$ cd $HOME/go/src/out-of-cluster-client-configuration
$ curl -O https://raw.githubusercontent.com/kubernetes/client-go/master/examples/out-of-cluster-client-configuration/main.go
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2880 100 2880 0 0 6260 0 --:--:-- --:--:-- --:--:-- 6260
$ ls -l
total 8
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
我成功了。
建设
根据out-of-cluster-client-configuration的指导进行构建尝试。
$ go build -o app .
main.go:27:2: cannot find package "k8s.io/apimachinery/pkg/api/errors" in any of:
/usr/local/Cellar/go/1.13.1/libexec/src/k8s.io/apimachinery/pkg/api/errors (from $GOROOT)
/Users/xxxxx/go/src/k8s.io/apimachinery/pkg/api/errors (from $GOPATH)
main.go:28:2: cannot find package "k8s.io/apimachinery/pkg/apis/meta/v1" in any of:
/usr/local/Cellar/go/1.13.1/libexec/src/k8s.io/apimachinery/pkg/apis/meta/v1 (from $GOROOT)
/Users/xxxxx/go/src/k8s.io/apimachinery/pkg/apis/meta/v1 (from $GOPATH)
main.go:29:2: cannot find package "k8s.io/client-go/kubernetes" in any of:
/usr/local/Cellar/go/1.13.1/libexec/src/k8s.io/client-go/kubernetes (from $GOROOT)
/Users/xxxxx/go/src/k8s.io/client-go/kubernetes (from $GOPATH)
main.go:30:2: cannot find package "k8s.io/client-go/tools/clientcmd" in any of:
/usr/local/Cellar/go/1.13.1/libexec/src/k8s.io/client-go/tools/clientcmd (from $GOROOT)
/Users/xxxxx/go/src/k8s.io/client-go/tools/clientcmd (from $GOPATH)
我被责备了。似乎需要处理一些Modules的设置。
$ go mod init
go: creating new go.mod: module out-of-cluster-client-configuration
Airine:out-of-cluster-client-configuration xxxxx$ ls -l
total 16
-rw-r--r-- 1 xxxxx staff 52 10 19 01:00 go.mod
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
go.mod文件已添加。内容如下所示。
module out-of-cluster-client-configuration
go 1.13
重新构建。
$ go build -o app .
go: finding k8s.io/client-go v11.0.0+incompatible
go: finding k8s.io/apimachinery latest
go: downloading k8s.io/client-go v11.0.0+incompatible
go: downloading k8s.io/apimachinery v0.0.0-20191017185446-6e68a40eebf9
go: extracting k8s.io/apimachinery v0.0.0-20191017185446-6e68a40eebf9
go: extracting k8s.io/client-go v11.0.0+incompatible
go: downloading github.com/golang/protobuf v1.3.1
go: downloading k8s.io/klog v1.0.0
go: downloading github.com/spf13/pflag v1.0.3
go: downloading github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d
go: extracting github.com/spf13/pflag v1.0.3
go: extracting k8s.io/klog v1.0.0
go: extracting github.com/golang/protobuf v1.3.1
go: finding golang.org/x/crypto latest
go: finding k8s.io/api latest
go: downloading github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d
go: downloading golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: extracting github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d
go: downloading k8s.io/api v0.0.0-20191016225839-816a9b7df678
go: extracting golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: extracting k8s.io/api v0.0.0-20191016225839-816a9b7df678
go: extracting github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d
go: finding github.com/imdario/mergo v0.3.8
go: downloading github.com/imdario/mergo v0.3.8
go: extracting github.com/imdario/mergo v0.3.8
go: downloading github.com/modern-go/reflect2 v1.0.1
go: extracting github.com/modern-go/reflect2 v1.0.1
go: downloading github.com/json-iterator/go v1.1.7
go: extracting github.com/json-iterator/go v1.1.7
go: downloading sigs.k8s.io/yaml v1.1.0
go: extracting sigs.k8s.io/yaml v1.1.0
go: finding golang.org/x/time latest
go: downloading golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0
go: extracting golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0
go: downloading golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
go: downloading gopkg.in/yaml.v2 v2.2.4
go: downloading github.com/davecgh/go-spew v1.1.1
go: extracting github.com/davecgh/go-spew v1.1.1
go: downloading gopkg.in/inf.v0 v0.9.0
go: extracting gopkg.in/yaml.v2 v2.2.4
go: extracting gopkg.in/inf.v0 v0.9.0
go: extracting golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
go: downloading github.com/google/gofuzz v1.0.0
go: extracting github.com/google/gofuzz v1.0.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: extracting github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading golang.org/x/text v0.3.2
go: extracting golang.org/x/text v0.3.2
go: finding k8s.io/utils latest
go: downloading k8s.io/utils v0.0.0-20191010214722-8d271d903fe4
go: finding golang.org/x/oauth2 latest
go: downloading golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
go: extracting golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
go: extracting k8s.io/utils v0.0.0-20191010214722-8d271d903fe4
go: downloading golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f
go: extracting golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f
go: finding github.com/golang/protobuf v1.3.1
go: finding github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d
go: finding github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d
go: finding github.com/spf13/pflag v1.0.3
go: finding github.com/google/gofuzz v1.0.0
go: finding golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f
go: finding gopkg.in/yaml.v2 v2.2.4
go: finding gopkg.in/inf.v0 v0.9.0
go: finding github.com/json-iterator/go v1.1.7
go: finding github.com/modern-go/reflect2 v1.0.1
go: finding k8s.io/klog v1.0.0
go: finding github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: finding golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
go: finding sigs.k8s.io/yaml v1.1.0
go: finding golang.org/x/text v0.3.2
go: finding github.com/davecgh/go-spew v1.1.1
# k8s.io/client-go/rest
../../pkg/mod/k8s.io/client-go@v11.0.0+incompatible/rest/request.go:598:31: not enough arguments in call to watch.NewStreamWatcher
have (*versioned.Decoder)
want (watch.Decoder, watch.Reporter)
看起来一切顺利进行,但最后出现了个错误。
go.mod文件的内容已经变成以下这样。
module out-of-cluster-client-configuration
go 1.13
require (
github.com/imdario/mergo v0.3.8 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
k8s.io/api v0.0.0-20191016225839-816a9b7df678 // indirect
k8s.io/apimachinery v0.0.0-20191017185446-6e68a40eebf9
k8s.io/client-go v11.0.0+incompatible
k8s.io/utils v0.0.0-20191010214722-8d271d903fe4 // indirect
)
在寻找错误原因的过程中,我意识到GitHub上有关于client-go安装方法的说明。
$ go get k8s.io/client-go@master
go: finding k8s.io/client-go master
go: downloading k8s.io/client-go v0.0.0-20191016230210-14c42cd304d9
go: extracting k8s.io/client-go v0.0.0-20191016230210-14c42cd304d9
成功安装了。
go.mod文件的内容再次发生了变化。k8s.io/client-go的版本发生了变化,golang.org/x/oauth2和k8s.io/utils已经消失。
module out-of-cluster-client-configuration
go 1.13
require (
github.com/imdario/mergo v0.3.8 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
k8s.io/api v0.0.0-20191016225839-816a9b7df678 // indirect
k8s.io/apimachinery v0.0.0-20191017185446-6e68a40eebf9
k8s.io/client-go v0.0.0-20191016230210-14c42cd304d9
)
再次建立。
$ go build -o app .
$ ls -l
total 68608
-rwxr-xr-x 1 xxxxx staff 35096508 10 19 01:04 app
-rw-r--r-- 1 xxxxx staff 412 10 19 01:03 go.mod
-rw-r--r-- 1 xxxxx staff 18923 10 19 01:03 go.sum
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
这一次通过了。
可执行文件已经完成了。有35MB大小。client-go是很大的吗?
我会试着运行一下。
$ ./app
There are 15 pods in the cluster
Pod example-xxxxx in namespace default not found
There are 15 pods in the cluster
Pod example-xxxxx in namespace default not found
^C
在源代码中直接写入的Pod名称为什么会显示为“not found”,但似乎可以访问到Kubernetes集群。
重新从头开始建立
我会重新做一遍,只保留原始代码。
$ ls -l
total 68608
-rwxr-xr-x 1 xxxxx staff 35096508 10 19 01:04 app
-rw-r--r-- 1 xxxxx staff 412 10 19 01:03 go.mod
-rw-r--r-- 1 xxxxx staff 18923 10 19 01:03 go.sum
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
$ rm app go.mod go.sum
$ ls -l
total 8
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
初始化模块并获取client-go。
$ go mod init
go: creating new go.mod: module out-of-cluster-client-configuration
$ go get k8s.io/client-go@master
go: finding k8s.io master
go: finding k8s.io/client-go master
这是go.mod文件的内容。
module out-of-cluster-client-configuration
go 1.13
require k8s.io/client-go v0.0.0-20191016230210-14c42cd304d9 // indirect
我将进行建设。
$ go build -o app .
go: downloading k8s.io/apimachinery v0.0.0-20191016060620-86f2f1b9c076
go: downloading k8s.io/api v0.0.0-20191010143144-fbf594f18f80
go: downloading github.com/imdario/mergo v0.3.5
go: extracting github.com/imdario/mergo v0.3.5
go: extracting k8s.io/apimachinery v0.0.0-20191016060620-86f2f1b9c076
go: extracting k8s.io/api v0.0.0-20191010143144-fbf594f18f80
go: downloading golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8
go: downloading golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
go: extracting golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
go: extracting golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8
go: finding k8s.io/apimachinery v0.0.0-20191016060620-86f2f1b9c076
go: finding k8s.io/api v0.0.0-20191010143144-fbf594f18f80
go: finding github.com/imdario/mergo v0.3.5
go: finding golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8
go: finding golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
$ ls -l
total 68592
-rwxr-xr-x 1 xxxxx staff 35088188 10 19 01:10 app
-rw-r--r-- 1 xxxxx staff 174 10 19 01:10 go.mod
-rw-r--r-- 1 xxxxx staff 18118 10 19 01:10 go.sum
-rw-r--r-- 1 xxxxx staff 2880 10 19 00:58 main.go
没问题。
module out-of-cluster-client-configuration
go 1.13
require (
k8s.io/apimachinery v0.0.0-20191016060620-86f2f1b9c076
k8s.io/client-go v0.0.0-20191016230210-14c42cd304d9
)
嗯?比剛剛做的時候,”require”減少了。
下面有四個消失了。全行//indirec下有注釋。
github.com/imdario/mergo v0.3.8 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
k8s.io/api v0.0.0-20191016225839-816a9b7df678 // indirect
刚刚完成的可执行文件,文件大小也相同,看起来和刚才的一样。
最后
目前先以试着去操作为进展方向。
虽然需要认真学习相关模块,但由于想要写代码,所以我选择先向前推进。