Skip to content

Commit c6af3e4

Browse files
committed
Code cleanup
Signed-off-by: Tobias Wolf <wolf@b1-systems.de> On-behalf-of: SAP <tobias.wolf@sap.com>
1 parent 81582ce commit c6af3e4

9 files changed

Lines changed: 132 additions & 31 deletions

File tree

‎src/gardenlinux/constants.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
GL_RELEASE_ID = "gardenlinux"
160160
GL_REPOSITORY_URL = "https://github.com/gardenlinux/gardenlinux"
161161
GL_SUPPORT_URL = "https://github.com/gardenlinux/gardenlinux"
162+
GL_VENDOR_NAME = "SAP SE"
162163

163164
OCI_ANNOTATION_SIGNATURE_KEY = "io.gardenlinux.oci.signature"
164165
OCI_ANNOTATION_SIGNED_STRING_KEY = "io.gardenlinux.oci.signed-string"

‎src/gardenlinux/oci/__main__.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def get_parser() -> argparse.ArgumentParser:
357357
_add_oci_index_args_to_parser(add_image_to_index_parser)
358358
_add_oci_repository_to_parser(add_image_to_index_parser)
359359
_add_oci_tag_to_parser(add_image_to_index_parser)
360+
_add_oci_platform_to_parser(add_image_to_index_parser, False)
360361
_add_oci_insecure_to_parser(add_image_to_index_parser)
361362
_add_additional_tag_list_to_parser(add_image_to_index_parser)
362363

@@ -491,12 +492,22 @@ def main() -> None:
491492

492493
manifest = manifest_repository.read_manifest()
493494

495+
if args.platform and ImageManifest.ANNOTATION_OS_ID_KEY not in manifest:
496+
platform_data = args.platform.split("/", 2)
497+
manifest[ImageManifest.ANNOTATION_OS_ID_KEY] = platform_data[0]
498+
499+
if (
500+
len(platform_data) > 1
501+
and ImageManifest.ANNOTATION_ARCH_KEY not in manifest
502+
):
503+
manifest[ImageManifest.ANNOTATION_ARCH_KEY] = platform_data[1]
504+
494505
index_resource = Repository(
495506
f"{args.index}:{args.index_tag}", insecure=args.insecure
496507
)
497508

498509
image_index = index_resource.read_or_generate_index()
499-
image_index.append_manifest(manifest)
510+
image_index.add_manifest(manifest)
500511

501512
index_resource.push_index(image_index)
502513
index_resource.push_index_for_tags(image_index, args.additional_tag)

‎src/gardenlinux/oci/image_manifest.py‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
from oras.defaults import default_manifest_media_type as IMAGE_MANIFEST_MEDIA_TYPE
1212

