CodeBlanks Logo Icon CodeBlanks

Equality and set selectors

Learn equality and set-based selectors.

Equality and set selectors

There are two kinds of selectors:

  • Equality-based: =, ==, !=.
  • Set-based: in, notin, existence (key) and absence (!key).
kubectl get pods -l 'ortam in (prod,staging)'
kubectl get pods -l 'katman notin (test)'
kubectl get pods -l 'surum'                  # those with a surum label
kubectl get pods -l 'app=web,ortam in (prod,staging),!eski'

Since parentheses and exclamation marks have special meaning in the shell, the expression is quoted.

Counterparts in YAML

The set expressions on the command line correspond to matchExpressions operators in YAML:

# -l 'ortam in (prod,staging)'
- key: ortam
  operator: In
  values: [prod, staging]
# -l '!eski'
- key: eski
  operator: DoesNotExist
# -l 'surum'
- key: surum
  operator: Exists

Practice Set queries

Write three commands listing: the pods whose ortam is prod or staging; the pods whose katman isn't test; the pods without an eski label.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl get pods -l 'ortam in (prod,staging)'
kubectl get pods -l 'katman notin (test)'
kubectl get pods -l '!eski'

Expected output

NAME                  READY   STATUS    RESTARTS   AGE
web-7c5b9f6d8-abcde   1/1     Running   0          2h
web-staging-9f8e7d    1/1     Running   0          1h

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Does the expression katman notin (test) select pods with no katman label at all?

  • It errors
  • Yes; notin also covers those without the key Correct answer
  • No
  • Only in the default namespace

Explanation: For only those that have the label and it isn't test, write katman,katman notin (test).

2 Select those whose value is in a set.

Question
kubectl get pods -l 'ortam ____ (prod,staging)'
Answer
kubectl get pods -l 'ortam in (prod,staging)'

Explanation: in is a set-based operator.

3 Match.

Term Meaning
in In
notin NotIn
anahtar Exists
!anahtar DoesNotExist

All lessons in Intermediate