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
Change your import from "k8s.io/kubernetes/pkg/apis/rbac" to rbac "k8s.io/api/rbac/v1".
It should work.