Skip to content

Commit

Permalink
fix: properly resolve exit handler inputs (fixes #12283) (#12288)
Browse files Browse the repository at this point in the history
Signed-off-by: ivancili <16418251+ivancili@users.noreply.github.com>
  • Loading branch information
ivancili authored and sarabala1979 committed Jan 8, 2024
1 parent b998c50 commit c1c4936
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
14 changes: 8 additions & 6 deletions workflow/controller/exit_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (woc *wfOperationCtx) runOnExitNode(ctx context.Context, exitHook *wfv1.Lif
woc.log.WithField("lifeCycleHook", exitHook).Infof("Running OnExit handler")
onExitNodeName := common.GenerateOnExitNodeName(parentNode.Name)
resolvedArgs := exitHook.Arguments
if !resolvedArgs.IsEmpty() && outputs != nil {
if !resolvedArgs.IsEmpty() {
resolvedArgs, err = woc.resolveExitTmplArgument(exitHook.Arguments, prefix, outputs, scope)
if err != nil {
return true, nil, err
Expand All @@ -56,11 +56,13 @@ func (woc *wfOperationCtx) resolveExitTmplArgument(args wfv1.Arguments, prefix s
if scope == nil {
scope = createScope(nil)
}
for _, param := range outputs.Parameters {
scope.addParamToScope(fmt.Sprintf("%s.outputs.parameters.%s", prefix, param.Name), param.Value.String())
}
for _, arts := range outputs.Artifacts {
scope.addArtifactToScope(fmt.Sprintf("%s.outputs.artifacts.%s", prefix, arts.Name), arts)
if outputs != nil {
for _, param := range outputs.Parameters {
scope.addParamToScope(fmt.Sprintf("%s.outputs.parameters.%s", prefix, param.Name), param.Value.String())
}
for _, arts := range outputs.Artifacts {
scope.addArtifactToScope(fmt.Sprintf("%s.outputs.artifacts.%s", prefix, arts.Name), arts)
}
}

stepBytes, err := json.Marshal(args)
Expand Down
73 changes: 73 additions & 0 deletions workflow/controller/exit_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,76 @@ status:
woc.operate(ctx)
assert.Equal(t, woc.wf.Status.Phase, wfv1.WorkflowRunning)
}

func TestStepsTemplateOnExitStatusArgument(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(`
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: lifecycle-hook-tmpl-level-
labels:
test: test
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: main
template: echo
hooks:
exit:
template: hook
arguments:
parameters:
- name: status
value: "{{steps.main.status}}"
- name: echo
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo hi"]
- name: hook
inputs:
parameters:
- name: status
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo {{inputs.parameters.status}}"]
`)
cancel, controller := newController(wf)
defer cancel()

ctx := context.Background()
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)

makePodsPhase(ctx, woc, apiv1.PodFailed)

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

var hasExitNode bool
var exitNodeName string

for _, node := range woc.wf.Status.Nodes {
if node.IsExitNode() {
hasExitNode = true
exitNodeName = node.DisplayName
break
}
}

assert.True(t, hasExitNode)
assert.NotEmpty(t, exitNodeName)

hookNode := woc.wf.Status.Nodes.FindByDisplayName(exitNodeName)

if assert.NotNil(t, hookNode) {
assert.NotNil(t, hookNode.Inputs)
if assert.Len(t, hookNode.Inputs.Parameters, 1) {
assert.NotNil(t, hookNode.Inputs.Parameters[0].Value)
assert.Equal(t, hookNode.Inputs.Parameters[0].Value.String(), string(apiv1.PodFailed))
}
}
}

0 comments on commit c1c4936

Please sign in to comment.