使用K8S Go客户端列出ClusterServiceVersions。

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

List ClusterServiceVersions using K8S Go client

问题

我正在尝试使用K8S Go客户端列出ClusterServiceVersion。只需要原始响应体即可。

我尝试了以下代码:

data, err := clientset.RESTClient().Get().Namespace(namespace).
	Resource("ClusterServiceVersions").
	DoRaw(context.TODO())

if err != nil {
	panic(err.Error())
}

fmt.Printf("%v", string(data))

但是返回了以下错误:

panic: 服务器无法找到请求的资源(获取 ClusterServiceVersions.meta.k8s.io)

我该如何指定使用operators.coreos.com组?

根据一些现有代码,我还尝试添加了以下代码:

VersionedParams(&v1.ListOptions{}, scheme.ParameterCodec)

但是导致了另一个错误:

panic: v1.ListOptions 不适合在 scheme "pkg/runtime/scheme.go:100" 中转换为 "meta.k8s.io/v1"
英文:

I'm trying to use the K8S Go client to list the ClusterServiceVersions.
It could be enough to have the raw response body.

I've tried this:

data, err := clientset.RESTClient().Get().Namespace(namespace).
	Resource("ClusterServiceVersions").
	DoRaw(context.TODO())

if err != nil {
	panic(err.Error())
}

fmt.Printf("%v", string(data))

But it returns the following error:

panic: the server could not find the requested resource (get ClusterServiceVersions.meta.k8s.io)

How do I specify to use the operators.coreos.com group?

Looking at some existing code I've also tried to add

VersionedParams(&v1.ListOptions{}, scheme.ParameterCodec)

But it result in this other error:

panic: v1.ListOptions is not suitable for converting to "meta.k8s.io/v1" in scheme "pkg/runtime/scheme.go:100"

答案1

得分: 2

可以使用AbsPath()方法进行原始请求。

path := fmt.Sprintf("/apis/operators.coreos.com/v1alpha1/namespaces/%s/clusterserviceversions", namespace)

data, err := clientset.RESTClient().Get().
	AbsPath(path).
	DoRaw(ctx)

还要注意,如果你想使用接口(kubernetes.Interface)来定义clientset,而不是具体类型(*kubernetes.Clientset),则无法直接访问clientset.RESTClient()方法,但可以使用以下方法:

clientset.Discovery().RESTClient()
英文:

It is possible to do a raw request using the AbsPath() method.

path := fmt.Sprintf("/apis/operators.coreos.com/v1alpha1/namespaces/%s/clusterserviceversions", namespace)

data, err := clientset.RESTClient().Get().
	AbsPath(path).
	DoRaw(ctx)

Also notice that if you want to define clientset using the interface (kubernetes.Interface) instead of the concrete type (*kubernetes.Clientset) the method clientset.RESTClient() is not directly accessible, but you can use the following one:

clientset.Discovery().RESTClient()

huangapple
  • 本文由 发表于 2022年10月24日 17:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/74178993.html
匿名

发表评论

匿名网友

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

确定