Skip to content

Commit

Permalink
fix(backend): Fix condition with multi parameter issue (kubeflow#455)
Browse files Browse the repository at this point in the history
* enhance the when condition UI display

* Fix condition with multi parameter issue
  • Loading branch information
fenglixa authored Feb 3, 2021
1 parent 8c04dfb commit 6e1f367
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 2 deletions.
3 changes: 1 addition & 2 deletions sdk/python/kfp_tekton/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,8 @@ def _create_pipeline_workflow(self, args, pipeline, op_transformers=None, pipeli
'operator': 'in',
'values': ['true']
}]
most_recent_condition = cur_opsgroup.name
condition_task_ref['params'][param_iter]['value'] = input_params[param_iter]

most_recent_condition = cur_opsgroup.name
opsgroup_stack.extend(cur_opsgroup.groups)
condition_stack.extend([most_recent_condition for x in range(len(cur_opsgroup.groups))])
# add task dependencies and add condition refs to the task ref that depends on the condition
Expand Down
7 changes: 7 additions & 0 deletions sdk/python/tests/compiler/compiler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def test_withparam_output_workflow(self):
from .testdata.withparam_output import pipeline
self._test_pipeline_workflow(pipeline, 'withparam_output.yaml')

def test_conditions_with_global_params_workflow(self):
"""
Test conditions with global params in workflow.
"""
from .testdata.conditions_with_global_params import conditions_with_global_params
self._test_pipeline_workflow(conditions_with_global_params, 'conditions_with_global_params.yaml')

def test_pipelineparams_workflow(self):
"""
Test compiling a pipelineparams workflow.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[add-numbers : main] 20

[print-number : main] 20

[notify-success : main] SUCCESS!

[condition-1 : unnamed-0] + python -c import sys
[condition-1 : unnamed-0] input1=str.rstrip(sys.argv[1])
[condition-1 : unnamed-0] input2=str.rstrip(sys.argv[2])
[condition-1 : unnamed-0] try:
[condition-1 : unnamed-0] input1=int(input1)
[condition-1 : unnamed-0] input2=int(input2)
[condition-1 : unnamed-0] except:
[condition-1 : unnamed-0] input1=str(input1)
[condition-1 : unnamed-0] status="true" if (input1 > input2) else "false"
[condition-1 : unnamed-0] f = open("/tekton/results/status", "w")
[condition-1 : unnamed-0] f.write(status)
[condition-1 : unnamed-0] f.close() 20 10

[condition-2 : unnamed-0] + python -c import sys
[condition-2 : unnamed-0] input1=str.rstrip(sys.argv[1])
[condition-2 : unnamed-0] input2=str.rstrip(sys.argv[2])
[condition-2 : unnamed-0] try:
[condition-2 : unnamed-0] input1=int(input1)
[condition-2 : unnamed-0] input2=int(input2)
[condition-2 : unnamed-0] except:
[condition-2 : unnamed-0] input1=str(input1)
[condition-2 : unnamed-0] status="true" if (input1 <= input2) else "false"
[condition-2 : unnamed-0] f = open("/tekton/results/status", "w")
[condition-2 : unnamed-0] f.write(status)
[condition-2 : unnamed-0] f.close() 20 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2021 kubeflow.org
#
# 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.

from kfp import dsl
from kfp_tekton.compiler import TektonCompiler
from kfp import components


@components.func_to_container_op
def add_numbers(a: int, b: int) -> int:
print(a + b)
return a + b


@components.func_to_container_op
def print_number(a: int) -> int:
print(a)
return a


@components.func_to_container_op
def notify_success():
print('SUCCESS!')


@components.func_to_container_op
def notify_failure():
print('FAILED!')


@components.func_to_container_op
def produce_number() -> int:
import random
rn = random.randrange(0, 1000)
print(rn)
return rn


@dsl.pipeline(name='Conditions with global params')
def conditions_with_global_params(n='5', threshold='10', lower_bound='15'):
add_numbers_task = add_numbers(n, lower_bound)
print_number_task = print_number(add_numbers_task.output)
with dsl.Condition(print_number_task.output > threshold):
notify_success()

with dsl.Condition(print_number_task.output <= threshold):
notify_failure()


if __name__ == '__main__':
TektonCompiler().compile(conditions_with_global_params, __file__.replace('.py', '.yaml'))
Loading

0 comments on commit 6e1f367

Please sign in to comment.