Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!