From 1efa7aab813959c0722736db35ab5d15d7f73c61 Mon Sep 17 00:00:00 2001 From: machichima Date: Sat, 7 Dec 2024 17:12:29 +0800 Subject: [PATCH 1/4] use ImageSpec in Raw Container image Signed-off-by: machichima --- .../customizing_dependencies/raw_container.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/customizing_dependencies/customizing_dependencies/raw_container.py b/examples/customizing_dependencies/customizing_dependencies/raw_container.py index 9d986da56..b8cead02a 100644 --- a/examples/customizing_dependencies/customizing_dependencies/raw_container.py +++ b/examples/customizing_dependencies/customizing_dependencies/raw_container.py @@ -1,6 +1,6 @@ import logging -from flytekit import ContainerTask, kwtypes, task, workflow +from flytekit import ContainerTask, kwtypes, task, workflow, ImageSpec from flytekit.core.base_task import TaskMetadata logger = logging.getLogger(__file__) @@ -35,7 +35,12 @@ output_data_dir="/var/outputs", inputs=kwtypes(a=float, b=float), outputs=kwtypes(area=float, metadata=str), - image="ghcr.io/flyteorg/rawcontainers-python:v2", + image=ImageSpec( + base_image="ghcr.io/flyteorg/rawcontainers-python:v2", + registry="localhost:30000", + builder="default", + copy=["calculate-ellipse-area.py"], + ), command=[ "python", "calculate-ellipse-area.py", From e01efc276137b34a81740b655c643435abeb798b Mon Sep 17 00:00:00 2001 From: machichima Date: Sat, 7 Dec 2024 17:16:44 +0800 Subject: [PATCH 2/4] add comments on ImageSpec Signed-off-by: machichima --- .../customizing_dependencies/raw_container.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/customizing_dependencies/customizing_dependencies/raw_container.py b/examples/customizing_dependencies/customizing_dependencies/raw_container.py index b8cead02a..ad6615fc8 100644 --- a/examples/customizing_dependencies/customizing_dependencies/raw_container.py +++ b/examples/customizing_dependencies/customizing_dependencies/raw_container.py @@ -29,6 +29,7 @@ metadata=TaskMetadata(cache=True, cache_version="1.0"), ) +# use `ImageSpec` to copy files or directories into container `/root` calculate_ellipse_area_python = ContainerTask( name="ellipse-area-metadata-python", input_data_dir="/var/inputs", From 3e7e2b2744e762f8b0be85cb75164522cd389902 Mon Sep 17 00:00:00 2001 From: machichima Date: Sat, 14 Dec 2024 00:49:40 +0800 Subject: [PATCH 3/4] fix: let ImageSpec copy new file into container Signed-off-by: machichima --- .../calculate-ellipse-area-new.py | 29 +++++++++++++++++++ .../customizing_dependencies/raw_container.py | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 examples/customizing_dependencies/customizing_dependencies/calculate-ellipse-area-new.py diff --git a/examples/customizing_dependencies/customizing_dependencies/calculate-ellipse-area-new.py b/examples/customizing_dependencies/customizing_dependencies/calculate-ellipse-area-new.py new file mode 100644 index 000000000..7c589da7c --- /dev/null +++ b/examples/customizing_dependencies/customizing_dependencies/calculate-ellipse-area-new.py @@ -0,0 +1,29 @@ +import math +import sys + + +def write_output(output_dir, output_file, v): + with open(f"{output_dir}/{output_file}", "w") as f: + f.write(str(v)) + + +def calculate_area(a, b): + return math.pi * a * b + + +def main(a, b, output_dir): + a = float(a) + b = float(b) + + area = calculate_area(a, b) + + write_output(output_dir, "area", area) + write_output(output_dir, "metadata", "[from python rawcontainer]") + + +if __name__ == "__main__": + a = sys.argv[1] + b = sys.argv[2] + output_dir = sys.argv[3] + + main(a, b, output_dir) diff --git a/examples/customizing_dependencies/customizing_dependencies/raw_container.py b/examples/customizing_dependencies/customizing_dependencies/raw_container.py index ad6615fc8..6d29e0761 100644 --- a/examples/customizing_dependencies/customizing_dependencies/raw_container.py +++ b/examples/customizing_dependencies/customizing_dependencies/raw_container.py @@ -40,11 +40,11 @@ base_image="ghcr.io/flyteorg/rawcontainers-python:v2", registry="localhost:30000", builder="default", - copy=["calculate-ellipse-area.py"], + copy=["calculate-ellipse-area-new.py"], ), command=[ "python", - "calculate-ellipse-area.py", + "calculate-ellipse-area-new.py", "{{.inputs.a}}", "{{.inputs.b}}", "/var/outputs", From 61eca0469bb19a6baee989dee80d0e6215ca5a2b Mon Sep 17 00:00:00 2001 From: machichima Date: Sat, 14 Dec 2024 10:02:45 +0800 Subject: [PATCH 4/4] fix: run lint Signed-off-by: machichima --- .../customizing_dependencies/raw_container.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/customizing_dependencies/customizing_dependencies/raw_container.py b/examples/customizing_dependencies/customizing_dependencies/raw_container.py index 6d29e0761..6e579283f 100644 --- a/examples/customizing_dependencies/customizing_dependencies/raw_container.py +++ b/examples/customizing_dependencies/customizing_dependencies/raw_container.py @@ -1,6 +1,6 @@ import logging -from flytekit import ContainerTask, kwtypes, task, workflow, ImageSpec +from flytekit import ContainerTask, ImageSpec, kwtypes, task, workflow from flytekit.core.base_task import TaskMetadata logger = logging.getLogger(__file__)