-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
exit_handler.go
95 lines (87 loc) · 3.16 KB
/
exit_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package controller
import (
"context"
"encoding/json"
"fmt"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util/expr/argoexpr"
"github.com/argoproj/argo-workflows/v3/util/expr/env"
"github.com/argoproj/argo-workflows/v3/util/template"
"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/templateresolution"
)
func (woc *wfOperationCtx) runOnExitNode(ctx context.Context, exitHook *wfv1.LifecycleHook, parentNode *wfv1.NodeStatus, boundaryID string, tmplCtx *templateresolution.Context, prefix string, scope *wfScope) (bool, *wfv1.NodeStatus, error) {
outputs := parentNode.Outputs
if parentNode.Type == wfv1.NodeTypeRetry {
lastChildNode := getChildNodeIndex(parentNode, woc.wf.Status.Nodes, -1)
outputs = lastChildNode.Outputs
}
if exitHook != nil && woc.GetShutdownStrategy().ShouldExecute(true) {
execute := true
var err error
if exitHook.Expression != "" {
execute, err = argoexpr.EvalBool(exitHook.Expression, env.GetFuncMap(template.EnvMap(woc.globalParams.Merge(scope.getParameters()))))
if err != nil {
return true, nil, err
}
}
if execute {
woc.log.WithField("lifeCycleHook", exitHook).Infof("Running OnExit handler")
onExitNodeName := common.GenerateOnExitNodeName(parentNode.Name)
resolvedArgs := exitHook.Arguments
if !resolvedArgs.IsEmpty() && outputs != nil {
resolvedArgs, err = woc.resolveExitTmplArgument(exitHook.Arguments, prefix, outputs, scope)
if err != nil {
return true, nil, err
}
}
onExitNode, err := woc.executeTemplate(ctx, onExitNodeName, &wfv1.WorkflowStep{Template: exitHook.Template, TemplateRef: exitHook.TemplateRef}, tmplCtx, resolvedArgs, &executeTemplateOpts{
boundaryID: boundaryID,
onExitTemplate: true,
})
woc.addChildNode(parentNode.Name, onExitNodeName)
return true, onExitNode, err
}
}
return false, nil, nil
}
func (woc *wfOperationCtx) resolveExitTmplArgument(args wfv1.Arguments, prefix string, outputs *wfv1.Outputs, scope *wfScope) (wfv1.Arguments, error) {
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)
}
stepBytes, err := json.Marshal(args)
if err != nil {
return args, err
}
newStepStr, err := template.Replace(string(stepBytes), woc.globalParams.Merge(scope.getParameters()), true)
if err != nil {
return args, err
}
var newArgs wfv1.Arguments
err = json.Unmarshal([]byte(newStepStr), &newArgs)
if err != nil {
return args, err
}
// Step 2: replace all artifact references
for j, art := range newArgs.Artifacts {
if art.From == "" && art.FromExpression == "" {
continue
}
resolvedArt, err := scope.resolveArtifact(&art)
if err != nil {
if art.Optional {
continue
}
return args, fmt.Errorf("unable to resolve references: %s", err)
}
resolvedArt.Name = art.Name
newArgs.Artifacts[j] = *resolvedArt
}
return newArgs, nil
}