Files
cozystack/packages
Aleksei Sviridkin 811f030f15 fix(cozy-lib): round the sanitized CPU request up to a whole milli-CPU (#3522)
## What this PR does

Fixes #2917.

`cozy-lib.resources.sanitize` divides the CPU limit by
`cpuAllocationRatio` and rendered the quotient as a bare float. When the
ratio does not divide the value evenly the result is finer than a
milli-CPU, and the VPA admission webhook rejects the object outright:

```
admission webhook "vpa.k8s.io" denied the request:
minAllowed: CPU [62500u] must be a whole number of milli CPUs
```

`packages/extra/etcd/templates/vpa.yaml` routes its VPA
`minAllowed`/`maxAllowed` through the helper, so with
`cpuAllocationRatio: 4` its floor renders as `250m / 4 = 62.5m` and the
`etcd` HelmRelease loops `UpgradeFailed` → `RollbackFailed` forever: the
rollback target renders the same invalid value. Since v1.6.0
HelmReleases wait on each other, so that one stuck app also blocks
`tenant-root` → `cozystack-basics` → `kubevirt-cdi-operator`, and
without CDI the Talos worker rollover cannot import the worker OS image.
Only the divisors of 250 worked as ratios, which is an artefact of one
chart's hardcoded `250m` floor rather than a real constraint on the
operator.

- `packages/library/cozy-lib/templates/_resources.tpl`: round the
sanitized CPU request **up** to the next whole milli-CPU. Up, never
down, so the request stays at or above the intended fraction of the
limit. And only when rounding is actually needed: an exactly divisible
value keeps its current float rendering, so nothing changes on the
default ratio.
- The whole-milli test is exact, with no tolerance around it. sprig's
`divf`/`mulf` run on `shopspring/decimal` rather than float64, so `mulf
(divf 0.25 10.0) 1000.0` renders exactly `25` in Helm and there is no
binary-float noise on this codepath to smooth over. A tolerance would in
fact reintroduce the bug: a value that is whole-milli only to within
`5e-7` would read as exact and render verbatim, so `cpu: 100.0000004m`
at ratio 1 would render `0.1000000004`, rejected for the same reason
`62.5m` is. There is a test for that case.
- `packages/tests/cozy-lib-tests/`: new `resources` template plus a
`resources_test.yaml` suite covering the indivisible ratio, the round-up
direction, the two unchanged-rendering cases, a
whole-milli-only-within-a-tolerance value, sub-milli input at an
ordinary and at an extreme ratio, a genuinely zero request, and that
memory is untouched.
- `packages/extra/etcd/tests/vpa_test.yaml`: regression guard asserting
the chart's rendered VPA `minAllowed.cpu` is a whole milli-CPU (`63m`)
with `cpu-allocation-ratio: 4`, and that the default ratio still renders
`0.025` / `0.5`.

### Why in `sanitize` and not in the etcd chart

The issue also suggests taking `minAllowed`/`maxAllowed` out of
`sanitize` in the etcd chart, on the grounds that VPA policy bounds are
not workload requests. I looked at that first and it is the wrong fix
here:

- `minAllowed`/`maxAllowed` bound the **request** the VPA writes, and in
Cozystack every request is `limit / cpuAllocationRatio` —
`packages/extra/etcd/templates/etcd-cluster.yaml` sanitizes
`.Values.resources` the same way. On the default ratio the etcd request
is `1000m / 10 = 100m`, bracketed by `minAllowed 25m` and `maxAllowed
500m`. Dropping `sanitize` would make the floor a literal `250m`, i.e.
**10x** the intended request floor, and let the ceiling reach `5000m`.
It would also change rendered output on the default ratio, for every
existing cluster.
- It fixes one chart. The same helper feeds every app's requests, and at
`cpuAllocationRatio: 4` seven other charts (`http-cache`, `mariadb`,
`nats`, `rabbitmq`, `redis`, `tcp-balancer`, `valkey` — all from the
`250m` nano presets) also render `cpu: "0.0625"`. Those go into pod
requests rather than a VPA object so they are not rejected, but they are
still not quantities we should be emitting.
- No other chart routes `minAllowed` through `sanitize`, so there is no
consistency argument either way: the other VPA templates
(`etcd-operator`, `monitoring`, `monitoring-agents`, `vpa-for-vpa`) use
hand-picked literals in system charts where no allocation ratio applies.

### How it was validated

- `hack/helm-unit-tests.sh`, full run: 81 packages green, before and
after. The only difference between the two runs is the two new suites
appearing as `PASS`; no existing suite changed status.
- Both new suites were confirmed to **fail** against `main`'s helper (5
tests in `resources_test.yaml`, 1 in the etcd `vpa_test.yaml`), so they
guard the regression rather than merely describing the new behaviour.
- Breadth of the bug: over the 905000 combinations of a whole-milli
limit `1m`..`1000m` against an integer ratio `1`..`905`, `main` renders
a quantity finer than 1m for **898026** of them (99.2%) and this branch
for **none**.
- Rendered-output check on the default ratio: `helm template` over all
164 chart directories under `packages/{apps,extra,system,core}`, before
and after, byte-diffed. 129 render standalone; all 129 are identical
apart from 8 whose diff is only their per-render random secrets and
self-signed certs (no `cpu` line differs in any of them).
- Same sweep with `--set _cluster.cpu-allocation-ratio=4`: `main` emits
11 sub-milli `cpu: "0.0625"` requests across 7 charts, this branch emits
**0**.
- The quoted webhook error was checked against the VPA we actually ship
(1.5.1): `pkg/admission-controller/resource/vpa/handler.go` wraps the
quantity error as lowercase `minAllowed:`/`maxAllowed:`, and `62.5m`
canonicalises to `62500u`.
- Not verified: nothing from this branch was applied to a live cluster,
so the end of the chain (the webhook accepting the object, the etcd
HelmRelease converging, CDI unblocking) is inferred from the rendered
manifests and the webhook's own error message, not observed.

