How to Import KFServing Client Package in Golang

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

How to Import KFServing Client Package in Golang

问题

我们在我们的Kubernetes集群中使用KFServing。该项目在pkg/文件夹中提供了InferenceService结构和clientset。

如何在pkg/client下导入这些包?我的代码如下:

package main
import (
    "fmt"
    kfs 
   "github.com/kubeflow/kfserving/pkg/client/clientset/versioned"
)

func main() {
   var clientset   *kfs.Cientset
   clientset = kfs.NewForConfig(nil)
   fmt.Println(clientset)
}

尝试运行go mod tidy,我得到了以下错误(我需要指定版本v0.5.1的dev):

main imports
github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
imports
k8s.io/client-go/discovery imports
github.com/googleapis/gnostic/OpenAPIv2: 
 module github.com/googleapis/gnostic@latest found (v0.5.5), 
 but does not contain package 
 github.com/googleapis/gnostic/OpenAPIv2
 main imports
github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
imports
...

尝试编译这些代码,我得到了很多错误.....

在运行go get github.com/kubeflow/kfserving之后,我得到了:

然后我更改了go.mod文件,并将kfseving版本从v0.6.0更改为v0.5.1,然后重新编译这些代码,仍然得到以下错误:

go build .
 go: github.com/kubeflow/kfserving@v0.5.1: missing go.sum entry; to add it:
    go mod download github.com/kubeflow/kfserving

go.mod文件的内容如下:

module main

go 1.16

require github.com/kubeflow/kfserving v0.5.1 // indirect

更新
对于kfserving v0.4.0,一切正常

英文:

We are using KFServing in our kubernetes cluster. The project provides InferenceService struct and clientset in the folder pkg/.

How to import these packages under pkg/client? My code is as below:

package main
import (
    "fmt"
    kfs 
   "github.com/kubeflow/kfserving/pkg/client/clientset/versioned"
)

func main() {
   var clientset   *kfs.Cientset
   clientset = kfs.NewForConfig(nil)
   fmt.Println(clientset)
}

Try go mod tidy i got (I need dev on specified version v0.5.1):

    main imports
    github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
    imports
    k8s.io/client-go/discovery imports
    github.com/googleapis/gnostic/OpenAPIv2: 
     module github.com/googleapis/gnostic@latest found (v0.5.5), 
     but does not contain package 
     github.com/googleapis/gnostic/OpenAPIv2
     main imports
    github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
    imports
    ...

Try compiling these code i got:

  a lot of errors .....

After go get github.com/kubeflow/kfserving I got:

Then I changed the go.mod and reconfiged the kfseving version from v0.6.0 to v0.5.1 and then recompile these code, still got below error:

go build .
 go: github.com/kubeflow/kfserving@v0.5.1: missing go.sum entry; to add it:
    go mod download github.com/kubeflow/kfserving

contens of go.mod:

module main

go 1.16

require github.com/kubeflow/kfserving v0.5.1 // indirect

UPDATE
for kfserving v0.4.0 everything works well

答案1

得分: 2

在查看kfservinggo.mod文件(版本为v0.5.1)时,我看到了一个大块的replace指令。根据设计,replace指令“仅适用于主模块的go.mod文件,在其他模块中会被忽略”,所以看起来kfserving在这里存在一些技术债务,他们将这些债务传递给了你。

我从一个空的go.mod文件开始,粘贴了这些replace指令。然后执行以下操作:

$ go get -d github.com/kubeflow/kfserving/pkg/client/clientset/versioned@v0.5.1
go get: added github.com/PuerkitoBio/purell v1.1.1
go get: added sigs.k8s.io/yaml v1.2.0

.go源文件需要进行一些修复,包括拼写错误和类型不匹配。我将其修改为:

package main

import (
	"fmt"

	kfs "github.com/kubeflow/kfserving/pkg/client/clientset/versioned"
)

func main() {
	var clientset *kfs.Clientset
	clientset = kfs.NewForConfigOrDie(nil)
	fmt.Println(clientset)
}

然后执行go build .成功:

$ go build -o /dev/null .

现在我将运行go mod tidy来清理go.modgo.sum文件:

$ go mod tidy
go: downloading github.com/stretchr/testify v1.5.1
go: downloading github.com/jmespath/go-jmespath v0.3.0

