StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 3 · Labels, Selectors & Annotations

Selector Queries

easyLabel SelectorsSet-Based Requirements—No data yet

Problem statement

The operational scripts target pods by hand-maintained name lists. Every list is wrong within a day: something is renamed, something is added, something moves environment, and the restart script skips it or hits the wrong one. The namespace fleet-ops runs eight labeled workloads. Answer seven operational questions about them without naming a single pod.

About this exercise

A selector is a query over labels. It is how everything in Kubernetes finds a set of objects it did not write down in advance - a Service finds its endpoints this way, a Deployment finds its pods this way, and so can you. A selector describes the set by what its members are, so it stays correct when the membership changes; a name list describes yesterday's membership and starts rotting immediately.

There are two families of requirement, and both can be attached to a listing:

  • Equality-based - a key must equal a value, or must not equal it. Several requirements can be given at once, and they are joined by AND: an object must satisfy every one of them to match.
  • Set-based - a key's value must be in a set of values, or notin it; or the key must merely exist on the object, or must not exist on it.

The subtle one is what happens to an object that does not carry the key at all:

  • exists matches only objects that carry the key.
  • notin matches objects whose value is outside the set - and also every object with no such key, because such an object has no value inside the set either.

So env notin (prod) and env exists are different questions with different answers, and neither is the complement of the other. That distinction is why an audit written with the wrong one quietly skips the unlabeled objects, which are exactly the ones worth finding.

An empty result is a legitimate answer to a legitimate query, not a failure.

kubectl get --help documents how a selector is attached to a listing.

How this project is graded

The grader runs each query against the live cluster itself and compares the seeded matches against your answer file, name for name. An answer that happens to be right because you typed the names out is still counted right here - but the seeded fleet is arranged so that the names do not tell you the answer, and the next exercises will not let you get away with it.

What you will build

Seven answer files in answers/, listed in answers/questions.md. Every file holds the matching pod names, one per line, sorted ascending, and nothing else - no headers, no pod/ prefix, no counts:

tier-web.txt        the web tier
prod-workers.txt    workers running in prod
batch-or-web.txt    everything whose tier is batch or web
not-prod.txt        everything whose env is not prod
env-set.txt         everything that carries an env label at all
env-unset.txt       everything that carries no env label
none.txt            batch workloads running in prod

The labels in use are app, tier (web, worker, batch) and env (prod, staging, dev). Not every pod carries all three.

Step 1 - equality, alone and combined

Answer the first two: one requirement, then two requirements that must both hold.

equality_selector_returns_its_matches and combined_requirements_intersect should pass

Step 2 - set membership

Answer the third with a single requirement over a set of values, not two queries stitched together.

set_based_in_returns_the_whole_set should pass

Step 3 - the key that is not there

Answer the next three. Two of them look like opposites and are not: one of them must include the pods that carry no env label at all, and one of them must exclude them.

notin_includes_objects_missing_the_key, exists_returns_only_objects_carrying_the_key and does_not_exist_returns_the_unlabeled_objects should pass

Step 4 - the empty answer

The last query matches nothing. answers/none.txt must exist and be empty - zero bytes, no error text, no "none".

empty_match_is_an_empty_answer_file should pass

When every test passes

You have:

  • Sets described by what their members are, not by the names they had yesterday
  • Requirements combined into one query instead of filtered by hand afterwards
  • The difference between "not that value" and "has the key" straight, and the unlabeled objects landing where they belong
  • An empty result treated as an answer

You do not have yet:

  • Anywhere to keep the metadata that is not a selector key - ticket numbers, owners, build JSON. Labels are the wrong home for it, and annotations are the next exercise.

Verify

Click Test after every step.

More in Labels, Selectors & Annotations

See the module
Labeling SchemeBring an inconsistently labeled namespace to one label scheme - applied at creation, corrected on live objects, a retired key removed, and read back from the cluster.—No data yeteasyAnnotations for MachineryAttach deploy metadata - a ticket, an owner, a build JSON and an over-length summary - to the object it describes, update one key, retire another, and prove selectors never see any of it.—No data yeteasyFleet BookkeepingBring an unmanaged namespace under one label scheme, attach the machine-readable deploy record, and answer every fleet operation by selector - proven by an object the grader adds behind your back.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.