Fundament plugin voor Capsule multitancy
  • Go 66.2%
  • JavaScript 28.4%
  • Just 3.4%
  • HTML 1.5%
  • Dockerfile 0.5%
Find a file
henk.denhartog@uwv.nl ef32277a23 Show the project's Quota page in the console menu (v0.4.0)
The console only lists namespaced CRDs in a project's menu (and
cluster-scoped ones at organization level), so the project entry declared
on the cluster-scoped ResourcePool was silently dropped. Hang it on
ResourcePoolClaim instead; its list and detail pages are the quota page in
project mode, which now needs no selected resource. README: explain the
menu scope rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:23:36 +01:00
console Show the project's Quota page in the console menu (v0.4.0) 2026-11-25 13:23:36 +01:00
.dockerignore Default scafolded project 2026-11-24 10:02:21 +01:00
.gitignore Show project names instead of IDs in the console 2026-11-24 16:47:09 +01:00
console.go Default scafolded project 2026-11-24 10:02:21 +01:00
definition.yaml Show the project's Quota page in the console menu (v0.4.0) 2026-11-25 13:23:36 +01:00
definition_test.go Switch to Capsule ResourcePool for a hard per-project guarantee (v0.2.0) 2026-11-25 11:07:50 +01:00
Dockerfile Default scafolded project 2026-11-24 10:02:21 +01:00
go.mod Manage a Capsule GlobalResourceQuota per Fundament project 2026-11-24 15:00:23 +01:00
go.sum Default scafolded project 2026-11-24 10:02:21 +01:00
Justfile Add a just recipe to publish to the local sandbox 2026-11-25 11:54:54 +01:00
LICENSE Default scafolded project 2026-11-24 10:02:21 +01:00
main.go Default scafolded project 2026-11-24 10:02:21 +01:00
mise.toml Default scafolded project 2026-11-24 10:02:21 +01:00
plugin.go Switch to Capsule ResourcePool for a hard per-project guarantee (v0.2.0) 2026-11-25 11:07:50 +01:00
plugin_test.go Default scafolded project 2026-11-24 10:02:21 +01:00
quota.go Add a claim manager for project admins (v0.3.0) 2026-11-25 11:15:58 +01:00
quota_test.go Add a claim manager for project admins (v0.3.0) 2026-11-25 11:15:58 +01:00
README.md Show the project's Quota page in the console menu (v0.4.0) 2026-11-25 13:23:36 +01:00

Capsule

A Fundament plugin that gives every project a guaranteed CPU and memory budget, which the project's admins divide over its namespaces.

Kubernetes' own ResourceQuota only limits a single namespace. This plugin installs Capsule and uses its ResourcePool: a project total that namespaces draw from through claims. The sum of the namespaces' shares can never exceed the total, not even when pods start at the same moment in several namespaces. It uses nothing else from Capsule: no Tenants, owners or replication.

How it works

Fundament cluster-worker         this plugin                  Capsule
────────────────────────         ───────────                  ───────
creates namespaces with   ──▶    every 30s: one         ──▶   gives each namespace a
fundament.io/project-id          ResourcePool per             ResourceQuota equal to its
                                 project (default +           allocated claims; never
                                 override), a starter         allocates more than the pool
                                 claim per new namespace
                                                              project admins add, change
                                                              and remove claims
  • Finding projects. The plugin lists the namespaces Fundament manages (fundament.io/managed-by=cluster-worker) for its own organization (fundament.io/organization-id), and groups them by fundament.io/project-id. It never calls the Fundament API.
  • One pool per project. Each project gets a ResourcePool named fundament-project-<project-id> that selects the project's namespaces by label. Its size is the default quota, or the project's override.
  • Namespaces get capacity only through claims. The pool gives every namespace 0 by default (defaultsZero). Each namespace's capsule-pool-<pool> ResourceQuota equals the claims Capsule allocated to it, and Kubernetes enforces it atomically. Capsule allocates claims one at a time and never more than the pool holds.
  • A starter claim for every new namespace. The plugin creates one claim named starter (by default 1 CPU and 2Gi of requests) the first time it sees a namespace, so workloads run right away. It does this once: a starter claim that a project admin changes or deletes stays that way. If the pool is fully claimed, the starter claim waits like any other.
  • Project admins divide the pool. The plugin gives the built-in admin role the right to create, change and delete ResourcePoolClaims, and view the right to read them, through two aggregated ClusterRoles. A claim can only draw from a pool that selects its namespace, so admins only ever divide their own project's total.
  • The plugin owns the pool. Manual edits to the pools (kubectl edit) are reverted on the next pass. Change a project's total through the default or an override instead.
  • A readable copy for project members. ResourcePools are cluster-scoped, and project admins and viewers only have rights in their own namespaces, so they cannot read the pool itself. The plugin therefore keeps a read-only copy, the ConfigMap capsule-quota-status, in every namespace of the project: the pool, plus per namespace its claims and what it actually uses. The project's console page reads that. Editing a copy changes nothing: it is display only, rewritten when anything in it changes.
  • Cleanup. When a project's last namespace leaves the cluster, its pool and its override are deleted, so a returning project starts from the default. Pools and claims the plugin did not create are never touched.
  • Uninstall. The plugin deletes its pools first (Capsule allows that while claims are in use; the claims become unbound and running pods keep running), then its starter claims, the status copies, the claim ClusterRoles and its ConfigMaps, then uninstalls the Capsule chart. Claims that project admins made stay, unbound. Capsule's CRDs stay, as with any Helm release.

What users see when a namespace's share is used up: the pod is rejected with exceeded quota: capsule-pool-fundament-project-<project-id>. For a Deployment that shows up as FailedCreate events on its ReplicaSet, not as an error on the Deployment. The fix is a bigger claim for that namespace, if the pool has room.

Dividing a project's pool

Project admins manage claims on the project's Quota page in the console; see The claim manager. The same works with kubectl in the project's namespaces:

apiVersion: capsule.clastix.io/v1beta2
kind: ResourcePoolClaim
metadata:
  name: more
  namespace: tnt-acme-project--web
spec:
  pool: fundament-project-<project-id>
  claim:
    requests.cpu: "4"
    requests.memory: 8Gi
Situation What Capsule does
The pool has room Allocates the claim; the namespace's quota grows by it
The pool is fully claimed The claim waits (Exhausted); the project gets a warning
The claim names another project's pool The claim is not bound and gets nothing; the project gets a warning
Deleting or changing a claim in use Refused. Scale the workloads down first
Changing a claim not in use Its allocation is released and it queues again; a waiting claim may take the capacity first

A namespace without any allocated claim gets 0: every pod there is refused.

Configuration

Set at install time:

Key Default Meaning
QUOTA_REQUESTS_CPU 8 Project total: CPU all pods may request
QUOTA_REQUESTS_MEMORY 16Gi Project total: memory all pods may request
QUOTA_LIMITS_CPU empty Project total: CPU limits (advanced)
QUOTA_LIMITS_MEMORY empty Project total: memory limits (advanced)
QUOTA_PODS empty Project total: number of pods (advanced)
STARTER_REQUESTS_CPU 1 Starter claim: CPU requests per new namespace
STARTER_REQUESTS_MEMORY 2Gi Starter claim: memory requests per new namespace
STARTER_LIMITS_CPU empty Starter claim: CPU limits (advanced)
STARTER_LIMITS_MEMORY empty Starter claim: memory limits (advanced)
STARTER_PODS empty Starter claim: pods (advanced)

Values are Kubernetes quantities (8, 500m, 16Gi). An empty QUOTA_* key puts no limit on that resource. Every resource a pool limits needs a STARTER_* amount too, or new namespaces get 0 of it until someone claims some; the plugin warns when one is missing.

Quotas need container defaults

Kubernetes rejects a pod that sets no value for a resource under quota. Pods without explicit resources therefore depend on the fundament-defaults LimitRange that Fundament puts in each namespace. Its fields are optional, configured per cluster and per project:

The LimitRange sets Covers requests.* Covers limits.*
request and limit defaults yes yes
limit defaults only yes (request = limit) yes
request defaults only yes no
nothing (no LimitRange) no no

That is why the default quota covers only requests. The plugin checks every project namespace and puts a warning on the project, in the console and in the plugin status when a resource under quota has no default there.

Overrides

Organization admins can give a project a different total on its quota page at organization level (Project quotas → a project → Override). An override replaces the default per resource; resources it leaves out keep the default. Reset to default removes it.

Lowering a project total below what is claimed

Capsule refuses to make a pool smaller than what its claims hold: its webhook rejects the change (can not reduce requests.cpu usage to 3 because quantity 4 is claimed). Such a limit is therefore pending:

  1. The plugin keeps the current limit for that resource (or no limit, if there was none yet) and applies the rest of the change.
  2. The project gets a warning, for example requests.cpu: 2 requested but the namespaces' claims hold 3; Capsule only lowers a pool once its claims fit, so it stays at 8 until claims are reduced.
  3. On every pass (30 seconds) the plugin checks again, and applies the new limit as soon as the claims fit. The warning then disappears.

The claims are what has to shrink, not the usage: the project admins reduce or remove claims (claims in use only after scaling their workloads down). Running pods are never evicted.

This applies to anything that lowers a limit: an override, Reset to default when the default is lower, and lowering a default in the plugin's config.

Where a pending limit shows:

Place What you see
The project's quota page Under Warnings; Project total still shows the current limit
The editor, right after saving Pending: with the requested limit and what is claimed
The plugin status … N with warnings (a pending limit is not Degraded)
The ResourcePool Annotation capsule.fundament.io/warnings

Quota from says override as soon as an override exists, even while part of it is pending: compare Project total with the override to see what has been applied.

Overrides are stored in the ConfigMap capsule-quota-overrides in the plugin's namespace, one key per project ID:

data:
  <project-id>: '{"requests.cpu":"32","requests.memory":"64Gi"}'

An override that doesn't parse is ignored with a warning, and the default applies.

Why only organization admins: a console write passes only if both the user and the plugin may make it. Organization admins hold cluster-admin; project admins and viewers only hold admin/view in their own project's namespaces, never in the plugin namespace. Keep the ConfigMap there: stored in a project namespace, overrides would let that project's admins raise their own total.

Console

Level Menu entry Shows Reads
Project Quota The project total, claimed, free to claim and in use; per namespace its use and share; the claim manager; warnings capsule-quota-status in the project's namespaces; claims live per namespace
Organization Project quotas Every project's claimed and total; the override editor on each project the ResourcePools and status copies (org admins)

The project's Quota entry is declared on resourcepoolclaims, not on resourcepools: the console only lists namespaced CRDs in a project's menu (and cluster-scoped ones at organization level), and pools are cluster-scoped. Declared on the pool, the entry is silently left out of the menu; the browser console then logs [PluginNav] … is Cluster-scoped; not shown under menu.project. Its page is the same as a pool's detail page, in project mode.

The claim manager

On a project's Quota page, below the usage tables, the Claims section has one block per namespace of the project:

  • The namespace's claims, read live from Kubernetes, so a change shows straight away: each claim's name (the plugin's is marked starter), its amounts, its state, and whether pods use it.
  • Add claim: a name (lowercase letters, digits and dashes) and an amount for one or more of the resources the project total limits. The page checks both before sending anything.
  • Remove, on claims that are not in use. It takes a second click within four seconds to confirm, because the console's sandbox doesn't allow confirmation dialogs.
State Meaning
allocated Capsule gave the namespace this share; pods can use it
waiting: the project total is fully claimed Nothing left to claim; it is allocated once other claims shrink or the total grows
not bound to this project The claim names a pool that doesn't serve this namespace; it holds nothing
being processed Just created; Capsule allocates it within seconds
…, in use Pods use what it holds: it can't be removed until they are scaled down

There's no resize, on purpose. Changing an unused claim makes Capsule release its capacity first, and a claim waiting elsewhere in the project can take it before this one is allocated again. To give a namespace more, add a claim; to give it less, remove one.

Free to claim at the top of the section comes from the status copy, which follows within 30 seconds; the claim lists are live. So right after adding a claim, the usage figures may still show the old numbers.

What the messages mean:

Message Cause
Claim … added. Created; Capsule allocates it
… asks for more than is free …: it waits … Created, but larger than what is left; it shows as waiting
A claim named … already exists here. Names are unique per namespace
Claim … is in use: scale the workloads … down first. Capsule refuses removing a claim pods use
Capsule refused: … Another Capsule rule; its own reason follows
Only project admins can change claims. The user isn't an admin of this project

Who sees what. Everyone who can open the project's page sees the claim manager, viewers included: the page can't ask what the current user may do, because its requests reach Kubernetes as the plugin, and only kube-api-proxy checks the user. A viewer's change is refused (403) and shows the last message above. Project admins may change claims through the admin role (the plugin's aggregated ClusterRole). Organization admins hold cluster-admin, so they can use the claim manager on any project's page too; the organization-level pages have no claim manager.

Things to know before running it

  • Capsule runs as cluster-admin. The chart binds the Capsule controller to cluster-admin, and the plugin's ClusterRole needs bind and escalate on ClusterRoles to install it, and to create the claim ClusterRoles.
  • Capsule's webhooks fail closed. Several match every resource with failurePolicy: Fail. While the controller is down, the API server rejects the writes they cover, cluster-wide. The plugin status turns Degraded when the controller has no available replicas (checked every FUNP_RECONCILE_INTERVAL, default 5 minutes).
  • No cert-manager needed. The plugin installs the chart with certManager.generateCertificates=false and tls.enableController=true, so Capsule issues its own webhook certificate. Both are needed: with only the first, the controller crash-loops on a missing certificate. On a fresh install the controller restarts once while the certificate is created.
  • Claim rights reach every namespace admin. The aggregated ClusterRole gives anyone with admin in a namespace the right to create claims there, in Fundament namespaces and others alike. That's harmless: a claim only gets something from a pool that selects its namespace.
  • Capsule won't shrink a pool below its claims. Lowered totals are held as pending; see Lowering a project total below what is claimed.
  • One installation per cluster. The Helm release capsule in capsule-system is cluster-wide; a second organization installing the plugin on the same cluster would share it.

Why ResourcePool, not GlobalResourceQuota

Version 0.1.0 used Capsule's GlobalResourceQuota, where namespaces share one limit freely. Tested in the sandbox, it can be exceeded: three pods of 3 CPU created at the same moment in three namespaces were all admitted against a limit of 8. Capsule gives each namespace "its own use + whatever the project still has free", so simultaneous pods each see the same free amount. With ResourcePool each namespace has a fixed share that Kubernetes enforces atomically, and the same test stays within the total. Upgrading from 0.1.0 replaces each project's GlobalResourceQuota with a pool; the old object is deleted once the pool exists.

Layout

Path What it is
definition.yaml The plugin manifest: metadata, RBAC, config, console menu and pages
plugin.go Lifecycle: install the chart, run the quota loop, health, uninstall
quota.go The reconciler: pools, starter claims, claim roles, status copies
quota_test.go Reconciler tests against controller-runtime's fake client
main.go Hands the plugin to the SDK runtime
console.go Embeds the console UI assets
console/ Console pages: quota list, detail, override editor, claim manager
Justfile Build, test, lint and image recipes
mise.toml Pinned tool versions (Go, just, skaffold, kubectl)
LICENSE Apache-2.0

Changing the plugin

  • Bumping the Capsule chart: render it with the plugin's values and compare the kinds of object against spec.permissions.rbac. Helm can only create what that ClusterRole allows.

    helm template capsule oci://ghcr.io/projectcapsule/charts/capsule --version <new> \
      -n capsule-system --set certManager.generateCertificates=false \
      --set tls.enableController=true | grep '^kind:' | sort | uniq -c
    

    Also check:

    • Stored pool shape. Capsule fills in config flags and zero defaults on a stored pool; desiredPoolSpec in quota.go writes the same shape, so an unchanged pool compares equal. If Capsule adds defaulted fields, the plugin rewrites every pool on every pass. Watch for "updated": counts above 0 in the plugin log without anything having changed.
    • Rejection text. Capsule must still reject shrinking a pool below its claims with a message containing can not reduce. The plugin avoids sending such a change (holdBelowClaimed), but when claims move between its read and its write it recognises the rejection by that text (isReductionBelowUsage). If the text changes, such a pass is reported as a failed sync until the next pass holds the limit.
    • Claim conditions. claimState and claimInUse in quota.go read the claims' Ready, Exhausted and Bound conditions to report waiting, unbound and in-use claims. The claim manager does the same in the browser (claimInfo in console/_quota.js); change both together.
    • Refusal texts. A Capsule refusal and a missing permission both reach the console as HTTP 403. The claim manager tells them apart by the message: Capsule's carries admission webhook … denied the request, and the in-use refusal mentions used. If Capsule rewords these, admins get "Capsule refused: …" instead of the friendlier text, or are wrongly told only project admins can change claims.
  • Adding a quota resource: add it to quotaConfigKeys and starterConfigKeys in plugin.go, to configSchema in definition.yaml and to EDITABLE_RESOURCES in console/_quota.js. definition_test.go fails when the code and configSchema disagree, including their defaults.

  • Defaults live in two places. Fundament does not apply configSchema defaults: only keys set in the installation's spec.config reach the plugin, and an install through functl or Terraform sets none. So the config key maps carry the same defaults as definition.yaml, used when a key is unset. A key set to an empty value means no limit (or no starter amount) for that resource.

  • Console contract: spec.menu, spec.customComponents, spec.allowedResources and the files under console/ belong together. definition_test.go checks that referenced files exist; nothing checks the rest, so change them together. A menu.project entry needs a namespaced CRD and a menu.organization entry a cluster-scoped one, or the console drops it without an error (see Console).

  • What the console reads is written by quota.go: the pool annotations (capsule.fundament.io/warnings, quota-source, default-quota, overrides-configmap, project-name) and the status copy's shape (pool plus namespaces.<name>.claims and .quota). Change them in both places.

Develop

Tool versions are pinned in mise.toml. With mise installed, mise install gets Go, just, skaffold and kubectl at the versions this project expects. Recipes live in the Justfile; run just --list to see them all.

go mod tidy      # needs network access, or a warm module cache
just build
just test

Image builds need the docker buildx plugin (with Homebrew and Colima: brew install docker-buildx):

just docker                         # linux/amd64 image, also on an Apple Silicon Mac
PLATFORM=linux/arm64 just docker    # for an arm64 cluster

Trying it on a local cluster

The binary runs outside Fundament too. Point it at a throwaway cluster through a kubeconfig that contains only that cluster: it installs Capsule cluster-wide, and Helm follows the same kubeconfig.

kind create cluster --name capsule-test
kind get kubeconfig --name capsule-test > /tmp/capsule-test.kubeconfig
kubectl --kubeconfig /tmp/capsule-test.kubeconfig create ns plugin-test

just build
KUBECONFIG=/tmp/capsule-test.kubeconfig \
FUNDAMENT_CLUSTER_ID=c-1 FUNDAMENT_INSTALL_ID=i-1 FUNDAMENT_ORGANIZATION_ID=o-1 \
POD_NAMESPACE=plugin-test ./capsule-plugin

