Skip to content

Commit

Permalink
surface kubernetes configuration in container builder
Browse files Browse the repository at this point in the history
  • Loading branch information
casassg committed Jul 21, 2021
1 parent 354e425 commit e58637e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions sdk/python/kfp/containers/_container_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ContainerBuilder(object):
ContainerBuilder helps build a container image
"""
def __init__(self, gcs_staging=None, default_image_name=None, namespace=None,
service_account='kubeflow-pipelines-container-builder', kaniko_executor_image=KANIKO_EXECUTOR_IMAGE_DEFAULT):
service_account='kubeflow-pipelines-container-builder', kaniko_executor_image=KANIKO_EXECUTOR_IMAGE_DEFAULT, kubernetes_configuration=None):
"""
Args:
gcs_staging (str): GCS bucket/blob that can store temporary build files,
Expand All @@ -71,13 +71,16 @@ def __init__(self, gcs_staging=None, default_image_name=None, namespace=None,
The default value is "kubeflow-pipelines-container-builder". It works with Kubeflow Pipelines clusters installed using Google Cloud Marketplace or Standalone with version > 0.4.0.
The service account should have permission to read and write from staging gcs path and upload built images to gcr.io.
kaniko_executor_image (str): Docker image used to run kaniko executor. Defaults to gcr.io/kaniko-project/executor:v0.10.0.
kubernetes_configuration (kubernetes.Configuration): Kubernetes client configuration object to be used when talking with Kubernetes API.
This is optional. If not specified, it will use the default configuration. This can be used to personalize the client used to talk to the Kubernetes server and change authentication parameters.
"""
self._gcs_staging = gcs_staging
self._gcs_staging_checked = False
self._default_image_name = default_image_name
self._namespace = namespace
self._service_account = service_account
self._kaniko_image = kaniko_executor_image
self._kubernetes_configuration = kubernetes_configuration

def _get_namespace(self):
if self._namespace is None:
Expand Down Expand Up @@ -183,7 +186,7 @@ def build(self, local_dir, docker_filename : str = 'Dockerfile', target_image=No
target_image=target_image)
logging.info('Start a kaniko job for build.')
from ._k8s_job_helper import K8sJobHelper
k8s_helper = K8sJobHelper()
k8s_helper = K8sJobHelper(self._kubernetes_configuration)
result_pod_obj = k8s_helper.run_job(kaniko_spec, timeout)
logging.info('Kaniko job complete.')

Expand Down
10 changes: 5 additions & 5 deletions sdk/python/kfp/containers/_k8s_job_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
class K8sJobHelper(object):
""" Kubernetes Helper """

def __init__(self):
if not self._configure_k8s():
def __init__(self, kubernetes_configuration=None):
if not self._configure_k8s(kubernetes_configuration):
raise Exception('K8sHelper __init__ failure')

def _configure_k8s(self):
def _configure_k8s(self, kubernetes_configuration=None):
k8s_config_file = os.environ.get('KUBECONFIG')
if k8s_config_file:
try:
logging.info('Loading kubernetes config from the file %s', k8s_config_file)
config.load_kube_config(config_file=k8s_config_file)
config.load_kube_config(config_file=k8s_config_file, client_configuration=kubernetes_configuration)
except Exception as e:
raise RuntimeError('Can not load kube config from the file %s, error: %s', k8s_config_file, e)
else:
Expand All @@ -42,7 +42,7 @@ def _configure_k8s(self):
except:
logging.info('Cannot find in-cluster config, trying the local kubernetes config. ')
try:
config.load_kube_config()
config.load_kube_config(client_configuration=kubernetes_configuration)
logging.info('Found local kubernetes config. Initialized with kube_config.')
except:
raise RuntimeError('Forgot to run the gcloud command? Check out the link: \
Expand Down

0 comments on commit e58637e

Please sign in to comment.