Skip to content

Commit 05ca7b2

Browse files
committed
Add a Registry abstraction
This removes a bit of if() condition wars in the BaseContainerImage and delegates it into its own class hierarchy, which allows to build a python container for appcol without having to do more subclasses.
1 parent b136d7b commit 05ca7b2

File tree

7 files changed

+128
-95
lines changed

7 files changed

+128
-95
lines changed

src/bci_build/package/__init__.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import jinja2
1717
from packaging import version
1818

19+
from bci_build.registry import ApplicationCollectionRegistry
20+
from bci_build.registry import RegistryABC
21+
from bci_build.registry import get_registry
1922
from bci_build.templates import DOCKERFILE_TEMPLATE
2023
from bci_build.templates import INFOHEADER_TEMPLATE
2124
from bci_build.templates import KIWI_TEMPLATE
@@ -370,7 +373,6 @@ def __post_init__(self) -> None:
370373
source package.
371374
372375
"""
373-
374376
if self.file_name and "readme" in self.file_name.lower():
375377
raise ValueError(f"Cannot replace variables in {self.file_name}!")
376378

@@ -552,6 +554,13 @@ class BaseContainerImage(abc.ABC):
552554
#: webui sorting)
553555
_min_release_counter: int | None = None
554556

557+
#: The registry implementation for which this container is being built.
558+
_publish_registry: RegistryABC | None = None
559+
560+
@property
561+
def publish_registry(self):
562+
return self._publish_registry
563+
555564
def __post_init__(self) -> None:
556565
self.pretty_name = self.pretty_name.strip()
557566

@@ -577,6 +586,16 @@ def __post_init__(self) -> None:
577586
if self.os_version.is_tumbleweed
578587
else "SUSE LLC (https://www.suse.com/)"
579588
)
589+
if not self._publish_registry:
590+
self._publish_registry = get_registry(self)
591+
592+
# AppCollection preferences
593+
if isinstance(self._publish_registry, ApplicationCollectionRegistry):
594+
# Limit to aarch64 and x86_64
595+
if not self.exclusive_arch:
596+
self.exclusive_arch = [Arch.AARCH64, Arch.X86_64]
597+
# Disable maintainer listing
598+
self.maintainer = None
580599

581600
# limit to tech preview for beta releases
582601
if (
@@ -673,36 +692,17 @@ def url(self) -> str:
673692
``org.opencontainers.image.url`` label
674693
675694
"""
676-
if self.os_version.is_tumbleweed:
677-
return "https://www.opensuse.org"
678-
if self.os_version.is_ltss:
679-
return "https://www.suse.com/products/long-term-service-pack-support/"
680-
681-
return "https://www.suse.com/products/base-container-images/"
682-
683-
@property
684-
def vendor(self) -> str:
685-
"""The vendor that is put into the ``org.opencontainers.image.vendor``
686-
label
687-
688-
"""
689-
if self.os_version.is_tumbleweed:
690-
return "openSUSE Project"
691-
return "SUSE LLC"
695+
return self._publish_registry.url(container=self)
692696

693697
@property
694698
def base_image_registry(self) -> str:
695699
"""The registry where the base image is available on."""
696-
if self.os_version.is_tumbleweed:
697-
return "registry.opensuse.org"
698-
return "registry.suse.com"
700+
return get_registry(self).registry
699701

700702
@property
701703
def registry(self) -> str:
702704
"""The registry where the image is available on."""
703-
if self.os_version.is_tumbleweed:
704-
return "registry.opensuse.org"
705-
return "registry.suse.com"
705+
return self._publish_registry.registry
706706

707707
@property
708708
def dockerfile_custom_end(self) -> str:
@@ -823,7 +823,10 @@ def _from_image(self) -> str | None:
823823
return f"{_build_tag_prefix(self.os_version)}/bci-base:{self.os_version}"
824824
if self.os_version in ALL_OS_LTSS_VERSIONS:
825825
return f"{_build_tag_prefix(self.os_version)}/sle15:15.{self.os_version}"
826-
if self.image_type == ImageType.APPLICATION:
826+
if (
827+
not isinstance(self._publish_registry, ApplicationCollectionRegistry)
828+
and self.image_type == ImageType.APPLICATION
829+
):
827830
return f"suse/sle15:15.{self.os_version}"
828831

829832
return f"bci/bci-base:15.{self.os_version}"
@@ -1404,9 +1407,7 @@ def prepare_template(self) -> None:
14041407

14051408
@property
14061409
def _registry_prefix(self) -> str:
1407-
if self.os_version.is_tumbleweed:
1408-
return "opensuse/bci"
1409-
return "bci"
1410+
return self._publish_registry.registry_prefix(is_application=False)
14101411

