fix(image): act on the review of the image metadata
Label parser: split the LABEL instruction like a shell (a value may hold NAME=value text), read the last stage only, require base.name and tie it to the FROM tag, require the documentation link to be <source>/src/branch/main/docs/images/<name>.md and the page to be non-empty. Docs: do not name a Python version the base may not ship, show the lint.yml pin check as it is, say which workflow paths start a build, name the system-language local hooks, add the apparmor option and the registry path to the molecule usage, and soften unverified Renovate and host-release statements. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
c3c665e799
commit
aa1848a995
8 files changed
+142
-85
No files matched your search
@@ -53,6 +53,7 @@ import fnmatch
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -359,6 +360,12 @@ def dockerfile_releases(dockerfile_text):
|
||||
return releases, problems
|
||||
|
||||
|
||||
def file_size(path):
|
||||
"""Size in bytes of a regular file, or None when there is none."""
|
||||
p = pathlib.Path(path)
|
||||
return p.stat().st_size if p.is_file() else None
|
||||
|
||||
|
||||
def check_job_images(matrix, read_file, workflow_text):
|
||||
"""Errors where an image's job container (`job_image`) is not the Ubuntu release its Dockerfile builds.
|
||||
|
||||
@@ -392,47 +399,62 @@ def check_job_images(matrix, read_file, workflow_text):
|
||||
REQUIRED_LABELS = ("org.opencontainers.image.title", "org.opencontainers.image.description",
|
||||
"org.opencontainers.image.source", "org.opencontainers.image.url",
|
||||
"org.opencontainers.image.documentation", "org.opencontainers.image.licenses",
|
||||
"org.opencontainers.image.authors", "net.rkdr.mortis.purpose",
|
||||
"org.opencontainers.image.authors", "org.opencontainers.image.base.name", "net.rkdr.mortis.purpose",
|
||||
"net.rkdr.mortis.contents", "net.rkdr.mortis.usage")
|
||||
LABEL_KEY_RE = re.compile(r"(?:^|\s)(?P<key>[A-Za-z0-9][A-Za-z0-9._-]*)=")
|
||||
|
||||
|
||||
def dockerfile_labels(dockerfile_text):
|
||||
"""{key: raw value text} of the LABEL instructions of a Dockerfile (quotes kept)."""
|
||||
"""{key: value} of the LABEL instructions of the LAST stage of a Dockerfile (labels of an earlier
|
||||
stage are not inherited by a stage that starts from another image). The instruction is split like a
|
||||
shell would split it, so a quoted value may hold spaces and `NAME=value` text."""
|
||||
labels = {}
|
||||
for unused, line in _instructions(dockerfile_text):
|
||||
parts = line.split(None, 1)
|
||||
if len(parts) == 2 and parts[0].upper() == "LABEL":
|
||||
rest = parts[1]
|
||||
keys = list(LABEL_KEY_RE.finditer(rest))
|
||||
for n, m in enumerate(keys):
|
||||
end = keys[n + 1].start() if n + 1 < len(keys) else len(rest)
|
||||
labels[m["key"]] = rest[m.end():end].strip().rstrip("\\").strip()
|
||||
if not parts:
|
||||
continue
|
||||
if parts[0].upper() == "FROM":
|
||||
labels = {}
|
||||
elif parts[0].upper() == "LABEL" and len(parts) == 2:
|
||||
try:
|
||||
tokens = shlex.split(parts[1])
|
||||
except ValueError:
|
||||
continue # an unbalanced quote: the labels stay missing, which the check reports
|
||||
for token in tokens:
|
||||
key, equals, value = token.partition("=")
|
||||
if equals and key:
|
||||
labels[key] = value
|
||||
return labels
|
||||
|
||||
|
||||
def check_image_metadata(matrix, read_file, exists):
|
||||
"""Errors where an image lacks the labels its registry page shows, or the document they link to."""
|
||||
def check_image_metadata(matrix, read_file, size_of):
|
||||
"""Errors where an image lacks the labels its registry page shows, or the document they link to.
|
||||
size_of(path) is the size in bytes of a file, or None when there is no such file."""
|
||||
errors = []
|
||||
for name, entry in matrix.items():
|
||||
path = str(pathlib.PurePosixPath(entry["subpath"]) / entry["dockerfile"]) if entry["subpath"] else entry["dockerfile"]
|
||||
try:
|
||||
labels = dockerfile_labels(read_file(path))
|
||||
text = read_file(path)
|
||||
except OSError as exc:
|
||||
errors.append(f"image '{name}': cannot read {path}: {exc}")
|
||||
continue
|
||||
missing = [key for key in REQUIRED_LABELS if not labels.get(key, "").strip("\"' ")]
|
||||
labels = dockerfile_labels(text)
|
||||
missing = [key for key in REQUIRED_LABELS if not labels.get(key, "").strip()]
|
||||
if missing:
|
||||
errors.append(f"image '{name}': {path} lacks the label(s) {', '.join(missing)}")
|
||||
errors.append(f"image '{name}': the last stage of {path} lacks the label(s) {', '.join(missing)}")
|
||||
doc = f"docs/images/{name}.md"
|
||||
link = labels.get("org.opencontainers.image.documentation", "")
|
||||
if link and not link.strip("\"' ").endswith(f"/{doc}"):
|
||||
errors.append(f"image '{name}': the documentation label must link to {doc}")
|
||||
if not exists(doc):
|
||||
errors.append(f"image '{name}': {doc} is missing (the documentation label links to it)")
|
||||
title = labels.get("org.opencontainers.image.title", "").strip("\"' ")
|
||||
source = labels.get("org.opencontainers.image.source", "").strip()
|
||||
link = labels.get("org.opencontainers.image.documentation", "").strip()
|
||||
if link and link != f"{source}/src/branch/main/{doc}":
|
||||
errors.append(f"image '{name}': the documentation label must be <source label>/src/branch/main/{doc}")
|
||||
if not size_of(doc):
|
||||
errors.append(f"image '{name}': {doc} is missing or empty (the documentation label links to it)")
|
||||
title = labels.get("org.opencontainers.image.title", "").strip()
|
||||
if title and title != name:
|
||||
errors.append(f"image '{name}': the title label is '{title}', expected the image name")
|
||||
base = labels.get("org.opencontainers.image.base.name", "").strip()
|
||||
releases, unused = dockerfile_releases(text)
|
||||
if base and sorted(set(releases)) and base not in {f"ubuntu:{r}" for r in releases}:
|
||||
errors.append(f"image '{name}': base.name label '{base}' differs from the FROM line(s) of {path}")
|
||||
return errors
|
||||
|
||||
|
||||
@@ -526,7 +548,7 @@ def run(argv):
|
||||
texts = {f: pathlib.Path(f).read_text() for f in CONSUMERS if pathlib.Path(f).exists()}
|
||||
errors = check_pins(matrix, texts)
|
||||
errors += check_job_images(matrix, lambda p: pathlib.Path(p).read_text(), image_yml)
|
||||
errors += check_image_metadata(matrix, lambda p: pathlib.Path(p).read_text(), lambda p: pathlib.Path(p).is_file())
|
||||
errors += check_image_metadata(matrix, lambda p: pathlib.Path(p).read_text(), file_size)
|
||||
workflows = sorted(pathlib.Path(WORKFLOW_DIR).glob("*.y*ml")) if pathlib.Path(WORKFLOW_DIR).is_dir() else []
|
||||
errors += check_foreign_images({str(f): f.read_text() for f in workflows})
|
||||
note = " (checked: " + ", ".join(texts) + ")"
|
||||
|
||||
Reference in new issue
Block a user