-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Separate codegen from containerbuild 2 #1680
Merged
k8s-ci-robot
merged 16 commits into
kubeflow:master
from
gaoning777:separate-codegen-from-containerbuild-2
Jul 30, 2019
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1a1f3a1
refactor component build code
gaoning777 f706329
remove unnecessary import
gaoning777 53ba475
minor changes
gaoning777 ad43511
fix unit tests
gaoning777 f6a1af5
separate the container build from the component build; add support fo…
gaoning777 ca4ad0f
minor fixes
gaoning777 c3281bc
fix unit test
gaoning777 aaa9bcf
fix tarball error
gaoning777 3df3342
Merge branch 'master' into separate-codegen-from-containerbuild-2
gaoning777 1be7dea
revert changes
gaoning777 cf4cedc
unit test fix
gaoning777 e06fda6
minor fix
gaoning777 784889e
addressing comments
gaoning777 333e79a
removing the check_gcs_path function
gaoning777 57488d6
move namespace to the contructor of containerbuilder
gaoning777 d9f4ba0
fix bugs
gaoning777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import tarfile | ||
import tempfile | ||
import os | ||
import uuid | ||
|
||
class ContainerBuilder(object): | ||
""" | ||
ContainerBuilder helps build a container image | ||
hongye-sun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
def __init__(self, gcs_staging, namespace): | ||
""" | ||
Args: | ||
gcs_staging (str): GCS blob that can store temporary build files | ||
""" | ||
if not gcs_staging.startswith('gs://'): | ||
raise ValueError('Error: {} should be a GCS path.'.format(gcs_staging)) | ||
self._gcs_staging = gcs_staging | ||
self._namespace = namespace | ||
|
||
def _generate_kaniko_spec(self, context, docker_filename, target_image): | ||
"""_generate_kaniko_yaml generates kaniko job yaml based on a template yaml """ | ||
content = { | ||
'apiVersion': 'v1', | ||
'metadata': { | ||
'generateName': 'kaniko-', | ||
'namespace': self._namespace, | ||
}, | ||
'kind': 'Pod', | ||
'spec': { | ||
'restartPolicy': 'Never', | ||
'containers': [{ | ||
'name': 'kaniko', | ||
'args': ['--cache=true', | ||
'--dockerfile=' + docker_filename, | ||
'--context=' + context, | ||
'--destination=' + target_image], | ||
'image': 'gcr.io/kaniko-project/executor@sha256:78d44ec4e9cb5545d7f85c1924695c89503ded86a59f92c7ae658afa3cff5400', | ||
'env': [{ | ||
'name': 'GOOGLE_APPLICATION_CREDENTIALS', | ||
gaoning777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'value': '/secret/gcp-credentials/user-gcp-sa.json' | ||
}], | ||
'volumeMounts': [{ | ||
'mountPath': '/secret/gcp-credentials', | ||
'name': 'gcp-credentials', | ||
}], | ||
}], | ||
'volumes': [{ | ||
'name': 'gcp-credentials', | ||
'secret': { | ||
'secretName': 'user-gcp-sa', | ||
}, | ||
}], | ||
'serviceAccountName': 'default'} | ||
} | ||
return content | ||
|
||
def _wrap_dir_in_tarball(self, tarball_path, dir_name): | ||
""" _wrap_files_in_tarball creates a tarball for all the files in the directory""" | ||
old_wd = os.getcwd() | ||
os.chdir(dir_name) | ||
gaoning777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not tarball_path.endswith('.tar.gz'): | ||
raise ValueError('the tarball path should end with .tar.gz') | ||
gaoning777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with tarfile.open(tarball_path, 'w:gz') as tarball: | ||
for f in os.listdir(dir_name): | ||
tarball.add(f) | ||
os.chdir(old_wd) | ||
|
||
|
||
def build(self, local_dir, docker_filename, target_image, timeout): | ||
""" | ||
Args: | ||
local_dir (str): local directory that stores all the necessary build files | ||
docker_filename (str): the dockerfile name that is in the local_dir | ||
target_image (str): the target image tag to push the final image. | ||
timeout (int): time out in seconds | ||
""" | ||
# Prepare build context | ||
with tempfile.TemporaryDirectory() as local_build_dir: | ||
from ._gcs_helper import GCSHelper | ||
gaoning777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logging.info('Generate build files.') | ||
local_tarball_path = os.path.join(local_build_dir, 'docker.tmp.tar.gz') | ||
self._wrap_dir_in_tarball(local_tarball_path, local_dir) | ||
# Upload to the context | ||
context = os.path.join(self._gcs_staging, str(uuid.uuid4()) + '.tar.gz') | ||
GCSHelper.upload_gcs_file(local_tarball_path, context) | ||
|
||
# Run kaniko job | ||
kaniko_spec = self._generate_kaniko_spec(context=context, | ||
docker_filename=docker_filename, | ||
target_image=target_image) | ||
logging.info('Start a kaniko job for build.') | ||
from ._k8s_helper import K8sHelper | ||
k8s_helper = K8sHelper() | ||
k8s_helper.run_job(kaniko_spec, timeout) | ||
logging.info('Kaniko job complete.') | ||
|
||
# Clean up | ||
GCSHelper.remove_gcs_blob(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is only called once and most of the parameters are not used in that call. Maybe we should inline it and save 15 lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm declaring the function to make it easier in the future to prepare build files.