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

7/5/2017

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?

-- vickeyshrestha
go
kubernetes
kubernetes-go-client

1 Answer

7/5/2017

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.

-- lang2
Source: StackOverflow