如何使用ReST接口从Kubernetes APIServer获取“kind”响应?

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

How to get "kind" response from Kubernetes APIServer using ReST interface

问题

我正在使用Golang的client-go库与Kubernetes API服务器进行通信,使用的是ReST GET和POST级别的通信。收到的响应既不是格式良好的JSON结构,也不是“kind”API对象。

程序片段如下:

    kubeconfig := filepath.Join(
         os.Getenv("HOME"), ".kube", "config",
    )
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        log.Fatal(err)
    }
    config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()


    groupVersion, _ := schema.ParseGroupVersion("api/v1")
    config.GroupVersion = &groupVersion
    config.ContentType = "application/json"

    config.AcceptContentTypes = "application/json"
	
	exampleRestClient, err := rest.RESTClientFor(config)
    if err != nil {
        panic(err)
    }
	
    var statusCode int
    var contentType string

	response, err := exampleRestClient.
		Get().
		Resource("nodes").
		Do(context.Background()).
        StatusCode(&statusCode).
        ContentType(&contentType).
        Get()
    
    if err != nil {
        panic(err)
    }

    fmt.Printf("Content-type is %s\n", contentType)
    fmt.Printf("Status Code is %d\n", statusCode)

	fmt.Printf("Received response %v\n", response)

响应的起始部分如下:

Status Code is 200
Received response &NodeList{ListMeta:{ 17299  <nil>},Items:[]Node{Node{ObjectMeta:{dev-cluster-control-plane    7fe038c9-8be6-41a9-9f3f-5900abb0e34b 16922 0 2023-02-19 16:32:44 +0530 IST <nil> <nil> map[beta.kubernetes.io/arch:amd64 beta.kubernetes.io/os:linux kubernetes.io/arch:amd64 kubernetes.io/hostname:dev-cluster-control-plane kubernetes.io/os:linux node-role.kubernetes.io/control-plane: node.kubernetes.io/exclude-from-external-load-balancers:] map[kubeadm.alpha.kubernetes.io/cri-socket:unix:///run/containerd/containerd.sock node.alpha.kubernetes.io/ttl:0 volumes.kubernetes.io/controller-managed-attach-detach:true] [] [] ...

我期望的输出应该像以下命令返回的结果一样:

$ kubectl get --raw /api/v1/nodes
{"kind":"NodeList","apiVersion":"v1","metadata":{"resourceVersion":"17481"},"items":[{"metadata":{"name":"dev-cluster-control-plane","uid":"7fe038c9-8be6-41a9-9f3f-5900abb0e34b","resourceVersion":"17351","creationTimestamp":"2023-02-19T11:02:44Z","labels":{"beta.kubernetes.io/arch":"amd64","beta.kubernetes.io/os":"linux","kubernetes.io/arch":"amd64","kubernetes.io/hostname":"dev-cluster-control-plane","kubernetes.io/os":"linux","node-role.kubernetes.io/control-plane":"","node.kubernetes.io/exclude-from-external-load-balancers":""},"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"unix:///run/containerd/containerd.sock","node.alpha.kubernetes.io/ttl":"0" ...
英文:

I am using the Golang client-go library to communicate with the Kubernetes API Server at the ReST GET, POST level. The response received is not a well-formed JSON structure and also not a "kind" API Object.

The program snippet is:

    kubeconfig := filepath.Join(
         os.Getenv(&quot;HOME&quot;), &quot;.kube&quot;, &quot;config&quot;,
    )
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, kubeconfig)
    if err != nil {
        log.Fatal(err)
    }
    config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()


    groupVersion, _ := schema.ParseGroupVersion(&quot;api/v1&quot;)
    config.GroupVersion = &amp;groupVersion
    config.ContentType = &quot;application/json&quot;

    config.AcceptContentTypes = &quot;application/json&quot;
	
	exampleRestClient, err := rest.RESTClientFor(config)
    if err != nil {
        panic(err)
    }
	
    var statusCode int
    var contentType string

	response, err := exampleRestClient.
		Get().
		Resource(&quot;nodes&quot;).
		Do(context.Background()).
        StatusCode(&amp;statusCode).
        ContentType(&amp;contentType).
        Get()
    
    if err != nil {
        panic(err)
    }

    fmt.Printf(&quot;Content-type is %s\n&quot;, contentType)
    fmt.Printf(&quot;Status Code is %d\n&quot;, statusCode)

	fmt.Printf(&quot;Received response %v\n&quot;, response)

The response starts as:

Status Code is 200
Received response &amp;NodeList{ListMeta:{ 17299  &lt;nil&gt;},Items:[]Node{Node{ObjectMeta:{dev-cluster-control-plane    7fe038c9-8be6-41a9-9f3f-5900abb0e34b 16922 0 2023-02-19 16:32:44 +0530 IST &lt;nil&gt; &lt;nil&gt; map[beta.kubernetes.io/arch:amd64 beta.kubernetes.io/os:linux kubernetes.io/arch:amd64 kubernetes.io/hostname:dev-cluster-control-plane kubernetes.io/os:linux node-role.kubernetes.io/control-plane: node.kubernetes.io/exclude-from-external-load-balancers:] map[kubeadm.alpha.kubernetes.io/cri-socket:unix:///run/containerd/containerd.sock node.alpha.kubernetes.io/ttl:0 volumes.kubernetes.io/controller-managed-attach-detach:true] [] [] ...

I expected the output to be like what the following command returns:

$ kubectl get --raw /api/v1/nodes
{&quot;kind&quot;:&quot;NodeList&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{&quot;resourceVersion&quot;:&quot;17481&quot;},&quot;items&quot;:[{&quot;metadata&quot;:{&quot;name&quot;:&quot;dev-cluster-control-plane&quot;,&quot;uid&quot;:&quot;7fe038c9-8be6-41a9-9f3f-5900abb0e34b&quot;,&quot;resourceVersion&quot;:&quot;17351&quot;,&quot;creationTimestamp&quot;:&quot;2023-02-19T11:02:44Z&quot;,&quot;labels&quot;:{&quot;beta.kubernetes.io/arch&quot;:&quot;amd64&quot;,&quot;beta.kubernetes.io/os&quot;:&quot;linux&quot;,&quot;kubernetes.io/arch&quot;:&quot;amd64&quot;,&quot;kubernetes.io/hostname&quot;:&quot;dev-cluster-control-plane&quot;,&quot;kubernetes.io/os&quot;:&quot;linux&quot;,&quot;node-role.kubernetes.io/control-plane&quot;:&quot;&quot;,&quot;node.kubernetes.io/exclude-from-external-load-balancers&quot;:&quot;&quot;},&quot;annotations&quot;:{&quot;kubeadm.alpha.kubernetes.io/cri-socket&quot;:&quot;unix:///run/containerd/containerd.sock&quot;,&quot;node.alpha.kubernetes.io/ttl&quot;:&quot;0&quot; ...

答案1

得分: 0

收到。以下是翻译的内容:

收到的响应不是一个格式良好的 JSON 结构

我认为你对 client-go 模块的操作方式有些困惑。

来自 REST API 的响应绝对是一个格式良好的 JSON 响应,但是它会被解组成一个 Go 数据结构(例如 这个)。

如果你想要访问返回的节点,可以使用标准的 Go 语法与结果进行交互:

response, err := exampleRestClient.
  Get().
  Resource("nodes").
  Do(context.Background()).
  StatusCode(&statusCode).
  ContentType(&contentType).
  Get()

if err != nil {
  panic(err)
}

nodes := response.(*v1.NodeList)
for _, node := range nodes.Items {
  fmt.Printf("name: %s\n", node.ObjectMeta.GetName())
  fmt.Printf("addresses:\n")
  for _, addr := range node.Status.Addresses {
    fmt.Printf("  %s: %s\n", addr.Type, addr.Address)
  }
}

我期望的输出结果应该像下面的命令返回的那样:

为什么?client-go 绑定返回的数据对你的 Go 代码非常有用。如果你想要生成 JSON 输出,你需要显式地将资源编组为 JSON 格式:

response, err := exampleRestClient.
  Get().
  Resource("nodes").
  Do(context.Background()).
  StatusCode(&statusCode).
  ContentType(&contentType).
  Get()

if err != nil {
  panic(err)
}

out, err := json.Marshal(response)
fmt.Print(string(out))
英文:

> The response received is not a well-formed JSON structure

I think you are confused about how the client-go module operates.

The response from the REST API absolutely was a well-formed JSON response, but this gets unmarshaled into a Go data structure (such as this).

If you want to access the returned nodes, you interact with the result using standard Go syntax:

response, err := exampleRestClient.
  Get().
  Resource(&quot;nodes&quot;).
  Do(context.Background()).
  StatusCode(&amp;statusCode).
  ContentType(&amp;contentType).
  Get()

if err != nil {
  panic(err)
}

nodes := response.(*v1.NodeList)
for _, node := range nodes.Items {
  fmt.Printf(&quot;name: %s\n&quot;, node.ObjectMeta.GetName())
  fmt.Printf(&quot;addresses:\n&quot;)
  for _, addr := range node.Status.Addresses {
    fmt.Printf(&quot;  %s: %s\n&quot;, addr.Type, addr.Address)
  }
}

> I expected the output to be like what the following command returns:

Why? The client-go bindings return data that's going to be useful to your Go code. If you want to produce JSON output, you need to explicitly marshal the resources to JSON format:

response, err := exampleRestClient.
  Get().
  Resource(&quot;nodes&quot;).
  Do(context.Background()).
  StatusCode(&amp;statusCode).
  ContentType(&amp;contentType).
  Get()

if err != nil {
  panic(err)
}

out, err := json.Marshal(response)
fmt.Print(string(out))

huangapple
  • 本文由 发表于 2023年2月21日 01:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75512684.html
匿名

发表评论

匿名网友

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

确定