但是等等!我仍然有来自kfserving/go.mod的所有技术债务,而且没有任何解释版本选择的注释!我将尽量改善当前的情况。

Go 1.16对exclude指令的处理比之前的Go版本要好得多,所以也许我可以使用一些有针对性的exclude指令来替代那些(相当庞大的)replace指令。

首先,我会提交到目前为止的修复(至少,我想能够使用git diff查看已更改的要求)。

$ git add *.go go.mod go.sum

$ git commit -m 'fixed kfserving build'
[main fd93b1d] fixed kfserving build
 3 files changed, 1643 insertions(+), 2 deletions(-)
 create mode 100644 go.sum

我注意到所有的replace指令都适用于k8s.io路径,所以我将列出这些路径的当前版本,看看需要修复什么:

$ go list -m k8s.io/...
k8s.io/api v0.19.2 => k8s.io/api v0.19.2
k8s.io/apiextensions-apiserver v0.19.2 => k8s.io/apiextensions-apiserver v0.19.2
k8s.io/apimachinery v0.19.2 => k8s.io/apimachinery v0.19.2
k8s.io/apiserver v0.19.2 => k8s.io/apiserver v0.19.2
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible => k8s.io/client-go v0.19.2
k8s.io/cloud-provider v0.19.2 => k8s.io/cloud-provider v0.19.2
k8s.io/code-generator v0.19.2 => k8s.io/code-generator v0.19.2
k8s.io/component-base v0.19.2 => k8s.io/component-base v0.19.2
k8s.io/csi-translation-lib v0.19.2 => k8s.io/csi-translation-lib v0.19.2
k8s.io/gengo v0.0.0-20200428234225-8167cfdcfc14
k8s.io/klog v1.0.0 => k8s.io/klog v1.0.0
k8s.io/klog/v2 v2.2.0
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6
k8s.io/legacy-cloud-providers v0.17.4 => k8s.io/legacy-cloud-providers v0.19.2
k8s.io/test-infra v0.0.0-20200803112140-d8aa4e063646 => k8s.io/test-infra v0.0.0-20200803112140-d8aa4e063646
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

其中大部分看起来都没问题,但有三个版本不匹配:

k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible => k8s.io/client-go v0.19.2
k8s.io/legacy-cloud-providers v0.17.4 => k8s.io/legacy-cloud-providers v0.19.2
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

legacy-cloud-providers只需要升级,所以修复起来应该很容易,只需使用go get命令即可。但是它与此包无关,所以我可以让它浮动到任何版本。我将缩小replace指令,只保留剩下的两个模块,然后看看进展如何:

replace (
	k8s.io/client-go => k8s.io/client-go v0.19.2
	k8s.io/utils => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
)

现在:

$ go mod tidy

$ go build -o /dev/null .

好的,构建仍然成功 - 我已经取得了渐进的进展,所以我将锁定它:

$ git add go.mod go.sum

$ git commit -m 'go.mod: remove irrelevant replacements'
[main cdbc1db] go.mod: remove irrelevant replacements
 3 files changed, 456 insertions(+), 43 deletions(-)
 rewrite go.mod (85%)
 create mode 100755 m

现在让我们尝试使用exclude指令来排除不良的client-go版本:

$ go mod edit -exclude=k8s.io/client-go@v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible

$ go list -m k8s.io/client-go
k8s.io/client-go v9.0.0+incompatible => k8s.io/client-go v0.19.2

$ go mod edit -exclude=k8s.io/client-go@v9.0.0+incompatible

$ go list -m k8s.io/client-go
k8s.io/client-go v0.19.2 => k8s.io/client-go v0.19.2

现在我使用了所需的client-go版本,所以我可以删除该replace指令:

$ go mod edit -dropreplace=k8s.io/client-go

$ go mod tidy

$ go build -o /dev/null .

再次锁定它:

$ git add go.mod go.sum

$ git commit -m 'go.mod: use exclude instead of replace to notch out unwanted client-go versions'
[main de69965] go.mod: use exclude instead of replace to notch out unwanted client-go versions
 2 files changed, 21 insertions(+), 3 deletions(-)

现在让我们看看这个k8s.io/utils替换的情况。我怀疑它实际上是不需要的 - 版本会升级一点,但我敢打赌它不会破坏构建。事实证明,它似乎没问题:

