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 10 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
53 changes: 35 additions & 18 deletions flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

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 +182,15 @@

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

return new_image_spec


class ImageSpecBuilder:
@abstractmethod
Expand Down Expand Up @@ -223,25 +233,32 @@

img_name = image_spec.image_name()
if img_name in cls._BUILT_IMAGES or image_spec.exist():
click.secho(f"Image {img_name} found. Skip building.", fg="blue")
if image_spec._is_force_push:
click.secho(f"Image {img_name} found. but overwriting existing image.", fg="blue")
cls.register_image(builder, image_spec, img_name)

Check warning on line 238 in flytekit/image_spec/image_spec.py

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L237-L238

Added lines #L237 - L238 were not covered by tests
else:
click.secho(f"Image {img_name} found. Skip building.", fg="blue")

Check warning on line 240 in flytekit/image_spec/image_spec.py

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L240

Added line #L240 was not covered by tests
else:
click.secho(f"Image {img_name} not found. Building...", fg="blue")
if builder not in cls._REGISTRY:
raise Exception(f"Builder {builder} is not registered.")
if builder == "envd":
envd_version = metadata.version("envd")
# flytekit v1.10.2+ copies the workflow code to the WorkDir specified in the Dockerfile. However, envd<0.3.39
# overwrites the WorkDir when building the image, resulting in a permission issue when flytekit downloads the file.
if Version(envd_version) < Version("0.3.39"):
raise FlyteAssertion(
f"envd version {envd_version} is not compatible with flytekit>v1.10.2."
f" Please upgrade envd to v0.3.39+."
)

fully_qualified_image_name = cls._REGISTRY[builder][0].build_image(image_spec)
if fully_qualified_image_name is not None:
cls._IMAGE_NAME_TO_REAL_NAME[img_name] = fully_qualified_image_name
cls._BUILT_IMAGES.add(img_name)
click.secho(f"Image {img_name} not found. building...", fg="blue")
cls.register_image(builder, image_spec, img_name)

@classmethod
def register_image(cls, builder, image_spec, img_name):
if builder not in cls._REGISTRY:
raise Exception(f"Builder {builder} is not registered.")
if builder == "envd":
envd_version = metadata.version("envd")

Check warning on line 250 in flytekit/image_spec/image_spec.py

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L250

Added line #L250 was not covered by tests
# flytekit v1.10.2+ copies the workflow code to the WorkDir specified in the Dockerfile. However, envd<0.3.39
# overwrites the WorkDir when building the image, resulting in a permission issue when flytekit downloads the file.
if Version(envd_version) < Version("0.3.39"):
raise FlyteAssertion(

Check warning on line 254 in flytekit/image_spec/image_spec.py

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L254

Added line #L254 was not covered by tests
f"envd version {envd_version} is not compatible with flytekit>v1.10.2."
f" Please upgrade envd to v0.3.39+."
)
fully_qualified_image_name = cls._REGISTRY[builder][0].build_image(image_spec)
if fully_qualified_image_name is not None:
cls._IMAGE_NAME_TO_REAL_NAME[img_name] = fully_qualified_image_name
cls._BUILT_IMAGES.add(img_name)


@lru_cache
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