K8s Go客户端库在go get时找不到包。

huangapple go评论92阅读模式
英文:

K8s Go client library fails to find package on go get

问题

我们编写了一些Go代码,用于与我们的Kubernetes集群通信并获取公开服务的IP。我们的代码如下所示:

import "gopkg.in/kubernetes/kubernetes.v1/pkg/client/restclient"
import kubectl "gopkg.in/kubernetes/kubernetes.v1/pkg/client/unversioned"

svc, err := c.Services(k8sNS).Get(svcName)
if err != nil {
  panic(l.Errorf("Could not retrieve svc details. %s", err.Error()))
}
svcIP := svc.Status.LoadBalancer.Ingress[0].IP

go get正常工作,当我们执行go run ...时,脚本也能正常运行,一切都很顺利。但是,从昨天开始(从发布这个问题的时间算起),在同一个脚本上执行go get失败了。错误信息如下:

[09.07.2016 10:56 AM]$ go get
package k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install: cannot find package "k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install" in any of:
        /usr/local/go/src/k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install (from $GOROOT)
        /home/ckotha/godir/src/k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install (from $GOPATH)

我们的代码中没有明确使用authentication包。我们导入Kubernetes库的方式是否正确?还有其他方法可以解决这个问题吗?

$GOPATH/k8s.io/kubernetes/pkg/apis/目录下执行ls命令,发现如下内容:

:~/godir/src/k8s.io/kubernetes/pkg/apis
[09.07.2016 10:53 AM]$ ls
abac  apps  authentication  authorization  autoscaling  batch  certificates  componentconfig  extensions  imagepolicy  OWNERS  policy  rbac  storage

请注意,以上是翻译的内容,不包括代码部分。

英文:

We wrote some Go code to talk to our Kubernetes cluster and fetch the IP of a Service exposed. We do it like so:

(import "gopkg.in/kubernetes/kubernetes.v1/pkg/client/restclient")
(import kubectl "gopkg.in/kubernetes/kubernetes.v1/pkg/client/unversioned")

 svc, err := c.Services(k8sNS).Get(svcName)
 if err != nil {
   panic(l.Errorf("Could not retrieve svc details. %s", err.Error()))
 }
 svcIP := svc.Status.LoadBalancer.Ingress[0].IP

go get works fine, and our script executes when we do go run ... and everybody is happy. Now, as of yesterday (from the time this question is posted) on the same script - go get fails. The error is like so:

[09.07.2016 10:56 AM]$ go get
package k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install: cannot find package "k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install" in any of:
        /usr/local/go/src/k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install (from $GOROOT)
        /home/ckotha/godir/src/k8s.io/kubernetes/pkg/apis/authentication.k8s.io/install (from $GOPATH)

We have not specifically used authentication package in our code. Are we importing kubernetes libraries correctly? is there another way to do this ?

ls on $GOPATH/k8s.io/kubernetes/pkg/apis/ and found this:

:~/godir/src/k8s.io/kubernetes/pkg/apis
[09.07.2016 10:53 AM]$ ls
abac  apps  authentication  authorization  autoscaling  batch  certificates  componentconfig  extensions  imagepolicy  OWNERS  policy  rbac  storage

答案1

得分: 1

看起来你导入的一个包发生了变化。

你可以更新现有的仓库:

go get -u

-u 标志指示 get 使用网络更新指定的包及其依赖项。默认情况下,get 使用网络来检出缺失的包,但不使用网络来查找现有包的更新。

你确实使用 gopkg.io 来固定版本为 v1,但我认为你可能想更具体一些,例如 v1.3.6(编辑:这样做不起作用,因为 gopkg.in 不允许比主要版本更具体的包选择器)。

另外,确保代码保持不变的一个好方法是编译你的二进制文件并执行它,而不是使用 go run

英文:

It looks like a package you imported has changed.

You can update existing repositories:

go get -u

> The -u flag instructs get to use the network to update the named
> packages and their dependencies. By default, get uses the network to
> check out missing packages but does not use it to look for updates to
> existing packages.

You do use gopkg.io to pin the version to v1, but I think you want to be more specific, eg, v1.3.6 (EDIT: this won't work because gopkg.in doesn't permit package selectors more specific than the major version.).

Alternatively, a good way to ensure code stays the same is to compile your binary and execute that, instead of using go run.

huangapple
  • 本文由 发表于 2016年9月8日 02:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/39376548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定