13-
from ..constants import GL_DISTRIBUTION_NAME, GL_REPOSITORY_URL
13+
from ..constants import (
14+
GL_DISTRIBUTION_NAME,
15+
GL_RELEASE_ID,
16+
GL_REPOSITORY_URL,
17+
GL_VENDOR_NAME,
18+
)
1419
from ..features import ArtifactBaseName, Flavor
1520
from .layer import Layer
1621
from .manifest import Manifest
@@ -45,9 +50,13 @@ class ImageManifest(Manifest):
4550
"""
4651
OCI image manifest Garden Linux canonical name deprecated annotation
4752
"""
53+
ANNOTATION_OS_ID_KEY = "io.gardenlinux.image.os-id"
54+
"""
55+
OCI image manifest Garden Linux operating system annotation
56+
"""
4857
ANNOTATION_COMMIT_KEY = "org.opencontainers.image.revision"
4958
"""
50-
OCI image manifest GardenLinux commit hash annotation
59+
OCI image manifest commit hash annotation
5160
"""
5261
ANNOTATION_FEATURE_SET_KEY = "io.gardenlinux.image.feature-set"
5362
"""
@@ -59,11 +68,15 @@ class ImageManifest(Manifest):
5968
"""
6069
ANNOTATION_SOURCE_REPO_KEY = "org.opencontainers.image.source"
6170
"""
62-
OCI image manifest Garden Linux source repository URL annotation
71+
OCI image manifest source repository URL annotation
72+
"""
73+
ANNOTATION_VENDOR_KEY = "org.opencontainers.image.vendor"
74+
"""
75+
OCI image manifest vendor annotation
6376
"""
6477
ANNOTATION_VERSION_KEY = "org.opencontainers.image.version"
6578
"""
66-
OCI image manifest GardenLinux version annotation
79+
OCI image manifest version annotation
6780
"""
6881
IMAGE_MANIFEST_MEDIA_TYPE = IMAGE_MANIFEST_MEDIA_TYPE
6982
"""
@@ -242,39 +255,52 @@ def extended_dict(self) -> Dict[str, Any]:
242255

243256
manifest_description = manifest_annotations[ImageManifest.ANNOTATION_TITLE_KEY]
244257

245-
if ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations:
258+
annotation_arch_key = ImageManifest.ANNOTATION_ARCH_KEY
259+
annotation_commit_key = ImageManifest.ANNOTATION_COMMIT_KEY
260+
annotation_version_key = ImageManifest.ANNOTATION_VERSION_KEY
261+
262+
if annotation_arch_key in manifest_annotations:
246263
manifest_description += f" ({self.arch})"
247264

248265
if ImageManifest.ANNOTATION_SOURCE_REPO_KEY not in manifest_annotations:
249266
manifest_annotations[ImageManifest.ANNOTATION_SOURCE_REPO_KEY] = (
250267
GL_REPOSITORY_URL
251268
)
252269

253-
if ImageManifest.ANNOTATION_VERSION_KEY in manifest_annotations:
270+
if annotation_version_key in manifest_annotations:
254271
manifest_annotations[Manifest.ANNOTATION_VERSION_DEPRECATED_KEY] = (
255272
self.version
256273
)
257274
manifest_description += " " + self.version
258275

259-
if ImageManifest.ANNOTATION_COMMIT_KEY in manifest_annotations:
276+
if annotation_commit_key in manifest_annotations:
260277
manifest_annotations[Manifest.ANNOTATION_COMMIT_DEPRECATED_KEY] = (
261278
self.commit
262279
)
263280
manifest_description += f" ({self.commit_id})"
264281

265282
if (
266283
ImageManifest.ANNOTATION_CNAME_KEY in manifest_annotations
267-
and ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations
268-
and ImageManifest.ANNOTATION_VERSION_KEY in manifest_annotations
269-
and ImageManifest.ANNOTATION_COMMIT_KEY in manifest_annotations
284+
and annotation_arch_key in manifest_annotations
285+
and annotation_version_key in manifest_annotations
286+
and annotation_commit_key in manifest_annotations
270287
):
271288
manifest_annotations[ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY] = (
272289
self.artifact_base_name
273290
)
291+
274292
manifest_annotations[ImageManifest.ANNOTATION_CNAME_DEPRECATED_KEY] = (
275293
self.artifact_base_name
276294
)
277295

296+
if ImageManifest.ANNOTATION_OS_ID_KEY not in manifest_annotations:
297+
manifest_annotations[ImageManifest.ANNOTATION_OS_ID_KEY] = GL_RELEASE_ID
298+
299+
annotation_vendor_key = ImageManifest.ANNOTATION_VENDOR_KEY
300+
301+
if annotation_vendor_key not in manifest_annotations:
302+
manifest_annotations[annotation_vendor_key] = GL_VENDOR_NAME
303+
278304
if ImageManifest.ANNOTATION_FEATURE_SET_KEY in manifest_annotations:
279305
manifest_description += (
280306
" - " + manifest_annotations[ImageManifest.ANNOTATION_FEATURE_SET_KEY]
@@ -285,7 +311,7 @@ def extended_dict(self) -> Dict[str, Any]:
285311
manifest_description
286312
)
287313

288-
if ImageManifest.ANNOTATION_ARCH_KEY in manifest_annotations:
314+
if annotation_arch_key in manifest_annotations:
289315
# @TODO: Deprectated annotation
290316
manifest_annotations["architecture"] = self.arch
291317

@@ -317,7 +343,7 @@ def layers_as_dict(self) -> Dict[str, Any]:
317343

318344
return layers
319345

320-
def append_layer(self, layer: Layer | Dict[str, Any]) -> None:
346+
def add_layer(self, layer: Layer | Dict[str, Any]) -> None:
321347
"""
322348
Appends the given OCI image manifest layer to the manifest
323349

‎src/gardenlinux/oci/index.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def manifests_as_dict(self) -> Dict[str, Dict[str, Any]]:
8787

8888
return manifests
8989

