Kubernetes: Add -N,--names-only flag to kubectl get

Created on 3 Nov 2015  ·  3Comments  ·  Source: kubernetes/kubernetes

Many admin operations are made easier by fetching a list of the names of things, e.g. into a shell variable. See below for an example. Rather than hacking up a template, asking for just the names should be a normal flag.

% nodes=$(kubectl get nodes -lfoo=bar -otemplate --template='{{range .items}}{{.metadata.name}} {{end}}')
% for node in $nodes; do
for> echo $node
for> done

# Sanity check before beginning work.
% echo $nodes | wc -w
4

% for node in $nodes; do 
for> kubectl drain --force $node
for> maintenance_script $node
for> kubectl undrain $node
for> sleep 900
for> done

Most helpful comment

Try kubectl get no -o name

All 3 comments

You could use jsonpath to make it a little shorter but still not great

k get no -o jsonpath="{.items[*].metadata.name}" | xargs -d ' ' -I {} kubectl drain {}

Try kubectl get no -o name

you want -o name

On Mon, Nov 2, 2015 at 4:37 PM, Matt Liggett [email protected]
wrote:

Many admin operations are made easier by fetching a list of the names of
things, e.g. into a shell variable. See below for an example. Rather than
hacking up a template, asking for just the names should be a normal flag.

% nodes=$(kubectl get nodes -lfoo=bar -otemplate --template='{{range .items}}{{.metadata.name}} {{end}}')
% for node in $nodes; do
for> echo $node
for> done

Sanity check before beginning work.

% echo $nodes | wc -w
4

% for node in $nodes; do
for> kubectl drain --force $node
for> maintenance_script $node
for> kubectl undrain $node
for> sleep 900
for> done


Reply to this email directly or view it on GitHub
https://github.com/kubernetes/kubernetes/issues/16700.

Was this page helpful?
0 / 5 - 0 ratings