Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/reference/limits-and-quotas.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
type: reference
audience: developer or operator who needs to know what a caller or a tenant may do, and how the API says no
verified: 2026-11-11
verified: 2026-11-14
---

# Limits and quotas
Expand Down Expand Up @@ -31,9 +31,9 @@ Creating an instance, a network or a profile that would pass a limit is refused

A profile bigger than what is left is refused with the limit it passes when an instance takes it: a `l`, or a third `s`.

### The plan is deprecated
### There is no plan

There used to be three plans (`small`, `medium`, `large`) that set these limits, and only the platform administrators changed them. There is no choice of plan any more: the `plan` of a tenant limits nothing and changing it changes no limit. It is still in the body of a tenant and in the answer of the quota, and a plan other than `small` is refused with `403` on creation and on change, as before; a tenant is created without one. It stays only until the console stops reading it, then it is removed from the API. An existing tenant has the same limits as a new one, whatever plan it was created with.
Every tenant has these limits, the ones of a tenant created today and of one created long ago. A tenant has no `plan`: it is not in its body, nor in the answer of the quota, and a body that gives one, to create or to change a tenant, is `400` like any field the API does not know.

## The rate

Expand Down
8 changes: 4 additions & 4 deletions docs/reference/tenants.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
type: reference
audience: developer or operator who works with tenants through the API
verified: 2026-11-11
verified: 2026-11-14
---

# Tenants

A tenant is a `Tenant` object of the `phorge.fr` group in `phorge-system` (`internal/kube/tenants.go`); Crossplane makes its namespace `t-<name>`, its Incus project, and its network.

- **Create** is for any person signed in with Authentik, for themselves: the owner is the caller (`owner` is left out or is their own email, another one is `403`), every tenant has the same limits (2 instances, 2 vCPU, 4 GiB of memory, 128 GiB of disk, 2 networks and 2 profiles, see [Limits and quotas](limits-and-quotas.md)) so a body leaves `plan` out, and a person owns at most `MAX_TENANTS_PER_USER` tenants (`409`, `quota_exceeded`, counted by owner, whoever created them). A key never creates a tenant. `GET /v1/whoami` says `canCreateTenants`. It answers `202` with a `Location`: poll it until `phase` is `Ready`. `phase` is `Provisioning`, `Ready`, `Deleting`, or `Failed` when the tenant has not become ready for 3 minutes and either `problems` says what is wrong in words made for the user (a limit that is reached, or that the platform could not complete a step yet) or the platform could not apply what makes the tenant for 3 minutes (the same rule holds for every kind: an object whose composition fails is `Failed` with a sentence that the platform administrators can see why, instead of staying `Provisioning`). It never carries what Crossplane, a provider or Incus report: those stay in `status.problems` of the object, for the administrators.
- **Patch** changes `displayName`: the admins of the tenant, the owner among them, do. The `plan` is deprecated and changes no limit, since every tenant has the same ones: a body that gives it is `403`, as it always was. A key never patches a tenant unless it holds `tenants:write`. The owner cannot change (it is an OpenFGA tuple): a body that holds it is refused. To hand a tenant over, give the new person the `admin` role with a membership.
- **Create** is for any person signed in with Authentik, for themselves: the owner is the caller (`owner` is left out or is their own email, another one is `403`), every tenant has the same limits (2 instances, 2 vCPU, 4 GiB of memory, 128 GiB of disk, 2 networks and 2 profiles, see [Limits and quotas](limits-and-quotas.md)), and a person owns at most `MAX_TENANTS_PER_USER` tenants (`409`, `quota_exceeded`, counted by owner, whoever created them). A key never creates a tenant. `GET /v1/whoami` says `canCreateTenants`. It answers `202` with a `Location`: poll it until `phase` is `Ready`. `phase` is `Provisioning`, `Ready`, `Deleting`, or `Failed` when the tenant has not become ready for 3 minutes and either `problems` says what is wrong in words made for the user (a limit that is reached, or that the platform could not complete a step yet) or the platform could not apply what makes the tenant for 3 minutes (the same rule holds for every kind: an object whose composition fails is `Failed` with a sentence that the platform administrators can see why, instead of staying `Provisioning`). It never carries what Crossplane, a provider or Incus report: those stay in `status.problems` of the object, for the administrators.
- **Patch** changes `displayName`: the admins of the tenant, the owner among them, do. A key never patches a tenant unless it holds `tenants:write`. The owner cannot change (it is an OpenFGA tuple): a body that holds it is refused. To hand a tenant over, give the new person the `admin` role with a membership.
- **Delete** is for the admins of the tenant, the owner among them. It removes what the tenant holds first (its instances, then its memberships, then its networks, then the profiles its users made, then its images), then the tenant, all in the foreground: `202`, then the tenant stays as `Deleting` until everything it is made of is gone (about two minutes, longer with a virtual machine to stop), and a `404` on a later get means the person no longer sees the tenant: it is gone or about to be, since the rights of the person (the owner tuple) go with the tenant a little before its last resources do (measured: the `404` came while the tenant was still `Deleting`). Stop polling on it, and do not take it for proof that Incus is empty. It destroys the virtual machines of the tenant. The order is not cosmetic: the platform deletes what a tenant holds in its namespace only while the namespace exists, so the tenant waits for them (see the Hangar issue 185).
- Whoever cannot read a tenant is told it does not exist (`404`), whoever can but may not do this gets `403`. Bodies are JSON only, 64 KiB at most, with no field the API does not know.
- **The `plan` is deprecated.** There is no choice of plan: every tenant has the same limits, whatever its `plan`. The field stays in the body of a tenant (`small` for a new one, what the object holds for an existing one) and in the answer of `GET /v1/tenants/{t}/quota`, only so that what reads it keeps working until the console stops using it; it is then removed from the API. A creation without a plan works, and a plan other than `small` on creation or on change is `403`.
- **There is no plan.** Every tenant has the same limits. A body that gives a `plan`, to create or to change a tenant, is `400`, like any field the API does not know.
2 changes: 1 addition & 1 deletion internal/api/access_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestMembershipsOnTheRealCluster(t *testing.T) {
{"POST", base + "/instances", `{"name":"web","image":"nope"}`, http.StatusForbidden},
{"POST", base + "/images", `{"name":"x","source":{"name":"alpine/3.22"}}`, http.StatusForbidden},
{"GET", base + "/memberships", "", http.StatusForbidden},
{"PATCH", base, `{"plan":"large"}`, http.StatusForbidden},
{"PATCH", base, `{"displayName":"x"}`, http.StatusForbidden},
{"DELETE", base, "", http.StatusForbidden},
} {
if code := call(t, e, c.method, c.path, "bob", c.body); code != c.want {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/compute_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestImageAndInstanceOnTheRealCluster(t *testing.T) {
})
}

