Saturday, July 8, 2023

Krew custom columns

My contribution to the custom-cols plugin for Krew: show what nodes pods are running on.

Create a file ~/.krew/store/custom-cols/v0.0.5/tamplates/node.tpl so that it contains:

 NAME             NODE             STATUS 
 .metadata.name   .spec.nodeName   .status.phase 

The output will look something like:

 tim@cf-desk:~$ kubectl custom-cols -o node pods -n weave 
 NAME                                         NODE   STATUS 
 weave-scope-agent-g9jgh                      cf1    Running 
 weave-scope-agent-gllg5                      cf2    Running 
 weave-scope-agent-kkm2z                      cf3    Running 
 weave-scope-app-658845597b-wnt9b             cf2    Running 
 weave-scope-cluster-agent-84f7b6767c-2vdkw   cf2    Running 

There may also be some value in making it sortable, based on the node. To do so, create another template (I called mine "nodes.tpl")and swap the first and second columns in each row. Then you can pipe the output through the tail and sort commands. Example template:

 NODE              NAME            STATUS 
 .spec.nodeName    .metadata.name  .status.phase 

The output will look something like:

 tim@cf-desk:~$ k custom-cols -o nodes pods -n weave|tail -n +2|sort 
 cf1    weave-scope-agent-g9jgh                      Running 
 cf2    weave-scope-agent-gllg5                      Running 
 cf2    weave-scope-app-658845597b-wnt9b             Running 
 cf2    weave-scope-cluster-agent-84f7b6767c-2vdkw   Running 
 cf3    weave-scope-agent-kkm2z                      Running 

For info: the "-n +2" in the above tells tail to start processing on the second line (i.e., skip the line with the column headers).