Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 '/'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading