Search code examples
bashkubernetessyntaxopenshift

Bash - access oc node properties


The problem is pretty simple, but I am struggling to understand where to even look for a solution. I want to iterate over a list of pods I retrieve from openshift and then output some of its properties.

In other words, what I want to do is this:

for node in $(oc get nodes);
do
  echo  ${node.name}
  echo  ${node.role}
done

Unfortunately that leads to the error "line 67: ${node.name}: bad substitution".

Just iterating over the nodes with echo node works fine, but it just lists all the properties with one line per property value.

the output of just oc get nodes is:

NAME                                    STATUS   ROLES    AGE   VERSION
avaloq-abcde-master-0-xyz               Ready    master   38d   v1.xx
avaloq-abcde-master-1-dfs               Ready    master   38d   v1.xx
avaloq-abcde-master-2-gsd               Ready    master   38d   v1.xx

the output of

for node in $(oc get nodes -o name);
do
  echo  ${node}
done

is

node/avaloq-abcde-master-0-xyz
node/avaloq-abcde-master-1-dfs
node/avaloq-abcde-master-2-gsd

If I try to add another property via -o (the output format?), it throws the following error:

error: unable to match a printer suitable for the output format "roles", allowed formats are: custom-columns,custom-columns-file,go-template,go-template-file,json,jsonpath,jsonpath-as-json,jsonpath-file,name,template,templatefile,wide,yaml

What I expect is the following output

node/avaloq-abcde-master-0-xyz
master
node/avaloq-abcde-master-1-dfs
master
node/avaloq-abcde-master-2-gsd
master

I assume that is where my understanding is lacking. Is the "array of objects/ table" returned via "oc get nodes" not an array of objects but rather just a text list separated by tabs and return characters?


Solution

  • You can use something like this if you only need specific columns from oc get node command

    oc get nodes | awk '{print $1, $3}'
    

    which will print output like

    avaloq-abcde-master-0-xyz master 
    avaloq-abcde-master-1-dfs master
    avaloq-abcde-master-2-gsd master