serhii.net

In the middle of the desert you can say anything you want

29 May 2026

Kubectl get info about resource with templates and yaml

I wanted the metadata.annotation pod-requests, viewable in node config -> YAML on the Rancher webgui.

Came up w/ this:

kubectl get node node-name -o template  --template='{{ index .metadata.annotations "management.cattle.io/pod-requests" }}' 

#  | jq '.["nvidia.com/gpu"]' 

It refused to allow me to use - and / inside the name (k get node nodename -o template --template={{.metadata.annotations.management.cattle.io/pod-requests}} didn’t work), solution from Helm templating doesn’t let me use dash in names - Stack Overflow.

Fun version w/ templates and json (there has to be a better way):

k get node nodename -o template  --template='{"requests": {{ index .metadata.annotations "management.cattle.io/pod-requests" }}, "limits": {{ index .metadata.annotations "management.cattle.io/pod-requests" }}, "total": {"nvidia.com/gpu": {{ index .metadata.labels "nvidia.com/gpu.count" }} } }' | jq .

Returns:

{
  "requests": {
    "cpu": "19795m",
    "memory": "7450Mi",
    "nvidia.com/gpu": "1",
    "pods": "37"
  },
  "limits": {
    "cpu": "19795m",
    "memory": "7450Mi",
    "nvidia.com/gpu": "1",
    "pods": "37"
  },
  "total": {
    "nvidia.com/gpu": 1
  }
}

Or even

k get node nodename -o template  --template='{"requests": {{ index .metadata.annotations "management.cattle.io/pod-requests" }}, "limits": {{ index .metadata.annotations "management.cattle.io/pod-requests" }}, "total": {"nvidia.com/gpu": {{ index .metadata.labels "nvidia.com/gpu.count" }} } }' | jq '. | [.requests["nvidia.com/gpu"], .limits["nvidia.com/gpu"],  .total["nvidia.com/gpu"] | tonumber]' -c

# -> [2,1,4]

nu

I’m bored an why not. If I didn’t need this more often than once a year would do this properly but..

# k get nodes -A  -o yaml > /tmp/xxx
# let $d = open /tmp/xxx.yaml
k get nodes -A  -o yaml | from yaml | let $d

$d.items | get metadata | select name labels."nvidia.com/gpu.count"? annotations."management.cattle.io/pod-limits"? annotations."management.cattle.io/pod-requests"? | rename name gpucount limits requests   | update limits {|| from json} | update requests  {|| from json} | update limits {|$i| $i.limits."nvidia.com/gpu"?} | update requests {|$i| $i.requests."nvidia.com/gpu"?}
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus