fix(cozy-lib)!: refuse memory below 1Mi instead of rendering a few bytes

A quantity without a unit counts bytes. The app schemas take a bare
number for memory and the dashboard sends it as typed, so 8 meant as
8Gi rendered a memory limit and request of 8 bytes and the app did not
work. Charts size their containers through sanitize, directly or from
defaultingSanitize, so the floor goes there and the render stops with a
message that names the value and asks for a unit. 1Mi is far below the
smallest preset, 128Mi, and above any bare number typed for Mi or Gi.

0 is refused as well, since kubelet sets no cgroup memory limit for a
zero limit. flatten renders tenant quotas from the same conversion
without the floor, because a quota of 0 memory is a valid way to allow
none.

Assisted-by: LLM
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
This commit is contained in:
Yan Bondarenko committed 2026-10-21 04:14:26 +03:00
1 parent 1f92bcd20e
commit 4a706901b5
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 }}
@@ -204,7 +231,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"