Bartosz Bierkowski - Low dose cloud

OpenShift Morsels: displaying more information using go template

On day #12 an alternative to json path – go templates.

Lets start with the same examples as in case of json path. Getting pod names from single project looks quite familiar. Iterating over items does not require [*] an all mustache is doubled.

Getting information about containers also looks similar. Here I explicitly iterate over images in single pod.

Similar examples to the ones from json path do not give the full picture. The go-template is full programming language where you can create variables, use if/else statements.

You can find more interesting query with some logic applied below.

The template prints out only pods with "restricted" scc in annotation field. It is an extension of the second template above.

In detail the additional code is:

  • {{$scc := index .metadata.annotations ""}}  – creates a variable called $scc and assigns to it a value from .metadata.annotations map. The index  function returns an item with key ""  from map .metadata.annotations
  • {{if eq $scc "restricted"}} ... {{end}}  – here we compare if $scc is equal to "restricted"  using the eq function.

The rest is iteration over items and images like before.

Alternatively, when the template grows too big, you can use  go-template-file  output and store the template in the file instead of command line.

Documentation – I actually found some good json path examples on kubernetes documentation page with single one using go-template – go lang documentation about templates. All the functions and elements I used are documented there. – I found this answer very helpful to learn about accessing the map and extend the knowledge of go-templates


The commands were executed using minishift and the following client/server versions of OpenShift.

oc v3.6.1+008f2d5
client kubernetes v1.6.1+5115d708d7
openshift v3.6.0+c4dd4cf
server kubernetes v1.6.1+5115d708d7


Thanks for reading the OpenShift morsels. To get updates about new articles, you can sign up to the newsletter below.

As a thank you message, you will also get access to OpenShift CLI CheatSheet listing most commonly used commands together with a short explanation.

Did you like the article?
Join the newsletter to receive notifications about new articles.
I respect your privacy.