How to use expressions by name to select multiple pods to apply labels or annotations?

9/6/2020

I want to understand how the following commands works and what kind of expressions are supported:

kubectl label pod foo{1..3} fizz=buzz

The foo{1..3} selects:

foo1
foo2
foo3

I couldn't have found any documentation so far.

-- Sebastian Aburto
kubectl
kubernetes

1 Answer

9/6/2020

That syntax is the GNU Bash brace expansion extension syntax. Some other shells like zsh support it too, but it is not one of the Word Expansions in the POSIX shell spec; it won't work with some minimalist shells like the default dash shell in Debian GNU/Linux or the Busybox shell in an Alpine Docker image.

That means this is expanded by your local shell, to construct the arguments to kubectl. Most of the expansion possibilities focus on filenames or environment variables. (foo* would match local files whose names begin with foo, not Kubernetes pods.) Potentially you could find $(command) substitution or $(( 1 + 2 )) arithmetic substitution useful. There's not any broader Kubernetes name-matching syntax that gets used here, this is exclusively local-shell processing.

-- David Maze
Source: StackOverflow