Skip to content

Commit

Permalink
feat(sdk): add --build-image option to 'kfp components build' to allo…
Browse files Browse the repository at this point in the history
…w users to skip docker build. Fixes kubeflow#8382 for 2.0 (kubeflow#8387)

* feat: added --build-image/--no-build-image flags to skip docker build

* fix: logic error for warnings/errors
  • Loading branch information
Jonny Browning (Datatonic) authored and jlyaoyuli committed Jan 5, 2023
1 parent be120ec commit 3668c3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
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

0 comments on commit 3668c3f

Please sign in to comment.