Skip to content

Commit

Permalink
fix(sdk): update condition template (kubeflow#975)
Browse files Browse the repository at this point in the history
eliminate the use of `script`, replace it with
command and args. this also removes the init container that
prepares the script.

Signed-off-by: Yihong Wang <yh.wang@ibm.com>
  • Loading branch information
yhwang authored Jun 7, 2022
1 parent f043833 commit 66c0944
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 79 deletions.
9 changes: 6 additions & 3 deletions sdk/python/kfp_tekton/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
def _get_super_condition_template():

python_script = textwrap.dedent('''\
'import sys
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -72,7 +72,8 @@ def _get_super_condition_template():
%(s)s="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/%(s)s", "w")
f.write(%(s)s)
f.close()' '''
f.close()
'''
% {'s': DEFAULT_CONDITION_OUTPUT_KEYWORD})

template = {
Expand All @@ -88,7 +89,9 @@ def _get_super_condition_template():
{'name': 'operator'}
],
'steps': [{
'script': 'python -c ' + python_script + "'$(inputs.params.operand1)' '$(inputs.params.operand2)'",
'name': 'main',
'command': ['sh', '-ec', 'program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u "$program_path" "$1" "$2"'],
'args': [python_script, '$(inputs.params.operand1)', '$(inputs.params.operand2)'],
'image': 'python:alpine3.6',
}]
}
Expand Down
60 changes: 48 additions & 12 deletions sdk/python/tests/compiler/testdata/condition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -238,7 +245,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
- name: condition-2
params:
Expand All @@ -258,8 +267,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -270,7 +286,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
when:
- input: $(tasks.condition-1.results.outcome)
Expand All @@ -295,8 +313,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -307,7 +332,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
when:
- input: $(tasks.condition-2.results.outcome)
Expand All @@ -332,8 +359,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -344,6 +378,8 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
timeout: 525600m
45 changes: 36 additions & 9 deletions sdk/python/tests/compiler/testdata/condition_dependency.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -222,7 +229,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
- name: condition-2
params:
Expand All @@ -242,8 +251,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -254,7 +270,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
when:
- input: $(tasks.condition-1.results.outcome)
Expand All @@ -279,8 +297,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -291,6 +316,8 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
timeout: 525600m
30 changes: 24 additions & 6 deletions sdk/python/tests/compiler/testdata/conditions_and_loops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,15 @@ spec:
- name: operator
type: string
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3
-u "$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -408,7 +415,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
metadata:
labels:
Expand Down Expand Up @@ -436,8 +445,15 @@ spec:
- name: operator
type: string
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3
-u "$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -448,7 +464,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ metadata:
">"}], "taskSpec": {"params": [{"name": "operand1", "type": "string"}, {"name":
"operand2", "type": "string"}, {"name": "operator", "type": "string"}], "results":
[{"description": "Conditional task outcome", "name": "outcome", "type": "string"}],
"steps": [{"image": "python:alpine3.6", "script": "python -c ''import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
"steps": [{"args": ["import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
if (input1 $(inputs.params.operator) input2) else \"false\"\nf = open(\"/tekton/results/outcome\",
\"w\")\nf.write(outcome)\nf.close()'' ''$(inputs.params.operand1)'' ''$(inputs.params.operand2)''"}]}},
\"w\")\nf.write(outcome)\nf.close()\n", "$(inputs.params.operand1)", "$(inputs.params.operand2)"],
"command": ["sh", "-ec", "program_path=$(mktemp); printf \"%s\" \"$0\" > \"$program_path\"; python3
-u \"$program_path\" \"$1\" \"$2\""], "image": "python:alpine3.6", "name": "main"}]}},
{"name": "condition-3", "params": [{"name": "operand1", "value": "$(tasks.print-number.results.output)"},
{"name": "operand2", "value": "$(params.threshold)"}, {"name": "operator", "value":
"<="}], "taskSpec": {"params": [{"name": "operand1", "type": "string"}, {"name":
"operand2", "type": "string"}, {"name": "operator", "type": "string"}], "results":
[{"description": "Conditional task outcome", "name": "outcome", "type": "string"}],
"steps": [{"image": "python:alpine3.6", "script": "python -c ''import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
"steps": [{"args": ["import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
if (input1 $(inputs.params.operator) input2) else \"false\"\nf = open(\"/tekton/results/outcome\",
\"w\")\nf.write(outcome)\nf.close()'' ''$(inputs.params.operand1)'' ''$(inputs.params.operand2)''"}]}}]}}}]'
\"w\")\nf.write(outcome)\nf.close()\n", "$(inputs.params.operand1)", "$(inputs.params.operand2)"],
"command": ["sh", "-ec", "program_path=$(mktemp); printf \"%s\" \"$0\" > \"$program_path\"; python3
-u \"$program_path\" \"$1\" \"$2\""], "image": "python:alpine3.6", "name": "main"}]}}]}}}]'
spec:
params:
- name: "n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -307,7 +314,9 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
- name: condition-2
params:
Expand All @@ -327,8 +336,15 @@ spec:
- name: operand2
- name: operator
steps:
- script: |-
python -c 'import sys
- name: main
command:
- sh
- -ec
- program_path=$(mktemp); printf "%s" "$0" > "$program_path"; python3 -u
"$program_path" "$1" "$2"
args:
- |
import sys
input1=str.rstrip(sys.argv[1])
input2=str.rstrip(sys.argv[2])
try:
Expand All @@ -339,6 +355,8 @@ spec:
outcome="true" if (input1 $(inputs.params.operator) input2) else "false"
f = open("/tekton/results/outcome", "w")
f.write(outcome)
f.close()' '$(inputs.params.operand1)' '$(inputs.params.operand2)'
f.close()
- $(inputs.params.operand1)
- $(inputs.params.operand2)
image: python:alpine3.6
timeout: 525600m
7 changes: 4 additions & 3 deletions sdk/python/tests/compiler/testdata/loop_in_recursion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ metadata:
"", "pipelines.kubeflow.org/pipelinename": ""}}, "params": [{"name": "operand1",
"type": "string"}, {"name": "operand2", "type": "string"}, {"name": "operator",
"type": "string"}], "results": [{"description": "Conditional task outcome",
"name": "outcome", "type": "string"}], "steps": [{"image": "python:alpine3.6",
"script": "python -c ''import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
"name": "outcome", "type": "string"}], "steps": [{"args": ["import sys\ninput1=str.rstrip(sys.argv[1])\ninput2=str.rstrip(sys.argv[2])\ntry:\n input1=int(input1)\n input2=int(input2)\nexcept:\n input1=str(input1)\noutcome=\"true\"
if (input1 $(inputs.params.operator) input2) else \"false\"\nf = open(\"/tekton/results/outcome\",
\"w\")\nf.write(outcome)\nf.close()'' ''$(inputs.params.operand1)'' ''$(inputs.params.operand2)''"}]}},
\"w\")\nf.write(outcome)\nf.close()\n", "$(inputs.params.operand1)", "$(inputs.params.operand2)"],
"command": ["sh", "-ec", "program_path=$(mktemp); printf \"%s\" \"$0\" > \"$program_path\"; python3
-u \"$program_path\" \"$1\" \"$2\""], "image": "python:alpine3.6", "name": "main"}]}},
{"name": "flip-component", "params": [{"name": "flip-coin-output", "value":
"$(tasks.flip-coin-2.results.output)"}, {"name": "just_one_iteration", "value":
["1"]}, {"name": "maxVal", "value": "$(params.maxVal)"}, {"name": "my_pipe_param",
Expand Down
Loading

0 comments on commit 66c0944

Please sign in to comment.