Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 532
vickeyshrestha Avatar asked Aug 01 '26 00:08

vickeyshrestha


1 Answers

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.

like image 130
lang2 Avatar answered Aug 05 '26 13:08

lang2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!