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

Make kubernetes a dependency of kfp package. #133

Merged
merged 2 commits into from
Nov 7, 2018
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
35 changes: 4 additions & 31 deletions sdk/python/kfp/compiler/_k8s_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
# limitations under the License.

from datetime import datetime
from kubernetes import client as k8s_client
from kubernetes import config
import time
import logging


class K8sHelper(object):
""" Kubernetes Helper """

Expand All @@ -24,25 +27,13 @@ def __init__(self):
raise Exception('K8sHelper __init__ failure')

def _configure_k8s(self):
try:
from kubernetes import client as k8s_client
from kubernetes import config
except ImportError:
logging.error('Kubernetes client is not installed')
return False
config.load_incluster_config()
self._api_client = k8s_client.ApiClient()
self._corev1 = k8s_client.CoreV1Api(self._api_client)
return True

def _create_k8s_job(self, yaml_spec):
""" _create_k8s_job creates a kubernetes job based on the yaml spec """
try:
from kubernetes import client as k8s_client
from kubernetes import config
except ImportError:
logging.error('Kubernetes client is not installed')
return '', False
pod = k8s_client.V1Pod(metadata=k8s_client.V1ObjectMeta(generate_name=yaml_spec['metadata']['generateName']))
container = k8s_client.V1Container(name = yaml_spec['spec']['containers'][0]['name'],
image= yaml_spec['spec']['containers'][0]['image'],
Expand All @@ -59,12 +50,6 @@ def _create_k8s_job(self, yaml_spec):

def _wait_for_k8s_job(self, pod_name, yaml_spec, timeout):
""" _wait_for_k8s_job waits for the job to complete """
try:
from kubernetes import client as k8s_client
from kubernetes import config
except ImportError:
logging.error('Kubernetes client is not installed')
return False
status = 'running'
start_time = datetime.now()
while status in ['pending', 'running']:
Expand All @@ -85,24 +70,12 @@ def _wait_for_k8s_job(self, pod_name, yaml_spec, timeout):

def _delete_k8s_job(self, pod_name, yaml_spec):
""" _delete_k8s_job deletes a pod """
try:
from kubernetes import client as k8s_client
from kubernetes import config
except ImportError:
logging.error('Kubernetes client is not installed')
return False
try:
api_response = self._corev1.delete_namespaced_pod(pod_name, yaml_spec['metadata']['namespace'], k8s_client.V1DeleteOptions())
except k8s_client.rest.ApiException as e:
logging.exception('Exception when calling CoreV1Api->delete_namespaced_pod: {}\n'.format(str(e)))

def _read_pod_log(self, pod_name, yaml_spec):
try:
from kubernetes import client as k8s_client
from kubernetes import config
except ImportError:
logging.error('Kubernetes client is not installed')
return False
try:
api_response = self._corev1.read_namespaced_pod_log(pod_name, yaml_spec['metadata']['namespace'])
except k8s_client.rest.ApiException as e:
Expand All @@ -123,4 +96,4 @@ def run_job(self, yaml_spec, timeout=600):
#TODO: investigate the read log error
# print(self._read_pod_log(pod_name, yaml_spec))
self._delete_k8s_job(pod_name, yaml_spec)
return succ
return succ
3 changes: 2 additions & 1 deletion sdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
NAME = 'kfp'
VERSION = '0.1'

REQUIRES = ['urllib3 >= 1.15', 'six >= 1.10', 'certifi', 'python-dateutil', 'PyYAML', 'google-cloud-storage >= 1.13.0']
REQUIRES = ['urllib3 >= 1.15', 'six >= 1.10', 'certifi', 'python-dateutil', 'PyYAML',
'google-cloud-storage == 1.13.0', 'kubernetes == 8.0.0']

setup(
name=NAME,
Expand Down