Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(helm): match reused chart archives by the chart they declare (#933)

Shai Shmuel32b55d3fb5e2
diff --git a/tools/ci/tests/test_helm_task.py b/tools/ci/tests/test_helm_task.py
index 9717b613c..c9e08e92b 100644
--- a/tools/ci/tests/test_helm_task.py
+++ b/tools/ci/tests/test_helm_task.py
@@ -28,12 +28,12 @@ def test_dependency_repositories_are_derived_and_idempotent() -> None:
assert all(not url.startswith("oci://") for url in urls)
-def test_locked_archives_follow_the_checked_in_lock() -> None:
- """The task's line parser and a YAML parser agree on the pinned archives."""
+def test_locked_dependencies_follow_the_checked_in_lock() -> None:
+ """The task's line parser and a YAML parser agree on the pinned dependencies."""
lock = yaml.safe_load((helm.resolve_project_root() / helm.CHART_DIR / "Chart.lock").read_text())
- expected = {f"{entry['name']}-{entry['version']}.tgz" for entry in lock["dependencies"]}
+ expected = {(entry["name"], entry["version"]) for entry in lock["dependencies"]}
assert expected
- assert set(helm.locked_dependency_archives()) == expected
+ assert helm.locked_dependencies() == expected
def test_lock_digest_is_computed_the_way_helm_writes_it() -> None:
@@ -56,7 +56,7 @@ def locked_chart(tmp_path: Path, monkeypatch) -> Path:
source = helm.resolve_project_root() / helm.CHART_DIR
chart = tmp_path / helm.CHART_DIR
(chart / "charts").mkdir(parents=True)
- for name in ("Chart.yaml", "Chart.lock"):
+ for name in ("Chart.yaml", "Chart.lock", ".helmignore"):
shutil.copy2(source / name, chart / name)
for entry in yaml.safe_load((chart / "Chart.lock").read_text())["dependencies"]:
archive = chart / "charts" / f"{entry['name']}-{entry['version']}.tgz"
@@ -76,12 +76,19 @@ def test_dependencies_are_present_when_charts_match_the_lock(locked_chart: Path)
assert helm.locked_dependencies_present()
+def test_dependencies_are_present_whatever_the_archives_are_called(locked_chart: Path) -> None:
+ """Helm keeps the file name a repository publishes, which need not be ``<name>-<version>.tgz``."""
+ archive, _, _ = a_locked_dependency(locked_chart)
+ archive.rename(archive.with_name(f"{archive.stem} copy.tgz"))
+ assert helm.locked_dependencies_present()
+
+
@pytest.mark.parametrize(
"state",
[
"missing",
- "other-version",
"leftover",
+ "duplicate",
"directory",
"no-charts",
"no-lock",
@@ -90,6 +97,10 @@ def test_dependencies_are_present_when_charts_match_the_lock(locked_chart: Path)
"declares-other-version",
"declares-other-chart",
"unreadable",
+ "named-tar-gz",
+ "named-hidden",
+ "named-underscore",
+ "named-ignored",
],
)
def test_dependencies_are_absent_unless_charts_match_a_current_lock(locked_chart: Path, state: str) -> None:
@@ -101,10 +112,11 @@ def test_dependencies_are_absent_unless_charts_match_a_current_lock(locked_chart
constraint = re.compile(r"(?m)^( version: ).*$")
if state == "missing":
archive.unlink()
- elif state == "other-version":
- archive.rename(charts / f"{archive.stem}-older.tgz")
elif state == "leftover":
- (charts / f"{archive.stem}-older.tgz").write_bytes(b"archive")
+ # Another version of a locked chart, left beside the locked one.
+ write_chart_archive(charts / f"{name}-0.0.0.tgz", name, "0.0.0")
+ elif state == "duplicate":
+ shutil.copy2(archive, charts / f"{archive.stem} copy.tgz")
elif state == "directory":
archive.unlink()
archive.mkdir()
@@ -116,12 +128,23 @@ def test_dependencies_are_absent_unless_charts_match_a_current_lock(locked_chart
# Chart.yaml asks for another version and the lock was not regenerated.
manifest.write_text(constraint.sub(r"\g<1>0.0.x", manifest.read_text(), count=1))
elif state == "declares-other-version":
- # The file name matches the lock, the chart inside does not.
+ # The file is named after the locked version, the chart inside is another one.
write_chart_archive(archive, name, "0.0.0")
elif state == "declares-other-chart":
write_chart_archive(archive, "another-chart", version)
elif state == "unreadable":
archive.write_bytes(b"not an archive")
+ elif state == "named-tar-gz":
+ # The right chart under a name Helm does not load as a dependency.
+ archive.rename(archive.with_suffix(".tar.gz"))
+ elif state == "named-hidden":
+ archive.rename(archive.with_name(f".{archive.name}"))
+ elif state == "named-underscore":
+ archive.rename(archive.with_name(f"_{archive.name}"))
+ elif state == "named-ignored":
+ # A name the chart's .helmignore excludes, so Helm does not load the file.
+ (locked_chart / ".helmignore").write_text("# build residue\ntmpcharts-*\n")
+ archive.rename(archive.with_name(f"tmpcharts-{archive.name}"))
else:
# A field the task does not read: the digest cannot be confirmed.
manifest.write_text(constraint.sub(r"\g<0>\n tags:\n - extra", manifest.read_text(), count=1))
diff --git a/tools/mise_tasks/helm.py b/tools/mise_tasks/helm.py
index 561c00e0c..f47cb449b 100755
--- a/tools/mise_tasks/helm.py
+++ b/tools/mise_tasks/helm.py
@@ -19,6 +19,7 @@ from __future__ import annotations
import argparse
import fcntl
+import fnmatch
import hashlib
import json
import os
@@ -219,13 +220,13 @@ def lock_matches_chart() -> bool:
return f"digest: sha256:{digest}" in lock.read_text().splitlines()
-def locked_dependency_archives() -> dict[str, tuple[str, str]] | None:
- """Return each archive Chart.lock pins with its chart name and version, or None when unknown."""
+def locked_dependencies() -> set[tuple[str, str]] | None:
+ """Return the chart name and version of every dependency Chart.lock pins, or None when unknown."""
lock = resolve_project_root() / CHART_DIR / "Chart.lock"
locked = _dependency_entries(lock) if lock.is_file() else None
if not locked or not all(entry.get("name") and entry.get("version") for entry in locked):
return None
- return {f"{entry['name']}-{entry['version']}.tgz": (entry["name"], entry["version"]) for entry in locked}
+ return {(entry["name"], entry["version"]) for entry in locked}
def _archive_identity(archive: Path) -> tuple[str, str] | None:
@@ -248,22 +249,45 @@ def _archive_identity(archive: Path) -> tuple[str, str] | None:
return declared.get("name", ""), declared.get("version", "")
+def _helmignore_name_patterns() -> list[str]:
+ """Return the plain file-name patterns in the chart's .helmignore.
+
+ Helm skips a file in charts/ whose name matches one of them. Patterns
+ with a path or a negation are not evaluated.
+ """
+ ignore = resolve_project_root() / CHART_DIR / ".helmignore"
+ if not ignore.is_file():
+ return []
+ lines = (line.strip() for line in ignore.read_text().splitlines())
+ return [line for line in lines if line and not line.startswith(("#", "!")) and "/" not in line]
+
+
def locked_dependencies_present() -> bool:
"""Return whether charts/ holds exactly what the current Chart.yaml locks.
Helm downloads every dependency again on each build, even when the
archive is already there. No build is needed when the lock is still the
- one generated from Chart.yaml and charts/ matches it exactly: nothing
- missing, nothing left over from another version, and every archive
- declaring the chart name and version the lock pins.
+ one generated from Chart.yaml and charts/ holds one archive for each
+ locked dependency and nothing else.
+
+ Archives are matched by the chart name and version they declare. Their
+ file names only have to be ones Helm loads: Helm keeps the name the
+ repository publishes, which is not always ``<name>-<version>.tgz``.
"""
- expected = locked_dependency_archives()
+ expected = locked_dependencies()
charts = resolve_project_root() / CHART_DIR / "charts"
if expected is None or not charts.is_dir() or not lock_matches_chart():
return False
- return {entry.name for entry in charts.iterdir()} == set(expected) and all(
- _archive_identity(charts / name) == identity for name, identity in expected.items()
- )
+ entries = list(charts.iterdir())
+ # Helm loads only .tgz files from charts/. It skips names that start with
+ # "." or "_" and names the chart's .helmignore excludes.
+ ignored = _helmignore_name_patterns()
+ for entry in entries:
+ if entry.suffix != ".tgz" or entry.name[0] in "._":
+ return False
+ if any(fnmatch.fnmatchcase(entry.name, pattern) for pattern in ignored):
+ return False
+ return len(entries) == len(expected) and {_archive_identity(entry) for entry in entries} == expected
def validation_args(extra_args: list[str]) -> list[str]: