fix(roles): resolve lookup('role_version') pins in image checks

A pulled custom role pins its images through lookup('role_version', '<id>'). The image discovery behind the pin policy, reachability and mirror checks and the integration version test read that template literally and rejected it. resolve_role_version in utils/roles/custom.py replaces the lookup with the version the role's pull marker records; a role without one keeps the literal so the checks still fail like a deploy would.

🎯 Fix: 90 %
💥 Break: 3 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 87
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-11-10 21:46:49 +00:00
1 parent e615f5fa22
commit ed9101780e
4 files changed
+73 -3

No files matched your search

@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any
from utils.cache.yaml import load_yaml_str
from utils.docker.image.ref import is_valid_image_name
from utils.roles.custom import resolve_role_version
from utils.roles.mapping import ROLE_FILE_META_SERVICES
from . import PROJECT_ROOT
@@ -89,7 +90,11 @@ class TestDockerServicesImageVersionValid(unittest.TestCase):
f"{cfg_path}: services.{svc_name}.image invalid: {value!r}"
)
if field == "version" and not _is_valid_version(value):
if field == "version" and not _is_valid_version(
resolve_role_version(value, repo_root / "roles")
if isinstance(value, str)
else value
):
failures.append(
f"{cfg_path}: services.{svc_name}.version invalid: {value!r}"
)
+40 -1
View File
@@ -1,8 +1,17 @@
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from utils.roles.custom import is_custom, is_custom_path, manifest_version
from utils.roles.custom import (
MARKER_FILE,
MARKER_HEADER,
is_custom,
is_custom_path,
manifest_version,
resolve_role_version,
)
from utils.roles.mapping import ROLE_FILE_TASKS_MAIN
CARGO = '[package]\nname = "x"\nversion = "0.4.3"\n'
@@ -119,5 +128,35 @@ class TestIsCustomPath(unittest.TestCase):
self.assertFalse(is_custom_path("roles"))
class TestResolveRoleVersion(unittest.TestCase):
PIN = "full-v{{ lookup('role_version', 'web-app-cst-store') }}"
def _roles(self, marker: str | None) -> Path:
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
roles = Path(tmp.name)
if marker is not None:
role = roles / "web-app-cst-store"
role.mkdir()
(role / MARKER_FILE).write_text(marker, encoding="utf-8")
return roles
def test_a_pulled_version_replaces_the_lookup(self):
roles = self._roles(
f"{MARKER_HEADER}\ndirectory: web-app-cst-store\nversion: 0.3.4\n"
)
self.assertEqual(resolve_role_version(self.PIN, roles), "full-v0.3.4")
def test_a_marker_without_version_keeps_the_lookup(self):
roles = self._roles(f"{MARKER_HEADER}\ndirectory: web-app-cst-store\n")
self.assertEqual(resolve_role_version(self.PIN, roles), self.PIN)
def test_a_role_without_marker_keeps_the_lookup(self):
self.assertEqual(resolve_role_version(self.PIN, self._roles(None)), self.PIN)
def test_a_plain_version_passes_unchanged(self):
self.assertEqual(resolve_role_version("1.2.3", self._roles(None)), "1.2.3")
if __name__ == "__main__":
unittest.main()
+4 -1
View File
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
from utils.cache.yaml import load_yaml_any as _load_yaml_cached
from utils.docker.image.pin import pull_reference
from utils.roles.custom import resolve_role_version
from utils.roles.mapping import ROLE_FILE_META_SERVICES
from utils.update.base import is_maintained
@@ -261,7 +262,9 @@ def iter_role_images(repo_root: Path) -> Iterable[ImageRef]:
continue
image = str(service.get("image") or "").strip()
version = str(service.get("version") or "").strip()
version = resolve_role_version(
str(service.get("version") or "").strip(), roles_dir
)
if not image or not version:
continue
+23
View File
@@ -32,6 +32,9 @@ VERSION_MANIFESTS: tuple[str, ...] = ("Cargo.toml", "pyproject.toml", "package.j
_MARKER_LIMIT = 4096
_SEMVER = re.compile(r"\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.+-]+)?", re.ASCII)
_ROLE_VERSION_LOOKUP = re.compile(
r"\{\{\s*lookup\(\s*(['\"])role_version\1\s*,\s*(['\"])(?P<role>[^'\"]+)\2\s*\)\s*\}\}"
)
class CustomRoleError(Exception):
@@ -82,6 +85,26 @@ def marker_fields(role: Path) -> dict[str, str]:
return dict(line.split(": ", 1) for line in lines[1:] if ": " in line)
def resolve_role_version(value: str, roles_dir: Path) -> str:
"""Replace each ``{{ lookup('role_version', '<id>') }}`` with the version the pull recorded.
Args:
value: a ``meta/services.yml`` field, e.g.
``v{{ lookup('role_version', 'web-app-cst-store') }}``.
roles_dir: the ``roles/`` directory holding the pulled role.
Returns:
*value* with every lookup whose role marker carries a version replaced
by it; a lookup without one stays as written.
"""
return _ROLE_VERSION_LOOKUP.sub(
lambda match: (
marker_fields(roles_dir / match["role"]).get("version") or match[0]
),
value,
)
def manifest_version(read: Callable[[str], str | None]) -> str:
"""Return the semver the first version manifest of a repository declares.