Then create namespaces carrying Fundament's labels (fundament.io/managed-by=cluster-worker, fundament.io/organization-id=o-1, fundament.io/project-id=<id>) and watch kubectl get resourcepools and kubectl get resourcepoolclaims -A. Note that kind create cluster switches your current kubectl context.

Hot reload

functl plugin dev runs the plugin in the Fundament plugin sandbox and rebuilds it when you save Go or console/ source, go.mod or go.sum. Changes to definition.yaml need a restart of functl plugin dev. Prerequisites: the sandbox cluster, docker, skaffold, kubectl, and a functl auth login session. Set up the sandbox as described in https://github.com/fundament-oss/fundament/blob/master/docs/developer/plugins/testing-plugins-locally.md.

functl plugin dev                  # deletes the dev installation on Ctrl-C
functl plugin dev --no-cleanup     # keeps it after exit
functl plugin dev-down             # removes a leftover dev installation

Config values come from --set KEY=VALUE (repeatable) or a --config YAML file; --set wins. functl checks them against configSchema before building.

functl plugin dev --set QUOTA_REQUESTS_CPU=4 --set STARTER_REQUESTS_CPU=500m

The sandbox doesn't show real permissions. Console pages run with the sandbox admin's access rather than the plugin's RBAC, and the binding fundament-sandbox-user-access makes every Fundament user cluster-admin. To check what a project member may do, ask Kubernetes as that user's ServiceAccount, without the sandbox-only group, after creating the bindings cluster-worker would (admin or view in the project's namespaces):

kubectl auth can-i create resourcepoolclaims.capsule.clastix.io -n tnt-acme-project--web \
  --as=system:serviceaccount:fundament-system:fundament-<user-id> \
  --as-group=system:serviceaccounts --as-group=system:authenticated

Console changes need a cache bypass. plugin-proxy serves plugin files with Cache-Control: private, max-age=31536000, immutable: the URL carries the plugin version, so a published version never changes. Under functl plugin dev the version stays <version>-dev, so after hot reload the browser keeps running the old console/ files, even on a normal refresh. Go changes are not affected. Open the developer tools (F12), tick Network → Disable cache and reload, or hard-refresh with Ctrl+Shift+R.

After a restart of the local platform, cluster-worker's simulated Gardener (GARDENER_MODE=mock) is empty, so it marks the sandbox cluster as lost and holds every namespace, user and plugin change for it ("shoot API server not available yet"). Change one of the cluster's node pools to bring it back.

Publish

For the local sandbox, one recipe does everything: set metadata.version in definition.yaml, commit, then

just publish-sandbox 0.4.0

It refuses to run with uncommitted changes or a version that doesn't match definition.yaml, so the image always matches the code. It builds and pushes the image to localhost:5112, publishes the definition with the pushed digest, and replaces the acme-corp--capsule installation with the new version. Override the defaults with SANDBOX_ORG, SANDBOX_REGISTRY and FUNDAMENT_DIR (the Fundament checkout whose mise environment points functl at the local installation; default ../fundament). REPLACE=1 republishes an existing draft version, which can then never be submitted for review.

Replacing the installation runs the old version's uninstall: overrides and claims made by project admins are gone afterwards.

The steps by hand, for the local sandbox:

just docker v0.2.0
docker tag capsule-plugin:v0.2.0 localhost:5112/capsule:v0.2.0
docker push localhost:5112/capsule:v0.2.0
functl plugin publish definition.yaml --image=localhost:5112/capsule@sha256:<digest from push>

This adds a draft version to the organization's marketplace listing (--create reserves the listing the first time). Versions are create-only: bump metadata.version for changed content, or republish a draft with --replace. An installation's version can't be changed in place either; remove the installation and create it again with the new version and the hash publish printed.

Docs