Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds custom string format parameter to customize ImageSpec's tag #2349

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class ImageSpec:
pip_extra_index_url: Specify one or more pip index urls as a list
registry_config: Specify the path to a JSON registry config file
commands: Command to run during the building process
tag_format: Custom string format for image tag. The ImageSpec hash is set as `image_hash`. For example,
to add a "dev" suffix to the image tag, set `tag_format="{image_hash}-dev"`
"""

name: str = "flytekit"
Expand All @@ -67,6 +69,7 @@ class ImageSpec:
pip_extra_index_url: Optional[List[str]] = None
registry_config: Optional[str] = None
commands: Optional[List[str]] = None
tag_format: Optional[str] = None

def __post_init__(self):
self.name = self.name.lower()
Expand All @@ -85,6 +88,9 @@ def image_name(self) -> str:
def _image_name(self) -> str:
"""Construct full image name with tag."""
tag = calculate_hash_from_image_spec(self)
if self.tag_format:
tag = self.tag_format.format(image_hash=tag)

container_image = f"{self.name}:{tag}"
if self.registry:
container_image = f"{self.registry}/{container_image}"
Expand Down
10 changes: 10 additions & 0 deletions tests/flytekit/unit/core/image_spec/test_image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ def test_build_existing_image_with_force_push():

ImageBuildEngine.build(image_spec)
ImageBuildEngine._build_image.assert_called_once()


def test_custom_tag():
spec = ImageSpec(
name="my_image",
python_version="3.11",
tag_format="{image_hash}-dev",
)
spec_hash = calculate_hash_from_image_spec(spec)
assert spec.image_name() == f"my_image:{spec_hash}-dev"
Loading