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

feat: implement force push functionality in ImageSpec #2234

Merged
merged 15 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
19 changes: 17 additions & 2 deletions flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ImageSpec:
pip_index: Specify the custom pip index url
registry_config: Specify the path to a JSON registry config file
commands: Command to run during the building process
_is_force_push: forcefully pushed to the registry
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
"""

name: str = "flytekit"
Expand All @@ -67,6 +68,7 @@ class ImageSpec:

def __post_init__(self):
self.name = self.name.lower()
self._is_force_push = False # False by default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update:

def calculate_hash_from_image_spec(image_spec: ImageSpec):

such that the force_push field is not a part of the hash?

if self.registry:
self.registry = self.registry.lower()

Expand Down Expand Up @@ -181,6 +183,16 @@ def with_apt_packages(self, apt_packages: Union[str, List[str]]) -> "ImageSpec":

return new_image_spec

def force_push(self) -> "ImageSpec":
"""
Builder that returns a new image spec with force push enabled.
"""
new_image_spec = copy.deepcopy(self)
new_image_spec._is_force_push = True
self._is_force_push = True
pingsutw marked this conversation as resolved.
Show resolved Hide resolved

return new_image_spec


class ImageSpecBuilder:
@abstractmethod
Expand Down Expand Up @@ -222,10 +234,13 @@ def build(cls, image_spec: ImageSpec) -> str:
builder = image_spec.builder

img_name = image_spec.image_name()
if img_name in cls._BUILT_IMAGES or image_spec.exist():
if (img_name in cls._BUILT_IMAGES or image_spec.exist()) and not image_spec._is_force_push:
click.secho(f"Image {img_name} found. Skip building.", fg="blue")
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
else:
click.secho(f"Image {img_name} not found. Building...", fg="blue")
if image_spec._is_force_push:
click.secho(f"Image {img_name} found. but overwriting existing image.", fg="blue")
else:
click.secho(f"Image {img_name} not found. Building...", fg="blue")
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
if builder not in cls._REGISTRY:
raise Exception(f"Builder {builder} is not registered.")
if builder == "envd":
Expand Down
3 changes: 3 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 @@ -26,10 +26,12 @@ def test_image_spec(mock_image_spec_builder):
requirements=REQUIREMENT_FILE,
registry_config=REGISTRY_CONFIG_FILE,
)
assert image_spec._is_force_push is False

image_spec = image_spec.with_commands("echo hello")
image_spec = image_spec.with_packages("numpy")
image_spec = image_spec.with_apt_packages("wget")
image_spec = image_spec.force_push()

assert image_spec.python_version == "3.8"
assert image_spec.base_image == "cr.flyte.org/flyteorg/flytekit:py3.8-latest"
Expand All @@ -47,6 +49,7 @@ def test_image_spec(mock_image_spec_builder):
assert image_spec.pip_index is None
assert image_spec.is_container() is True
assert image_spec.commands == ["echo hello"]
assert image_spec._is_force_push is True

tag = calculate_hash_from_image_spec(image_spec)
assert image_spec.image_name() == f"flytekit:{tag}"
Expand Down
Loading