3

我正在尝试使用 Kubernetes Client for Go 更新 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 有问题吗?

4

1 回答 1

5

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

于 2017-07-05T23:52:06.180 回答