Files
cozystack/packages/library
Aleksei Sviridkin bf24d28fde fix(cozy-lib)!: refuse memory below 1Mi instead of rendering a few bytes (#5009)
## What this PR does

`cozy-lib.resources.sanitize` now refuses a memory below 1Mi. The render
stops with a message that names the value and asks for a unit.

```text
memory (8) must be at least 1Mi. A value without a unit is read as bytes, so add the unit, such as 512Mi or 8Gi.
```

A quantity without a unit counts bytes, so `8` typed for 8Gi gave the
pods a memory limit and request of 8 bytes, as described in #5004. The
app schemas accept a bare number and the dashboard sends the field as
typed, so nothing refused it before. Charts size their containers
through `sanitize`, directly as etcd does or through
`defaultingSanitize`, so the check covers all of them. The smallest
preset is `t1.nano` at 128Mi, and 1Mi stays above any bare number
someone would type for Mi or Gi.

`0` and negative values are refused too. kubelet sets no cgroup memory
limit when the limit is zero ([kuberuntime_container_linux.go
L359-L374](https://github.com/kubernetes/kubernetes/blob/v1.37.1/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go#L359-L374)),
so `memory: 0` removed the limit rather than sizing anything.

`flatten`, which renders the tenant ResourceQuota, is not affected. A
quota of 0 memory is a valid way to allow none, so `flatten` now calls
the conversion part of `sanitize`, renamed `requestsAndLimits`, without
the floor. cpu gets no floor, since a bare cpu number is cores and a
zero cpu request is pinned by an existing test.

This is marked breaking like #4569. A release with a memory below 1Mi,
`0` included, rendered before and now fails until the value gets a unit.

Fixes #5004

The new suite
`packages/tests/cozy-lib-tests/tests/resources_memory_floor_test.yaml`
has 16 cases, with a small template for `defaultingSanitize`. It refuses
`"8"`, an unquoted `8`, `0`, `"1048575"`, `1023Ki`, `-1Gi` and `"8"`
over `t1.nano`, and renders `1Mi`, an unquoted `1048576`, `512Mi`,
`8Gi`, a map without memory, `t1.nano`, the legacy `nano`, `8Gi` over
`t1.nano` and a tenant quota of 0 memory.

- `helm unittest .` in `packages/tests/cozy-lib-tests`, with
helm-unittest 1.1.2 as CI pins it, passes 84 of 84 in 9 suites, against
68 of 68 in 8 on main. With the template from main the 7 refusal cases
fail and the other 9 pass.
- Seven mutations of the change each turn at least one case red (no
check, `le` for `lt`, a floor of 1M, `flatten` through the check,
strings only, zero exempt, another message).
- `hack/helm-unit-tests.sh` passes in all 106 chart directories, 549
suites and 4023 tests, against 547 and 3991 on main. The only difference
per chart is cozy-lib-tests, which the sweep runs twice.
- `bats` 1.14.0, the version CI pins, over the 34 `hack/*.bats` files
that call `helm template` passes 282 of 283 on main and on this branch,
with the same result for every test. The one failure, `a malformed
package name fails before emitting YAML` in
`hack/platform-packages_test.bats`, fails the same way on main on this
machine.
- A scratch chart calling `sanitize`, `defaultingSanitize` and `flatten`
with 22 inputs gives the same results with Helm 3.17.3, 4.2.3 and 4.3.1,
and every input that still renders gives the same output as on main.

I have not run it on a live cluster. `_resources.tpl` on release-1.6
differs from main only in two `toFloat` suffixes, and that backport is a
separate PR.

### Screenshots

Not a UI change.

### Downstream repositories

One cozy-lib template and its unittest chart. No values key, schema,
default, README or package change, so no trigger map entry applies.

- [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
fix(cozy-lib)!: a memory below 1Mi in an app's resources now fails the render with a message that asks for a unit. A number without a unit is read as bytes, so `8` meant as 8Gi gave the pods an 8 byte memory limit. `0` and negative values are refused as well. Tenant resource quotas are not affected.
```


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

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **Bug Fixes**
- Resource memory values below 1Mi now fail validation with a clear
error.
- Invalid memory overrides are rejected, including when presets provide
defaults.
- Valid memory values now produce consistent requests and limits, while
resources without memory remain unchanged.
  - Quota configurations can still use a memory value of `0`.

- **Tests**
- Added coverage for memory validation, unit formats, presets,
overrides, and quota behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-13 18:25:40 +03:00
..