$ go list -m k8s.io/utils
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

$ go mod edit -dropreplace=k8s.io/utils

$ go mod tidy

$ go build -o /dev/null .

$ go list -m k8s.io/utils
k8s.io/utils v0.0.0-20200912215256-4140de9c8800

所以现在,我已经成功将kfserving的大量replace指令减少到只有几个针对k8s.io/client-goexclude指令。这是我在此过程结束时的整个go.mod文件:

module example.com/m

go 1.16

exclude (
	k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
	k8s.io/client-go v9.0.0+incompatible
)

require github.com/kubeflow/kfserving v0.5.1
英文:

Looking at the kfserving go.mod file at v0.5.1, I see a big block of replace directives. By design, replace directives “only apply in the main module's go.mod file and are ignored in other modules”, so it looks like kfserving has some technical debt here that they're passing on to you.

I started with an empty go.mod file and pasted in those replace directives. Then:

$ go get -d github.com/kubeflow/kfserving/pkg/client/clientset/versioned@v0.5.1
go get: added github.com/PuerkitoBio/purell v1.1.1
…
go get: added sigs.k8s.io/yaml v1.2.0

The .go source file needed a few fixes for typos and mismatched types. I fudged it to:

package main

import (
	"fmt"

	kfs "github.com/kubeflow/kfserving/pkg/client/clientset/versioned"
)

func main() {
	var clientset *kfs.Clientset
	clientset = kfs.NewForConfigOrDie(nil)
	fmt.Println(clientset)
}

and then go build . succeeds:

$ go build -o /dev/null .

Now I'll run go mod tidy to clean up the go.mod and go.sum files:

$ go mod tidy
go: downloading github.com/stretchr/testify v1.5.1
…
go: downloading github.com/jmespath/go-jmespath v0.3.0

But wait! I've still got all of the technical debt from kfserving/go.mod — without any comments explaining the choice of versions! — and I'm passing that technical debt on to any downstream users. Let's see if I can improve the situation while I'm here.

Go 1.16 handles the exclude directive a lot better than previous Go versions did, so maybe I can use a couple of targeted exclude directives instead of the (rather large) replace hammer.

I'll start by committing the fixes so far. (If nothing else, I want to be able to git diff the requirements to see what has changed.)

$ git add *.go go.mod go.sum

$ git commit -m 'fixed kfserving build'
[main fd93b1d] fixed kfserving build
 3 files changed, 1643 insertions(+), 2 deletions(-)
 create mode 100644 go.sum

I notice that all of the replace directives apply to k8s.io paths, so I'll list the current versions of those paths to see what needs to be fixed:

$ go list -m k8s.io/...
k8s.io/api v0.19.2 => k8s.io/api v0.19.2
k8s.io/apiextensions-apiserver v0.19.2 => k8s.io/apiextensions-apiserver v0.19.2
k8s.io/apimachinery v0.19.2 => k8s.io/apimachinery v0.19.2
k8s.io/apiserver v0.19.2 => k8s.io/apiserver v0.19.2
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible => k8s.io/client-go v0.19.2
k8s.io/cloud-provider v0.19.2 => k8s.io/cloud-provider v0.19.2
k8s.io/code-generator v0.19.2 => k8s.io/code-generator v0.19.2
k8s.io/component-base v0.19.2 => k8s.io/component-base v0.19.2
k8s.io/csi-translation-lib v0.19.2 => k8s.io/csi-translation-lib v0.19.2
k8s.io/gengo v0.0.0-20200428234225-8167cfdcfc14
k8s.io/klog v1.0.0 => k8s.io/klog v1.0.0
k8s.io/klog/v2 v2.2.0
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6
k8s.io/legacy-cloud-providers v0.17.4 => k8s.io/legacy-cloud-providers v0.19.2
k8s.io/test-infra v0.0.0-20200803112140-d8aa4e063646 => k8s.io/test-infra v0.0.0-20200803112140-d8aa4e063646
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

Most of those look fine, but there are three mismatched versions:

k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible => k8s.io/client-go v0.19.2
…
k8s.io/legacy-cloud-providers v0.17.4 => k8s.io/legacy-cloud-providers v0.19.2
…
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

