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: Add 'outputs.result' to Container templates #2584

Merged
merged 5 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ The following variables are made available to reference various metadata of a wo
| Variable | Description|
|----------|------------|
| `steps.<STEPNAME>.ip` | IP address of a previous daemon container step |
| `steps.<STEPNAME>.status` | Phase status of any previous script step |
| `steps.<STEPNAME>.outputs.result` | Output result of any previous script step |
| `steps.<STEPNAME>.status` | Phase status of any previous step |
| `steps.<STEPNAME>.outputs.result` | Output result of any previous container or script step |
| `steps.<STEPNAME>.outputs.parameters.<NAME>` | Output parameter of any previous step |
| `steps.<STEPNAME>.outputs.artifacts.<NAME>` | Output artifact of any previous step |

## DAG Templates
| Variable | Description|
|----------|------------|
| `tasks.<TASKNAME>.ip` | IP address of a previous daemon container task |
| `tasks.<TASKNAME>.status` | Phase status of any previous task step |
| `tasks.<TASKNAME>.outputs.result` | Output result of any previous script task |
| `tasks.<TASKNAME>.status` | Phase status of any previous task |
| `tasks.<TASKNAME>.outputs.result` | Output result of any previous container or script task |
| `tasks.<TASKNAME>.outputs.parameters.<NAME>` | Output parameter of any previous task |
| `tasks.<TASKNAME>.outputs.artifacts.<NAME>` | Output artifact of any previous task |

Expand Down
14 changes: 5 additions & 9 deletions examples/steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ spec:
template: whalesay
arguments:
parameters: [{name: message, value: "hello1"}]
- - name: hello2a
- - name: hello2
template: whalesay
arguments:
parameters: [{name: message, value: "hello2a"}]
- name: hello2b
template: whalesay
arguments:
parameters: [{name: message, value: "hello2b"}]
parameters: [{name: message, value: "{{steps.hello1.outputs.result}}"}]

- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
image: alpine:latest
command: [echo]
args: ["{{pod.name}}: {{inputs.parameters.message}}"]
49 changes: 35 additions & 14 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1767,8 +1767,18 @@ func (woc *wfOperationCtx) executeContainer(nodeName string, templateScope strin
return node, nil
}

// Check if the output of this container is referenced elsewhere in the Workflow. If so, make sure to include it during
// execution.
includeScriptOutput, err := woc.includeScriptOutput(nodeName, opts.boundaryID)
if err != nil {
return node, err
}

woc.log.Debugf("Executing node %s with container template: %v\n", nodeName, tmpl)
_, err := woc.createWorkflowPod(nodeName, *tmpl.Container, tmpl, &createWorkflowPodOpts{onExitPod: opts.onExitTemplate})
_, err = woc.createWorkflowPod(nodeName, *tmpl.Container, tmpl, &createWorkflowPodOpts{
includeScriptOutput: includeScriptOutput,
onExitPod: opts.onExitTemplate,
})

if apierr.IsForbidden(err) && isResubmitAllowed(tmpl) {
// Our error was most likely caused by a lack of resources. If pod resubmission is allowed, keep the node pending
Expand Down Expand Up @@ -1894,23 +1904,16 @@ func (woc *wfOperationCtx) executeScript(nodeName string, templateScope string,
}
node = woc.initializeExecutableNode(nodeName, wfv1.NodeTypePod, templateScope, tmpl, orgTmpl, opts.boundaryID, wfv1.NodePending)

includeScriptOutput := false
if boundaryNode, ok := woc.wf.Status.Nodes[opts.boundaryID]; ok {
tmplCtx, err := woc.createTemplateContext(boundaryNode.TemplateScope)
if err != nil {
return node, err
}
_, parentTemplate, err := tmplCtx.ResolveTemplate(&boundaryNode)
if err != nil {
return node, err
}
name := getStepOrDAGTaskName(nodeName)
includeScriptOutput = hasOutputResultRef(name, parentTemplate)
// Check if the output of this script is referenced elsewhere in the Workflow. If so, make sure to include it during
// execution.
includeScriptOutput, err := woc.includeScriptOutput(nodeName, opts.boundaryID)
if err != nil {
return node, err
}

mainCtr := tmpl.Script.Container
mainCtr.Args = append(mainCtr.Args, common.ExecutorScriptSourcePath)
_, err := woc.createWorkflowPod(nodeName, mainCtr, tmpl, &createWorkflowPodOpts{
_, err = woc.createWorkflowPod(nodeName, mainCtr, tmpl, &createWorkflowPodOpts{
includeScriptOutput: includeScriptOutput,
onExitPod: opts.onExitTemplate,
})
Expand Down Expand Up @@ -2525,3 +2528,21 @@ func (woc *wfOperationCtx) deletePDBResource() error {
woc.log.Infof("Deleted PDB resource for workflow.")
return nil
}

// Check if the output of this node is referenced elsewhere in the Workflow. If so, make sure to include it during
// execution.
func (woc *wfOperationCtx) includeScriptOutput(nodeName, boundaryID string) (bool, error) {
if boundaryNode, ok := woc.wf.Status.Nodes[boundaryID]; ok {
tmplCtx, err := woc.createTemplateContext(boundaryNode.TemplateScope)
if err != nil {
return false, err
}
_, parentTemplate, err := tmplCtx.ResolveTemplate(&boundaryNode)
if err != nil {
return false, err
}
name := getStepOrDAGTaskName(nodeName)
return hasOutputResultRef(name, parentTemplate), nil
}
return false, nil
}
54 changes: 54 additions & 0 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,3 +2568,57 @@ func TestPodSpecLogForAllPods(t *testing.T) {
}

}

var containerOutputsResult = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: steps-
spec:
entrypoint: hello-hello-hello
templates:
- name: hello-hello-hello
steps:
- - name: hello1
template: whalesay
arguments:
parameters: [{name: message, value: "hello1"}]
- - name: hello2
template: whalesay
arguments:
parameters: [{name: message, value: "{{steps.hello1.outputs.result}}"}]

- name: whalesay
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [echo]
args: ["{{pod.name}}: {{inputs.parameters.message}}"]
`

func TestContainerOutputsResult(t *testing.T) {

controller := newController()
wfcset := controller.wfclientset.ArgoprojV1alpha1().Workflows("")

// operate the workflow. it should create a pod.
wf := unmarshalWF(containerOutputsResult)
wf, err := wfcset.Create(wf)
assert.NoError(t, err)

assert.True(t, hasOutputResultRef("hello1", &wf.Spec.Templates[0]))
assert.False(t, hasOutputResultRef("hello2", &wf.Spec.Templates[0]))

woc := newWorkflowOperationCtx(wf, controller)
woc.operate()

for _, node := range wf.Status.Nodes {
if strings.Contains(node.Name, "hello1") {
assert.True(t, getStepOrDAGTaskName(node.Name) == "hello1")
} else if strings.Contains(node.Name, "hello2") {
assert.True(t, getStepOrDAGTaskName(node.Name) == "hello2")
}
}
}
simster7 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion workflow/controller/workflowpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ func (woc *wfOperationCtx) addMetadata(pod *apiv1.Pod, tmpl *wfv1.Template, incl

if woc.workflowDeadline != nil {
execCtl.Deadline = woc.workflowDeadline

}

if woc.workflowDeadline != nil || includeScriptOutput {
execCtlBytes, err := json.Marshal(execCtl)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion workflow/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ func (we *WorkflowExecutor) CaptureScriptResult() error {
log.Infof("No Script output reference in workflow. Capturing script output ignored")
return nil
}
if we.Template.Script == nil {
if we.Template.Script == nil && we.Template.Container == nil {
log.Infof("Template type is neither of Script or Container. Capturing script output ignored")
return nil
}
log.Infof("Capturing script output")
Expand All @@ -713,6 +714,14 @@ func (we *WorkflowExecutor) CaptureScriptResult() error {
if outputLen > 0 && out[outputLen-1] == '\n' {
out = out[0 : outputLen-1]
}

const maxAnnotationSize int = 256 * (1 << 10) // 256 kB
// A character in a string is a byte
if len(out) > maxAnnotationSize {
log.Warnf("Output is larger than the maximum allowed size of 256 kB, only the last 256 kB were saved")
out = out[len(out)-maxAnnotationSize:]
}

we.Template.Outputs.Result = &out
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ func (ctx *templateValidationCtx) addOutputsToScope(tmpl *wfv1.Template, prefix
if tmpl.Daemon != nil && *tmpl.Daemon {
scope[fmt.Sprintf("%s.ip", prefix)] = true
}
if tmpl.Script != nil {
if tmpl.Script != nil || tmpl.Container != nil {
scope[fmt.Sprintf("%s.outputs.result", prefix)] = true
}
for _, param := range tmpl.Outputs.Parameters {
Expand Down Expand Up @@ -798,6 +798,8 @@ func (ctx *templateValidationCtx) addOutputsToScope(tmpl *wfv1.Template, prefix
}
if aggregate {
switch tmpl.GetType() {
// Not that we don't also include TemplateTypeContainer here, even though it uses `outputs.result` it uses
// `outputs.parameters` as its aggregator.
case wfv1.TemplateTypeScript:
scope[fmt.Sprintf("%s.outputs.result", prefix)] = true
default:
Expand Down