/
updater.go
37 lines (34 loc) · 1.2 KB
/
updater.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
package resource
import (
wfv1 "github.com/argoproj/argo/v3/pkg/apis/workflow/v1alpha1"
)
func UpdateResourceDurations(wf *wfv1.Workflow) {
wf.Status.ResourcesDuration = wfv1.ResourcesDuration{}
for nodeID, node := range wf.Status.Nodes {
// pods are already calculated and so we do not need to compute them,
// AND they are the only node that should contribute to the total
if node.Type == wfv1.NodeTypePod {
wf.Status.ResourcesDuration = wf.Status.ResourcesDuration.Add(node.ResourcesDuration)
} else if node.Fulfilled() {
// compute the sum of all children
node.ResourcesDuration = resourceDuration(wf, node, make(map[string]bool))
wf.Status.Nodes[nodeID] = node
}
}
}
func resourceDuration(wf *wfv1.Workflow, node wfv1.NodeStatus, visited map[string]bool) wfv1.ResourcesDuration {
v := wfv1.ResourcesDuration{}
for _, childID := range node.Children {
// we do not want to visit the same node twice, as will (a) do 2x work and (b) make `v` incorrect
if visited[childID] {
continue
}
visited[childID] = true
child := wf.Status.Nodes[childID]
if child.Type == wfv1.NodeTypePod {
v = v.Add(child.ResourcesDuration)
}
v = v.Add(resourceDuration(wf, child, visited))
}
return v
}