fix(sdk): allow user to choose platform for component container build. Fixes #8883 (#9212)

Co-authored-by: Jonny Browning <jonathan.browning@datatonic.com>
This commit is contained in:
Jonny Browning 2023-04-25 20:41:45 +01:00 committed by GitHub
parent f5514c7662
commit b59b70875e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -292,7 +292,7 @@ class ComponentBuilder():
self._maybe_write_file(_DOCKERFILE, dockerfile_contents,
overwrite_dockerfile)
def build_image(self, push_image: bool):
def build_image(self, platform: str, push_image: bool):
logging.info(f'Building image {self._target_image} using Docker...')
client = docker.from_env()
@ -305,6 +305,7 @@ class ComponentBuilder():
dockerfile='Dockerfile',
tag=self._target_image,
decode=True,
platform=platform,
)
for log in logs:
message = log.get('stream', '').rstrip('\n')
@ -378,6 +379,11 @@ def component():
is_flag=True,
default=True,
help='Build the container image.')
@click.option(
'--platform',
type=str,
default='linux/amd64',
help='The platform to build the container image for.')
@click.option(
'--push-image/--no-push-image',
type=bool,
@ -386,7 +392,7 @@ 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,
build_image: bool, push_image: bool):
build_image: bool, platform: str, push_image: bool):
"""Builds containers for KFP v2 Python-based components."""
if build_image and engine != 'docker':
@ -428,4 +434,4 @@ def build(components_directory: str, component_filepattern: str, engine: str,
builder.maybe_generate_dockerignore()
builder.maybe_generate_dockerfile(overwrite_dockerfile=overwrite_dockerfile)
if build_image:
builder.build_image(push_image=push_image)
builder.build_image(platform=platform, push_image=push_image)

View File

@ -407,7 +407,13 @@ class Test(unittest.TestCase):
)
self.assertEqual(result.exit_code, 0)
self._docker_client.api.build.assert_called_once()
self._docker_client.api.build.assert_called_once_with(
path=mock.ANY,
dockerfile='Dockerfile',
tag=mock.ANY,
decode=True,
platform='linux/amd64',
)
self._docker_client.images.push.assert_called_once_with(
'custom-image', stream=True, decode=True)