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

Allow for flytekit version to be specified in default image builder #2563

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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: 18 additions & 1 deletion flytekit/image_spec/default_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -98,11 +99,23 @@ def get_flytekit_for_pypi():
return f"flytekit=={__version__}"


_PACKAGE_NAME_RE = re.compile(r"^[\w-]+")


def _is_flytekit(package: str) -> bool:
"""Return True if `package` is flytekit."""
m = _PACKAGE_NAME_RE.match(package)
if not m:
return False
name = m.group()
return name == "flytekit"


def create_docker_context(image_spec: ImageSpec, tmp_dir: Path):
"""Populate tmp_dir with Dockerfile as specified by the `image_spec`."""
base_image = image_spec.base_image or "debian:bookworm-slim"

requirements = [get_flytekit_for_pypi()]
requirements = []

if image_spec.cuda is not None or image_spec.cudnn is not None:
msg = (
Expand All @@ -124,6 +137,10 @@ def create_docker_context(image_spec: ImageSpec, tmp_dir: Path):
if image_spec.packages:
requirements.extend(image_spec.packages)

# Adds flytekit if if it is not specified
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
if not any(_is_flytekit(package) for package in requirements):
requirements.append(get_flytekit_for_pypi())

uv_requirements = []

# uv does not support git + subdirectory, so we use pip to install them instead
Expand Down
36 changes: 36 additions & 0 deletions tests/flytekit/unit/core/image_spec/test_default_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

import flytekit
from flytekit.image_spec import ImageSpec
from flytekit.image_spec.default_builder import DefaultImageBuilder, create_docker_context

Expand Down Expand Up @@ -99,6 +100,41 @@ def test_create_docker_context_with_null_entrypoint(tmp_path):
assert "ENTRYPOINT []" in dockerfile_content


@pytest.mark.parametrize("flytekit_spec", [None, "flytekit>=1.12.3", "flytekit==1.12.3"])
def test_create_docker_context_with_flytekit(tmp_path, flytekit_spec, monkeypatch):

# pretend version is 1.13.0
mock_version = "1.13.0"
monkeypatch.setattr(flytekit, "__version__", mock_version)

docker_context_path = tmp_path / "builder_root"
docker_context_path.mkdir()

if flytekit_spec:
packages = [flytekit_spec]
else:
packages = []

image_spec = ImageSpec(
name="FLYTEKIT", packages=packages
)

create_docker_context(image_spec, docker_context_path)

dockerfile_path = docker_context_path / "Dockerfile"
assert dockerfile_path.exists()

requirements_path = docker_context_path / "requirements_uv.txt"
assert requirements_path.exists()

requirements_content = requirements_path.read_text()
if flytekit_spec:
flytekit_spec in requirements_content
assert f"flytekit=={mock_version}" not in requirements_content
else:
assert f"flytekit=={mock_version}" in requirements_content


def test_create_docker_context_cuda(tmp_path):
docker_context_path = tmp_path / "builder_root"
docker_context_path.mkdir()
Expand Down
Loading