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 -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-13 18:25:40 +03:00
commit bf24d28fde
3 files changed
+242 -1

No files matched your search

@@ -68,6 +68,8 @@
number of milli-CPUs (a CPU quantity finer than 1m is invalid for the VPA
admission webhook and meaningless for the scheduler).
A memory below 1Mi fails the render, see cozy-lib.resources.checkMemory.
The template now expects **one flat map** as input (no nested `requests:` /
`limits:` sections). Each value in that map is taken as the *limit* for the
corresponding resource. Usage example:
@@ -92,6 +94,31 @@
devices.com/nvidia: "1" # = limit
*/}}
{{- define "cozy-lib.resources.sanitize" }}
{{- include "cozy-lib.resources.checkMemory" (index . 0) }}
{{- include "cozy-lib.resources.requestsAndLimits" . }}
{{- end }}
{{- /*
A quantity without a unit counts bytes, so a memory of `8` typed for 8Gi
sizes the container at 8 bytes and nothing else refuses it. Every preset is
128Mi or more, so a floor of 1Mi only stops that kind of slip.
*/}}
{{- define "cozy-lib.resources.checkMemory" }}
{{- if hasKey . "memory" }}
{{- $memory := index . "memory" }}
{{- if not (kindIs "invalid" $memory) }}
{{- if lt (include "cozy-lib.resources.toFloat" $memory | float64) (include "cozy-lib.resources.toFloat" "1Mi" | float64) }}
{{- fail (printf "memory (%v) must be at least 1Mi. A value without a unit is read as bytes, so add the unit, such as 512Mi or 8Gi." $memory) }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- /*
sanitize without the memory floor. flatten renders a ResourceQuota with it,
where a memory of 0 is a valid quota that allows none.
*/}}
{{- define "cozy-lib.resources.requestsAndLimits" }}
{{- $cpuAllocationRatio := include "cozy-lib.resources.cpuAllocationRatio" . | float64 }}
{{- $memoryAllocationRatio := include "cozy-lib.resources.memoryAllocationRatio" . | float64 }}
{{- $ephemeralStorageAllocationRatio := include "cozy-lib.resources.ephemeralStorageAllocationRatio" . | float64 }}
@@ -224,7 +251,7 @@
{{- define "cozy-lib.resources.flatten" -}}
{{- $out := dict -}}
{{- $res := include "cozy-lib.resources.sanitize" . | fromYaml -}}
{{- $res := include "cozy-lib.resources.requestsAndLimits" . | fromYaml -}}
{{- range $section, $values := $res }}
{{- range $k, $v := $values }}
{{- with include "cozy-lib.resources.flattenResource" (list $section $k) }}
@@ -0,0 +1,11 @@
{{- with .Values.defaulting }}
apiVersion: v1
kind: Pod
metadata:
name: {{ $.Release.Name }}-defaulting
spec:
containers:
- name: main
image: busybox
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .preset .resources $) | nindent 6 }}
{{- end }}
@@ -0,0 +1,203 @@
# ./tests/resources_memory_floor_test.yaml
suite: resources memory floor
# A quantity without a unit counts bytes, so a memory of 8 typed for 8Gi would
# size the container at 8 bytes. sanitize refuses anything below 1Mi, which
# every preset clears by far. flatten renders tenant quotas without the floor,
# because a quota of 0 memory is a valid way to allow none.
templates:
- templates/tests/resources.yaml
- templates/tests/resources-defaulting.yaml
- templates/tests/quota.yaml
release:
name: myrelease
namespace: default
tests:
- it: refuses a quoted number without a unit
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: "8"
asserts:
- failedTemplate:
errorMessage: 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.
- it: refuses an unquoted number without a unit
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 8
asserts:
- failedTemplate:
errorPattern: '^memory \(8\) must be at least 1Mi\.'
- it: refuses a zero memory
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 0
asserts:
- failedTemplate:
errorPattern: '^memory \(0\) must be at least 1Mi\.'
- it: refuses a byte count one below 1Mi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: "1048575"
asserts:
- failedTemplate:
errorPattern: '^memory \(1048575\) must be at least 1Mi\.'
- it: refuses a suffixed value below 1Mi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 1023Ki
asserts:
- failedTemplate:
errorPattern: '^memory \(1023Ki\) must be at least 1Mi\.'
- it: refuses a negative memory
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: -1Gi
asserts:
- failedTemplate:
errorPattern: '^memory \(-1Gi\) must be at least 1Mi\.'
- it: renders exactly 1Mi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 1Mi
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 1Mi
- equal:
path: spec.containers[0].resources.requests.memory
value: "1048576"
- it: renders an unquoted byte count of exactly 1Mi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 1048576
asserts:
- notFailedTemplate: {}
- equal:
path: spec.containers[0].resources.requests.memory
value: "1048576"
- it: renders 512Mi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 512Mi
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 512Mi
- equal:
path: spec.containers[0].resources.requests.memory
value: "536870912"
- it: renders 8Gi
template: templates/tests/resources.yaml
set:
resources:
cpu: "1"
memory: 8Gi
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 8Gi
- equal:
path: spec.containers[0].resources.requests.memory
value: "8589934592"
- it: leaves a map without memory alone
template: templates/tests/resources.yaml
set:
resources:
ephemeral-storage: 2Gi
asserts:
- notFailedTemplate: {}
- notExists:
path: spec.containers[0].resources.limits.memory
- it: refuses a number without a unit set over a preset
template: templates/tests/resources-defaulting.yaml
set:
defaulting:
preset: t1.nano
resources:
memory: "8"
asserts:
- failedTemplate:
errorPattern: '^memory \(8\) must be at least 1Mi\.'
- it: renders the smallest preset unchanged
template: templates/tests/resources-defaulting.yaml
set:
defaulting:
preset: t1.nano
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 128Mi
- equal:
path: spec.containers[0].resources.requests.memory
value: "134217728"
- it: renders the smallest legacy preset unchanged
template: templates/tests/resources-defaulting.yaml
set:
defaulting:
preset: nano
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 128Mi
- it: renders an explicit memory with a unit over a preset
template: templates/tests/resources-defaulting.yaml
set:
defaulting:
preset: t1.nano
resources:
memory: 8Gi
asserts:
- equal:
path: spec.containers[0].resources.limits.memory
value: 8Gi
- equal:
path: spec.containers[0].resources.limits.cpu
value: 250m
- it: still renders a tenant quota of 0 memory
template: templates/tests/quota.yaml
set:
quota:
memory: 0
asserts:
- equal:
path: spec.hard["limits.memory"]
value: "0"
- equal:
path: spec.hard["requests.memory"]
value: "0"