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

return self for container op initialization methods to allow chaining the construction #313

Merged
merged 4 commits into from
Nov 20, 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
8 changes: 7 additions & 1 deletion sdk/python/kfp/dsl/_container_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def __init__(self, name: str, image: str, command: str=None, arguments: str=None
if len(self.outputs) == 1:
self.output = list(self.outputs.values())[0]


def after(self, op):
"""Specify explicit dependency on another op."""
self.dependent_op_names.append(op.name)
Expand Down Expand Up @@ -124,6 +123,7 @@ def set_memory_request(self, memory):

self._validate_memory_string(memory)
self.memory_request = memory
return self

def set_memory_limit(self, memory):
"""Set memory limit (maximum) for this operator.
Expand All @@ -134,6 +134,7 @@ def set_memory_limit(self, memory):
"""
self._validate_memory_string(memory)
self.memory_limit = memory
return self

def set_cpu_request(self, cpu):
"""Set cpu request (minimum) for this operator.
Expand All @@ -144,6 +145,7 @@ def set_cpu_request(self, cpu):

self._validate_cpu_string(cpu)
self.cpu_request = cpu
return self

def set_cpu_limit(self, cpu):
"""Set cpu limit (maximum) for this operator.
Expand All @@ -154,6 +156,7 @@ def set_cpu_limit(self, cpu):

self._validate_cpu_string(cpu)
self.cpu_limit = cpu
return self

def add_volume(self, volume):
"""Add K8s volume to the container
Expand All @@ -165,6 +168,7 @@ def add_volume(self, volume):
"""

self.volumes.append(volume)
return self

def add_volume_mount(self, volume_mount):
"""Add volume to the container
Expand All @@ -176,6 +180,7 @@ def add_volume_mount(self, volume_mount):
"""

self.volume_mounts.append(volume_mount)
return self

def add_env_variable(self, env_variable):
"""Add environment variable to the container.
Expand All @@ -187,6 +192,7 @@ def add_env_variable(self, env_variable):
"""

self.env_variables.append(env_variable)
return self

def __repr__(self):
return str({self.__class__.__name__: self.__dict__})
10 changes: 5 additions & 5 deletions sdk/python/tests/compiler/compiler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def test_operator_to_template(self):
msg2 = dsl.PipelineParam('msg2', value='value2')
op = dsl.ContainerOp(name='echo', image='image', command=['sh', '-c'],
arguments=['echo %s %s | tee /tmp/message.txt' % (msg1, msg2)],
file_outputs={'merged': '/tmp/message.txt'})
op.add_volume_mount(k8s_client.V1VolumeMount(
file_outputs={'merged': '/tmp/message.txt'}) \
.add_volume_mount(k8s_client.V1VolumeMount(
mount_path='/secret/gcp-credentials',
name='gcp-credentials'))
op.add_env_variable(k8s_client.V1EnvVar(
name='gcp-credentials')) \
.add_env_variable(k8s_client.V1EnvVar(
name='GOOGLE_APPLICATION_CREDENTIALS',
value='/secret/gcp-credentials/user-gcp-sa.json'))
golden_output = {
Expand Down Expand Up @@ -228,7 +228,7 @@ def _test_py_compile(self, file_base_name):
self.assertEqual(golden, compiled)
finally:
shutil.rmtree(tmpdir)

def test_py_compile_basic(self):
"""Test basic sequential pipeline."""
self._test_py_compile('basic')
Expand Down
18 changes: 9 additions & 9 deletions sdk/python/tests/compiler/testdata/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ def volume_pipeline():
image='google/cloud-sdk',
command=['sh', '-c'],
arguments=['ls | tee /tmp/results.txt'],
file_outputs={'downloaded': '/tmp/results.txt'})
op1.add_volume(k8s_client.V1Volume(name='gcp-credentials',
secret=k8s_client.V1SecretVolumeSource(
secret_name='user-gcp-sa')))
op1.add_volume_mount(k8s_client.V1VolumeMount(
mount_path='/secret/gcp-credentials', name='gcp-credentials'))
op1.add_env_variable(k8s_client.V1EnvVar(
file_outputs={'downloaded': '/tmp/results.txt'}) \
.add_volume(k8s_client.V1Volume(name='gcp-credentials',
secret=k8s_client.V1SecretVolumeSource(
secret_name='user-gcp-sa'))) \
.add_volume_mount(k8s_client.V1VolumeMount(
mount_path='/secret/gcp-credentials', name='gcp-credentials')) \
.add_env_variable(k8s_client.V1EnvVar(
name='GOOGLE_APPLICATION_CREDENTIALS',
value='/secret/gcp-credentials/user-gcp-sa.json'))
op1.add_env_variable(k8s_client.V1EnvVar(name='Foo',value='bar'))
value='/secret/gcp-credentials/user-gcp-sa.json')) \
.add_env_variable(k8s_client.V1EnvVar(name='Foo', value='bar'))
op2 = dsl.ContainerOp(
name='echo',
image='library/bash',
Expand Down