docs/src/kubectl-and-helm.md
The kubectl and helm commands produce tabular-looking output, which Miller can parse -- however,
there's a bit of whitespace-handling to be dealt with first.
The output of kubectl looks tabular -- so PPRINT format is perhaps a good choice.
We can verify this using kubectl -n my-namespace get pods | vim -, then :set list within vim;
or, perhaps piping the output to cat -t, or bat -A -- in any
case what looks like whitespace is really all space characters.
To double-check, it's helpful to run tabular-looking output through a format-converter, and make sure the column headers are being correctly identified as keys, and the remaining lines are being correctly identified as values:
<pre class="pre-non-highlight-non-pair"> $ kubectl -n my-namespace get pods | mlr --ipprint --ojson head -n 1 [ { "NAME": "app-5mjwm4-274754k8468", "READY": "0/1", "STATUS": "Completed", "RESTARTS": 0, "AGE": "14m" } ] </pre>Suppose we want to sort the information for non-completed pods by age. We can use
dhms2sec to turn the AGE into something sortable.
The output of helm list is a bit fussier. Here it's already clear that something's amiss, since not everything lines up:
This isn't likely to be PPRINT format, as we soon see. Also note that the space before +0000 is an issue.
Running through bat -A or cat -t shows an issue. Namely, the Helm authors are mixing tabs and spaces -- cat -t shows tabs as ^I:
This mix of tabs and spaces, while not PPRINT, also isn't quite TSV either. As above, it's helpful to run tabular-looking data through a format-converter to see how it's structured:
<pre class="pre-non-highlight-non-pair"> $ helm list | mlr --itsv --ojson head -n 1 [ { "NAME ": "appdev-an-sc-1500-5mjwm4", "NAMESPACE ": "service-xyz", "REVISION": "1 ", "UPDATED ": "2022-03-28 11:33:05.389975262 +0000 UTC", "STATUS ": "deployed", "CHART ": "appdev-cloud-test-7.1.12", "APP VERSION": " " } ] </pre>A solution here is Miller's clean-whitespace verb:
<pre class="pre-non-highlight-non-pair"> $ helm list | mlr --itsv --ojson clean-whitespace then head -n 1 [ { "NAME": "appdev-an-sc-1500-5mjwm4", "NAMESPACE": "service-xyz", "REVISION": "1 ", "UPDATED": "2022-03-28 11:33:05.389975262 +0000 UTC", "STATUS ": "deployed", "CHART": "appdev-cloud-test-7.1.12", "APP VERSION": "" } ] </pre>Now we have the keys and values correctly identified within the tabular-looking data.
To find oldest items, it would suffice to sort by the UPDATED column, as that sorts lexically.
However, let's parse the timestamps and compute their ages from the present:
Switching to NIDX format lets us extract fields and pass them onto other commands -- e.g. helm uninstall.
We just need to switch the output format to --onidx, then cut out the NAME field. (Maybe add then filter $AGESEC > 86400 or somesuch.)
Then
<pre class="pre-non-highlight-non-pair"> $ for name in $(cat names.txt); do helm uninstall $name; done </pre>