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

feat(script): allow setting file extension #13555

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions api/jsonschema/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/executor_swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -3231,6 +3231,7 @@ cause implementors to also use a fixed point implementation.
| command | []string| `[]string` | | | Entrypoint array. Not executed within a shell.</br>The container image's ENTRYPOINT is used if this is not provided.</br>Variable references $(VAR_NAME) are expanded using the container's environment. If a variable</br>cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced</br>to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will</br>produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless</br>of whether the variable exists or not. Cannot be updated.</br>More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell</br>+optional | |
| env | [][EnvVar](#env-var)| `[]*EnvVar` | | | List of environment variables to set in the container.</br>Cannot be updated.</br>+optional</br>+patchMergeKey=name</br>+patchStrategy=merge | |
| envFrom | [][EnvFromSource](#env-from-source)| `[]*EnvFromSource` | | | List of sources to populate environment variables in the container.</br>The keys defined within a source must be a C_IDENTIFIER. All invalid keys</br>will be reported as an event when the container is starting. When a key exists in multiple</br>sources, the value associated with the last source will take precedence.</br>Values defined by an Env with a duplicate key will take precedence.</br>Cannot be updated.</br>+optional | |
| extension | string| `string` | | | Extension specifies the file extension to use | |
| image | string| `string` | | | Container image name.</br>More info: https://kubernetes.io/docs/concepts/containers/images</br>This field is optional to allow higher level config management to default or override</br>container images in workload controllers like Deployments and StatefulSets.</br>+optional | |
| imagePullPolicy | [PullPolicy](#pull-policy)| `PullPolicy` | | | | |
| lifecycle | [Lifecycle](#lifecycle)| `Lifecycle` | | | | |
Expand Down
1 change: 1 addition & 0 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,7 @@ ScriptTemplate is a template subtype to enable scripting through code steps
|`command`|`Array< string >`|Entrypoint array. Not executed within a shell. The container image's ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container's environment. If a variable cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless of whether the variable exists or not. Cannot be updated. More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell|
|`env`|`Array<`[`EnvVar`](#envvar)`>`|List of environment variables to set in the container. Cannot be updated.|
|`envFrom`|`Array<`[`EnvFromSource`](#envfromsource)`>`|List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.|
|`extension`|`string`|Extension specifies the file extension to use|
|`image`|`string`|Container image name. More info: https://kubernetes.io/docs/concepts/containers/images This field is optional to allow higher level config management to default or override container images in workload controllers like Deployments and StatefulSets.|
|`imagePullPolicy`|`string`|Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated. More info: https://kubernetes.io/docs/concepts/containers/images#updating-images|
|`lifecycle`|[`Lifecycle`](#lifecycle)|Actions that the management system should take in response to container lifecycle events. Cannot be updated.|
Expand Down
31 changes: 27 additions & 4 deletions docs/walk-through/scripts-and-results.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Scripts And Results

Often, we 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
Expand Down Expand Up @@ -45,6 +45,29 @@ spec:
var rand = Math.floor(Math.random() * 100);
console.log(rand);

- name: gen-random-int-java
script:
image: eclipse-temurin:22.0.2_9-jdk
command: [java] # the Java interpreter requires files to end in `.java`
extension: java # the file will now end in `.java`
source: |
import java.util.*;

public class Main {
public static void main(String[] args) {
System.out.println((int)(Math.random()*100));
}
}

- name: gen-random-scala
script:
image: virtuslab/scala-cli:1.5.0
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
println(Random.between(0, 100))

- name: print-message
inputs:
parameters:
Expand All @@ -55,6 +78,6 @@ spec:
args: ["echo result was: {{inputs.parameters.message}}"]
```

The `script` keyword allows the specification of the script body using the `source` tag. This creates a temporary file containing the script body and then passes the name of the temporary file as the final parameter to `command`, which should be an interpreter that executes the script body.

The use of the `script` feature also assigns the standard output of running the script to a special output parameter named `result`. This allows you to use the result of running the script itself in the rest of the workflow spec. In this example, the result is simply echoed by the print-message template.
You can specify a script body with the `source` field.
This creates a temporary file which is passed as the final parameter to `command`, which should be an interpreter.
You can set a file extension with `extension` field.
47 changes: 47 additions & 0 deletions examples/scripts-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.<stepname>.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] # the Java interpreter requires files to end in `.java`
extension: java # the file will now end in `.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}}"]
42 changes: 42 additions & 0 deletions examples/scripts-scala.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.<stepname>.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] # 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))

- name: print-message
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [sh, -c]
args: ["echo result was: {{inputs.parameters.message}}"]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions manifests/base/crds/full/argoproj.io_cronworkflows.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflows.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflowtasksets.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflowtemplates.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading