Aleksei Sviridkin 5e9c392352 feat(api): add the reservation evaluator for tenant quotas (#4983)
## What this PR does

First phase of the accepted [tenant quotas as reservation
limits](https://github.com/cozystack/community/pull/48) design: a
standalone, importable `pkg/reservation` package that computes what an
application consumes from a tenant's budget from its declared values.
Nothing calls it yet; the usage oracle, the generalized admission gate
and the per-kind declarations come in the following phases.

Today a tenant quota is declared in instance-type units but enforced
against `ResourceQuota.status.used`, which counts virt-launcher pods
with their additive virtualization overhead and drifts without
recomputation. The design makes the declared reservation the accounting
authority instead, and this package is the evaluator both the apiserver
and the controller will read.

- **Declarations are data, not Go.** A kind describes its consumption as
CEL over its values (`exempt`, ordered `let` bindings, one expression
per quota key), so the evaluator holds no per-kind knowledge and a kind
from an out-of-tree catalog participates without a change here. The
`Declaration` type is ready to be embedded in
`ApplicationDefinitionSpec` in phase 2. Environment functions are
`instanceType(name)`, `preset(name)` and `quantity(x)`, on top of the
Kubernetes CEL quantity library; scaling is `quantity.mul(n)`, because
the standard `_*_` cannot take a quantity overload.
- **Instance types resolve to scalars keyed by class**, read from the
type's spec and never from its name: `cpu`, `dedicated-cpu` (guest cores
plus the isolated emulator thread, the extra parity core when the type
itself carries the `EmulatorThreadCompleteToEvenParity` annotation, and
a supplemental I/O pool), `memory`, `hugepages-<size>`,
`overcommitted-memory`. The test pins the classification against every
`VirtualMachineClusterInstancetype` shipped in
`packages/system/kubevirt-instancetypes`.
- **Fail closed.** An unknown instance type or preset, a value of the
wrong type, an unguarded absent key, an unparseable quantity or a
negative figure is an error, and values are checked against the kind's
schema before defaulting, which would otherwise replace a mistyped value
with its default, never a smaller reservation. A definition that
declares neither its consumption nor an exemption yields
`ErrNoDeclaration`.
- **Stored values are defaulted first.** The aggregated apiserver
persists a tenant's spec without its schema defaults, so an evaluator
reading them raw would count zero for `kubernetes-nodes.maxReplicas` on
every pool created with defaults. The structural-schema defaulting moves
from `pkg/registry/apps/application` into `pkg/apis/apps/defaulting` so
the registry and the evaluator apply the same defaults; the registry now
only delegates to it, with no behaviour change.
- **Aggregation** sums reservations per namespace and returns the
applications that could not be evaluated separately, so a caller never
mistakes an incomplete sum for a complete one.

The evaluator tests run against the shipped OpenAPI schemas of
`postgres`, `vm-instance` and `kubernetes-nodes`: the cozy-lib per-key
preset fill, a `{cpu: 4, sockets: 2}` block reserving 8 vCPUs, the
autoscaling ceiling, `external: true` reserving a load balancer, and a
ComputePlane-shaped `KubernetesNodes` release carrying only `{roles,
minReplicas}` reserving exactly what the same pool with its defaults
written out reserves.

Left for the next phases on purpose: where presets are resolved from
(the design's direction is platform-shipped environment objects; here
they come through the `Environment` interface), the
`ApplicationDefinition` field itself, and any consumer.

### Screenshots

Not a UI change.

### Downstream repositories

The diff adds two Go packages and moves the defaulting helpers without
changing behaviour. It touches no package under `packages/`, no
`values.yaml`, no CRD and no `ApplicationDefinition` field, so none of
the triggers in the map is reached.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
feat(api): add the reservation evaluator that computes an application's tenant-quota consumption from its declared values. Internal only, with no consumer yet and no change in behaviour.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **New Features**
- Added reservation evaluation for application resources, including CPU,
memory, hugepages, and overcommitted memory.
- Added support for instance types, resource presets, schema-based
defaults, and CEL expressions.
- Added namespace-level reservation aggregation with clear reporting for
applications that cannot be evaluated.
- Added validation for invalid, negative, unsupported, or excessive
resource values.
- Added classification of KubeVirt instance types, including dedicated
CPUs, emulator threads, supplemental I/O threads, and hugepages.

- **Bug Fixes**
- Schema defaults are applied consistently to nested application values.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-10 03:26:01 +03:00
2025-10-08 09:43:34 +05:00
2024-02-08 12:04:32 +01:00

Cozystack Cozystack

GitHub Release Apache-2.0 License Commit activity OpenSSF Best Practices OpenSSF Scorecard CLOMonitor Support

Cozystack

Cozystack is a free and open-source platform and framework for building clouds.

Cozystack is a CNCF Sandbox project that was originally built and sponsored by Ænix.

With Cozystack, you can transform a bunch of servers into an intelligent system with a simple REST API for spawning Kubernetes clusters, Database-as-a-Service, virtual machines, load balancers, HTTP caching services, and other services with ease.

Use Cozystack to build your own cloud or provide a cost-effective development environment.

Cozystack user interface

Use-Cases

Documentation

The documentation is located on the cozystack.io website.

Read the Getting Started section for a quick start.

If you encounter any difficulties, start with the troubleshooting guide and work your way through the process that we've outlined.

Versioning

Versioning adheres to the Semantic Versioning principles.
A full list of the available releases is available in the GitHub repository's Release section.

Contributions

Contributions are highly appreciated and very welcomed!

In case of bugs, please check if the issue has already been opened by checking the GitHub Issues section. If it isn't, you can open a new one. A detailed report will help us replicate it, assess it, and work on a fix.

You are welcome to take on the fix yourself. Commits are used to generate the changelog, and their author will be referenced in it.

If you have a concrete feature request, open an issue. For a cross-cutting or architectural change, open a design proposal in the community repo. For questions or early ideas, use GitHub Discussions. See CONTRIBUTING.md for the full routing.

Community

You are welcome to join us in #cozystack, on either CNCF Slack (get an invite) or Kubernetes Slack (get an invite), in our Telegram group, and at our community meetings. Add them to your Google Calendar or iCal for convenience.

Project documents

Document What it covers
Governance How decisions are made and who makes them
Contributor Ladder How to grow from contributor to maintainer
Maintainers Who maintains the project
Code of Conduct What we expect of each other
Security Policy Reporting a vulnerability, and which releases get fixes
Adopters Who runs Cozystack in production
Roadmap Where the project is going

License

Cozystack is licensed under Apache 2.0.
The code is provided as-is with no warranties.

Commercial Support

A list of companies providing commercial support for this project can be found on official site.

Languages
Go 49.8%
Shell 35.8%
TypeScript 6.6%
Go Template 5.1%
Makefile 1.4%
Other 1.1%