Search code examples
kuberneteskubectlgo-templates

kubectl -o go-template > bad character U+002F '/'


I'm trying to get the restartCount of the pod for matching deployment but '/' causing an issue

Query: kubectl -n <namespace> get pod -o go-template='{{range .items}}{{if eq .metadata.labels.app.kubernetes.io/name "my-app"}}{{ (index .status.containerStatuses 0).restartCount}}{{end}}{{end}}'

Error: template: output:1: bad character U+002F '/'

I tried with the 'index' but did not help. Also tried adding quotes, escaping the special character '/' but no luck.

Any help is really appreciable. Thank you.


Solution

  • In the test

    if eq .metadata.labels.app.kubernetes.io/name "my-app"
    

    the / character isn't allowed in the Go text/template syntax. You are probably looking for an object like

    metadata:
      labels:
        app.kubernetes.io/name: my-app
    

    and . also has special syntax here: it will look up metadata, labels, and then a single level app within that and so on.

    The standard index function will let you use an arbitrary string as a map index (in other contexts, including a computed value, or an integer for an array index) and that's probably what you need here

    if eq (index .metadata.labels "app.kubernetes.io/name") "my-app"