90-
def append_manifest(self, manifest: Dict[str, Any]) -> None:
90+
def add_manifest(self, manifest: Dict[str, Any]) -> None:
9191
"""
9292
Appends the given OCI image manifest to the index
9393
@@ -141,9 +141,17 @@ def append_manifest(self, manifest: Dict[str, Any]) -> None:
141141
ImageManifest.ANNOTATION_ARCH_KEY in image_annotations
142142
and ImageManifest.ANNOTATION_VERSION_KEY in image_annotations
143143
):
144+
kwargs = {}
145+
146+
annotation_os_id_key = ImageManifest.ANNOTATION_OS_ID_KEY
147+
148+
if annotation_os_id_key in image_annotations:
149+
kwargs["os_name"] = image_annotations[annotation_os_id_key]
150+
144151
index_manifest["platform"] = new_platform(
145152
image_annotations[ImageManifest.ANNOTATION_ARCH_KEY],
146153
image_annotations[ImageManifest.ANNOTATION_VERSION_KEY],
154+
**kwargs,
147155
)
148156

149157
self["manifests"].append(index_manifest)

‎src/gardenlinux/oci/platform.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from .schemas import empty_platform
77

88

9-
def new_platform(architecture: str, version: str) -> Dict[str, Any]:
9+
def new_platform(
10+
architecture: str, version: str, /, os_name: str = ""
11+
) -> Dict[str, Any]:
1012
"""
1113
Create an OCI image platform for the architecture and version given.
1214
@@ -21,4 +23,7 @@ def new_platform(architecture: str, version: str) -> Dict[str, Any]:
2123
platform["architecture"] = architecture
2224
platform["os.version"] = version
2325

26+
if os_name:
27+
platform["os"] = os_name
28+
2429
return platform

‎src/gardenlinux/oci/repository.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
from oras.utils import extract_targz, make_targz
2424
from requests import HTTPError, Response
2525

26-
from ..constants import OCI_IMAGE_INDEX_MEDIA_TYPE
26+
from ..constants import (
27+
GL_CONTAINER_REGISTRY_BASE_URL,
28+
GL_DISTRIBUTION_NAME,
29+
OCI_IMAGE_INDEX_MEDIA_TYPE,
30+
)
2731
from ..features import ArtifactBaseName
2832
from ..logger import LoggerSetup
2933
from .image_manifest import ImageManifest
@@ -173,7 +177,11 @@ def generate_index(self) -> Index:
173177
:since: 0.7.0
174178
"""
175179

176-
return Index()
180+
index = Index()
181+
index[Manifest.ANNOTATION_DESCRIPTION_KEY] = (
182+
f"{GL_DISTRIBUTION_NAME} ({GL_CONTAINER_REGISTRY_BASE_URL})"
183+
)
184+
return index
177185

178186
def generate_manifest(
179187
self,
@@ -292,7 +300,7 @@ def push_index_from_directory(
292300

293301
continue
294302

295-
index.append_manifest(manifest)
303+
index.add_manifest(manifest)
296304

297305
self._logger.info(
298306
f"Index appended locally {manifest['annotations'][ImageManifest.ANNOTATION_ARTIFACT_BASE_NAME_KEY]}"
@@ -441,7 +449,7 @@ def push_manifest_and_artifacts(
441449
file_path_name = Path(make_targz(file_path_name))
442450
cleanup_blob = True
443451

444-
manifest.append_layer(layer)
452+
manifest.add_layer(layer)
445453
layer_dict = layer.dict
446454

447455
self._logger.debug(

‎src/gardenlinux/oci/schemas.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
# TODO: add more schemas, and validate dicts with schemas before accessing them.
77

8+
from ..constants import GL_RELEASE_ID
9+
810
schema_url = "http://json-schema.org/draft-07/schema"
911

1012
platform_properties = {
@@ -43,7 +45,7 @@
4345

4446
empty_platform = {
4547
"architecture": "",
46-
"os": "gardenlinux",
48+
"os": GL_RELEASE_ID,
4749
"os.version": "experimental",
4850
}
4951

‎tests/oci/test_image_manifest.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_ImageManifest_layer(tmp_path: Path) -> None:
147147
layer = Layer(blob)
148148

149149
manifest = ImageManifest()
150-
manifest.append_layer(layer)
150+
manifest.add_layer(layer)
151151

152152
# Assert
153153

@@ -156,7 +156,7 @@ def test_ImageManifest_layer(tmp_path: Path) -> None:
156156

157157
# Assert
158158
with pytest.raises(RuntimeError):
159-
manifest.append_layer({"test": "invalid"})
159+
manifest.add_layer({"test": "invalid"})
160160

161161

162162
def test_ImageManifest_version() -> None:

0 commit comments

Comments
 (0)