Files
cozystack/hack/check-rd-presets.sh
Myasnikov Daniil 47fe04673a fix(tests): reject unreadable RD preset schemas
Discarding jq's exit status lets malformed schemas pass the preset check
or turns a partial read into a misleading missing-preset report.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-09-28 14:29:17 +05:00

80 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Verify every ApplicationDefinition in packages/system/*-rd/cozyrds/*.yaml
# that defines a resourcesPreset enum carries the full set of 40
# instance-type names plus the 7 legacy aliases. Catches the regression
# where one chart's Makefile forgets to invoke hack/update-crd.sh and the
# RD schema drifts from the chart's values.schema.json.
set -euo pipefail
EXPECTED=(
t1.nano t1.micro t1.small t1.medium t1.large t1.xlarge t1.2xlarge t1.4xlarge
c1.nano c1.micro c1.small c1.medium c1.large c1.xlarge c1.2xlarge c1.4xlarge
s1.nano s1.micro s1.small s1.medium s1.large s1.xlarge s1.2xlarge s1.4xlarge
u1.nano u1.micro u1.small u1.medium u1.large u1.xlarge u1.2xlarge u1.4xlarge
m1.nano m1.micro m1.small m1.medium m1.large m1.xlarge m1.2xlarge m1.4xlarge
nano micro small medium large xlarge 2xlarge
)
fail=0
jq_err=$(mktemp)
trap 'rm -f "$jq_err"' EXIT
for f in packages/system/*-rd/cozyrds/*.yaml; do
schema=$(yq -r '.spec.application.openAPISchema // ""' "$f")
if [ -z "$schema" ]; then
continue
fi
# Pull every resourcesPreset enum out of the schema. Key on the JSON
# path ending in "resourcesPreset" rather than a description heuristic,
# so an unrelated field with "preset" in its description does not match.
# A failed read can leave empty or partial output; neither is an enum to check.
if ! enums=$(jq -r '
[paths(type == "object" and has("enum")) as $p
| select($p[-1] == "resourcesPreset")
| getpath($p).enum[]]
| .[]
' <<<"$schema" 2>"$jq_err"); then
echo "FAIL: $f openAPISchema could not be read: $(tr '\n' ' ' <"$jq_err")" >&2
fail=1
continue
fi
if [ -z "$enums" ]; then
continue
fi
missing=()
for want in "${EXPECTED[@]}"; do
# -F: literal match so the `.` in t1.nano does not match any character.
# here-string, not a pipe: a `printf ... | grep -q` pipeline SIGPIPEs the
# printf when grep matches early and exits, and under `set -o pipefail` that
# 141 makes the whole pipeline read as failure — a false "missing" for a
# preset that is present (hit on the larger enums, e.g. kubernetes-rd).
if ! grep -Fqx -- "$want" <<<"$enums"; then
missing+=("$want")
fi
done
if [ ${#missing[@]} -gt 0 ]; then
echo "FAIL: $f resourcesPreset enum missing: ${missing[*]}" >&2
fail=1
fi
done
if [ "$fail" -ne 0 ]; then
echo "Some RD schemas failed the preset check; see the errors above." >&2
echo "For missing presets, run 'make generate' inside the affected chart directory." >&2
exit 1
fi
echo "All RD schemas carry the full 47-preset enum."
# Also reject hardcoded legacy preset literals in chart template files.
# New code should use instance-type names; legacy aliases exist only for
# user-supplied values in existing HelmRelease and app CR specs.
echo "Checking chart templates for hardcoded legacy preset literals..."
legacy_hits=$(grep -rEn 'defaultingSanitize.*\(list "(nano|micro|small|medium|large|xlarge|2xlarge)"' \
packages/apps packages/extra packages/system 2>/dev/null || true)
if [ -n "$legacy_hits" ]; then
echo "FAIL: hardcoded legacy preset literals in chart templates:" >&2
echo "$legacy_hits" >&2
echo "Replace with the instance-type form from docs/operations/resource-presets.md." >&2
exit 1
fi
echo "No hardcoded legacy preset literals in chart templates."