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

fix(sdk): Add error handling. Fixes #11164 #11356

Merged
merged 1 commit into from
Dec 3, 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
1 change: 1 addition & 0 deletions sdk/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
## Deprecations

## Bug fixes and other changes
* Add error handling for image build/push failures in KFP SDK. [\#11164](https://github.com/kubeflow/pipelines/pull/11356)

## Documentation updates

Expand Down
11 changes: 10 additions & 1 deletion sdk/python/kfp/cli/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
_COMPONENT_ROOT_DIR = pathlib.Path('/usr/local/src/kfp/components')


class ComponentBuildError(Exception):
HumairAK marked this conversation as resolved.
Show resolved Hide resolved
pass


@contextlib.contextmanager
def _registered_modules():
registered_modules = {}
Expand Down Expand Up @@ -313,8 +317,12 @@ def build_image(self, platform: str, push_image: bool):
)
for log in logs:
message = log.get('stream', '').rstrip('\n')
error = log.get('error', '').rstrip('\n')
if message:
logging.info(f'{docker_log_prefix}: {message}')
if error:
raise ComponentBuildError(
f'{docker_log_prefix} ERROR: {error}')

except docker.errors.BuildError as e:
for log in e.build_log:
Expand All @@ -339,7 +347,8 @@ def build_image(self, platform: str, push_image: bool):
if status:
logging.info(f'{docker_log_prefix}: {layer} {status}')
if error:
logging.error(f'{docker_log_prefix} ERROR: {error}')
raise ComponentBuildError(
f'{docker_log_prefix} ERROR: {error}')
except docker.errors.BuildError as e:
logging.error(f'{docker_log_prefix}: {e}')
raise e
Expand Down
28 changes: 28 additions & 0 deletions sdk/python/kfp/cli/component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,34 @@ def test_docker_client_is_called_to_build_but_skips_pushing(self):
self._docker_client.api.build.assert_called_once()
self._docker_client.images.push.assert_not_called()

def test_docker_client_failed_to_build_img(self):
self._docker_client.api.build.return_value = [{'error': 'Error log'}]
created_component = _make_component(
func_name='train', target_image='custom-image')
_write_components('components.py', created_component)

result = self.runner.invoke(
self.cli,
['build', str(self._working_dir), '--push-image'],
)

self.assertEqual(result.exit_code, 1)
self._docker_client.api.build.assert_called_once()
self._docker_client.images.push.assert_not_called()

def test_docker_client_failed_to_push_img(self):
self._docker_client.images.push.return_value = [{'error': 'Error log'}]
created_component = _make_component(
func_name='train', target_image='custom-image')
_write_components('components.py', created_component)

result = self.runner.invoke(
self.cli,
['build', str(self._working_dir), '--push-image'],
)
self.assertEqual(result.exit_code, 1)
self._docker_client.images.push.assert_called_once()

@mock.patch('kfp.__version__', '1.2.3')
def test_docker_file_is_created_correctly(self):
component = _make_component(
Expand Down
Loading