Skip to content

Commit

Permalink
Simplified the build_docker_image function (#1887)
Browse files Browse the repository at this point in the history
* Simplified the build_docker_image function

* Extracted 'Dockerfile' into a variable
Addressed PR feedback

* Fixed conflict with updated master.

* Addressed the feedback
  • Loading branch information
Ark-kun authored and k8s-ci-robot committed Aug 21, 2019
1 parent 3b7340f commit 151c534
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions sdk/python/kfp/compiler/_component_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,6 @@ def __init__(self, gcs_staging, target_image, namespace):
self._arc_requirement_filename = 'requirements.txt'
self._container_builder = ContainerBuilder(gcs_staging, gcr_image_tag=target_image, namespace=namespace)

def _prepare_files(self, local_dir, docker_filename, python_filename=None, requirement_filename=None):
""" _prepare_buildfiles generates the tarball with all the build files
Args:
local_dir (dir): a directory that stores all the build files
docker_filename (str): docker filename
python_filename (str): python filename
requirement_filename (str): requirement filename
"""
dst_docker_filepath = os.path.join(local_dir, self._arc_docker_filename)
shutil.copyfile(docker_filename, dst_docker_filepath)
if python_filename is not None:
dst_python_filepath = os.path.join(local_dir, self._arc_python_filename)
shutil.copyfile(python_filename, dst_python_filepath)
if requirement_filename is not None:
dst_requirement_filepath = os.path.join(local_dir, self._arc_requirement_filename)
shutil.copyfile(requirement_filename, dst_requirement_filepath)

def build_image_from_func(self, component_func, base_image, timeout, dependency, python_version='python3'):
""" build_image builds an image for the given python function
args:
Expand All @@ -325,12 +308,6 @@ def build_image_from_func(self, component_func, base_image, timeout, dependency,
logging.info('Generate build files.')
return self._container_builder.build(local_build_dir, self._arc_docker_filename, timeout=timeout)

def build_image_from_dockerfile(self, docker_filename, timeout):
""" build_image_from_dockerfile builds an image based on the dockerfile """
with tempfile.TemporaryDirectory() as local_build_dir:
self._prepare_files(local_build_dir, docker_filename)
return self._container_builder.build(local_build_dir, self._arc_docker_filename, timeout=timeout)

def _configure_logger(logger):
""" _configure_logger configures the logger such that the info level logs
go to the stdout and the error(or above) level logs go to the stderr.
Expand Down Expand Up @@ -455,7 +432,14 @@ def build_docker_image(staging_gcs_path, target_image, dockerfile_path, timeout=
namespace (str): the namespace within which to run the kubernetes kaniko job, default is "kubeflow"
"""
_configure_logger(logging.getLogger())
builder = ComponentBuilder(gcs_staging=staging_gcs_path, target_image=target_image, namespace=namespace)
image_name_with_digest = builder.build_image_from_dockerfile(docker_filename=dockerfile_path, timeout=timeout)

with tempfile.TemporaryDirectory() as local_build_dir:
dockerfile_rel_path = 'Dockerfile'
dst_dockerfile_path = os.path.join(local_build_dir, dockerfile_rel_path)
shutil.copyfile(dockerfile_path, dst_dockerfile_path)

container_builder = ContainerBuilder(staging_gcs_path, target_image, namespace=namespace)
image_name_with_digest = container_builder.build(local_build_dir, dockerfile_rel_path, timeout)

logging.info('Build image complete.')
return image_name_with_digest

0 comments on commit 151c534

Please sign in to comment.