k8s client-go 之 依赖问题解决
k8s client-go 之 依賴問(wèn)題解決(2)
記一次go mod依賴版本不一致的解決過(guò)程
克隆下client-go的工程,并checkout對(duì)應(yīng)kubernetes版本上,可以直接使用example中的例子,但是在自定義開(kāi)發(fā)時(shí)遇到了接口不一致問(wèn)題,很明顯這是依賴版本問(wèn)題所致。
我們使用client-go/examples/create-update-delete-deployment的例子測(cè)試
測(cè)試環(huán)境
? k8s go version go version go1.11.13 darwin/amd64 ? k8s echo $GO111MODULE on測(cè)試流程
將main.go復(fù)制到$GOPATH/src/test/k8s下
在test/k8s中初始化mod,go mod init
? k8s go mod init go: creating new go.mod: module test/k8s ? k8s ls go.mod main.go測(cè)試go run main.go,mod會(huì)自動(dòng)下載依賴(需要開(kāi)啟代理)
? k8s go run main.go go: finding k8s.io/client-go/kubernetes latest go: finding k8s.io/client-go/util/homedir latest go: finding k8s.io/client-go/util/retry latest go: finding k8s.io/client-go/tools/clientcmd latest go: finding k8s.io/api/core latest go: finding k8s.io/api/apps latest go: finding k8s.io/client-go/util latest go: finding k8s.io/client-go/tools latest go: finding k8s.io/api latest go: finding k8s.io/apimachinery/pkg/apis/meta latest go: finding k8s.io/apimachinery/pkg/apis latest go: finding k8s.io/apimachinery/pkg latest go: finding k8s.io/apimachinery latest go: finding golang.org/x/time/rate latest go: finding golang.org/x/time latest go: finding golang.org/x/oauth2 latest go: finding k8s.io/utils/integer latest go: finding k8s.io/utils latest # 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.NewStreamWatcherhave (*versioned.Decoder)want (watch.Decoder, watch.Reporter)測(cè)試結(jié)果
第3步,go run main.go 報(bào)出接口不一致,再看下載的依賴全是latest的。
之前我們測(cè)試client-go時(shí)已經(jīng)切換到tag kubernetes-1.14.6上,而go mod又拉取最先的依賴,這就造成了版本不一致。
解決依賴
查看client-go@kubernetes-1.14.6的go.mod下的依賴
? client-go git:(7e43eff7) ? cat go.mod module k8s.io/client-gorequire (cloud.google.com/go v0.0.0-20160913182117-3b1ae45394a2github.com/Azure/go-autorest v11.1.0+incompatiblegithub.com/davecgh/go-spew v0.0.0-20170626231645-782f4967f2dcgithub.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebdagithub.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96github.com/evanphx/json-patch v4.2.0+incompatiblegithub.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903github.com/golang/protobuf v1.1.0github.com/google/btree v0.0.0-20160524151835-7d79101e329egithub.com/google/gofuzz v0.0.0-20170612174753-24818f796fafgithub.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2dgithub.com/gophercloud/gophercloud v0.0.0-20190126172459-c818fa66e4c8github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7github.com/hashicorp/golang-lru v0.5.0github.com/imdario/mergo v0.3.5github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74begithub.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1ddgithub.com/modern-go/reflect2 v1.0.1github.com/peterbourgon/diskv v2.0.1+incompatiblegithub.com/pmezard/go-difflib v0.0.0-20181226105442-5d4384ee4fb2github.com/spf13/pflag v1.0.1github.com/stretchr/testify v0.0.0-20180319223459-c679ae2cc0cbgolang.org/x/crypto v0.0.0-20180808211826-de0752318171golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fcgolang.org/x/oauth2 v0.0.0-20170412232759-a6bd8cefa181golang.org/x/sys v0.0.0-20171031081856-95c657629925golang.org/x/text v0.0.0-20170810154203-b19bf474d317golang.org/x/time v0.0.0-20161028155119-f51c12702a4dgopkg.in/inf.v0 v0.9.1 // indirectgopkg.in/yaml.v2 v2.2.1k8s.io/api v0.0.0-20190816222004-e3a6b8045b0bk8s.io/apimachinery v0.0.0-20190816221834-a9f1d8a9c101k8s.io/klog v0.0.0-20190306015804-8e90cee79f82k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30k8s.io/utils v0.0.0-20190221042446-c2654d5206dasigs.k8s.io/yaml v1.1.0 )查看測(cè)試所需要的例子
? k8s cat go.mod module test/k8srequire (github.com/imdario/mergo v0.3.8 // indirectgolang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirectgolang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirectk8s.io/api v0.0.0-20191206001707-7edad22604e1 // indirectk8s.io/client-go v11.0.0+incompatible // indirectk8s.io/utils v0.0.0-20191114200735-6ca3b61696b6 // indirect )我們將例子中依賴的版本指定到client-go@kubernetes-v1.14.6相同的依賴的版本號(hào)上,這里主要針對(duì)k8s.io的依賴
k8s.io/api v0.0.0-20191206001707-7edad22604e1 // indirect k8s.io/client-go v11.0.0+incompatible // indirect k8s.io/utils v0.0.0-20191114200735-6ca3b61696b6 // indirectgo mod指定版本
go mod edit -require="k8s.io/client-go@kubernetes-v1.14.6" go mod edit -require="k8s.io/api@v0.0.0-20190816222004-e3a6b8045b0b" go mod edit -require="k8s.io/utils@v0.0.0-20190221042446-c2654d5206da"再測(cè)試
? k8s go mod edit -require="k8s.io/client-go@kubernetes-1.14.6" ? k8s go mod edit -require="k8s.io/api@v0.0.0-20190816222004-e3a6b8045b0b" ? k8s go mod edit -require="k8s.io/utils@v0.0.0-20190221042446-c2654d5206da" ? k8s go run main.go go: finding github.com/gogo/protobuf/sortkeys latest go: finding github.com/gogo/protobuf/proto latest go: finding k8s.io/apimachinery/pkg/runtime/serializer latest go: finding k8s.io/apimachinery/pkg/util/clock latest go: finding k8s.io/apimachinery/pkg/runtime latest go: finding k8s.io/apimachinery/pkg/runtime/serializer/streaming latest go: finding k8s.io/apimachinery/pkg/util/intstr latest go: finding k8s.io/apimachinery/pkg/apis/meta latest go: finding k8s.io/apimachinery/pkg/api/errors latest go: finding k8s.io/apimachinery/pkg/watch latest go: finding k8s.io/apimachinery/pkg/util latest go: finding k8s.io/apimachinery/pkg latest go: finding k8s.io/apimachinery/pkg/api latest go: finding k8s.io/apimachinery/pkg/apis latest go: finding k8s.io/apimachinery latest go: finding k8s.io/apimachinery/pkg/conversion latest go: finding k8s.io/apimachinery/pkg/types latest go: finding k8s.io/apimachinery/pkg/version latest go: finding k8s.io/apimachinery/pkg/util/wait latest go: finding k8s.io/apimachinery/pkg/util/sets latest go: finding k8s.io/apimachinery/pkg/api/resource latest go: finding k8s.io/apimachinery/pkg/fields latest go: finding k8s.io/apimachinery/pkg/runtime/schema latest go: finding k8s.io/apimachinery/pkg/api/meta latest go: finding k8s.io/apimachinery/pkg/util/validation latest go: finding k8s.io/apimachinery/pkg/runtime/serializer/json latest go: finding k8s.io/apimachinery/pkg/runtime/serializer/versioning latest go: finding k8s.io/apimachinery/pkg/util/errors latest go: finding github.com/googleapis/gnostic/OpenAPIv2 latest go: finding github.com/davecgh/go-spew/spew latest go: finding golang.org/x/crypto/ssh/terminal latest go: finding golang.org/x/crypto/ssh latest go: finding golang.org/x/crypto latest # k8s.io/client-go/rest ../../../pkg/mod/k8s.io/client-go@v11.0.1-0.20190820062731-7e43eff7c80a+incompatible/rest/request.go:598:31: not enough arguments in call to watch.NewStreamWatcherhave (*versioned.Decoder)want (watch.Decoder, watch.Reporter)這時(shí)又報(bào)出k8s.io/apimachinery接口,再次指定相同版本
go mod edit -require="k8s.io/apimachinery@v0.0.0-20190816221834-a9f1d8a9c101"測(cè)試
? k8s go run main.go go: finding github.com/davecgh/go-spew/spew latest build test/k8s: cannot find module for path sigs.k8s.io/yaml報(bào)出sigs.k8s.io/yaml接口,繼續(xù)指定相同版本
go mod edit -require="sigs.k8s.io/yaml@v1.1.0"測(cè)試
? k8s go run main.go Creating deployment... panic: Post https://kubernetes.docker.internal:6443/apis/apps/v1/namespaces/default/deployments: EOFgoroutine 1 [running]: main.main()/Users/liu/work/go/src/test/k8s2/main.go:105 +0xc80 exit status 2 ? k8s stopvpn 取消代理 ? k8s ? k8s go run main.go Creating deployment... Created deployment "demo-deployment". -> Press Return key to continue.^Csignal: interrupt ? k8s kubectl get pods NAME READY STATUS RESTARTS AGE demo-deployment-5fc8ffdb68-cqqgd 1/1 Running 0 6s demo-deployment-5fc8ffdb68-t74c7 1/1 Running 0 6s因?yàn)橹跋螺d依賴開(kāi)啟了鏡像而又沒(méi)有給k8s設(shè)置noproxy,所以出現(xiàn)了異常,取消代理,再測(cè)試,發(fā)現(xiàn)已經(jīng)創(chuàng)建部署了demo-deployment,至此版本管理完成,client-go測(cè)試完成。
總結(jié)
測(cè)試環(huán)境
? ~ kubectl version Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.6", GitCommit:"96fac5cd13a5dc064f7d9f4f23030a6aeface6cc", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:49Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"} Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.6", GitCommit:"96fac5cd13a5dc064f7d9f4f23030a6aeface6cc", GitTreeState:"clean", BuildDate:"2019-08-19T11:05:16Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"} ? ~ go version go version go1.11.13 darwin/amd64 ? ~ echo $GO111MODULE on測(cè)試demo,client-go@kubernetes-1.14.6/examples/create-update-delete-deployment/main.go
所需指定版本的依賴(使用k8s-1.14.6的client-go可直接指定如下的依賴版本)
其他,go清理mod cache命令
go clean --modcache總結(jié)
以上是生活随笔為你收集整理的k8s client-go 之 依赖问题解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电容电感测试
- 下一篇: 赛门铁克新通告再犯严重错误 忽视大陆用户