Kubernetes Go客户端 – 仅编码为映射或数组的错误可以解码为结构体。

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

Kubernetes client for Go - ERRORS only encoded map or array can be decoded into a struct

问题

我正在尝试使用Go语言的Kubernetes客户端更新Openshift中的现有部署。我正在使用以下JSON将副本数更新为3:

JSON:

{
"kind": "Deployment",
"spec": {
"template": {
"spec": {
"containers": {
"image": "docker.fmr.com/fmr-pr000105/testcontainer:1.0.0",
"name": "testcontainer",
"resources": {
"requests": {
"cpu": "50m"
},
"limits": {
"cpu": "50m",
"memory": "50M"
}
},
"ports": {
"protocol": "TCP",
"name": "test-con-http",
"containerPort": 22
}
}
},
"metadata": {
"labels": {
"app": "testcontainer"
}
}
},
"replicas": 3
},
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "testcontainer"
}
}

但是一直收到以下错误:

only encoded map or array can be decoded into a struct

我正在使用以下代码:

import (
"fmt"
"flag"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/kubernetes"
apiv1 "k8s.io/client-go/pkg/api/v1"

"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/apis/extensions"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"

"bufio"
"os"

)

func main() {
var jsonBody []byte
jsonBody = rteMockedUp.GetJsonBody()
d := api.Codecs.UniversalDecoder()
obj, _, err := d.Decode(jsonBody, nil, nil)
if err != nil {
log.Fatalf("could not decode json: %s\n%s", jsonBody, err)
}
src := obj.(*extensions.Deployment)
dst := &v1beta1.Deployment{}
err = api.Scheme.Convert(src,dst,0)
if err != nil {
log.Fatalf("failed to convert: %s", err)
}
updateStatus, err := deploymentsClient.Update(dst)
if err != nil {
log.Fatalf("Update failed %s", err)
}
}

错误发生在这里:

obj, _, err := d.Decode(jsonBody, nil, nil)

我的JSON有问题吗?

英文:

I am trying to update the existing deployments in Openshift using Kubernetes Client for Go. I am using the following JSON to update the replicas to 3:

JSON:

{
  "kind": "Deployment",
  "spec": {
    "template": {
      "spec": {
        "containers": {
          "image": "docker.fmr.com\/fmr-pr000105\/testcontainer:1.0.0",
          "name": "testcontainer",
          "resources": {
            "requests": {
              "cpu": "50m"
            },
            "limits": {
              "cpu": "50m",
              "memory": "50M"
            }
          },
          "ports": {
            "protocol": "TCP",
            "name": "test-con-http",
            "containerPort": 22
          }
        }
      },
      "metadata": {
        "labels": {
          "app": "testcontainer"
        }
      }
    },
    "replicas": 3
  },
  "apiVersion": "extensions\/v1beta1",
  "metadata": {
    "name": "testcontainer"
  }
}

But keep on getting the error:

only encoded map or array can be decoded into a struct

I am using the following code :

import (
"fmt"
"flag"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/kubernetes"
apiv1 "k8s.io/client-go/pkg/api/v1"

"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/apis/extensions"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"

"bufio"
"os"

)

func main() {
    var jsonBody []byte
    jsonBody = rteMockedUp.GetJsonBody()
    d := api.Codecs.UniversalDecoder()
    obj, _, err := d.Decode(jsonBody, nil, nil)
    if err != nil {
    	log.Fatalf("could not decode json: %s\n%s", jsonBody, err)
    }
    src := obj.(*extensions.Deployment)
    dst := &v1beta1.Deployment{}
    err = api.Scheme.Convert(src,dst,0)
    if err != nil {
	    log.Fatalf("failed to convert: %s", err)
    }
    updateStatus, err := deploymentsClient.Update(dst)
    if err != nil {
    	log.Fatalf("Update failed %s", err)
    }
}

An error is thrown over here:

obj, _, err := d.Decode(jsonBody, nil, nil)

Is there a problem with my JSON?

答案1

得分: 5

通常当出现这种情况时,意味着定义有问题。在你的情况下,containers 应该是一个数组而不是一个对象。

英文:

Normally when this happens it means there is something wrong with the definition. In your case the containers should be an array and not an object.

huangapple
  • 本文由 发表于 2017年7月5日 23:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/44930612.html
匿名

发表评论

匿名网友

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

确定