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: Support ImageSpec as base image #2277

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
apt_packages: Optional[List[str]] = None
cuda: Optional[str] = None
cudnn: Optional[str] = None
base_image: Optional[str] = None
base_image: Optional[Union[str, "ImageSpec"]] = None
platform: str = "linux/amd64"
pip_index: Optional[str] = None
registry_config: Optional[str] = None
Expand Down Expand Up @@ -226,6 +226,10 @@
@classmethod
@lru_cache
def build(cls, image_spec: ImageSpec) -> str:
if isinstance(image_spec.base_image, ImageSpec):
cls.build(image_spec.base_image)
image_spec.base_image = image_spec.base_image.image_name()

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

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L230-L231

Added lines #L230 - L231 were not covered by tests

if image_spec.builder is None and cls._REGISTRY:
builder = max(cls._REGISTRY, key=lambda name: cls._REGISTRY[name][1])
else:
Expand Down Expand Up @@ -267,6 +271,8 @@
"""
# copy the image spec to avoid modifying the original image spec. otherwise, the hash will be different.
spec = copy.deepcopy(image_spec)
if isinstance(spec.base_image, ImageSpec):
spec.base_image = spec.base_image.image_name()

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

View check run for this annotation

Codecov / codecov/patch

flytekit/image_spec/image_spec.py#L275

Added line #L275 was not covered by tests
spec.source_root = hash_directory(image_spec.source_root) if image_spec.source_root else b""
if spec.requirements:
spec.requirements = hashlib.sha1(pathlib.Path(spec.requirements).read_bytes()).hexdigest()
Expand Down
14 changes: 12 additions & 2 deletions plugins/flytekit-envd/tests/test_image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@ def register_envd_higher_priority():


def test_image_spec():
base_image = ImageSpec(
packages=["numpy"],
python_version="3.8",
registry="",
base_image="cr.flyte.org/flyteorg/flytekit:py3.8-latest",
)
# Replace the base image name with the default flytekit image name,
# so Envd can find the base image when building imageSpec below
ImageBuildEngine._IMAGE_NAME_TO_REAL_NAME[base_image.image_name()] = "cr.flyte.org/flyteorg/flytekit:py3.8-latest"

image_spec = ImageSpec(
packages=["pandas"],
apt_packages=["git"],
python_version="3.8",
base_image="cr.flyte.org/flyteorg/flytekit:py3.8-latest",
base_image=base_image,
pip_index="https://private-pip-index/simple",
)

image_spec = image_spec.with_commands("echo hello")

EnvdImageSpecBuilder().build_image(image_spec)
ImageBuildEngine.build(image_spec)
config_path = create_envd_config(image_spec)
assert image_spec.platform == "linux/amd64"
image_name = image_spec.image_name()
Expand Down
Loading