14111412
@property
14121413
def image_type(self) -> ImageType:
@@ -1529,16 +1530,16 @@ def __post_init__(self) -> None:
15291530

15301531
@property
15311532
def _registry_prefix(self) -> str:
1532-
if self.os_version.is_tumbleweed:
1533-
return "opensuse"
1534-
return "suse"
1533+
return self._publish_registry.registry_prefix(is_application=True)
15351534

15361535
@property
15371536
def image_type(self) -> ImageType:
15381537
return ImageType.APPLICATION
15391538

15401539
@property
15411540
def title(self) -> str:
1541+
if isinstance(self._publish_registry, ApplicationCollectionRegistry):
1542+
return self.pretty_name
15421543
return f"{self.os_version.distribution_base_name} {self.pretty_name}"
15431544

15441545
@property

src/bci_build/package/apache_tomcat.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
from bci_build.package import CAN_BE_LATEST_OS_VERSION
66
from bci_build.package import DOCKERFILE_RUN
7+
from bci_build.package import ApplicationStackContainer
78
from bci_build.package import OsContainer
89
from bci_build.package import OsVersion
910
from bci_build.package import Package
1011
from bci_build.package import PackageType
1112
from bci_build.package import Replacement
1213
from bci_build.package import _build_tag_prefix
13-
14-
from .appcollection import ApplicationCollectionContainer
14+
from bci_build.registry import ApplicationCollectionRegistry
1515

1616
# last version needs to be the newest
1717
_TOMCAT_VERSIONS: list[str] = ["9", "10.1"]
@@ -50,11 +50,16 @@ def _get_sac_supported_until(
5050

5151

5252
TOMCAT_CONTAINERS = [
53-
ApplicationCollectionContainer(
53+
ApplicationStackContainer(
5454
name="apache-tomcat",
55-
package_name=f"apache-tomcat-{tomcat_ver.partition('.')[0]}-java-{jre_version}-image"
56-
if os_version.is_tumbleweed
57-
else f"sac-apache-tomcat-{tomcat_ver.partition('.')[0]}-java{jre_version}-image",
55+
package_name=(
56+
f"apache-tomcat-{tomcat_ver.partition('.')[0]}-java-{jre_version}-image"
57+
if os_version.is_tumbleweed
58+
else f"sac-apache-tomcat-{tomcat_ver.partition('.')[0]}-java{jre_version}-image"
59+
),
60+
_publish_registry=(
61+
None if os_version.is_tumbleweed else ApplicationCollectionRegistry()
62+
),
5863
pretty_name="Apache Tomcat",
5964
custom_description=(
6065
"Apache Tomcat is a free and open-source implementation of the Jakarta Servlet, "

src/bci_build/package/appcollection.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/bci_build/package/postfix.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from bci_build.package import ALL_NONBASE_OS_VERSIONS
66
from bci_build.package import CAN_BE_LATEST_OS_VERSION
77
from bci_build.package import DOCKERFILE_RUN
8+
from bci_build.package import ApplicationStackContainer
89
from bci_build.package import OsVersion
910
from bci_build.package import ParseVersion
1011
from bci_build.package import Replacement
1112
from bci_build.package import SupportLevel
12-
13-
from .appcollection import ApplicationCollectionContainer
13+
from bci_build.registry import ApplicationCollectionRegistry
1414

1515
_POSTFIX_FILES = {}
1616
for filename in (
@@ -34,9 +34,12 @@
3434

3535

3636
POSTFIX_CONTAINERS = [
37-
ApplicationCollectionContainer(
37+
ApplicationStackContainer(
3838
name="postfix",
3939
package_name=None if os_version.is_tumbleweed else "sac-postfix-image",
40+
_publish_registry=(
41+
None if os_version.is_tumbleweed else ApplicationCollectionRegistry()
42+
),
4043
pretty_name="Postfix",
4144
custom_description="Postfix container is fast and secure mail server, {based_on_container}.",
4245
os_version=os_version,

src/bci_build/package/python.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from bci_build.package import OsVersion
1111
from bci_build.package import Replacement
1212
from bci_build.package import SupportLevel
13+
from bci_build.registry import ApplicationCollectionRegistry
1314

1415
_PYTHON_VERSIONS = Literal["3.6", "3.9", "3.10", "3.11", "3.12"]
1516

@@ -112,6 +113,9 @@ def _get_python_kwargs(py3_ver: _PYTHON_VERSIONS, os_version: OsVersion):
112113
PythonDevelopmentContainer(
113114
**_get_python_kwargs("3.9", os_version),
114115
package_name="sac-python-3.9-image",
116+
_publish_registry=(
117+
None if os_version.is_tumbleweed else ApplicationCollectionRegistry()
118+
),
115119
)
116120
for os_version in (OsVersion.SP6,)
117121
)

src/bci_build/registry.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Registry classes for container images."""
2+
3+
import dataclasses
4+
from abc import ABC
5+
from abc import abstractmethod
6+
from dataclasses import dataclass
7+
8+
9+
@dataclass(frozen=True, kw_only=True)
10+
class RegistryABC(ABC):
11+
"""Abstract Base Class for defining Registry specific content."""
12+
13+
_: dataclasses.KW_ONLY
14+
"""The base hostname for this registry instance"""
15+
registry: str
16+
"""The vendor that is put into the ``org.opencontainers.image.vendor`` label"""
17+
vendor: str
18+
19+
@abstractmethod
20+
def url(self, container) -> str:
21+
pass
22+
23+
@abstractmethod
24+
def registry_prefix(self, *, is_application) -> str:
25+
pass
26+
27+
28+
class ApplicationCollectionRegistry(RegistryABC):
29+
"""Registry for the Rancher Application Collection Distribution Platform."""
30+
31+
def __init__(self):
32+
super().__init__(registry="dp.apps.rancher.io", vendor="SUSE LLC")
33+
34+
def url(self, container) -> str:
35+
return f"https://apps.rancher.io/applications/{container.name}"
36+
37+
def registry_prefix(self, *, is_application) -> str:
38+
return "containers"
39+
40+
41+
class SUSERegistry(RegistryABC):
42+
"""Registry for the SUSE Registry."""
43+
44+
def __init__(self):
45+
super().__init__(registry="registry.suse.com", vendor="SUSE LLC")
46+
47+
def url(self, container) -> str:
48+
if container.os_version.is_ltss:
49+
return "https://www.suse.com/products/long-term-service-pack-support/"
50+
return "https://www.suse.com/products/base-container-images/"
51+
52+
def registry_prefix(self, *, is_application) -> str:
53+
return "suse" if is_application else "bci"
54+
55+
56+
class openSUSERegistry(RegistryABC):
57+
"""Registry for the openSUSE registry."""
58+
59+
def __init__(self):
60+
super().__init__(registry="registry.opensuse.org", vendor="openSUSE Project")
61+
62+
def url(self, container) -> str:
63+
return "https://www.opensuse.org"
64+
65+
def registry_prefix(self, *, is_application) -> str:
66+
return "opensuse" if is_application else "opensuse/bci"
67+
68+
69+
def get_registry(container) -> RegistryABC:
70+
"""Return the appropriate registry for the container."""
71+
if container.os_version.is_tumbleweed:
72+
return openSUSERegistry()
73+
return SUSERegistry()

src/bci_build/templates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
LABEL org.opencontainers.image.version="{{ image.oci_version }}"
6464
LABEL org.opencontainers.image.url="{{ image.url }}"
6565
LABEL org.opencontainers.image.created="%BUILDTIME%"
66-
LABEL org.opencontainers.image.vendor="{{ image.vendor }}"
66+
LABEL org.opencontainers.image.vendor="{{ image.publish_registry.vendor }}"
6767
LABEL org.opencontainers.image.source="%SOURCEURL%"
6868
LABEL org.opencontainers.image.ref.name="{{ image.image_ref_name }}"
6969
LABEL org.opensuse.reference="{{ image.reference }}"
@@ -120,7 +120,7 @@
120120
121121
<image schemaversion="7.4" name="{{ image.uid }}-image" xmlns:suse_label_helper="com.suse.label_helper">
122122
<description type="system">
123-
<author>{{ image.vendor }}</author>
123+
<author>{{ image.publish_registry.vendor }}</author>
124124
<contact>https://www.suse.com/</contact>
125125
<specification>{{ image.title }} Container Image</specification>
126126
</description>
@@ -145,7 +145,7 @@
145145
<label name="org.opencontainers.image.description" value="{{ image.description }}"/>
146146
<label name="org.opencontainers.image.version" value="{{ image.oci_version }}"/>
147147
<label name="org.opencontainers.image.created" value="%BUILDTIME%"/>
148-
<label name="org.opencontainers.image.vendor" value="{{ image.vendor }}"/>
148+
<label name="org.opencontainers.image.vendor" value="{{ image.publish_registry.vendor }}"/>
149149
<label name="org.opencontainers.image.source" value="%SOURCEURL%"/>
150150
<label name="org.opencontainers.image.url" value="{{ image.url }}"/>
151151
<label name="org.opencontainers.image.ref.name" value="{{ image.image_ref_name }}"/>

0 commit comments

Comments
 (0)