Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
JSON parsing of ClusterRole in Go is not working

I have a problem parsing the metadata in ClusterRole object from Json in Go.

I made this example:

package main

import (
    "encoding/json"
    "fmt"

    "k8s.io/kubernetes/pkg/apis/rbac"
)

func main() {
    clusterDemo := rbac.ClusterRole{}

    clusterRolesRaw := `{
        "apiVersion": "rbac.authorization.k8s.io/v1",
        "kind": "ClusterRole",
        "metadata": {
            "creationTimestamp": "2021-01-21T08:07:13Z",
            "managedFields": [],
            "name": "kubeadm:get-nodes",
            "resourceVersion": "220",
            "uid": "c78a8b10-cd20-4b64-8e4d-4f7f758c7b65"
        },
        "rules": []
    }`
    err := json.Unmarshal([]byte(clusterRolesRaw), &clusterDemo)
    if err != nil {
        fmt.Printf("unmarshal file: %v", err)
    }
    fmt.Printf("Name: %s\n", clusterDemo.Name)
    fmt.Printf("Kind: %s\n", clusterDemo.Kind)
}

Run it with

# go get -v ./... 
# go run <filename>
Name: 
Kind: ClusterRole

As you can see, the Kind field is parsed, but the Name field (part of the metadata) is not.

My question is why? It looks like the nested structure is not parsed.

Any help is appreciated, I think I am missing something

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Change your import from "k8s.io/kubernetes/pkg/apis/rbac" to rbac "k8s.io/api/rbac/v1".

It should work.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda