From fd469a351827907523dd3457af5f14786428f697 Mon Sep 17 00:00:00 2001 From: Andrzej Ressel Date: Sun, 15 Sep 2024 13:36:54 +0000 Subject: [PATCH] feat(script): allow setting file extension Signed-off-by: Andrzej Ressel --- docs/walk-through/scripts-and-results.md | 4 +- examples/scripts-java.yml | 4 +- examples/scripts-scala.yml | 4 +- pkg/apis/workflow/v1alpha1/workflow_types.go | 3 +- test/e2e/functional/scripts-java.yml | 48 +------------------- test/e2e/functional/scripts-scala.yml | 43 +----------------- workflow/controller/operator.go | 3 +- 7 files changed, 10 insertions(+), 99 deletions(-) mode change 100644 => 120000 test/e2e/functional/scripts-java.yml mode change 100644 => 120000 test/e2e/functional/scripts-scala.yml diff --git a/docs/walk-through/scripts-and-results.md b/docs/walk-through/scripts-and-results.md index fc81efa66a7a..9aceebb0e301 100644 --- a/docs/walk-through/scripts-and-results.md +++ b/docs/walk-through/scripts-and-results.md @@ -1,6 +1,6 @@ # Scripts And Results -Often, you just want a template that executes a script specified as a here-script (also known as a `here document`) in the workflow spec. This example shows how to do that: +You can use a `script` template to execute an inline script (also known as a ["here document"](https://en.wikipedia.org/wiki/Here_document)): ```yaml apiVersion: argoproj.io/v1alpha1 @@ -62,7 +62,7 @@ spec: - name: gen-random-scala script: image: virtuslab/scala-cli:1.5.0 - command: [scala-cli] # the scala-cli requires file to end in either `.scala` or `.sc` + command: [scala-cli] # the scala-cli requires files to end in either `.scala` or `.sc` extension: sc # the file will now end in `.sc` source: | import scala.util.Random diff --git a/examples/scripts-java.yml b/examples/scripts-java.yml index 07ec465531a6..80bbc02cd4f2 100644 --- a/examples/scripts-java.yml +++ b/examples/scripts-java.yml @@ -26,8 +26,8 @@ spec: - name: gen-random-int script: image: eclipse-temurin:22.0.2_9-jdk - command: [java] - extension: java + command: [java] # the Java interpreter requires files to end in `.java` + extension: java # the file will now end in `.java` source: | import java.util.*; diff --git a/examples/scripts-scala.yml b/examples/scripts-scala.yml index df0d30810394..4d96d8d6ef35 100644 --- a/examples/scripts-scala.yml +++ b/examples/scripts-scala.yml @@ -26,8 +26,8 @@ spec: - name: gen-random-int script: image: virtuslab/scala-cli:1.5.0 - command: [scala-cli] - extension: sc + command: [scala-cli] # the scala-cli requires file to end in either `.scala` or `.sc` + extension: sc # the file will now end in `.sc` source: | import scala.util.Random println(Random.between(0, 100)) diff --git a/pkg/apis/workflow/v1alpha1/workflow_types.go b/pkg/apis/workflow/v1alpha1/workflow_types.go index da02c87bd7fa..bbf7c65cbf88 100644 --- a/pkg/apis/workflow/v1alpha1/workflow_types.go +++ b/pkg/apis/workflow/v1alpha1/workflow_types.go @@ -823,8 +823,7 @@ const ( ) func (tmpl *Template) GetScriptSourcePath() string { - extension := tmpl.Script.Extension - if len(extension) != 0 { + if tmpl.Script != nil && len(tmpl.Script.Extension) != 0 { return executorScriptSourcePath + "." + tmpl.Script.Extension } else { return executorScriptSourcePath diff --git a/test/e2e/functional/scripts-java.yml b/test/e2e/functional/scripts-java.yml deleted file mode 100644 index 07ec465531a6..000000000000 --- a/test/e2e/functional/scripts-java.yml +++ /dev/null @@ -1,47 +0,0 @@ -# script templates provide a way to run arbitrary snippets of code -# in any language, to produce a output "result" via the standard out -# of the template. Results can then be referenced using the variable, -# {{steps..outputs.result}}, and used as parameter to other -# templates, and in 'when', and 'withParam' clauses. -# This example demonstrates the use of a java script to -# generate a random number which is printed in the next step. -apiVersion: argoproj.io/v1alpha1 -kind: Workflow -metadata: - generateName: scripts-java- -spec: - entrypoint: java-script-example - templates: - - name: java-script-example - steps: - - - name: generate - template: gen-random-int - - - name: print - template: print-message - arguments: - parameters: - - name: message - value: "{{steps.generate.outputs.result}}" - - - name: gen-random-int - script: - image: eclipse-temurin:22.0.2_9-jdk - command: [java] - extension: java - source: | - import java.util.*; - - public class Main { - public static void main(String[] args) { - System.out.println((int)(Math.random()*100)); - } - } - - - name: print-message - inputs: - parameters: - - name: message - container: - image: alpine:latest - command: [sh, -c] - args: ["echo result was: {{inputs.parameters.message}}"] \ No newline at end of file diff --git a/test/e2e/functional/scripts-java.yml b/test/e2e/functional/scripts-java.yml new file mode 120000 index 000000000000..80e3c485f976 --- /dev/null +++ b/test/e2e/functional/scripts-java.yml @@ -0,0 +1 @@ +../../../examples/scripts-java.yml \ No newline at end of file diff --git a/test/e2e/functional/scripts-scala.yml b/test/e2e/functional/scripts-scala.yml deleted file mode 100644 index df0d30810394..000000000000 --- a/test/e2e/functional/scripts-scala.yml +++ /dev/null @@ -1,42 +0,0 @@ -# script templates provide a way to run arbitrary snippets of code -# in any language, to produce a output "result" via the standard out -# of the template. Results can then be referenced using the variable, -# {{steps..outputs.result}}, and used as parameter to other -# templates, and in 'when', and 'withParam' clauses. -# This example demonstrates the use of a scala script to -# generate a random number which is printed in the next step. -apiVersion: argoproj.io/v1alpha1 -kind: Workflow -metadata: - generateName: scripts-scala- -spec: - entrypoint: scala-script-example - templates: - - name: scala-script-example - steps: - - - name: generate - template: gen-random-int - - - name: print - template: print-message - arguments: - parameters: - - name: message - value: "{{steps.generate.outputs.result}}" - - - name: gen-random-int - script: - image: virtuslab/scala-cli:1.5.0 - command: [scala-cli] - extension: sc - source: | - import scala.util.Random - println(Random.between(0, 100)) - - - name: print-message - inputs: - parameters: - - name: message - container: - image: alpine:latest - command: [sh, -c] - args: ["echo result was: {{inputs.parameters.message}}"] \ No newline at end of file diff --git a/test/e2e/functional/scripts-scala.yml b/test/e2e/functional/scripts-scala.yml new file mode 120000 index 000000000000..6e3b02fa993e --- /dev/null +++ b/test/e2e/functional/scripts-scala.yml @@ -0,0 +1 @@ +../../../examples/scripts-scala.yml \ No newline at end of file diff --git a/workflow/controller/operator.go b/workflow/controller/operator.go index 20d646ab24b5..598bc96c66f2 100644 --- a/workflow/controller/operator.go +++ b/workflow/controller/operator.go @@ -3088,8 +3088,7 @@ func (woc *wfOperationCtx) executeScript(ctx context.Context, nodeName string, t if len(tmpl.Script.Source) == 0 { woc.log.Warn("'script.source' is empty, suggest change template into 'container'") } else { - scriptSourcePath := tmpl.GetScriptSourcePath() - mainCtr.Args = append(mainCtr.Args, scriptSourcePath) + mainCtr.Args = append(mainCtr.Args, tmpl.GetScriptSourcePath()) } _, err = woc.createWorkflowPod(ctx, nodeName, []apiv1.Container{mainCtr}, tmpl, &createWorkflowPodOpts{ includeScriptOutput: includeScriptOutput,