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(sdk): add --build-image option to 'kfp components build' to allow users to skip docker build. Fixes #8382 for 2.0 #8387

Merged
merged 2 commits into from
Oct 26, 2022
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
19 changes: 14 additions & 5 deletions sdk/python/kfp/cli/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ def component():
default=False,
help='Set this to true to always generate a Dockerfile'
' as part of the build process')
@click.option(
'--build-image/--no-build-image',
type=bool,
is_flag=True,
default=True,
help='Build the container image.')
@click.option(
'--push-image/--no-push-image',
type=bool,
Expand All @@ -369,10 +375,10 @@ def component():
help='Push the built image to its remote repository.')
def build(components_directory: str, component_filepattern: str, engine: str,
kfp_package_path: Optional[str], overwrite_dockerfile: bool,
push_image: bool):
build_image: bool, push_image: bool):
"""Builds containers for KFP v2 Python-based components."""

if engine != 'docker':
if build_image and engine != 'docker':
warnings.warn(
'The --engine option is deprecated and does not need to be passed. Only Docker engine is supported and will be used by default.',
DeprecationWarning,
Expand All @@ -387,12 +393,14 @@ def build(components_directory: str, component_filepattern: str, engine: str,
f'{components_directory} does not seem to be a valid directory.')
raise sys.exit(1)

if not _DOCKER_IS_PRESENT:
if build_image and not _DOCKER_IS_PRESENT:
logging.error(
'The `docker` Python package was not found in the current'
' environment. Please run `pip install docker` to install it.'
' Optionally, you can also install KFP with all of its'
' optional dependencies by running `pip install kfp[all]`.')
' optional dependencies by running `pip install kfp[all]`.'
' Alternatively, you can skip the container image build using'
' the --no-build-image flag.')
raise sys.exit(1)

kfp_package_path = pathlib.Path(
Expand All @@ -408,4 +416,5 @@ def build(components_directory: str, component_filepattern: str, engine: str,
builder.generate_requirements_txt()
builder.maybe_generate_dockerignore()
builder.maybe_generate_dockerfile(overwrite_dockerfile=overwrite_dockerfile)
builder.build_image(push_image=push_image)
if build_image:
builder.build_image(push_image=push_image)
14 changes: 14 additions & 0 deletions sdk/python/kfp/cli/component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,20 @@ def test_docker_client_is_called_to_build_and_push_by_default(self):
self._docker_client.images.push.assert_called_once_with(
'custom-image', stream=True, decode=True)

def test_docker_client_is_not_called_to_build_or_push(self):
component = _make_component(
func_name='train', target_image='custom-image')
_write_components('components.py', component)

result = self.runner.invoke(
self.cli,
['build', str(self._working_dir), '--no-build-image'],
)
self.assertEqual(result.exit_code, 0)

self._docker_client.api.build.assert_not_called()
self._docker_client.images.push.assert_not_called()

def test_docker_client_is_called_to_build_but_skips_pushing(self):
component = _make_component(
func_name='train', target_image='custom-image')
Expand Down