Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a column with Kubectl

Tags:

kubernetes

I use a tmuxinator to generate a tmux window that watchs the output of some kubectl commands like:

watch -n 5 kubectl get pods/rc/svc/pv/pvc

But sometimes the output of Kubectl get too wide, i.e the selector column after rolling updates, and I would like not to show it on my setup. How do I do this with kubectl alone?

awk or cut can do the job too, but I could not figure out a way of doing this without loosing the table formating.

like image 777
CESCO Avatar asked Mar 14 '16 14:03

CESCO


2 Answers

Rather than using a second tool/binary like awk and column. you can use the flag -o=custom-columns in this way: kubectl get pods --all-namespaces -o=custom-columns=NAME:.metadata.name,Namespace:.metadata.namespace

This is also an alternative and easy way to output custom columns than go-templates or jsonpath!

like image 119
garlicFrancium Avatar answered Sep 30 '22 17:09

garlicFrancium


There is no explicit support for selecting a subset of columns in kubectl, but there are a few ways to accomplish this. You already mentioned awk which can be pared with column -t to get a nice table format:

$ kubectl get pods --all-namespaces | awk {'print $1" " $2'} | column -t
NAMESPACE    NAME
kube-system  fluentd-cloud-logging-k8s-stclair-minion-wnzd
kube-system  kube-dns-v10-fo6gl
kube-system  kube-proxy-k8s-stclair-minion-wnzd
...

Alternatively, you could use the go-template output of kubectl to create a custom output (which you could also pair with column), e.g. to print pod names and the first 8 characters of the UID:

$ kubectl get pods --all-namespaces -o=go-template='{{println "NAME UID"}}{{range .items}}{{.metadata.name}} {{printf "%.8s\n" .metadata.uid}}{{end}}' | column -t
NAME                                           UID
fluentd-cloud-logging-k8s-stclair-minion-wnzd  8bcb7129
kube-dns-v10-fo6gl                             90bce35e
kube-proxy-k8s-stclair-minion-wnzd             8bc752c8
kubernetes-dashboard-v0.1.0-cptxn              90d18852
l7-lb-controller-v0.5.2-n6i23                  90daf833

More on go-templates here. A variant of JSONpath is also supported.

like image 23
Tim Allclair Avatar answered Sep 30 '22 17:09

Tim Allclair