Skip to content

Commit 7ad7d39

Browse files
test(hack): keep every chart's stored Helm release within budget
A chart can grow past the 1 MiB Helm release Secret without any test noticing: it renders, lints and installs, and only the upgrade whose manifest is large enough fails, on a live cluster, with a release that stays failed. backupstrategy-controller sat under 1 KB from the limit before it crossed it. Estimate the stored size the way Helm encodes it (release JSON with the chart's templates and files, gzip, base64) and fail any chart under packages/ above 90 percent of the limit, plus the largest revision of backupstrategy-controller with every default Strategy rendered. The estimate counts files a .helmignore names, because the production path stores them. A size measured without its manifest is a lower bound that passes for the wrong reason, so every chart must render offline except an explicit list of charts that need cluster state, and the worst-case render must succeed and contain the default strategies before it is measured. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 513b508 commit 7ad7d39

2 files changed

Lines changed: 261 additions & 0 deletions

File tree

‎hack/helm-release-size.bats‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env bats
2+
# Helm keeps every release revision in one Secret, and the apiserver refuses a
3+
# Secret over 1 MiB. A chart past that budget installs and then fails the one
4+
# upgrade whose manifest is large enough ("Secret sh.helm.release.v1.<name>.vN
5+
# is invalid: data: Too long"), and that release stays failed. The stored
6+
# release carries the whole chart directory as it reaches helm-controller:
7+
# the packages artifact applies no .helmignore, so tests/, images/ and the
8+
# Makefile count too. hack/helm-release-size.py reproduces the encoding.
9+
#
10+
# The budget is 90 percent of the limit: the estimate runs about 1 percent
11+
# above real Secrets, and the rest is room for a chart to grow by more than
12+
# one change before anyone has to act.
13+
#
14+
# Harness compatibility note: CI runs this file under Bats through
15+
# `make bats-unit-tests`. It also remains compatible with the narrower legacy
16+
# `hack/cozytest.sh` translator, so it avoids `run`, `$status`, `$output`,
17+
# `skip`, and setup()/teardown(). Paths stay repo-root-relative for both
18+
# runners.
19+
#
20+
# Run with: bats hack/helm-release-size.bats
21+
22+
load test_helper
23+
24+
LIMIT=1048576
25+
BUDGET=943718
26+
27+
# Charts that do not render offline with default values (required values,
28+
# cluster lookups). They are measured on their stored files and templates
29+
# alone, a lower bound. Any other chart must render: a render failure there
30+
# would quietly shrink the measurement to the same lower bound. A chart that
31+
# stops rendering offline on purpose goes here; one that starts rendering
32+
# must come off, so the list never hides a renderable chart.
33+
NO_OFFLINE_RENDER="
34+
packages/apps/bucket
35+
packages/apps/clickhouse
36+
packages/apps/foundationdb
37+
packages/apps/harbor
38+
packages/apps/kafka
39+
packages/apps/kubernetes
40+
packages/apps/kubernetes-nodes
41+
packages/apps/mariadb
42+
packages/apps/mongodb
43+
packages/apps/nats
44+
packages/apps/openbao
45+
packages/apps/opensearch
46+
packages/apps/postgres
47+
packages/apps/qdrant
48+
packages/apps/rabbitmq
49+
packages/apps/redis
50+
packages/apps/tenant
51+
packages/apps/valkey
52+
packages/apps/vm-disk
53+
packages/apps/vm-instance
54+
packages/apps/vpc
55+
packages/apps/vpn
56+
packages/core/platform
57+
packages/extra/bootbox
58+
packages/extra/computeplane
59+
packages/extra/etcd
60+
packages/extra/external-dns
61+
packages/extra/gateway
62+
packages/extra/info
63+
packages/extra/ingress
64+
packages/extra/monitoring
65+
packages/extra/seaweedfs
66+
packages/library/cozy-lib
67+
packages/system/kuberture
68+
packages/system/telepresence
69+
packages/tests/cozy-lib-tests
70+
"
71+
72+
@test "the estimator counts chart files the way Helm stores them" {
73+
tmp=$(mktemp -d)
74+
mkdir -p "$tmp/chart/templates" "$tmp/chart/tests"
75+
printf 'apiVersion: v2\nname: probe\nversion: 0.0.0\n' > "$tmp/chart/Chart.yaml"
76+
# Incompressible, so the stored size cannot be smaller than its base64.
77+
head -c 300000 /dev/urandom > "$tmp/chart/tests/blob"
78+
size=$(printf '' | python3 -I hack/helm-release-size.py "$tmp/chart")
79+
if [ "$size" -lt 400000 ]; then
80+
echo "estimate $size for a chart carrying 300000 random bytes; files are not counted" >&2
81+
false
82+
fi
83+
rm -rf "$tmp"
84+
}
85+
86+
@test "every chart's stored release fits the budget with its default render" {
87+
tmp=$(mktemp -d)
88+
: > "$tmp/over"
89+
: > "$tmp/render"
90+
n=0
91+
for c in packages/*/*/; do
92+
c=${c%/}
93+
[ -f "$c/Chart.yaml" ] || continue
94+
n=$((n + 1))
95+
listed=no
96+
case "$NO_OFFLINE_RENDER" in *"
97+
$c
98+
"*) listed=yes ;; esac
99+
if helm template release "$c" -n default > "$tmp/manifest" 2> "$tmp/err"; then
100+
if [ "$listed" = yes ]; then
101+
echo "$c renders offline now; remove it from NO_OFFLINE_RENDER in hack/helm-release-size.bats" >> "$tmp/render"
102+
fi
103+
elif [ "$listed" = yes ]; then
104+
: > "$tmp/manifest"
105+
else
106+
echo "$c failed to render offline; fix the render, or add it to NO_OFFLINE_RENDER in hack/helm-release-size.bats if it needs cluster state: $(grep -m 1 '^Error' "$tmp/err" | cut -c1-300)" >> "$tmp/render"
107+
continue
108+
fi
109+
size=$(python3 -I hack/helm-release-size.py "$c" < "$tmp/manifest")
110+
if [ "$size" -gt "$BUDGET" ]; then
111+
echo "$c: $size bytes (budget $BUDGET, limit $LIMIT)" >> "$tmp/over"
112+
fi
113+
done
114+
if [ "$n" -lt 50 ]; then
115+
echo "only $n charts found under packages/; the sweep checked almost nothing" >&2
116+
false
117+
fi
118+
if [ -s "$tmp/render" ]; then
119+
cat "$tmp/render" >&2
120+
false
121+
fi
122+
if [ -s "$tmp/over" ]; then
123+
echo "charts whose stored Helm release is over budget:" >&2
124+
cat "$tmp/over" >&2
125+
false
126+
fi
127+
rm -rf "$tmp"
128+
}
129+
130+
@test "backupstrategy-controller fits the budget on its largest revision" {
131+
# The default render gates every Strategy CR off. The revision that renders
132+
# all of them, with the Velero location on as in production, is the one
133+
# that grew past the limit once.
134+
c=packages/system/backupstrategy-controller
135+
tmp=$(mktemp -d)
136+
# Rendered to a file rather than piped: in a pipe the estimator's status
137+
# would stand for the render's, and a failed render would be measured as an
138+
# empty manifest and pass.
139+
if ! helm template backupstrategy-controller "$c" -n cozy-backup-controller \
140+
--set backupStorage.bucketNameOverride=cozy-backups-0123 \
141+
--set velero.bslEnabled=true > "$tmp/manifest"; then
142+
echo "$c failed to render its largest revision; nothing was measured" >&2
143+
false
144+
fi
145+
if ! grep -q '^kind: Kafka$' "$tmp/manifest"; then
146+
echo "$c rendered without the default Strategy CRs; this is not its largest revision" >&2
147+
false
148+
fi
149+
size=$(python3 -I hack/helm-release-size.py "$c" < "$tmp/manifest")
150+
rm -rf "$tmp"
151+
if [ "$size" -gt "$BUDGET" ]; then
152+
echo "$c: $size bytes with every default Strategy rendered (budget $BUDGET, limit $LIMIT)" >&2
153+
false
154+
fi
155+
}

‎hack/helm-release-size.py‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python3
2+
"""Estimate the size of the Secret in which Helm stores a release of a chart.
3+
4+
Helm's Secret storage driver keeps each revision as one Secret whose `release`
5+
value is base64(gzip(json(release))), and the apiserver refuses a Secret whose
6+
data exceeds 1 MiB. A chart that crosses it installs fine at first and then
7+
fails the one upgrade whose rendered manifest is large enough, with "Secret
8+
sh.helm.release.v1.<name>.vN is invalid: data: Too long". The stored release
9+
carries the whole chart, not just what renders: every file under the chart dir
10+
that is not Chart.yaml, values.yaml, values.schema.json, templates/ or charts/
11+
ends up in chart.files, and the production path (the packages OCI artifact
12+
loaded by helm-controller) applies no .helmignore, so tests/, images/ and the
13+
Makefile are stored too.
14+
15+
This reproduces the encoding: the release JSON with the chart's templates and
16+
files as base64 []byte, the rendered manifest read from stdin (`helm template`
17+
output), gzip at Helm's BestCompression, then base64. It does not reproduce
18+
Helm's gzip byte for byte or the runtime fields (info, config); measured
19+
against real release Secrets it lands within about 0.5 percent, which is why
20+
callers compare it to a threshold with headroom rather than to the limit.
21+
22+
Usage: helm template ... | helm-release-size.py <chart-dir> [--exclude GLOB ...]
23+
Prints the estimated stored size in bytes.
24+
"""
25+
import base64
26+
import fnmatch
27+
import gzip
28+
import json
29+
import os
30+
import sys
31+
32+
# Paths Helm keeps out of chart.files: they are stored elsewhere in the chart
33+
# object (metadata, values, schema, templates) or are subcharts.
34+
NOT_FILES = {"Chart.yaml", "Chart.lock", "values.yaml", "values.schema.json"}
35+
NOT_FILE_DIRS = ("templates/", "charts/")
36+
37+
38+
def go_json(obj):
39+
# encoding/json escapes <, > and & inside strings; match it so the size
40+
# does not drift with how much HTML-ish text a chart carries.
41+
s = json.dumps(obj, separators=(",", ":"), ensure_ascii=False)
42+
return s.replace("<", "\\u003c").replace(">", "\\u003e").replace("&", "\\u0026")
43+
44+
45+
def main(argv):
46+
if len(argv) < 2:
47+
sys.stderr.write(__doc__)
48+
return 2
49+
chart = argv[1].rstrip("/")
50+
excludes = []
51+
rest = argv[2:]
52+
while rest:
53+
if rest[0] != "--exclude" or len(rest) < 2:
54+
sys.stderr.write("unexpected argument: %s\n" % rest[0])
55+
return 2
56+
excludes.append(rest[1])
57+
rest = rest[2:]
58+
59+
templates, files = [], []
60+
for root, dirs, names in os.walk(chart):
61+
dirs.sort()
62+
for n in sorted(names):
63+
path = os.path.join(root, n)
64+
rel = os.path.relpath(path, chart)
65+
if any(fnmatch.fnmatch(rel, g) for g in excludes):
66+
continue
67+
with open(path, "rb") as fh:
68+
data = base64.b64encode(fh.read()).decode()
69+
entry = {"name": rel, "data": data}
70+
if rel.startswith("templates/"):
71+
templates.append(entry)
72+
elif rel in NOT_FILES or rel.startswith(NOT_FILE_DIRS):
73+
continue
74+
else:
75+
files.append(entry)
76+
77+
def read(rel):
78+
p = os.path.join(chart, rel)
79+
return open(p).read() if os.path.exists(p) else ""
80+
81+
release = {
82+
"name": "release",
83+
"info": {"status": "deployed", "description": "Upgrade complete"},
84+
"chart": {
85+
"metadata": {"name": os.path.basename(chart), "version": "0.0.0", "apiVersion": "v2"},
86+
"lock": None,
87+
"templates": templates,
88+
# Helm stores the parsed values and the user config; raw text with
89+
# comments and no config errs high (about 6 KB on
90+
# backupstrategy-controller), the safe side for a budget.
91+
"values": read("values.yaml"),
92+
"schema": base64.b64encode(read("values.schema.json").encode()).decode() or None,
93+
"files": files,
94+
},
95+
"manifest": sys.stdin.read(),
96+
"version": 1,
97+
"namespace": "default",
98+
}
99+
raw = go_json(release).encode()
100+
stored = base64.b64encode(gzip.compress(raw, compresslevel=9))
101+
print(len(stored))
102+
return 0
103+
104+
105+
if __name__ == "__main__":
106+
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)