# Labels and selectors

> How a Minekube network picks its servers - labels on every instance, Kubernetes-style label selectors in text and Schema form, system labels, how Velocity finds its backends by selector, and mk instances and mk claim.

> **Preview: Connect in the Minekube plugin:**
> This page describes networks in the Minekube plugin, the next generation of Connect. Today's Connect plugin and its `<endpoint>.play.minekube.net` addresses are described in the [Connect docs](https://minekube.com/guide/) and keep working. [Networks in the Minekube plugin](https://minekube.com/guide/networks/) compares the two.

Every server and proxy in a Minekube network is an **instance** with **labels**: `stage=lobby`, `game=arena`, `mode=solo`. Where code needs a server, it names a **selector** instead of an address, the way Kubernetes picks pods. A server can restart under a new key, move to another host or sit behind NAT, and the selector still finds it.

## Instances and labels

An instance is a running server or proxy with the Minekube plugin and its connector. Its identity is its key, kept in `instance.key` in the plugin folder and made on first start. Its labels say what it is.

| What | Set by | Example |
| --- | --- | --- |
| Your labels | You, in the instance's `connector.json` | `stage=lobby`, `game=arena`, `mode=solo` |
| System labels, under `minekube.com/` | The registry | `minekube.com/platform=paper`, `minekube.com/version=26.3`, `minekube.com/region=…` |
| Entry label | You, on the instances players join first | `minekube.com/entry=true` |

- **The registry sets the system labels**: the platform (`paper`, `velocity`), its version, and the region of the instance's home relay when it is known. Your own labels under `minekube.com/` are dropped, except `minekube.com/entry`.
- **`minekube.com/entry=true` marks where players land** when they join the network's address: the proxy, or a server that runs without one.
- **Labels give no rights.** Only an instance whose key belongs to the network is part of it.
- The instance's **status** (online, players, free slots) is not a label. Selectors pick by labels, and the status decides among the matches.

## Selectors

A selector is a Kubernetes [`LabelSelector`](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors): `matchLabels` and `matchExpressions` with the operators `In`, `NotIn`, `Exists` and `DoesNotExist`. The same selector has a text form, as in `kubectl -l`:

| Text | Means | Becomes |
| --- | --- | --- |
| `stage=lobby` (or `==`) | The label equals the value | `matchLabels` |
| `tier!=cache` | The label differs, or is missing | `NotIn` |
| `mode in (solo,duo)` | The label is one of the values | `In` |
| `mode notin (solo,duo)` | The label is none of the values, or is missing | `NotIn` |
| `draining` | The label exists | `Exists` |
| `!beta` | The label does not exist | `DoesNotExist` |

Separate requirements with commas: `game=bedwars,mode in (solo,duo),!beta`. Every requirement must hold, and an empty selector matches every instance.

A key is a name of up to 63 characters, letters and digits at both ends and `-`, `_` or `.` between, optionally after a DNS prefix and a slash, as in `minekube.com/entry`. A value is empty or such a name.

### Selectors in code

`@minekube/network/LabelSelector` is the implementation the registry and the CLI use: the Schema, the text form and matching.

```ts

const lobbies = Effect.gen(function* () {
  // { matchLabels: { game: "bedwars" }, matchExpressions: [{ key: "mode", operator: "In", values: ["solo", "duo"] }, { key: "beta", operator: "DoesNotExist" }] }
  const selector = yield* LabelSelector.parse("game=bedwars,mode in (solo,duo),!beta");
  const matches = LabelSelector.matches(selector);
  return [matches({ game: "bedwars", mode: "solo" }), matches({ game: "bedwars", mode: "solo", beta: "true" })]; // [true, false]
});
```

`parse` fails with `InvalidSelector` and a message such as `a requirement is missing after the last ","`. `format` turns a selector back into its text form.

## How Velocity finds its servers

Velocity keeps its server names: `/server arena` still works, and so do your network bundle's `proxy.getServer("lobby")` calls. Behind each name, the proxy's connector resolves a selector. A player moves between servers by their labels, over the connection they joined with:

> Scene: The dev network: sam joins at the relay, and the proxy sends him to the arena, the server whose labels match game=arena. He types /lobby and the proxy moves him to the server matching stage=lobby; /server arena moves him back. When the arena kicks him, the network sends him to the lobby instead of disconnecting him: one connection the whole time. mk instances lists the Paper servers with their labels before and after.

`mk server --network` writes the proxy's `plugins/minekube/connector.json`:

```json title="velocity/plugins/minekube/connector.json"
{
  "relays": ["http://127.0.0.1:3340"],
  "directory": { "registry": "http://localhost:4776" },
  "labels": { "minekube.com/entry": "true" },
  "servers": { "lobby": "stage=lobby", "arena": "game=arena" }
}
```

and each Paper server's `plugins/Minekube/connector.json`, here the lobby's:

```json title="lobby/plugins/Minekube/connector.json"
{
  "relays": ["http://127.0.0.1:3340"],
  "directory": { "registry": "http://localhost:4776" },
  "labels": { "stage": "lobby" }
}
```

- **`servers`** maps each Velocity server name to a selector. The runtime registers every name with Velocity at a loopback port of the connector.
- **Each new connection to that port** asks the registry for the selector, picks a ready instance with free slots, and opens a stream to it over iroh, connector to connector. The servers need no open port and may run on other hosts, behind NAT.
- **A server that restarts under a new key** is reachable again without any change, because its labels still match.
- Velocity still needs its `[servers]` entries in `velocity.toml` to start; the connector replaces them with its selector ports.

## List instances

`mk instances` lists a network's instances with their labels, players and whether they are online. `-l` takes a selector, as `kubectl -l` does:

```sh
mk instances --network dev
mk instances --network dev -l stage=lobby
mk instances --network dev -l 'minekube.com/platform=paper,game in (arena,duels)'
```

Each line shows the start of the instance's id, `online` or `offline`, players and slots, and its labels. Quote a selector with spaces or parentheses for your shell.

## Add an instance to a network

An instance whose key belongs to no network logs a code of 8 characters, valid for 15 minutes; after that it logs a new one. Add it to your network with the code:

```sh
mk claim ABCD2345 --network dev
```

`mk server --network` adds its own instances to the `dev` network without a code. `mk claim` and `mk instances` talk to the registry at `MINEKUBE_REGISTRY`, or `--registry`, by default the local one that `mk registry` and `mk server --network` run on `http://localhost:4776`, with the token in `MINEKUBE_DEV_TOKEN`.