### Screenshots

Not applicable, no UI change.

### Downstream repositories

Walked the trigger map in `docs/agents/contributing.md` against the
diff. The diff is one Helm library template plus test files: no package
added, renamed or removed; no `values.yaml`, `values.schema.json`,
version enum or default changed; no `ApplicationDefinition`, CRD,
namespace, `hack/` file, package layout or release asset touched. The
terraform provider restates app defaults, but no default moves here —
the rendered value only changes for a non-default `cpuAllocationRatio`,
which the provider does not model.

- [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:

### Release note

```release-note
fix(cozy-lib): round the sanitized CPU request up to a whole milli-CPU. A `cpuAllocationRatio` that does not divide a chart's CPU value evenly (for example `4` against the `250m` VPA floor of the etcd chart) produced a quantity finer than 1m, which the VPA admission webhook rejects, leaving the etcd HelmRelease looping UpgradeFailed/RollbackFailed and — since v1.6.0, where HelmReleases wait on each other — blocking tenant-root, cozystack-basics and CDI behind it. Any allocation ratio now renders a valid quantity; rendering is unchanged whenever the ratio divides evenly, including on the default ratio of 10. Where a ratio does not divide a configured CPU value evenly, the rendered request changes once on upgrade: for example `cpu: 125m` at the default ratio goes from `0.0125` to `13m`, and so does every nano preset at a ratio like 4. The API server already rounds pod requests up the same way, but the string stored in operator-managed resources (CNPG clusters, KubeVirt VMs and so on) changes, and some operators may roll their pods for it.
```


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

- **Bug Fixes**
- CPU resource requests are now sanitized to avoid floating-point
precision issues.
- Fractional milli-CPU requests are rounded up to the nearest whole
milli-CPU, with positive sub-milli requests clamped to `1m`.
  - Exact and zero CPU values remain unchanged.
- CPU limits and memory requests continue to be preserved without
modification.

- **Documentation**
  - Updated resource guidance to describe CPU request rounding behavior.

- **Tests**
- Added coverage for allocation ratios, default bounds, rounding,
precision, and resource preservation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-29 21:17:44 +03:00
..