Skip to content

Commit

Permalink
refactor: refactor image building and registration logic
Browse files Browse the repository at this point in the history
- Refactor the `ImageBuildEngine` class to improve readability and maintainability
- Update the logic for handling image building and registration
- Fix formatting and messaging inconsistencies in the code

Signed-off-by: jason.lai <[email protected]>
  • Loading branch information
jasonlai1218 committed Mar 6, 2024
1 parent d3a0fb5 commit 7471785
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,33 @@ 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()) and not image_spec._is_force_push:
click.secho(f"Image {img_name} found. Skip building.", fg="blue")
else:
if img_name in cls._BUILT_IMAGES or image_spec.exist():
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} 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} 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")
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

0 comments on commit 7471785

Please sign in to comment.