legacy-cloud-providers just needs an upgrade, so that should be easy enough to fix with go get — but it's not relevant to this package anyway so I can just let it float to whatever version it ends up at. I'll cut down the replace directives to just the two remaining modules and see how far I get:

replace (
	k8s.io/client-go => k8s.io/client-go v0.19.2
	k8s.io/utils => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
)

Now:

$ go mod tidy

$ go build -o /dev/null .

Ok, so the build is still good — I've made incremental progress, so I'll lock that in:

$ git add go.mod go.sum

$ git commit -m 'go.mod: remove irrelevant replacements'
[main cdbc1db] go.mod: remove irrelevant replacements
 3 files changed, 456 insertions(+), 43 deletions(-)
 rewrite go.mod (85%)
 create mode 100755 m

Now let's try notching out the bad client-go versions with exclude directives:

$ go mod edit -exclude=k8s.io/client-go@v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible

$ go list -m k8s.io/client-go
k8s.io/client-go v9.0.0+incompatible => k8s.io/client-go v0.19.2

$ go mod edit -exclude=k8s.io/client-go@v9.0.0+incompatible

$ go list -m k8s.io/client-go
k8s.io/client-go v0.19.2 => k8s.io/client-go v0.19.2

Now I'm at the client-go version I needed, so I can drop that replace directive:

$ go mod edit -dropreplace=k8s.io/client-go

$ go mod tidy

$ go build -o /dev/null .

And again lock it in:

$ git add go.mod go.sum

$ git commit -m 'go.mod: use exclude instead of replace to notch out unwanted client-go versions'
[main de69965] go.mod: use exclude instead of replace to notch out unwanted client-go versions
 2 files changed, 21 insertions(+), 3 deletions(-)

Now let's see what's going on with this k8s.io/utils replacement. I suspect that it isn't really needed — the version will be upgraded a bit, but I'm betting it won't break the build. And, indeed, it seems to be fine:

$ go list -m k8s.io/utils
k8s.io/utils v0.0.0-20200912215256-4140de9c8800 => k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89

$ go mod edit -dropreplace=k8s.io/utils

$ go mod tidy

$ go build -o /dev/null .

$ go list -m k8s.io/utils
k8s.io/utils v0.0.0-20200912215256-4140de9c8800

So now I've successfully reduced that big nest of replace directives from kfserving to just a couple of exclude directives targeted retracted versions of k8s.io/client-go. Here's my whole go.mod file at the end of this process:

module example.com/m

go 1.16

exclude (
	k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
	k8s.io/client-go v9.0.0+incompatible
)

require github.com/kubeflow/kfserving v0.5.1

答案2

得分: -1

这似乎是一个模块缓存问题,因为该包在v0.5.1中是存在的。请按照以下步骤操作:

  1. 清理模块缓存:go clean --modcache
  2. 通过以下方式获取模块:go get github.com/kubeflow/kfserving@v0.5.1
  3. 运行go mod tidy

更新

根据额外的输出,我可以看到你遇到了一个依赖模块的导入错误。

github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
    imports
    k8s.io/client-go/discovery imports
    github.com/googleapis/gnostic/OpenAPIv2:

所以v0.5.1版本导入了k8s.io/client-go/discovery,而该模块又导入了github.com/googleapis/gnostic/OpenAPIv2,但在当前版本中该模块已经不存在了(但在旧版本中是存在的)。因此,看起来你需要修复client-go中的导入版本以避免此问题。

英文:

This seems like a module cache issue cause that package appears to be there for v0.5.1. Do the following:

  1. Clean the modcache go clean --modcache
  2. Get the module the nice way via go get github.com/kubeflow/kfserving@v0.5.1
  3. Run go mod tidy

Update

With the extra output I can see you you have an import error from a dependent module.

github.com/kubeflow/kfserving/pkg/client/clientset/versioned 
    imports
    k8s.io/client-go/discovery imports
    github.com/googleapis/gnostic/OpenAPIv2:

So version v0.5.1 imports k8s.io/client-go/discovery which imports github.com/googleapis/gnostic/OpenAPIv2 which no longer exists in the current release (but it is there is older releases). So it looks like you would need to fix the import version in client-go to avoid this issue.

huangapple
  • 本文由 发表于 2021年8月7日 09:17:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68688752.html
匿名

发表评论

匿名网友

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

确定