# Pools and leases

> For the complete documentation index, see [llms.txt](https://docs.ringleader.dev/llms.txt).


> A workstation pool is a set of workstations that Ringleader builds from one template and lends out one at a time.


A workstation pool is a set of workstations that Ringleader builds from one template and
lends out one at a time. A CI job, a script or a person
asks the pool for a workstation, works on it, and hands it back. The pool then deletes that
workstation, or, when you tell it to recycle, cleans it and keeps it for the next request.
Between requests, the pool keeps as many idle workstations, running or stopped, ready as you
declare. The next request then waits seconds
instead of the minutes a new workstation takes. To create a pool, lease a member or recycle
members step by step, follow
[Lease a workstation from a pool](https://docs.ringleader.dev/guides/lease-pool-workstation/).

Three words recur on this page:

- A **member** is a workstation the pool created and owns.
- A **lease** is a request for one member, recorded as a `WorkstationPoolLease`.
- The lease's **holder** is whoever created it.

## Pool template and owner

A pool's `spec.template.spec` is a Workstation spec, and Ringleader checks it the way it
checks a workstation you apply. The pool decides when a member runs and how long it lives.
So the template can't set `stopped`, `lifecycle`, `ttl`, `ttlAction`, `restartNonce` or the
other nonce fields. Every member carries the labels under `spec.template.metadata`. A label
that starts with `pools.ringleader.dev/` is rejected there, because Ringleader writes those
itself.

<div class="callout callout-warning">
  <p class="callout-title">Lease holders can read the template</p>
  <div class="callout-body">
    Every lease holder can read the template, and a Secret the template uses reaches every
holder.
  </div>
</div>


Every member belongs to the pool's owner. The owner can open a shell on every member, from
every device they've registered, including a member someone else holds. So a pool that
several people share is usually owned by a
[service account](https://docs.ringleader.dev/reference/resources/serviceaccount/) rather than a person.

A configuration layer reaches a member the way it reaches any workstation:

- A WorkstationConfig that selects by label reaches a member when its author's reach covers
  the pool's owner.
- A WorkstationConfig the template names in `configs` reaches every member.
- A personal layer never reaches a member.

`rl workstationpool create --from-workstation` adds some layers to the template's `configs`.
These are the layers that reached the workstation by label and would otherwise stop reaching
a member someone else owns. It leaves out personal layers, layers an administrator wrote, and layers an Integration
generates.
[Configuration layers](https://docs.ringleader.dev/workstations/configuration/configuration-layers/)
explains who a layer reaches.

## Pool leases

A lease names a pool, and its `spec.state` says what the holder wants: `Needed`, then `Done`
or `Recycle`. The lease ends at its `spec.expiresAt` if nobody releases it first.
[Lease spec fields](https://docs.ringleader.dev/reference/resources/workstationpool/#lease-spec-fields) gives the
rules for both fields. To renew a lease, write a later `expiresAt` before the current one
passes. A holder that stops renewing loses its member at most one `lease.maxTerm` after its
last renewal.

The pool picks a member for a waiting lease in this order:

1. An idle running member, the most recently used first.
2. A member that's already starting or being built.
3. An idle stopped member, which the pool starts.
4. A new member, while the pool is below `size.max` and its template is healthy.

When none of these is available, the lease waits as `Pending`, and its `status.message` says
what it's waiting for. The pool serves waiting leases oldest first.
[Lease phases](https://docs.ringleader.dev/reference/resources/workstationpool/#lease-phases) lists what each
phase means.

A lease ends in one of these ways:

- The holder writes `Done`. The pool removes the holder's access and deletes the member. The
  lease goes once the delete is accepted, while the member finishes shutting down.
- The holder writes `Recycle`. The pool removes the access and recycles the member, or
  deletes it when it can't recycle, as [Member recycling](#member-recycling) describes.
- The holder deletes the lease. That's a release: `Done`, unless the holder had already
  written `Recycle`.
- The lease expires. It goes `Failed` with reason `Expired`, and the pool deletes its member.
  The pool never recycles a member whose holder stopped renewing.
- The holder writes `Done` or `Recycle` on a lease that's still `Pending`. That cancels it,
  and the pool deletes the lease.

A `Failed` lease stays so its holder can read what happened. It goes when the holder writes
`Done` or `Recycle` or deletes it, or when `lease.failedRetention` passes. A pool keeps at
most `lease.maxPending` failed leases, and drops the oldest beyond that.

## Access for a lease holder

A lease gives its holder two things while it holds a member, and nothing else:

- Every key in `spec.authorizedKeys` can log in as the member's login user.
  [`rl workstationpool lease`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-workstationpool-lease-pool)
  puts your device's own key there unless you pass `--key`. So `rl shell --script`,
  `rl shell --copy`, `rl file cp` and `rl logs` work on the member at once. This is how a
  service account reaches a member.
- A person who holds a lease also gets an SSH share on the member, the same
  [Grant](https://docs.ringleader.dev/organizations-teams/accounts-access/roles-grants/#grants) that
  `rl workstation share` creates. The pool's owner already has it.

The holder doesn't own the member, and can't edit or delete it. Personal configuration the
holder declared for their own workstations stays off the member. When the lease ends, the
pool removes the keys and deletes the share before it deletes or recycles the member.
Removing a key stops new logins, but doesn't close a session that's already open.

Leasing is a right on the pool. The pool's owner holds it, and so does a namespace
administrator on every pool in the namespace. Anyone else needs a Grant on the pool with the `lease` right,
which
[`rl workstationpool share`](https://docs.ringleader.dev/reference/cli/commands/shares-access/#rl-workstationpool-share-name)
creates. Taking the Grant away stops new leases and leaves running ones alone. Ringleader
rejects a lease while its pool is being deleted, and when the pool already has
`lease.maxPending` leases waiting.

## Warm pool members

An idle member costs money while it exists, so the pool keeps only the idle members you
declare, and lets the rest go on timers:

- `size.max` is the most members the pool has at once, leased or idle. Members still being
  deleted don't count.
- `warm.running` idle members stay running. This is a floor, the fewest idle members the
  pool keeps in that state. To reach it, the pool starts a stopped member or builds a new one.
- `warm.stopped` idle members stay stopped, configured and ready to start. This is a floor too.
- An idle running member above `warm.running` stops after `idle.stopAfter`.
- A stopped idle member above the floors is deleted after `idle.deleteAfter`.
- A member older than `maxAge` is deleted once it's idle, and replaced if a floor needs it.
  It's never recycled.

[Spec fields](https://docs.ringleader.dev/reference/resources/workstationpool/#spec-fields) lists the limits and
defaults. A waiting lease takes an idle member even when that empties a floor, and the pool
refills the floor afterward. When
the pool lets members go, it keeps those with the most life left: the fewest recycles first,
then the youngest. A new member always boots running, so a member built for the stopped
floor is configured first and stopped once it's ready.

## Member recycling

A pool deletes every released member unless its spec declares a `recycle` block. To turn
recycling on, follow
[Recycle members between leases](https://docs.ringleader.dev/guides/lease-pool-workstation/#recycle-members-between-leases). With one, a
lease released with `Recycle` returns its member to the pool after a cleanup. A recycle runs
these steps on the member, in order:

1. The in-VM agent rewrites the block of authorized keys it manages, without the lease's keys
   and the holder's devices. Nothing the lease granted can log in once the cleanup starts.
2. It runs every script declared with `runPolicy: onRecycle`, from the template or from any
   layer that reaches the member, in configuration order.
3. It brings the member back to its template, as an ordinary configuration run does.
4. With `reboot: true`, the pool stops and starts the member, and the in-VM agent configures
   it again when it boots.

A recycle cleans nothing else. The next holder can read whatever the scripts leave behind:

- Files the scripts didn't remove, and container images and volumes.
- Processes still running, and SSH sessions still open.
- The login user's scheduled jobs.

A holder with `sudo` can also leave a way back in. It could be a key outside the block the
agent manages, a key for root, or a new user account.

<div class="callout callout-warning">
  <p class="callout-title">A recycled member keeps what the last holder left</p>
  <div class="callout-body">
    If your holders must not reach each other&rsquo;s work, release with <code>Done</code> instead.
  </div>
</div>


If a recycle fails, the pool deletes the member. A recycle fails when a script fails, when the
member doesn't reach `Configured` again, or when the whole recycle runs past
`recycle.timeout`. The lease goes `Failed` with reason `RecycleFailed` or `RecycleTimedOut`,
and the pool records an event that names the member and the failed script. The pool never
tries the recycle again on the same member.

A `Recycle` request deletes the member instead in six cases, and the lease's
`status.release.reason` names the one that applied:

- `RecycleDisabled`: the pool has no `recycle` block.
- `MaxReusesReached`: the member has been recycled `maxReuses` times.
- `MaxAgeReached`: the member is older than `maxAge`.
- `TemplateChanged`: the member was built from an older template.
- `Cordoned`: the member is cordoned.
- `RecycleUnsupported`: the in-VM agent on the member is too old to recycle.

Each member carries the annotation `pools.ringleader.dev/reuses`, which the pool raises after
every successful recycle. `rl workstation describe` on a member prints it beside the member's
pool, state, lease and cordon.

## Configuration changes during a lease

By default, a change to a configuration layer that reaches a member lands on it at once, as
on any workstation. That can happen in the middle of a CI job. With `lease.configChanges: Hold`,
a leased member keeps the configuration it had when its lease was bound. The change lands
when the lease ends, in the recycle or the replacement that also removes the holder's access.
The pool's `status.configChanges` shows the mode in force. A member's
`status.config.configHold` reads `Held` while a hold applies.

A hold covers the content of the configuration layers, including the text of a ConfigMap a
layer reads. It never covers access or control. These change at once, held or not:

- The lease's keys, the holder's share and the owner's devices.
- The member's power state and its egress policy.
- What an attached Integration makes the member reach, and the member's declared
  capabilities.

Setting the pool back to `Apply` ends every hold the next time the pool checks its members. A change to
`spec.template` never reaches a leased member in either mode. It replaces idle members only.

## Pool changes

Any change to `spec.template` makes the existing idle members out of date. The pool replaces
idle members built from the old template. It creates each new member first while it's below
`size.max`, so a floor never drops to zero. A leased member finishes its lease, and is then
deleted rather than recycled.

Lowering `size.max` deletes idle members above the new maximum, those with the least life
left first. It never touches a leased or cordoned member, so a pool can stay above its new
maximum until those are released or uncordoned. Every other field takes effect the next time
the pool checks its members.

## Pool failures

A new member has 30 minutes to report `Configured`. If it fails, or runs out of time, the pool
deletes it and records a `TemplateFailed` warning event with the member's reason. A lease
that was bound to it, and not yet `Ready`, is bound to another member. After three failures
in a row, the pool's `TemplateHealthy` condition goes `False` and its phase reads `Degraded`.
It then waits before building again: one minute, doubling up to an hour. Existing idle
members keep serving leases, and a change to the template clears the failure count.

The pool does more than build members, and other things can fail too. This table says what
the pool does in each case, and what the holder sees:

| What happened | What the pool does | What the holder sees |
|---------------|-------------------|----------------------|
| A leased member's virtual machine is lost, its Workstation is deleted, or it goes `Failed` | Deletes the member. A `Ready` lease fails and is never bound again, because work may have run on it. A lease not yet `Ready` goes back to `Pending` and is bound to another member. | `Failed`, reason `WorkstationLost`, if it was `Ready` |
| Nobody renews a lease | Fails it at `spec.expiresAt` and deletes its member. | `Failed`, reason `Expired` |
| The namespace's workstation limit is reached | Builds nothing, sets `WithinQuota` to `False`, and asks again once a minute. | `Pending`, with the reason |
| Starting a member would take its namespace or organization over a [running, vCPU or GPU cap](https://docs.ringleader.dev/organizations-teams/accounts-access/namespaces-limits/#running-limits) | Starts or creates nothing for that fill, sets `WithinRunningCaps` to `False` with a message that names the cap, and asks again once a minute. A lease that needs that member waits. | `Pending`, with the message that names the cap |
| The cloud refuses to create or start a member, because a quota is reached or the zone has no capacity | Sets `WithinCloudCapacity` to `False`, reason `CloudCapacityReached`, with the cloud's message. Deletes a member whose create was refused. Keeps a member whose start was refused, stopped, with its disk. Tries again every 3 minutes. | `Pending`, with `waiting for cloud quota or capacity:` and the cloud's message |
| The pool's owner may no longer create workstations in the namespace | Sets `OwnerAuthorized` to `False`, reads `Degraded`, builds no member, and withholds the owner's device keys from existing members. | `Pending`, naming the owner |
| The holder's own program crashes | Nothing. Leases persist, and each ends at its expiry unless the restarted program releases it first. | Its leases, unchanged |

A quota or capacity refusal counts as no template failure, and records no `TemplateFailed`
event. Instead, the pool stops creating and starting members for 3 minutes, whether a lease
or a warm floor needs one. It then tries one member, and waits another 3 minutes if the cloud
refuses that one too. While `WithinCloudCapacity` is `False`, the pool deletes no stopped
member when `idle.deleteAfter` passes, because it couldn't build a replacement.

`WithinCloudCapacity` goes back to `True` when any of these happens:

- A member the pool tried comes up.
- Any new member reports `Configured`.
- The next try is due, and the pool has no member to create or start.

<div class="callout callout-tip">
  <p class="callout-title">Tell a template that can never run from a full project</p>
  <div class="callout-body">
    A template your cloud project can never run looks the same as a project that&rsquo;s full for now.
One example is a template that asks for a GPU the project has no quota for. The pool keeps
trying every 3 minutes and never reads <code>Degraded</code>. Raise the quota or change the template.
  </div>
</div>


## Pool usage history

A pool keeps a week of hourly counters, so you can size its warm floors. Each hour records
three groups of numbers:

- Demand: leases created and bound, and the most held and the most waiting at once. Also how
  long a lease waited to become `Ready`, and leases that expired before they were bound.
- Endings: leases released with `Done`, recycled, whose recycle failed, expired, and whose
  member was lost.
- Supply: members built and deleted, and the seconds members spent idle running, idle
  stopped, leased, being built and being recycled.

Anyone who can read the pool can read the history. It belongs to the pool, so a deleted
pool's history is no longer served, and a new pool with the same name starts afresh. The
counters are an accounting record rather than a bill. A count can be lost when Ringleader
restarts at the wrong moment, but it's never counted twice. Ringleader doesn't change a
pool's warm floors on its own. The history is there for you, or a script of yours, to set
them by.

## Hold configuration changes until a lease ends

To hold configuration changes, set `lease.configChanges` to `Hold` on the pool:

```yaml
spec:
  lease:
    configChanges: Hold
```

## Take a member out of service

To stop the pool from leasing a member again, cordon it:

```bash
rl workstation cordon agents-linux-e5f6a7b8
```

If the member is leased, it takes no new lease once its current one ends. The pool then
deletes it, whether the holder asked for `Done` or `Recycle`. If it's idle, it stays exactly
as it is until you uncordon it. It's outside the warm floors, the idle timers, a template
change and a lower `size.max`, so nothing deletes it and you can work on it. The pool's owner
and a namespace administrator can cordon a member. To put it back in service, run
`rl workstation uncordon agents-linux-e5f6a7b8`.

## Delete a pool

To delete a pool and all of its members, run:

```bash
rl workstationpool delete agents-linux -n acme-dev
```

<div class="callout callout-warning">
  <p class="callout-title">Deleting a pool deletes its members</p>
  <div class="callout-body">
    Deleting a pool deletes each member&rsquo;s virtual machine and disk, so the command asks you to
confirm. A leased member is deleted when its lease ends, and the pool never recycles one.
  </div>
</div>


The pool fails every waiting lease with `PoolDeleted`, and deletes its idle members at once.
Ringleader rejects new leases. The pool stays in `Terminating` until the last leased member is
gone. Its `status.message` says how many leases it's waiting for, and when the last one expires.

The delete prints the command that releases those leases early:

```text
workstationpool/agents-linux: 2 members stay until their leases end. Release those leases with `rl workstationpoollease release -n acme-dev -l pools.ringleader.dev/pool=agents-linux`.
```

## Read a pool's usage history

To size a pool's warm floors, read its hourly usage history with
[`rl workstationpool usage`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-workstationpool-usage-pool):

```bash
rl workstationpool usage agents-linux --since 24h
```

A Ringleader deployment that keeps no usage history says so instead of printing a table.

## Next steps

- [`rl workstationpool`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-workstationpool) lists the
  pool and lease commands and their flags.
- [WorkstationPool and WorkstationPoolLease](https://docs.ringleader.dev/reference/resources/workstationpool/) lists every field of a pool
  and a lease.
- [Jobs and their results](https://docs.ringleader.dev/workstations/lifecycle/jobs-results/)
  reports a CI job's result from a member.

