Files
sim/scripts/sync-chart-appversion.ts
Waleed b42689d1b1 feat(helm): derive chart appVersion from the release, and lint with ct (#7643)
* improvement(helm): lint the chart with ct instead of hand-rolled checks

Replaces `helm lint` and the bespoke version-bump job with chart-testing,
the CNCF chart linter that ingress-nginx, prometheus-community and
external-secrets all gate on. It subsumes both: `helm lint` plus yamllint
over Chart.yaml and every values file, Chart.yaml schema validation, and
--check-version-increment, which is on by default and is exactly what the
33 lines of bash were reimplementing.

One trap worth recording. Passing `--charts` silently DISABLES the version
check -- it prints "Version increment checking disabled." and still exits 0,
so wiring it that way would have swapped a working gate for one that can
never fail. The PR path uses `--chart-dirs helm --target-branch <base>`; a
push has no base to diff, so `--charts` is correct there. Verified both
directions in a scratch repo with a real remote: content changed without a
bump gives "chart version not ok. Needs a version bump!" and exit 1, the
same change with a bump gives "Chart version ok."

Three chart fixes ct's yamllint required: 66 lines of trailing whitespace in
values.yaml, one inline comment a space short of the two yamllint wants, and
brace spacing in ci/full-values.yaml. Nothing but whitespace -- no
non-comment line changed, and both rendered manifest sets are byte-identical
before and after.

Renames ci/kind-values.yaml to ci/kind-overlay.yaml. ct treats every
ci/*-values.yaml as a standalone values set, but that file is a partial
layered on default-values.yaml, so linting it alone tripped the chart's own
required-secret guards. The new name is outside the glob and the header says
why.

Maintainer validation stays off: it resolves maintainers[].name against real
forge accounts and ours is the display name "Sim Team", so enabling it would
change what Artifact Hub shows.

* feat(helm): derive chart appVersion from the release instead of gating on it

appVersion is what the chart's image tags default to, so a stale one publishes
a chart that installs an older Sim than the release it ships with, and
images.yaml -- the list an operator mirrors into a disconnected registry --
names the wrong tags with it. Published chart versions are immutable, so each
stale value is frozen the moment it ships.

It was bumped by hand, drifted forty releases, and drifted twice more after a
check started catching it. That is the tell: the check could refuse to publish
but could not supply the value, so the only thing it reliably produced was a
red build on every release and a chart that never shipped. Only 1.11.0 was
ever published for exactly this reason.

The publish jobs now derive it. On a release merge the tag does not exist yet
-- this commit is what cuts it -- so the version comes from the commit
subject, and from the latest release for every other push. Both publish paths
run the same script before packaging, so the OCI artifact and the HTTP repo
cannot disagree.

Removes the post-merge check, which is now unreachable by construction, and
does not replace it with the pre-merge title gate that was considered: that
one guarded ground truth with a heuristic, and its failure mode was passing
silently.

Precedent is cert-manager, whose chart also lives in its application repo and
which injects the version at package time. The projects that commit the value
and bump by hand -- argo-cd, ingress-nginx, prometheus-community -- all keep
the chart in a separate repository, where a human is already editing
Chart.yaml as the unit of change. We are the former shape.

Verified the resolution across six subjects: release commit, multi-line body
carrying a decoy version, ordinary push, leading whitespace, shell
metacharacters (no expansion), and a version that is not at the start. Also
verified the script's exit codes directly rather than through a pipe, since a
gate that cannot fail is the thing being replaced here.

Sets appVersion to v0.8.26, two releases ahead of where it was stuck, and
regenerates images.yaml with it.

* fix(helm): match ci.yml's release predicate and resolve the version once

Two review findings, both real.

The leading-whitespace tolerance was wrong here. detect-version in ci.yml is
what actually cuts the tag and builds the images, and it anchors the version
at the first character. Accepting a subject it rejects meant the chart could
publish naming a release that was never created, pointing at images that do
not exist. The pattern is now identical to ci.yml's, with a comment saying it
has to stay that way. The tolerance came from a PR-title parsing problem in an
earlier design; commit subjects have a different authority.

The two publish jobs also resolved the version independently, and they run in
parallel. A release becoming public between those two API calls would package
the same immutable chart version with different appVersions, so the OCI
artifact and the HTTP repo would install different Sims. Resolution moved to
one job both consume, which also removes the duplicated logic.

Separately, --check only inspected Chart.yaml while the script writes both
that and the inventory derived from it, so it could report success over a
stale inventory. It now verifies both halves; confirmed it exits 1 when the
inventory is stale and 0 once regenerated.
2026-09-08 19:21:37 -07:00

109 lines
3.9 KiB
TypeScript

#!/usr/bin/env bun
/**
* Writes the application release a chart ships with into `helm/sim/Chart.yaml`
* and regenerates the image inventory that derives from it.
*
* `appVersion` is what the chart's image tags default to, so a stale one
* publishes a chart that silently installs an older Sim, and `helm/sim/images.yaml`
* — the list an operator mirrors into a disconnected registry — names the wrong
* tags with it. Because published chart versions are immutable, a stale value is
* frozen the moment it ships.
*
* It was bumped by hand and drifted for forty releases, then twice more after a
* check started catching it. The check could only refuse to publish; it could not
* supply the value. The publish jobs run this instead, so the number is derived
* from the release rather than remembered.
*
* Only node builtins are imported so this runs on a CI job with no dependencies
* installed, matching `generate-image-manifest.ts`.
*
* @example
* ```
* bun run scripts/sync-chart-appversion.ts --version v0.8.26
* bun run scripts/sync-chart-appversion.ts --version v0.8.26 --check
* ```
*
* `--check` verifies both halves: that Chart.yaml names the version and that the
* inventory generated from it is current.
*/
import { spawnSync } from 'node:child_process'
import { readFile, writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const CHART_PATH = resolve(ROOT, 'helm/sim/Chart.yaml')
/** A release tag as this repository cuts them: `v1.2.3`. */
const RELEASE_TAG = /^v\d+\.\d+\.\d+$/
/** Matches the top-level `appVersion:` key, quoted or bare. */
const APP_VERSION_LINE = /^appVersion:.*$/m
function parseArgs(argv: string[]): { version: string; check: boolean } {
const versionIndex = argv.indexOf('--version')
const version = versionIndex === -1 ? '' : (argv[versionIndex + 1] ?? '')
if (!RELEASE_TAG.test(version)) {
throw new Error(
`--version must be a release tag like v1.2.3, got ${version ? `"${version}"` : '<missing>'}`
)
}
return { version, check: argv.includes('--check') }
}
async function main() {
const { version, check } = parseArgs(process.argv.slice(2))
const chart = await readFile(CHART_PATH, 'utf8')
if (!APP_VERSION_LINE.test(chart)) {
throw new Error(`No top-level appVersion key in ${CHART_PATH}`)
}
const next = chart.replace(APP_VERSION_LINE, `appVersion: "${version}"`)
const changed = next !== chart
if (check) {
if (changed) {
console.error(
`helm/sim/Chart.yaml appVersion does not match ${version}. Run:\n` +
` bun run scripts/sync-chart-appversion.ts --version ${version}`
)
process.exit(1)
}
/**
* The inventory derives from appVersion, so a matching Chart.yaml alone does
* not mean the pair is consistent -- checking only half of what this script
* writes would report success over a stale inventory.
*/
const verified = spawnSync('bun', ['run', 'scripts/generate-image-manifest.ts', '--check'], {
cwd: ROOT,
stdio: 'inherit',
})
if (verified.status !== 0) process.exit(verified.status ?? 1)
console.log(`appVersion is ${version} and the image inventory matches.`)
return
}
if (changed) {
await writeFile(CHART_PATH, next)
console.log(`Set appVersion to ${version}.`)
} else {
console.log(`appVersion was already ${version}.`)
}
/**
* The inventory embeds appVersion in every first-party image tag, so it has to
* follow. Spawned rather than imported because the generator writes on import
* of its own main and owns its formatting.
*/
const generated = spawnSync('bun', ['run', 'scripts/generate-image-manifest.ts'], {
cwd: ROOT,
stdio: 'inherit',
})
if (generated.status !== 0) {
throw new Error(`generate-image-manifest.ts exited with ${generated.status}`)
}
}
if (import.meta.main) await main()