// The plan small allows one VM: Incus refuses the second, and says so in problems.
// Incus refuses the instance that passes a limit of the tenant, and says so in problems.
if code := call(t, e, "POST", base+"/instances", "owner", `{"name":"second","image":"debian"}`); code != http.StatusAccepted {
t.Fatalf("create the second instance: %d", code)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (i *instanceRoutes) create(w http.ResponseWriter, r *http.Request) {
req.Networks, req.Network = nws, ""
// The limits of the tenant hold the count and the total of CPU, memory and disk: say which before asking Incus, who would leave an instance
// that never becomes ready. The profiles the users made are read for it, an instance counts what its profile comes to.
_, used, made, ok := holdings(w, r, &i.base, i.store, i.networks, i.profiles, tenant)
used, made, ok := holdings(w, r, &i.base, i.store, i.networks, i.profiles, tenant)
if !ok {
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/keyscopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestAKeyWithTheScopesOfTheNetworksTheProfilesAndTheTenantPassesTheirRoutes(
{"GET", "/v1/apikeys", "", http.StatusForbidden},
{"GET", "/v1/tenants/other/networks", "", http.StatusNotFound},
{"POST", "/v1/tenants/other/networks", `{"name":"x"}`, http.StatusNotFound},
// the tenant: rename it, and do not choose its plan
// the tenant: rename it; there is no plan to give
{"PATCH", "/v1/tenants/demo", `{"displayName":"renamed"}`, http.StatusOK},
{"PATCH", "/v1/tenants/demo", `{"plan":"large"}`, http.StatusForbidden},
{"PATCH", "/v1/tenants/demo", `{"plan":"large"}`, http.StatusBadRequest},
{"POST", "/v1/tenants", `{"name":"fresh","owner":"x@example.org"}`, http.StatusForbidden},
{"DELETE", "/v1/tenants/demo", "", http.StatusAccepted},
} {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (n *networkRoutes) create(w http.ResponseWriter, r *http.Request) {
}
tenant := r.PathValue("t")
// The tenant is limited in how many: say it before asking, with a code, instead of leaving a network that never becomes ready.
_, used, _, ok := holdings(w, r, &n.base, n.instances, n.store, n.profiles, tenant)
used, _, ok := holdings(w, r, &n.base, n.instances, n.store, n.profiles, tenant)
if !ok {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestANetworkShowsAPhaseAndNoCrossplaneCondition(t *testing.T) {
}
}
if !strings.Contains(body.Problems, "number of networks") {
t.Errorf("the limit of the plan is told in words: %q", body.Problems)
t.Errorf("the limit of the tenant is told in words: %q", body.Problems)
}
}

Expand Down
24 changes: 10 additions & 14 deletions internal/api/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/phorge-fr/phorge-api/internal/quota"
"github.com/phorge-fr/phorge-api/internal/ratelimit"
"github.com/phorge-fr/phorge-api/internal/server"
"github.com/phorge-fr/phorge-api/internal/tenants"
)

// quotaRoutes tell a tenant what it is allowed to hold, the same for every tenant, and what it holds, so that a client draws what is
Expand All @@ -28,45 +27,42 @@ func (q *quotaRoutes) register(s *server.Server, authn func(http.Handler) http.H
}

type quotaBody struct {
// Plan is deprecated: it changes no limit, and goes when the console stops reading it.
Plan tenants.Plan `json:"plan"`
Limits quota.Limits `json:"limits"`
Used quota.Used `json:"used"`
}

func (q *quotaRoutes) get(w http.ResponseWriter, r *http.Request) {
tenant := r.PathValue("t")
plan, used, _, ok := holdings(w, r, &q.base, q.instances, q.networks, q.profiles, tenant)
used, _, ok := holdings(w, r, &q.base, q.instances, q.networks, q.profiles, tenant)
if !ok {
return
}
writeJSON(w, http.StatusOK, quotaBody{Plan: plan, Limits: quota.PerTenant, Used: used})
writeJSON(w, http.StatusOK, quotaBody{Limits: quota.PerTenant, Used: used})
}

// holdings reads the plan of a tenant (deprecated, it limits nothing), what it holds, and the profiles its users made (an instance counts what its profile comes to),
// holdings reads what a tenant holds, and the profiles its users made (an instance counts what its profile comes to),
// or answers.
func holdings(w http.ResponseWriter, r *http.Request, b *base, instances compute.InstanceStore, networks compute.NetworkStore, profiles compute.ProfileStore, tenant string) (tenants.Plan, quota.Used, []compute.Profile, bool) {
tn, err := b.tenants.Get(r.Context(), tenant)
if err != nil {
func holdings(w http.ResponseWriter, r *http.Request, b *base, instances compute.InstanceStore, networks compute.NetworkStore, profiles compute.ProfileStore, tenant string) (quota.Used, []compute.Profile, bool) {
if _, err := b.tenants.Get(r.Context(), tenant); err != nil {
b.storeError(w, err, "tenant")
return "", quota.Used{}, nil, false
return quota.Used{}, nil, false
}
in, err := instances.List(r.Context(), tenant)
if err != nil {
b.storeError(w, err, "instance")
return "", quota.Used{}, nil, false
return quota.Used{}, nil, false
}
nw, err := networks.List(r.Context(), tenant)
if err != nil {
b.storeError(w, err, "network")
return "", quota.Used{}, nil, false
return quota.Used{}, nil, false
}
made, err := profiles.List(r.Context(), tenant)
if err != nil {
b.storeError(w, err, "profile")
return "", quota.Used{}, nil, false
return quota.Used{}, nil, false
}
return tn.Plan, quota.UsedBy(in, nw, made), made, true
return quota.UsedBy(in, nw, made), made, true
}

// quotaExceeded answers a request that would pass a limit: 409, with a code a program can act on and a sentence that says which.
Expand Down
28 changes: 16 additions & 12 deletions internal/api/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// tiny is a tenant like any other (2 instances, 2 vCPU, 4 GiB, 128 GiB, 2 networks, 2 profiles) that alice may use as she uses demo.
func withTiny(e *env) {
e.store.tenants["tiny"] = tenants.Tenant{Name: "tiny", Owner: "alice@example.org", Plan: tenants.Small, CreatedAt: now, Ready: true, NotReadySince: now}
e.store.tenants["tiny"] = tenants.Tenant{Name: "tiny", Owner: "alice@example.org", CreatedAt: now, Ready: true, NotReadySince: now}
for _, rel := range []string{"can_read_tenant", "can_read_instances", "can_write_instances", "can_read_images", "can_write_images", "can_read_networks", "can_write_networks", "can_read_profiles", "can_write_profiles"} {
e.fga.grants["user:operator@example.org "+rel+" tenant:tiny"] = true
}
Expand All @@ -26,24 +26,28 @@ func TestQuotaSaysWhatEveryTenantMayHoldAndWhatItHolds(t *testing.T) {
e.instances.put("demo", compute.Instance{Name: "b", Image: "debian", Profile: "m", Running: false}) // stopped: it counts
var got quotaBody
mustDecode(t, e.do(t, "GET", "/v1/tenants/demo/quota", "viewer", ""), &got)
// the plan of demo is large and the limits are the same as for every tenant: the plan is shown, deprecated, and limits nothing
if got.Plan != tenants.Large || got.Limits != (quota.Limits{Instances: 2, VCPU: 2, MemoryMiB: 4096, DiskGiB: 128, Networks: 2, Profiles: 2}) {
t.Errorf("plan and limits: %+v", got)
if got.Limits != (quota.Limits{Instances: 2, VCPU: 2, MemoryMiB: 4096, DiskGiB: 128, Networks: 2, Profiles: 2}) {
t.Errorf("limits: %+v", got.Limits)
}
if want := (quota.Used{Instances: 2, VCPU: 4, MemoryMiB: 2048 + 4096, DiskGiB: 148, Networks: 1, Profiles: 0}); got.Used != want {
t.Errorf("used = %+v, want %+v (the network of the test environment, the stopped VM, the profile)", got.Used, want)
}
}

// The plan of a tenant is kept in the answer and limits nothing: a small one and a large one have the same limits.
func TestThePlanOfATenantChangesNoLimit(t *testing.T) {
// Every tenant has the same limits, and the answer names no plan: there is none.
func TestEveryTenantHasTheSameLimitsAndNoPlan(t *testing.T) {
e := newEnv(t)
withTiny(e) // small, demo is large
var small, large quotaBody
mustDecode(t, e.do(t, "GET", "/v1/tenants/tiny/quota", "operator", ""), &small)
mustDecode(t, e.do(t, "GET", "/v1/tenants/demo/quota", "viewer", ""), &large)
if small.Plan != tenants.Small || large.Plan != tenants.Large || small.Limits != large.Limits || small.Limits != quota.PerTenant {
t.Errorf("small %+v, large %+v: the limits are the same, whatever the plan", small, large)
withTiny(e)
var tiny, demo quotaBody
mustDecode(t, e.do(t, "GET", "/v1/tenants/tiny/quota", "operator", ""), &tiny)
mustDecode(t, e.do(t, "GET", "/v1/tenants/demo/quota", "viewer", ""), &demo)
if tiny.Limits != demo.Limits || tiny.Limits != quota.PerTenant {
t.Errorf("tiny %+v, demo %+v: the limits are the same for every tenant", tiny.Limits, demo.Limits)
}
var raw map[string]any
mustDecode(t, e.do(t, "GET", "/v1/tenants/demo/quota", "viewer", ""), &raw)
if _, ok := raw["plan"]; ok {
t.Errorf("the answer carries a plan: %v", raw)
}
}

Expand Down
Loading
Loading