-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
steps.go
633 lines (578 loc) · 22.7 KB
/
steps.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package controller
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Knetic/govaluate"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util/template"
"github.com/argoproj/argo-workflows/v3/workflow/common"
controllercache "github.com/argoproj/argo-workflows/v3/workflow/controller/cache"
"github.com/argoproj/argo-workflows/v3/workflow/templateresolution"
)
// stepsContext holds context information about this context's steps
type stepsContext struct {
// boundaryID is the node ID of the boundary which all immediate child steps are bound to
boundaryID string
// scope holds parameter and artifacts which are referenceable in scope during execution
scope *wfScope
// tmplCtx is the context of template search.
tmplCtx *templateresolution.Context
// onExitTemplate is a flag denoting this template as part of an onExit handler. This is necessary to ensure that
// further nodes stemming from this template are allowed to run when using "ShutdownStrategy: Stop"
onExitTemplate bool
}
func (woc *wfOperationCtx) executeSteps(ctx context.Context, nodeName string, tmplCtx *templateresolution.Context, templateScope string, tmpl *wfv1.Template, orgTmpl wfv1.TemplateReferenceHolder, opts *executeTemplateOpts) (*wfv1.NodeStatus, error) {
node, err := woc.wf.GetNodeByName(nodeName)
if err != nil {
node = woc.initializeExecutableNode(nodeName, wfv1.NodeTypeSteps, templateScope, tmpl, orgTmpl, opts.boundaryID, wfv1.NodeRunning, opts.nodeFlag)
}
defer func() {
nodePhase, err := woc.wf.Status.Nodes.GetPhase(node.ID)
if err != nil {
woc.log.Fatalf("was unable to obtain nodePhase for %s", node.ID)
panic(fmt.Sprintf("unable to obtain nodePhase for %s", node.ID))
}
if nodePhase.Fulfilled() {
woc.killDaemonedChildren(node.ID)
}
}()
// The template scope of this step.
stepTemplateScope := tmplCtx.GetTemplateScope()
stepsCtx := stepsContext{
boundaryID: node.ID,
scope: createScope(tmpl),
tmplCtx: tmplCtx,
onExitTemplate: opts.onExitTemplate,
}
woc.addOutputsToLocalScope("workflow", woc.wf.Status.Outputs, stepsCtx.scope)
for i, stepGroup := range tmpl.Steps {
sgNodeName := fmt.Sprintf("%s[%d]", nodeName, i)
{
sgNode, err := woc.wf.GetNodeByName(sgNodeName)
if err != nil {
_ = woc.initializeNode(sgNodeName, wfv1.NodeTypeStepGroup, stepTemplateScope, &wfv1.WorkflowStep{}, stepsCtx.boundaryID, wfv1.NodeRunning, &wfv1.NodeFlag{})
} else if !sgNode.Fulfilled() {
_ = woc.markNodePhase(sgNodeName, wfv1.NodeRunning)
}
}
// The following will connect the step group node to its parents.
if i == 0 {
// If this is the first step group, the boundary node is the parent
woc.addChildNode(nodeName, sgNodeName)
} else {
// Otherwise connect all the outbound nodes of the previous step group as parents to
// the current step group node.
prevStepGroupName := fmt.Sprintf("%s[%d]", nodeName, i-1)
prevStepGroupNode, err := woc.wf.GetNodeByName(prevStepGroupName)
if err != nil {
return nil, err
}
if len(prevStepGroupNode.Children) == 0 {
// corner case which connects an empty StepGroup (e.g. due to empty withParams) to
// the previous StepGroup node
woc.addChildNode(prevStepGroupName, sgNodeName)
} else {
for _, childID := range prevStepGroupNode.Children {
outboundNodeIDs := woc.getOutboundNodes(childID)
woc.log.Infof("SG Outbound nodes of %s are %s", childID, outboundNodeIDs)
for _, outNodeID := range outboundNodeIDs {
outNodeName, err := woc.wf.Status.Nodes.GetName(outNodeID)
if err != nil {
woc.log.Fatalf("was not able to obtain node name for %s", outNodeID)
panic(fmt.Sprintf("could not obtain the out noden name for %s", outNodeID))
}
woc.addChildNode(outNodeName, sgNodeName)
}
}
}
}
sgNode, err := woc.executeStepGroup(ctx, stepGroup.Steps, sgNodeName, &stepsCtx)
if err != nil {
return nil, err
}
if !sgNode.Fulfilled() {
woc.log.Infof("Workflow step group node %s not yet completed", sgNode.ID)
return node, nil
}
if sgNode.FailedOrError() {
failMessage := fmt.Sprintf("step group %s was unsuccessful: %s", sgNode.ID, sgNode.Message)
woc.log.Info(failMessage)
if err = woc.updateOutboundNodes(nodeName, tmpl); err != nil {
return nil, err
}
return woc.markNodePhase(nodeName, wfv1.NodeFailed, sgNode.Message), nil
}
// Add all outputs of each step in the group to the scope
for _, step := range stepGroup.Steps {
childNodeName := fmt.Sprintf("%s.%s", sgNodeName, step.Name)
childNode, err := woc.wf.GetNodeByName(childNodeName)
prefix := fmt.Sprintf("steps.%s", step.Name)
if err != nil {
// This happens when there was `withItem/withParam` expansion.
// We add the aggregate outputs of our children to the scope as a JSON list
var childNodes []wfv1.NodeStatus
for _, node := range woc.wf.Status.Nodes {
if node.BoundaryID == stepsCtx.boundaryID && strings.HasPrefix(node.Name, childNodeName+"(") && node.Type != wfv1.NodeTypeSkipped {
childNodes = append(childNodes, node)
}
}
if len(childNodes) > 0 {
// Expanded child nodes should be created from the same template.
_, _, templateStored, err := stepsCtx.tmplCtx.ResolveTemplate(&childNodes[0])
if err != nil {
return node, err
}
// A new template was stored during resolution, persist it
if templateStored {
woc.updated = true
}
err = woc.processAggregateNodeOutputs(stepsCtx.scope, prefix, childNodes)
if err != nil {
return node, err
}
} else {
woc.log.Infof("Step '%s' has no expanded child nodes", childNode)
}
} else {
woc.buildLocalScope(stepsCtx.scope, prefix, childNode)
}
}
}
err = woc.updateOutboundNodes(nodeName, tmpl)
if err != nil {
return nil, err
}
// If this template has outputs from any of its steps, copy them to this node here
outputs, err := getTemplateOutputsFromScope(tmpl, stepsCtx.scope)
if err != nil {
return node, err
}
if outputs != nil {
node.Outputs = outputs
woc.addOutputsToGlobalScope(node.Outputs)
woc.wf.Status.Nodes.Set(node.ID, *node)
}
if node.MemoizationStatus != nil {
c := woc.controller.cacheFactory.GetCache(controllercache.ConfigMapCache, node.MemoizationStatus.CacheName)
err := c.Save(ctx, node.MemoizationStatus.Key, node.ID, node.Outputs)
if err != nil {
woc.log.WithFields(log.Fields{"nodeID": node.ID}).WithError(err).Error("Failed to save node outputs to cache")
node.Phase = wfv1.NodeError
}
}
return woc.markNodePhase(nodeName, wfv1.NodeSucceeded), nil
}
// updateOutboundNodes set the outbound nodes from the last step group
func (woc *wfOperationCtx) updateOutboundNodes(nodeName string, tmpl *wfv1.Template) error {
outbound := make([]string, 0)
// Find the last, initialized stepgroup node
var lastSGNode *wfv1.NodeStatus
var err error
for i := len(tmpl.Steps) - 1; i >= 0; i-- {
var sgNode *wfv1.NodeStatus
sgNode, err = woc.wf.GetNodeByName(fmt.Sprintf("%s[%d]", nodeName, i))
if err == nil {
lastSGNode = sgNode
break
}
}
if lastSGNode == nil {
woc.log.Warnf("node '%s' had no initialized StepGroup nodes", nodeName)
return err
}
for _, childID := range lastSGNode.Children {
outboundNodeIDs := woc.getOutboundNodes(childID)
woc.log.Infof("Outbound nodes of %s is %s", childID, outboundNodeIDs)
outbound = append(outbound, outboundNodeIDs...)
}
node, err := woc.wf.GetNodeByName(nodeName)
if err != nil {
return err
}
woc.log.Infof("Outbound nodes of %s is %s", node.ID, outbound)
node.OutboundNodes = outbound
woc.wf.Status.Nodes.Set(node.ID, *node)
return nil
}
// executeStepGroup examines a list of parallel steps and executes them in parallel.
// Handles referencing of variables in scope, expands `withItem` clauses, and evaluates `when` expressions
func (woc *wfOperationCtx) executeStepGroup(ctx context.Context, stepGroup []wfv1.WorkflowStep, sgNodeName string, stepsCtx *stepsContext) (*wfv1.NodeStatus, error) {
node, err := woc.wf.GetNodeByName(sgNodeName)
if err != nil {
return nil, err
}
if node.Fulfilled() {
woc.log.Debugf("Step group node %v already marked completed", node)
return node, nil
}
// First, resolve any references to outputs from previous steps, and perform substitution
stepGroup, err = woc.resolveReferences(stepGroup, stepsCtx.scope)
if err != nil {
return woc.markNodeError(sgNodeName, err), nil
}
// Next, expand the step's withItems (if any)
stepGroup, err = woc.expandStepGroup(sgNodeName, stepGroup, stepsCtx)
if err != nil {
return woc.markNodeError(sgNodeName, err), nil
}
// Maps nodes to their steps
nodeSteps := make(map[string]wfv1.WorkflowStep)
// The template scope of this step group.
stepTemplateScope := stepsCtx.tmplCtx.GetTemplateScope()
// Kick off all parallel steps in the group
for _, step := range stepGroup {
childNodeName := fmt.Sprintf("%s.%s", sgNodeName, step.Name)
// Check the step's when clause to decide if it should execute
proceed, err := shouldExecute(step.When)
if err != nil {
woc.initializeNode(childNodeName, wfv1.NodeTypeSkipped, stepTemplateScope, &step, stepsCtx.boundaryID, wfv1.NodeError, &wfv1.NodeFlag{}, err.Error())
woc.addChildNode(sgNodeName, childNodeName)
woc.markNodeError(childNodeName, err)
return woc.markNodeError(sgNodeName, err), nil
}
if !proceed {
if _, err := woc.wf.GetNodeByName(childNodeName); err != nil {
skipReason := fmt.Sprintf("when '%s' evaluated false", step.When)
woc.log.Infof("Skipping %s: %s", childNodeName, skipReason)
woc.initializeNode(childNodeName, wfv1.NodeTypeSkipped, stepTemplateScope, &step, stepsCtx.boundaryID, wfv1.NodeSkipped, &wfv1.NodeFlag{}, skipReason)
woc.addChildNode(sgNodeName, childNodeName)
}
continue
}
if stepsCtx.boundaryID == "" {
woc.log.Warnf("boundaryID was nil")
}
childNode, err := woc.executeTemplate(ctx, childNodeName, &step, stepsCtx.tmplCtx, step.Arguments, &executeTemplateOpts{boundaryID: stepsCtx.boundaryID, onExitTemplate: stepsCtx.onExitTemplate})
if err != nil {
switch err {
case ErrDeadlineExceeded:
return node, nil
case ErrParallelismReached:
case ErrMaxDepthExceeded:
case ErrTimeout:
return woc.markNodePhase(node.Name, wfv1.NodeFailed, err.Error()), nil
default:
woc.addChildNode(sgNodeName, childNodeName)
return woc.markNodeError(node.Name, fmt.Errorf("step group deemed errored due to child %s error: %w", childNodeName, err)), nil
}
}
if childNode != nil {
nodeSteps[childNodeName] = step
woc.addChildNode(sgNodeName, childNodeName)
}
}
node, err = woc.wf.GetNodeByName(sgNodeName)
if err != nil {
return nil, err
}
// Return if not all children completed
completed := true
for _, childNodeID := range node.Children {
childNode, err := woc.wf.Status.Nodes.Get(childNodeID)
if err != nil {
errorMsg := fmt.Sprintf("was unable to obtain childNode for %s", childNodeID)
woc.log.Errorf(errorMsg)
return nil, fmt.Errorf(errorMsg)
}
step := nodeSteps[childNode.Name]
stepsCtx.scope.addParamToScope(fmt.Sprintf("steps.%s.status", childNode.DisplayName), string(childNode.Phase))
hookCompleted, err := woc.executeTmplLifeCycleHook(ctx, stepsCtx.scope, step.Hooks, childNode, stepsCtx.boundaryID, stepsCtx.tmplCtx, "steps."+step.Name)
if err != nil {
woc.markNodeError(node.Name, err)
}
// Check all hooks are completed
if !hookCompleted {
return node, nil
}
if !childNode.Fulfilled() {
completed = false
} else if childNode.Completed() {
hasOnExitNode, onExitNode, err := woc.runOnExitNode(ctx, step.GetExitHook(woc.execWf.Spec.Arguments), childNode, stepsCtx.boundaryID, stepsCtx.tmplCtx, "steps."+step.Name, stepsCtx.scope)
if hasOnExitNode && (onExitNode == nil || !onExitNode.Fulfilled() || err != nil) {
// The onExit node is either not complete or has errored out, return.
completed = false
}
}
}
if !completed {
return node, nil
}
woc.addOutputsToGlobalScope(node.Outputs)
// All children completed. Determine step group status as a whole
for _, childNodeID := range node.Children {
childNode, err := woc.wf.Status.Nodes.Get(childNodeID)
if err != nil {
woc.log.Fatalf("was unable to obtain node for %s", childNodeID)
panic(fmt.Sprintf("unable to get childNode for %s", childNodeID))
}
step := nodeSteps[childNode.Name]
if childNode.FailedOrError() && !step.ContinuesOn(childNode.Phase) {
failMessage := fmt.Sprintf("child '%s' failed", childNodeID)
woc.log.Infof("Step group node %s deemed failed: %s", node.ID, failMessage)
return woc.markNodePhase(node.Name, wfv1.NodeFailed, failMessage), nil
}
}
woc.log.Infof("Step group node %v successful", node.ID)
return woc.markNodePhase(node.Name, wfv1.NodeSucceeded), nil
}
// shouldExecute evaluates a already substituted when expression to decide whether or not a step should execute
func shouldExecute(when string) (bool, error) {
if when == "" {
return true, nil
}
expression, err := govaluate.NewEvaluableExpression(when)
if err != nil {
if strings.Contains(err.Error(), "Invalid token") {
return false, errors.Errorf(errors.CodeBadRequest, "Invalid 'when' expression '%s': %v (hint: try wrapping the affected expression in quotes (\"))", when, err)
}
return false, errors.Errorf(errors.CodeBadRequest, "Invalid 'when' expression '%s': %v", when, err)
}
// The following loop converts govaluate variables (which we don't use), into strings. This
// allows us to have expressions like: "foo != bar" without requiring foo and bar to be quoted.
tokens := expression.Tokens()
for i, tok := range tokens {
switch tok.Kind {
case govaluate.VARIABLE:
tok.Kind = govaluate.STRING
default:
continue
}
tokens[i] = tok
}
expression, err = govaluate.NewEvaluableExpressionFromTokens(tokens)
if err != nil {
return false, errors.InternalWrapErrorf(err, "Failed to parse 'when' expression '%s': %v", when, err)
}
result, err := expression.Evaluate(nil)
if err != nil {
return false, errors.InternalWrapErrorf(err, "Failed to evaluate 'when' expresion '%s': %v", when, err)
}
boolRes, ok := result.(bool)
if !ok {
return false, errors.Errorf(errors.CodeBadRequest, "Expected boolean evaluation for '%s'. Got %v", when, result)
}
return boolRes, nil
}
// resolveReferences replaces any references to outputs of previous steps, or artifacts in the inputs
// NOTE: by now, input parameters should have been substituted throughout the template, so we only
// are concerned with:
// 1) dereferencing output.parameters from previous steps
// 2) dereferencing output.result from previous steps
// 3) dereferencing output.exitCode from previous steps
// 4) dereferencing artifacts from previous steps
// 5) dereferencing artifacts from inputs
func (woc *wfOperationCtx) resolveReferences(stepGroup []wfv1.WorkflowStep, scope *wfScope) ([]wfv1.WorkflowStep, error) {
newStepGroup := make([]wfv1.WorkflowStep, len(stepGroup))
// Step 0: replace all parameter scope references for volumes
err := woc.substituteParamsInVolumes(scope.getParameters())
if err != nil {
return nil, err
}
for i, step := range stepGroup {
// Step 1: replace all parameter scope references in the step
// TODO: improve this
stepBytes, err := json.Marshal(step)
if err != nil {
return nil, errors.InternalWrapError(err)
}
newStepStr, err := template.Replace(string(stepBytes), woc.globalParams.Merge(scope.getParameters()), true)
if err != nil {
return nil, err
}
var newStep wfv1.WorkflowStep
err = json.Unmarshal([]byte(newStepStr), &newStep)
if err != nil {
return nil, errors.InternalWrapError(err)
}
// If we are not executing, don't attempt to resolve any artifact references. We only check if we are executing after
// the initial parameter resolution, since it's likely that the "when" clause will contain parameter references.
proceed, err := shouldExecute(newStep.When)
if err != nil {
// If we got an error, it might be because our "when" clause contains a task-expansion parameter (e.g. {{item}}).
// Since we don't perform task-expansion until later and task-expansion parameters won't get resolved here,
// we continue execution as normal
if newStep.ShouldExpand() {
proceed = true
} else {
return nil, err
}
}
if !proceed {
// We can simply return this WorkflowStep; the fact that it won't execute will be reconciled later on in execution
newStepGroup[i] = newStep
continue
}
// Step 2: replace all artifact references
for j, art := range newStep.Arguments.Artifacts {
if art.From == "" && art.FromExpression == "" {
continue
}
resolvedArt, err := scope.resolveArtifact(&art)
if err != nil {
if art.Optional {
continue
}
return nil, fmt.Errorf("unable to resolve references: %s", err)
}
resolvedArt.Name = art.Name
newStep.Arguments.Artifacts[j] = *resolvedArt
}
newStepGroup[i] = newStep
}
return newStepGroup, nil
}
// expandStepGroup looks at each step in a collection of parallel steps, and expands all steps using withItems/withParam
func (woc *wfOperationCtx) expandStepGroup(sgNodeName string, stepGroup []wfv1.WorkflowStep, stepsCtx *stepsContext) ([]wfv1.WorkflowStep, error) {
newStepGroup := make([]wfv1.WorkflowStep, 0)
for _, step := range stepGroup {
if !step.ShouldExpand() {
newStepGroup = append(newStepGroup, step)
continue
}
expandedStep, err := woc.expandStep(step)
if err != nil {
return nil, err
}
if len(expandedStep) == 0 {
// Empty list
childNodeName := fmt.Sprintf("%s.%s", sgNodeName, step.Name)
if _, err := woc.wf.GetNodeByName(childNodeName); err != nil {
stepTemplateScope := stepsCtx.tmplCtx.GetTemplateScope()
skipReason := "Skipped, empty params"
woc.log.Infof("Skipping %s: %s", childNodeName, skipReason)
woc.initializeNode(childNodeName, wfv1.NodeTypeSkipped, stepTemplateScope, &step, stepsCtx.boundaryID, wfv1.NodeSkipped, &wfv1.NodeFlag{}, skipReason)
woc.addChildNode(sgNodeName, childNodeName)
}
}
newStepGroup = append(newStepGroup, expandedStep...)
}
return newStepGroup, nil
}
// expandStep expands a step containing withItems or withParams into multiple parallel steps
// We want to be lazy with expanding. Unfortunately this is not quite possible as the When field might rely on
// expansion to work with the shouldExecute function. To address this we apply a trick, we try to expand, if we fail, we then
// check shouldExecute, if shouldExecute returns false, we continue on as normal else error out
func (woc *wfOperationCtx) expandStep(step wfv1.WorkflowStep) ([]wfv1.WorkflowStep, error) {
var err error
expandedStep := make([]wfv1.WorkflowStep, 0)
var items []wfv1.Item
if len(step.WithItems) > 0 {
items = step.WithItems
} else if step.WithParam != "" {
err = json.Unmarshal([]byte(step.WithParam), &items)
if err != nil {
mustExec, mustExecErr := shouldExecute(step.When)
if mustExecErr != nil || mustExec {
return nil, errors.Errorf(errors.CodeBadRequest, "withParam value could not be parsed as a JSON list: %s: %v", strings.TrimSpace(step.WithParam), err)
}
}
} else if step.WithSequence != nil {
items, err = expandSequence(step.WithSequence)
if err != nil {
mustExec, mustExecErr := shouldExecute(step.When)
if mustExecErr != nil || mustExec {
return nil, err
}
}
} else {
// this should have been prevented in expandStepGroup()
return nil, errors.InternalError("expandStep() was called with withItems and withParam empty")
}
// these fields can be very large (>100m) and marshalling 10k x 100m = 6GB of memory used and
// very poor performance, so we just nil them out
step.WithItems = nil
step.WithParam = ""
step.WithSequence = nil
stepBytes, err := json.Marshal(step)
if err != nil {
return nil, errors.InternalWrapError(err)
}
t, err := template.NewTemplate(string(stepBytes))
if err != nil {
return nil, fmt.Errorf("unable to parse argo variable: %w", err)
}
for i, item := range items {
var newStep wfv1.WorkflowStep
newStepName, err := processItem(t, step.Name, i, item, &newStep, step.When)
if err != nil {
return nil, err
}
newStep.Name = newStepName
newStep.Template = step.Template
expandedStep = append(expandedStep, newStep)
}
return expandedStep, nil
}
func (woc *wfOperationCtx) prepareDefaultMetricScope() (map[string]string, map[string]func() float64) {
durationCPU := fmt.Sprintf("%s.%s", common.LocalVarResourcesDuration, v1.ResourceCPU)
durationMem := fmt.Sprintf("%s.%s", common.LocalVarResourcesDuration, v1.ResourceMemory)
localScope := woc.globalParams.DeepCopy()
localScope[common.LocalVarDuration] = "0"
localScope[common.LocalVarStatus] = string(wfv1.NodePending)
localScope[durationCPU] = "0"
localScope[durationMem] = "0"
var realTimeScope = map[string]func() float64{
common.GlobalVarWorkflowDuration: func() float64 {
if woc.wf.Status.Phase.Completed() {
return woc.wf.Status.FinishedAt.Time.Sub(woc.wf.Status.StartedAt.Time).Seconds()
}
return time.Since(woc.wf.Status.StartedAt.Time).Seconds()
},
}
return localScope, realTimeScope
}
func (woc *wfOperationCtx) prepareMetricScope(node *wfv1.NodeStatus) (map[string]string, map[string]func() float64) {
localScope, realTimeScope := woc.prepareDefaultMetricScope()
if node.Fulfilled() {
localScope[common.LocalVarDuration] = fmt.Sprintf("%f", node.FinishedAt.Sub(node.StartedAt.Time).Seconds())
realTimeScope[common.LocalVarDuration] = func() float64 {
return node.FinishedAt.Sub(node.StartedAt.Time).Seconds()
}
} else {
localScope[common.LocalVarDuration] = fmt.Sprintf("%f", time.Since(node.StartedAt.Time).Seconds())
realTimeScope[common.LocalVarDuration] = func() float64 {
return time.Since(node.StartedAt.Time).Seconds()
}
}
if node.Phase != "" {
localScope[common.LocalVarStatus] = string(node.Phase)
}
if node.Inputs != nil {
for _, param := range node.Inputs.Parameters {
key := fmt.Sprintf("inputs.parameters.%s", param.Name)
if param.Value == nil {
localScope[key] = ""
} else {
localScope[key] = param.Value.String()
}
}
}
if node.Outputs != nil {
if node.Outputs.Result != nil {
localScope["outputs.result"] = *node.Outputs.Result
}
if node.Outputs.ExitCode != nil {
localScope[common.LocalVarExitCode] = *node.Outputs.ExitCode
}
for _, param := range node.Outputs.Parameters {
key := fmt.Sprintf("outputs.parameters.%s", param.Name)
if param.Value == nil {
localScope[key] = ""
} else {
localScope[key] = param.Value.String()
}
}
}
if node.ResourcesDuration != nil {
for name, duration := range node.ResourcesDuration {
localScope[fmt.Sprintf("%s.%s", common.LocalVarResourcesDuration, name)] = fmt.Sprint(duration.Duration().Seconds())
}
}
return localScope, realTimeScope
}