From 4bfe692e173ce5107fef14a6faa8ea273074988b Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Sun, 22 Nov 2026 15:56:42 +0100 Subject: [PATCH 1/6] Add support for reproducable sorted OCI image layers Signed-off-by: Tobias Wolf On-behalf-of: SAP --- .github/actions/setup/action.yml | 2 +- pyproject.toml | 2 +- src/gardenlinux/oci/image_manifest.py | 31 +++++++++++++++------------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index a12e2558..b574138d 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -4,7 +4,7 @@ description: Installs the given GardenLinux Python library inputs: version: description: GardenLinux Python library version - default: "1.0.6" + default: "1.0.7" python_version: description: Python version to setup default: "3.14" diff --git a/pyproject.toml b/pyproject.toml index f499bc21..40135c57 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "gardenlinux" -version = "1.0.6" +version = "1.0.7" description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames" authors = ["Garden Linux Maintainers "] license = "Apache-2.0" diff --git a/src/gardenlinux/oci/image_manifest.py b/src/gardenlinux/oci/image_manifest.py index 86cce988..6cc5af7a 100644 --- a/src/gardenlinux/oci/image_manifest.py +++ b/src/gardenlinux/oci/image_manifest.py @@ -340,27 +340,30 @@ def append_layer(self, layer: Layer | Dict[str, Any]) -> None: image_title = layer_dict["annotations"][ImageManifest.ANNOTATION_TITLE_KEY] existing_layer_index = 0 + sorted_layer_keys: Dict[str | int, int] = {image_title: -1} for existing_layer in self["layers"]: - if ImageManifest.ANNOTATION_TITLE_KEY not in existing_layer.get( - "annotations", {} - ): - raise RuntimeError( - f"Unexpected layer with missing annotation '{ImageManifest.ANNOTATION_TITLE_KEY}' found" - ) + existing_title = existing_layer["annotations"].get( + ImageManifest.ANNOTATION_TITLE_KEY + ) + + if not existing_title: + sorted_layer_keys[existing_layer_index] = existing_layer_index - if ( - image_title - == existing_layer["annotations"][ImageManifest.ANNOTATION_TITLE_KEY] - ): - break + if image_title != existing_title: + sorted_layer_keys[existing_title] = existing_layer_index existing_layer_index += 1 - if len(self["layers"]) > existing_layer_index: - self["layers"].pop(existing_layer_index) + layers_sorted = [] + + for key in sorted(sorted_layer_keys): + if sorted_layer_keys[key] >= 0: + layers_sorted.append(self["layers"][sorted_layer_keys[key]]) + else: + layers_sorted.append(layer_dict) - self["layers"].append(layer_dict) + self["layers"] = layers_sorted def write_metadata_file(self, manifest_file_path_name: PathLike[str] | str) -> None: """ From bc857909a7d578e3f8cbbb0a4b977e928c3d8216 Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Wed, 25 Nov 2026 10:44:26 +0100 Subject: [PATCH 2/6] Fix incorrectly shortened OCI image manifest commit values breaking digests Signed-off-by: Tobias Wolf On-behalf-of: SAP --- src/gardenlinux/oci/image_manifest.py | 26 ++++++++--------- src/gardenlinux/oci/manifest.py | 40 ++++++++++++++++++++------- tests/oci/test_image_manifest.py | 11 +++++--- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/gardenlinux/oci/image_manifest.py b/src/gardenlinux/oci/image_manifest.py index 6cc5af7a..6c4d8c34 100644 --- a/src/gardenlinux/oci/image_manifest.py +++ b/src/gardenlinux/oci/image_manifest.py @@ -8,7 +8,6 @@ from pathlib import Path from typing import Any, Dict -from oras.defaults import annotation_title as ANNOTATION_TITLE from oras.defaults import default_manifest_media_type as IMAGE_MANIFEST_MEDIA_TYPE from ..constants import GL_DISTRIBUTION_NAME, GL_REPOSITORY_URL @@ -50,10 +49,6 @@ class ImageManifest(Manifest): """ OCI image manifest GardenLinux commit hash annotation """ - ANNOTATION_DESCRIPTION_KEY = "org.opencontainers.image.description" - """ - OCI image manifest description annotation - """ ANNOTATION_FEATURE_SET_KEY = "io.gardenlinux.image.feature-set" """ OCI image manifest Garden Linux feature set annotation @@ -66,10 +61,6 @@ class ImageManifest(Manifest): """ OCI image manifest Garden Linux source repository URL annotation """ - ANNOTATION_TITLE_KEY = ANNOTATION_TITLE - """ - OCI image manifest title annotation - """ ANNOTATION_VERSION_KEY = "org.opencontainers.image.version" """ OCI image manifest GardenLinux version annotation @@ -122,7 +113,7 @@ def artifact_base_name(self) -> str: ): return self["annotations"][ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY] # type: ignore[no-any-return] - return f"{self.cname}-{self.arch}-{self.version}-{self.commit}" + return f"{self.cname}-{self.arch}-{self.version}-{self.commit_id}" @artifact_base_name.setter def artifact_base_name(self, value: str) -> None: @@ -139,7 +130,10 @@ def artifact_base_name(self, value: str) -> None: self.cname = abn_object.cname self.arch = abn_object.arch self.version = abn_object.version - self.commit = abn_object.commit_id + # Reuse the whole commit hash if available, otherwise fall back to the commit ID + self.commit = ( + abn_object.commit_hash if abn_object.commit_hash else abn_object.commit_id + ) @property def cname(self) -> str: @@ -257,12 +251,16 @@ def extended_dict(self) -> Dict[str, Any]: ) if ImageManifest.ANNOTATION_VERSION_KEY in manifest_annotations: - manifest_annotations[ImageManifest.ANNOTATION_VERSION_KEY] = self.version + manifest_annotations[Manifest.ANNOTATION_VERSION_DEPRECATED_KEY] = ( + self.version + ) manifest_description += " " + self.version if ImageManifest.ANNOTATION_COMMIT_KEY in manifest_annotations: - manifest_annotations[ImageManifest.ANNOTATION_COMMIT_KEY] = self.commit - manifest_description += f" ({self.commit})" + manifest_annotations[Manifest.ANNOTATION_COMMIT_DEPRECATED_KEY] = ( + self.commit + ) + manifest_description += f" ({self.commit_id})" if ( ImageManifest.ANNOTATION_CNAME_KEY in manifest_annotations diff --git a/src/gardenlinux/oci/manifest.py b/src/gardenlinux/oci/manifest.py index 7544006c..b308085f 100644 --- a/src/gardenlinux/oci/manifest.py +++ b/src/gardenlinux/oci/manifest.py @@ -10,6 +10,7 @@ from hashlib import sha256 from typing import Any, Dict +from oras.defaults import annotation_title as ANNOTATION_TITLE from oras.defaults import unknown_config_media_type as UNKNOWN_CONFIG_MEDIA_TYPE from oras.oci import EmptyManifest @@ -37,6 +38,14 @@ class Manifest(dict): # type: ignore[type-arg] """ OCI image manifest GardenLinux commit hash deprecated annotation """ + ANNOTATION_DESCRIPTION_KEY = "org.opencontainers.image.description" + """ + OCI image manifest description annotation + """ + ANNOTATION_TITLE_KEY = ANNOTATION_TITLE + """ + OCI image manifest title annotation + """ ANNOTATION_VERSION_KEY = "io.gardenlinux.manifest.version" """ OCI image manifest GardenLinux version annotation @@ -70,9 +79,9 @@ def __init__(self, *args: Any, **kwargs: Any): @property def commit(self) -> str: """ - Returns the GardenLinux Git commit ID of the OCI manifest. + Returns the GardenLinux Git commit hash of the OCI manifest. - :return: (str) OCI GardenLinux Git commit ID + :return: (str) OCI GardenLinux Git commit hash :since: 0.7.0 """ @@ -83,19 +92,14 @@ def commit(self) -> str: f"Unexpected manifest with missing config annotation '{annotation_commit_key}' found" ) - commit_id: str = self["annotations"][annotation_commit_key] - - if len(commit_id) == COMMIT_HASH_LENGTH: - commit_id = commit_id[:COMMIT_ID_LENGTH] - - return commit_id + return self["annotations"][annotation_commit_key] # type: ignore[no-any-return] @commit.setter def commit(self, value: str) -> None: """ - Sets the GardenLinux Git commit ID of the OCI manifest. + Sets the GardenLinux Git commit hash of the OCI manifest. - :param value: OCI GardenLinux Git commit ID + :param value: OCI GardenLinux Git commit hash :since: 0.7.0 """ @@ -103,6 +107,22 @@ def commit(self, value: str) -> None: self._ensure_annotations_dict() self["annotations"][self.__class__.ANNOTATION_COMMIT_KEY] = value + @property + def commit_id(self) -> str: + """ + Returns the GardenLinux Git commit ID of the OCI manifest. + + :return: (str) OCI GardenLinux Git commit ID + :since: 1.0.8 + """ + + commit_id = self.commit + + if len(commit_id) == COMMIT_HASH_LENGTH: + commit_id = commit_id[:COMMIT_ID_LENGTH] + + return commit_id + @property def config_json(self) -> bytes: """ diff --git a/tests/oci/test_image_manifest.py b/tests/oci/test_image_manifest.py index 7fd6842b..ce6a3cf4 100644 --- a/tests/oci/test_image_manifest.py +++ b/tests/oci/test_image_manifest.py @@ -60,12 +60,15 @@ def test_ImageManifest_commit() -> None: # Assert with pytest.raises(RuntimeError): - assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT + assert empty_manifest.commit_id == TEST_GARDENLINUX_COMMIT_SHORT - empty_manifest.commit = TEST_GARDENLINUX_COMMIT_SHORT - assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + empty_manifest.commit = TEST_GARDENLINUX_COMMIT + assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT + assert empty_manifest.commit_id == TEST_GARDENLINUX_COMMIT_SHORT - assert manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + assert manifest.commit == TEST_GARDENLINUX_COMMIT + assert manifest.commit_id == TEST_GARDENLINUX_COMMIT_SHORT def test_ImageManifest_cname() -> None: From 25e84a903531fe7c01e1e5a2e38240b85afa176c Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Wed, 25 Nov 2026 11:37:21 +0100 Subject: [PATCH 3/6] Code cleanup Signed-off-by: Tobias Wolf On-behalf-of: SAP --- src/gardenlinux/constants.py | 1 + src/gardenlinux/oci/__main__.py | 13 +++++- src/gardenlinux/oci/image_manifest.py | 50 +++++++++++++++++------ src/gardenlinux/oci/index.py | 33 +++++++++++---- src/gardenlinux/oci/platform.py | 15 +++++-- src/gardenlinux/oci/repository.py | 16 ++++++-- src/gardenlinux/oci/schemas.py | 8 ++-- tests/oci/test_image_manifest.py | 4 +- tests/oci/test_index.py | 58 ++++++++++++++++++++++----- 9 files changed, 155 insertions(+), 43 deletions(-) diff --git a/src/gardenlinux/constants.py b/src/gardenlinux/constants.py index d4f7b101..7538884e 100644 --- a/src/gardenlinux/constants.py +++ b/src/gardenlinux/constants.py @@ -159,6 +159,7 @@ GL_RELEASE_ID = "gardenlinux" GL_REPOSITORY_URL = "https://github.com/gardenlinux/gardenlinux" GL_SUPPORT_URL = "https://github.com/gardenlinux/gardenlinux" +GL_VENDOR_NAME = "SAP SE" OCI_ANNOTATION_SIGNATURE_KEY = "io.gardenlinux.oci.signature" OCI_ANNOTATION_SIGNED_STRING_KEY = "io.gardenlinux.oci.signed-string" diff --git a/src/gardenlinux/oci/__main__.py b/src/gardenlinux/oci/__main__.py index 135cc7ab..d864828c 100755 --- a/src/gardenlinux/oci/__main__.py +++ b/src/gardenlinux/oci/__main__.py @@ -357,6 +357,7 @@ def get_parser() -> argparse.ArgumentParser: _add_oci_index_args_to_parser(add_image_to_index_parser) _add_oci_repository_to_parser(add_image_to_index_parser) _add_oci_tag_to_parser(add_image_to_index_parser) + _add_oci_platform_to_parser(add_image_to_index_parser, False) _add_oci_insecure_to_parser(add_image_to_index_parser) _add_additional_tag_list_to_parser(add_image_to_index_parser) @@ -491,12 +492,22 @@ def main() -> None: manifest = manifest_repository.read_manifest() + if args.platform and ImageManifest.ANNOTATION_OS_ID_KEY not in manifest: + platform_data = args.platform.split("/", 2) + manifest[ImageManifest.ANNOTATION_OS_ID_KEY] = platform_data[0] + + if ( + len(platform_data) > 1 + and ImageManifest.ANNOTATION_ARCH_KEY not in manifest + ): + manifest[ImageManifest.ANNOTATION_ARCH_KEY] = platform_data[1] + index_resource = Repository( f"{args.index}:{args.index_tag}", insecure=args.insecure ) image_index = index_resource.read_or_generate_index() - image_index.append_manifest(manifest) + image_index.add_manifest(manifest) index_resource.push_index(image_index) index_resource.push_index_for_tags(image_index, args.additional_tag) diff --git a/src/gardenlinux/oci/image_manifest.py b/src/gardenlinux/oci/image_manifest.py index 6c4d8c34..9da69fc1 100644 --- a/src/gardenlinux/oci/image_manifest.py +++ b/src/gardenlinux/oci/image_manifest.py @@ -10,7 +10,12 @@ from oras.defaults import default_manifest_media_type as IMAGE_MANIFEST_MEDIA_TYPE -from ..constants import GL_DISTRIBUTION_NAME, GL_REPOSITORY_URL +from ..constants import ( + GL_DISTRIBUTION_NAME, + GL_RELEASE_ID, + GL_REPOSITORY_URL, + GL_VENDOR_NAME, +) from ..features import ArtifactBaseName, Flavor from .layer import Layer from .manifest import Manifest @@ -47,7 +52,7 @@ class ImageManifest(Manifest): """ ANNOTATION_COMMIT_KEY = "org.opencontainers.image.revision" """ - OCI image manifest GardenLinux commit hash annotation + OCI image manifest commit hash annotation """ ANNOTATION_FEATURE_SET_KEY = "io.gardenlinux.image.feature-set" """ @@ -57,13 +62,21 @@ class ImageManifest(Manifest): """ OCI image manifest Garden Linux feature set deprecated annotation """ + ANNOTATION_OS_ID_KEY = "io.gardenlinux.image.os-id" + """ + OCI image manifest Garden Linux operating system annotation + """ ANNOTATION_SOURCE_REPO_KEY = "org.opencontainers.image.source" """ - OCI image manifest Garden Linux source repository URL annotation + OCI image manifest source repository URL annotation + """ + ANNOTATION_VENDOR_KEY = "org.opencontainers.image.vendor" + """ + OCI image manifest vendor annotation """ ANNOTATION_VERSION_KEY = "org.opencontainers.image.version" """ - OCI image manifest GardenLinux version annotation + OCI image manifest version annotation """ IMAGE_MANIFEST_MEDIA_TYPE = IMAGE_MANIFEST_MEDIA_TYPE """ @@ -242,7 +255,11 @@ def extended_dict(self) -> Dict[str, Any]: manifest_description = manifest_annotations[ImageManifest.ANNOTATION_TITLE_KEY] - if ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations: + annotation_arch_key = ImageManifest.ANNOTATION_ARCH_KEY + annotation_commit_key = ImageManifest.ANNOTATION_COMMIT_KEY + annotation_version_key = ImageManifest.ANNOTATION_VERSION_KEY + + if annotation_arch_key in manifest_annotations: manifest_description += f" ({self.arch})" if ImageManifest.ANNOTATION_SOURCE_REPO_KEY not in manifest_annotations: @@ -250,13 +267,13 @@ def extended_dict(self) -> Dict[str, Any]: GL_REPOSITORY_URL ) - if ImageManifest.ANNOTATION_VERSION_KEY in manifest_annotations: + if annotation_version_key in manifest_annotations: manifest_annotations[Manifest.ANNOTATION_VERSION_DEPRECATED_KEY] = ( self.version ) manifest_description += " " + self.version - if ImageManifest.ANNOTATION_COMMIT_KEY in manifest_annotations: + if annotation_commit_key in manifest_annotations: manifest_annotations[Manifest.ANNOTATION_COMMIT_DEPRECATED_KEY] = ( self.commit ) @@ -264,17 +281,26 @@ def extended_dict(self) -> Dict[str, Any]: if ( ImageManifest.ANNOTATION_CNAME_KEY in manifest_annotations - and ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations - and ImageManifest.ANNOTATION_VERSION_KEY in manifest_annotations - and ImageManifest.ANNOTATION_COMMIT_KEY in manifest_annotations + and annotation_arch_key in manifest_annotations + and annotation_version_key in manifest_annotations + and annotation_commit_key in manifest_annotations ): manifest_annotations[ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY] = ( self.artifact_base_name ) + manifest_annotations[ImageManifest.ANNOTATION_CNAME_DEPRECATED_KEY] = ( self.artifact_base_name ) + if ImageManifest.ANNOTATION_OS_ID_KEY not in manifest_annotations: + manifest_annotations[ImageManifest.ANNOTATION_OS_ID_KEY] = GL_RELEASE_ID + + annotation_vendor_key = ImageManifest.ANNOTATION_VENDOR_KEY + + if annotation_vendor_key not in manifest_annotations: + manifest_annotations[annotation_vendor_key] = GL_VENDOR_NAME + if ImageManifest.ANNOTATION_FEATURE_SET_KEY in manifest_annotations: manifest_description += ( " - " + manifest_annotations[ImageManifest.ANNOTATION_FEATURE_SET_KEY] @@ -285,7 +311,7 @@ def extended_dict(self) -> Dict[str, Any]: manifest_description ) - if ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations: + if annotation_arch_key in manifest_annotations: # @TODO: Deprectated annotation manifest_annotations["architecture"] = self.arch @@ -317,7 +343,7 @@ def layers_as_dict(self) -> Dict[str, Any]: return layers - def append_layer(self, layer: Layer | Dict[str, Any]) -> None: + def add_layer(self, layer: Layer | Dict[str, Any]) -> None: """ Appends the given OCI image manifest layer to the manifest diff --git a/src/gardenlinux/oci/index.py b/src/gardenlinux/oci/index.py index b9b7f3f9..de48c7b0 100644 --- a/src/gardenlinux/oci/index.py +++ b/src/gardenlinux/oci/index.py @@ -87,7 +87,7 @@ def manifests_as_dict(self) -> Dict[str, Dict[str, Any]]: return manifests - def append_manifest(self, manifest: Dict[str, Any]) -> None: + def add_manifest(self, manifest: Dict[str, Any]) -> None: """ Appends the given OCI image manifest to the index @@ -137,13 +137,32 @@ def append_manifest(self, manifest: Dict[str, Any]) -> None: index_manifest["size"] = image_manifest.size index_manifest["annotations"] = image_annotations + annotation_arch_key = ImageManifest.ANNOTATION_ARCH_KEY + annotation_os_id_key = ImageManifest.ANNOTATION_OS_ID_KEY + annotation_version_key = ImageManifest.ANNOTATION_VERSION_KEY + if ( - ImageManifest.ANNOTATION_ARCH_KEY in image_annotations - and ImageManifest.ANNOTATION_VERSION_KEY in image_annotations + annotation_arch_key in image_annotations + or annotation_os_id_key in image_annotations + or annotation_version_key in image_annotations ): - index_manifest["platform"] = new_platform( - image_annotations[ImageManifest.ANNOTATION_ARCH_KEY], - image_annotations[ImageManifest.ANNOTATION_VERSION_KEY], - ) + kwargs = {} + + annotation_os_id_key = ImageManifest.ANNOTATION_OS_ID_KEY + + if annotation_os_id_key in image_annotations: + kwargs["os_name"] = image_annotations[annotation_os_id_key] + + annotation_version_key = ImageManifest.ANNOTATION_VERSION_KEY + + if annotation_version_key in image_annotations: + kwargs["os_version"] = image_annotations[annotation_version_key] + + annotation_arch_key = ImageManifest.ANNOTATION_ARCH_KEY + + if annotation_arch_key in image_annotations: + kwargs["architecture"] = image_annotations[annotation_arch_key] + + index_manifest["platform"] = new_platform(**kwargs) self["manifests"].append(index_manifest) diff --git a/src/gardenlinux/oci/platform.py b/src/gardenlinux/oci/platform.py index b18774cf..285623a4 100644 --- a/src/gardenlinux/oci/platform.py +++ b/src/gardenlinux/oci/platform.py @@ -6,7 +6,9 @@ from .schemas import empty_platform -def new_platform(architecture: str, version: str) -> Dict[str, Any]: +def new_platform( + os_name: str = "", os_version: str = "", architecture: str = "" +) -> Dict[str, Any]: """ Create an OCI image platform for the architecture and version given. @@ -18,7 +20,14 @@ def new_platform(architecture: str, version: str) -> Dict[str, Any]: """ platform = deepcopy(empty_platform) - platform["architecture"] = architecture - platform["os.version"] = version + + if architecture: + platform["architecture"] = architecture + + if os_name: + platform["os"] = os_name + + if os_version: + platform["os.version"] = os_version return platform diff --git a/src/gardenlinux/oci/repository.py b/src/gardenlinux/oci/repository.py index 99f71e24..d3ce8c06 100644 --- a/src/gardenlinux/oci/repository.py +++ b/src/gardenlinux/oci/repository.py @@ -23,7 +23,11 @@ from oras.utils import extract_targz, make_targz from requests import HTTPError, Response -from ..constants import OCI_IMAGE_INDEX_MEDIA_TYPE +from ..constants import ( + GL_CONTAINER_REGISTRY_BASE_URL, + GL_DISTRIBUTION_NAME, + OCI_IMAGE_INDEX_MEDIA_TYPE, +) from ..features import ArtifactBaseName from ..logger import LoggerSetup from .image_manifest import ImageManifest @@ -173,7 +177,11 @@ def generate_index(self) -> Index: :since: 0.7.0 """ - return Index() + index = Index() + index[Manifest.ANNOTATION_DESCRIPTION_KEY] = ( + f"{GL_DISTRIBUTION_NAME} ({GL_CONTAINER_REGISTRY_BASE_URL})" + ) + return index def generate_manifest( self, @@ -292,7 +300,7 @@ def push_index_from_directory( continue - index.append_manifest(manifest) + index.add_manifest(manifest) self._logger.info( f"Index appended locally {manifest['annotations'][ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY]}" @@ -441,7 +449,7 @@ def push_manifest_and_artifacts( file_path_name = Path(make_targz(file_path_name)) cleanup_blob = True - manifest.append_layer(layer) + manifest.add_layer(layer) layer_dict = layer.dict self._logger.debug( diff --git a/src/gardenlinux/oci/schemas.py b/src/gardenlinux/oci/schemas.py index bb9a59e4..c048ab12 100644 --- a/src/gardenlinux/oci/schemas.py +++ b/src/gardenlinux/oci/schemas.py @@ -5,6 +5,8 @@ # TODO: add more schemas, and validate dicts with schemas before accessing them. +from ..constants import GL_RELEASE_ID + schema_url = "http://json-schema.org/draft-07/schema" platform_properties = { @@ -41,11 +43,7 @@ "additionalProperties": True, } -empty_platform = { - "architecture": "", - "os": "gardenlinux", - "os.version": "experimental", -} +empty_platform = {"os": GL_RELEASE_ID} empty_manifest_metadata = { "mediaType": "application/vnd.oci.image.manifest.v1+json", diff --git a/tests/oci/test_image_manifest.py b/tests/oci/test_image_manifest.py index ce6a3cf4..141e7085 100644 --- a/tests/oci/test_image_manifest.py +++ b/tests/oci/test_image_manifest.py @@ -147,7 +147,7 @@ def test_ImageManifest_layer(tmp_path: Path) -> None: layer = Layer(blob) manifest = ImageManifest() - manifest.append_layer(layer) + manifest.add_layer(layer) # Assert @@ -156,7 +156,7 @@ def test_ImageManifest_layer(tmp_path: Path) -> None: # Assert with pytest.raises(RuntimeError): - manifest.append_layer({"test": "invalid"}) + manifest.add_layer({"test": "invalid"}) def test_ImageManifest_version() -> None: diff --git a/tests/oci/test_index.py b/tests/oci/test_index.py index ec8eeda3..304cbd3a 100644 --- a/tests/oci/test_index.py +++ b/tests/oci/test_index.py @@ -5,6 +5,8 @@ from gardenlinux.oci import ImageManifest, Index +from ..constants import TEST_GARDENLINUX_RELEASE_MAJOR + def test_index_init_and_json() -> None: """Ensure Index init works correctly""" @@ -49,8 +51,46 @@ def test_manifests_as_dict() -> None: assert result[manifest_no_abn_key] == manifest_no_abn -def test_append_manifest_replace() -> None: - """Ensure append_manifest replaces existing manifest with same cname.""" +def test_add_manifests() -> None: + """Ensure add_manifest handles manifests correctly.""" + # Arrange + idx = Index() + + idx["manifests"] = [ + { + "annotations": { + ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY: "other", + "digest": "sha256:other", + }, + }, + ] + + new_manifest = { + "annotations": { + ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY: "new", + ImageManifest.ANNOTATION_ARCH_KEY: "arm64", + ImageManifest.ANNOTATION_OS_ID_KEY: "linux", + ImageManifest.ANNOTATION_VERSION_KEY: TEST_GARDENLINUX_RELEASE_MAJOR, + "digest": "sha256:new", + }, + } + + # Act + idx.add_manifest(new_manifest) + + result = idx.manifests_as_dict + + # Assert + assert "new" in result + assert result["new"]["platform"] == { + "architecture": "arm64", + "os": "linux", + "os.version": TEST_GARDENLINUX_RELEASE_MAJOR, + } + + +def test_add_manifest_replace() -> None: + """Ensure add_manifest replaces existing manifest with same cname.""" # Arrange idx = Index() @@ -77,7 +117,7 @@ def test_append_manifest_replace() -> None: } # Act - idx.append_manifest(new_manifest) + idx.add_manifest(new_manifest) # Assert abns = [ @@ -93,7 +133,7 @@ def test_append_manifest_replace() -> None: ) -def test_append_manifest_cname_not_found() -> None: +def test_add_manifest_cname_not_found() -> None: """Test appending new manifest if cname isn't found.""" # Arrange idx = Index() @@ -115,7 +155,7 @@ def test_append_manifest_cname_not_found() -> None: } # Act - idx.append_manifest(new_manifest) + idx.add_manifest(new_manifest) # Assert abns = [ @@ -134,10 +174,10 @@ def test_append_manifest_cname_not_found() -> None: ], ) def test_append_accept_all_manifests(manifest: Dict[str, Any]) -> None: - """Test proper error handling for invalid append_manifest input.""" + """Test proper error handling for invalid add_manifest input.""" # Arrange idx = Index() - idx.append_manifest(manifest) + idx.add_manifest(manifest) # Act assert len(idx.manifests_as_dict) == 1 @@ -150,10 +190,10 @@ def test_append_accept_all_manifests(manifest: Dict[str, Any]) -> None: ], ) def test_append_invalid_input_raises(bad_manifest: Dict[str, Any]) -> None: - """Test proper error handling for invalid append_manifest input.""" + """Test proper error handling for invalid add_manifest input.""" # Arrange idx = Index() # Act / Assert with pytest.raises((TypeError, ValueError)): - idx.append_manifest(bad_manifest) + idx.add_manifest(bad_manifest) From 441120b1ff729811e605e6293f71ff7b8786c5fc Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Thu, 26 Nov 2026 10:46:49 +0100 Subject: [PATCH 4/6] Fix incorrectly attached values in `gl-oci add-image-to-index` command Signed-off-by: Tobias Wolf On-behalf-of: SAP --- src/gardenlinux/oci/__main__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/gardenlinux/oci/__main__.py b/src/gardenlinux/oci/__main__.py index d864828c..1ef2d5d6 100755 --- a/src/gardenlinux/oci/__main__.py +++ b/src/gardenlinux/oci/__main__.py @@ -491,16 +491,25 @@ def main() -> None: ) manifest = manifest_repository.read_manifest() + manifest_annotations = manifest["annotations"] - if args.platform and ImageManifest.ANNOTATION_OS_ID_KEY not in manifest: + if ( + args.platform + and ImageManifest.ANNOTATION_OS_ID_KEY not in manifest_annotations + ): platform_data = args.platform.split("/", 2) - manifest[ImageManifest.ANNOTATION_OS_ID_KEY] = platform_data[0] + + manifest_annotations[ImageManifest.ANNOTATION_OS_ID_KEY] = ( + platform_data[0] + ) if ( len(platform_data) > 1 - and ImageManifest.ANNOTATION_ARCH_KEY not in manifest + and ImageManifest.ANNOTATION_ARCH_KEY not in manifest_annotations ): - manifest[ImageManifest.ANNOTATION_ARCH_KEY] = platform_data[1] + manifest_annotations[ImageManifest.ANNOTATION_ARCH_KEY] = ( + platform_data[1] + ) index_resource = Repository( f"{args.index}:{args.index_tag}", insecure=args.insecure From bff38134f0a662eb40e517cdc74e7eca913264e0 Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Thu, 26 Nov 2026 11:16:56 +0100 Subject: [PATCH 5/6] Improve `gardenlinux.oci.Index.add_manifest()` Signed-off-by: Tobias Wolf --- src/gardenlinux/oci/index.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/gardenlinux/oci/index.py b/src/gardenlinux/oci/index.py index de48c7b0..34e56708 100644 --- a/src/gardenlinux/oci/index.py +++ b/src/gardenlinux/oci/index.py @@ -104,28 +104,22 @@ def add_manifest(self, manifest: Dict[str, Any]) -> None: if image_manifest.size < 1: raise ValueError("Manifest to be added is empty") - existing_manifest_index = 0 - - for existing_manifest in self["manifests"]: - if ( + if ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY in manifest.get( + "annotations", {} + ): + image_abn = manifest["annotations"][ ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY - not in existing_manifest.get("annotations", {}) - ): - existing_manifest_index += 1 - continue - - if ( - image_manifest.artifact_base_name - == existing_manifest["annotations"][ - ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY - ] - ): - break + ] - existing_manifest_index += 1 + manifests = [] - if len(self["manifests"]) > existing_manifest_index: - self["manifests"].pop(existing_manifest_index) + for existing_manifest in self["manifests"]: + if image_abn != existing_manifest.get("annotations", {}).get( + ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY + ): + manifests.append(existing_manifest) + else: + manifests = self["manifests"] image_manifest_data = image_manifest.extended_dict @@ -165,4 +159,5 @@ def add_manifest(self, manifest: Dict[str, Any]) -> None: index_manifest["platform"] = new_platform(**kwargs) - self["manifests"].append(index_manifest) + manifests.append(index_manifest) + self["manifests"] = manifests From a6eaeac4d6701328da0a2b86103ea839eecbe66a Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Thu, 26 Nov 2026 11:58:17 +0100 Subject: [PATCH 6/6] Move OCI `config` related methods to `gardenlinux.oci.ImageManifest` Signed-off-by: Tobias Wolf On-behalf-of: SAP --- src/gardenlinux/oci/image_manifest.py | 56 +++++++++++++++++++++++++++ src/gardenlinux/oci/manifest.py | 45 ++------------------- src/gardenlinux/oci/repository.py | 28 +++++++------- src/gardenlinux/oci/schemas.py | 3 +- tests/oci/test_index.py | 2 +- 5 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/gardenlinux/oci/image_manifest.py b/src/gardenlinux/oci/image_manifest.py index 9da69fc1..777ae5dd 100644 --- a/src/gardenlinux/oci/image_manifest.py +++ b/src/gardenlinux/oci/image_manifest.py @@ -4,11 +4,14 @@ OCI image manifest """ +from hashlib import sha256 +from json import dumps as json_dumps from os import PathLike from pathlib import Path from typing import Any, Dict from oras.defaults import default_manifest_media_type as IMAGE_MANIFEST_MEDIA_TYPE +from oras.defaults import unknown_config_media_type as UNKNOWN_CONFIG_MEDIA_TYPE from ..constants import ( GL_DISTRIBUTION_NAME, @@ -83,6 +86,23 @@ class ImageManifest(Manifest): OCI image manifest media type """ + def __init__(self, *args: Any, **kwargs: Any): + """ + Constructor __init__(Manifest) + + :since: 0.7.0 + """ + + Manifest.__init__(self, *args, **kwargs) + + self._config_bytes = b"{}" + + if "config" not in self: + self["config"] = {} + + if "layers" not in self: + self["layers"] = [] + @property def arch(self) -> str: """ @@ -177,6 +197,17 @@ def cname(self, value: str) -> None: self._ensure_annotations_dict() self["annotations"][ImageManifest.ANNOTATION_CNAME_KEY] = value + @property + def config_json(self) -> bytes: + """ + Returns the OCI image manifest config. + + :return: (bytes) OCI image manifest config + :since: 0.7.0 + """ + + return self._config_bytes + @property def feature_set(self) -> str: """ @@ -389,6 +420,31 @@ def add_layer(self, layer: Layer | Dict[str, Any]) -> None: self["layers"] = layers_sorted + def config_from_dict( + self, config: Dict[str, Any], annotations: Dict[str, Any] + ) -> None: + """ + Write a new OCI configuration to file, and generate oci metadata for it. + + For reference see https://github.com/opencontainers/image-spec/blob/main/config.md + annotations, mediatype, size, digest are not part of digest and size calculation, + and therefore must be attached to the output dict and not written to the file. + + :param config: dict with custom configuration (the payload of the configuration) + :param annotations: dict with custom annotations to be attached to metadata part of config + + :since: 0.7.0 + """ + + self._config_bytes = json_dumps(config).encode("utf-8") + + config["annotations"] = annotations + config["mediaType"] = UNKNOWN_CONFIG_MEDIA_TYPE + config["size"] = len(self._config_bytes) + config["digest"] = f"sha256:{sha256(self._config_bytes).hexdigest()}" + + self["config"] = config + def write_metadata_file(self, manifest_file_path_name: PathLike[str] | str) -> None: """ Create OCI image manifest metadata and write it to the file given. diff --git a/src/gardenlinux/oci/manifest.py b/src/gardenlinux/oci/manifest.py index b308085f..1e81a26c 100644 --- a/src/gardenlinux/oci/manifest.py +++ b/src/gardenlinux/oci/manifest.py @@ -11,10 +11,9 @@ from typing import Any, Dict from oras.defaults import annotation_title as ANNOTATION_TITLE -from oras.defaults import unknown_config_media_type as UNKNOWN_CONFIG_MEDIA_TYPE -from oras.oci import EmptyManifest from ..constants import COMMIT_HASH_LENGTH, COMMIT_ID_LENGTH +from .schemas import empty_manifest_metadata class Manifest(dict): # type: ignore[type-arg] @@ -64,9 +63,7 @@ def __init__(self, *args: Any, **kwargs: Any): dict.__init__(self) - self._config_bytes = b"{}" - - self.update(deepcopy(EmptyManifest)) + self.update(deepcopy(empty_manifest_metadata)) for arg in args: if not isinstance(arg, Mapping): @@ -89,7 +86,7 @@ def commit(self) -> str: if annotation_commit_key not in self.get("annotations", {}): raise RuntimeError( - f"Unexpected manifest with missing config annotation '{annotation_commit_key}' found" + f"Unexpected manifest with missing annotation '{annotation_commit_key}' found" ) return self["annotations"][annotation_commit_key] # type: ignore[no-any-return] @@ -123,17 +120,6 @@ def commit_id(self) -> str: return commit_id - @property - def config_json(self) -> bytes: - """ - Returns the OCI image manifest config. - - :return: (bytes) OCI image manifest config - :since: 0.7.0 - """ - - return self._config_bytes - @property def digest(self) -> str: """ @@ -228,31 +214,6 @@ def version(self, value: str) -> None: self._ensure_annotations_dict() self["annotations"][self.__class__.ANNOTATION_VERSION_KEY] = value - def config_from_dict( - self, config: Dict[str, Any], annotations: Dict[str, Any] - ) -> None: - """ - Write a new OCI configuration to file, and generate oci metadata for it. - - For reference see https://github.com/opencontainers/image-spec/blob/main/config.md - annotations, mediatype, size, digest are not part of digest and size calculation, - and therefore must be attached to the output dict and not written to the file. - - :param config: dict with custom configuration (the payload of the configuration) - :param annotations: dict with custom annotations to be attached to metadata part of config - - :since: 0.7.0 - """ - - self._config_bytes = json.dumps(config).encode("utf-8") - - config["annotations"] = annotations - config["mediaType"] = UNKNOWN_CONFIG_MEDIA_TYPE - config["size"] = len(self._config_bytes) - config["digest"] = f"sha256:{sha256(self._config_bytes).hexdigest()}" - - self["config"] = config - def _ensure_annotations_dict(self) -> None: """ Ensure the annotations dictionary is initialized. diff --git a/src/gardenlinux/oci/repository.py b/src/gardenlinux/oci/repository.py index d3ce8c06..e25137bb 100644 --- a/src/gardenlinux/oci/repository.py +++ b/src/gardenlinux/oci/repository.py @@ -203,8 +203,6 @@ def generate_manifest( manifest.version = version manifest.commit = commit - manifest.config_from_dict({}, {}) - return manifest def _get_index_without_response_parsing(self) -> Response: @@ -358,23 +356,23 @@ def push_manifest( fd, config_file = mkstemp() - try: - with fdopen(fd, mode="wb") as fp: - fp.write(manifest.config_json) + manifest_url = f"{self._repository_url}:{self._repository_tag}" - self._check_200_response( - self.upload_blob(config_file, repository_name, manifest["config"]) - ) + if isinstance(manifest, ImageManifest): + try: + with fdopen(fd, mode="wb") as fp: + fp.write(manifest.config_json) - self._logger.debug( - f"gardenlinux.oci.Repository.push_manifest(): Successfully pushed config for {repository_name}" - ) - finally: - Path(config_file).unlink() + self._check_200_response( + self.upload_blob(config_file, repository_name, manifest["config"]) + ) - manifest_url = f"{self._repository_url}:{self._repository_tag}" + self._logger.debug( + f"gardenlinux.oci.Repository.push_manifest(): Successfully pushed config for {repository_name}" + ) + finally: + Path(config_file).unlink() - if isinstance(manifest, ImageManifest): manifest_url += f"-{manifest.flavor}-{manifest.version}-{manifest.commit}-{manifest.arch}" elif not self._repository_tag: raise RuntimeError("Failed to push OCI manifest with invalid tag") diff --git a/src/gardenlinux/oci/schemas.py b/src/gardenlinux/oci/schemas.py index c048ab12..2f6275bf 100644 --- a/src/gardenlinux/oci/schemas.py +++ b/src/gardenlinux/oci/schemas.py @@ -46,9 +46,8 @@ empty_platform = {"os": GL_RELEASE_ID} empty_manifest_metadata = { + "schemaVersion": 2, "mediaType": "application/vnd.oci.image.manifest.v1+json", - "digest": "", - "size": 0, "annotations": {}, } diff --git a/tests/oci/test_index.py b/tests/oci/test_index.py index 304cbd3a..2a36d1cf 100644 --- a/tests/oci/test_index.py +++ b/tests/oci/test_index.py @@ -36,7 +36,7 @@ def test_manifests_as_dict() -> None: } manifest_no_abn_key = ( - "sha256:59c0c0fff3045cd6620473355babb85fc8b83cf93ee461a9d2519d333cb2a8d7" + "sha256:fd3685b29dfd577fd6369bd8f43ae887fb0570ff4650b96e5e6b427a972367e3" ) manifest_no_abn = {"annotations": {"digest": "sha256:def"}}