In kubernetes, how do I select by label is X or not set?

11/8/2017

I would like to be able to use kubectl apply and have it select all of the kubernetes objects that either have the label or don't have the label set at all.

I can do this with

kubectl apply -Rf config/ -l label1=val1

and

kubectl apply -Rf config/ -l '!label1'

But if I have multiple labels, this quickly gets out of hand:

kubectl apply -Rf config/ -l '!label1,!label2'

kubectl apply -Rf config/ -l 'label1=val1,!label2'

kubectl apply -Rf config/ -l '!label1,label2=val2'

kubectl apply -Rf config/ -l 'label1=val1,label2=val2'

What I'd like to do is something like

kubectl apply -Rf config/ -l 'label1 in (val1, <null>),label2 in (val2, <null>)'

Is that possible?

-- kg.
kubectl
kubernetes

0 Answers