-
Notifications
You must be signed in to change notification settings - Fork 132
/
workflow_methods.go
119 lines (103 loc) · 2.93 KB
/
workflow_methods.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
package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// WorkflowIDAnnotation is used by the controller to store the
// ID assigned to the workflow by Tinkerbell for migrated workflows.
WorkflowIDAnnotation = "workflow.tinkerbell.org/id"
)
// TinkID returns the Tinkerbell ID associated with this Workflow.
func (w *Workflow) TinkID() string {
return w.Annotations[WorkflowIDAnnotation]
}
// SetTinkID sets the Tinkerbell ID associated with this Workflow.
func (w *Workflow) SetTinkID(id string) {
if w.Annotations == nil {
w.Annotations = make(map[string]string)
}
w.Annotations[WorkflowIDAnnotation] = id
}
// GetStartTime returns the start time, for the first action of the first task.
func (w *Workflow) GetStartTime() *metav1.Time {
if len(w.Status.Tasks) > 0 {
if len(w.Status.Tasks[0].Actions) > 0 {
return w.Status.Tasks[0].Actions[0].StartedAt
}
}
return nil
}
type taskInfo struct {
CurrentWorker string
CurrentTask string
CurrentTaskIndex int
CurrentAction string
CurrentActionIndex int
CurrentActionState WorkflowState
TotalNumberOfActions int
}
// helper function for task info.
func (w *Workflow) getTaskActionInfo() taskInfo {
var (
found bool
taskIndex = -1
actionIndex int
actionTaskIndex int
actionCount int
)
for ti, task := range w.Status.Tasks {
actionCount += len(task.Actions)
if found {
continue
}
INNER:
for ai, action := range task.Actions {
// Find the first non-successful action
switch action.Status {
case WorkflowStateSuccess:
actionIndex++
continue
case WorkflowStatePending, WorkflowStateRunning, WorkflowStateFailed, WorkflowStateTimeout:
taskIndex = ti
actionTaskIndex = ai
found = true
break INNER
}
}
}
ti := taskInfo{
TotalNumberOfActions: actionCount,
CurrentActionIndex: actionIndex,
}
if taskIndex >= 0 {
ti.CurrentWorker = w.Status.Tasks[taskIndex].WorkerAddr
ti.CurrentTask = w.Status.Tasks[taskIndex].Name
ti.CurrentTaskIndex = taskIndex
}
if taskIndex >= 0 && actionIndex >= 0 {
ti.CurrentAction = w.Status.Tasks[taskIndex].Actions[actionTaskIndex].Name
ti.CurrentActionState = w.Status.Tasks[taskIndex].Actions[actionTaskIndex].Status
}
return ti
}
func (w *Workflow) GetCurrentWorker() string {
return w.getTaskActionInfo().CurrentWorker
}
func (w *Workflow) GetCurrentTask() string {
return w.getTaskActionInfo().CurrentTask
}
func (w *Workflow) GetCurrentTaskIndex() int {
return w.getTaskActionInfo().CurrentTaskIndex
}
func (w *Workflow) GetCurrentAction() string {
return w.getTaskActionInfo().CurrentAction
}
func (w *Workflow) GetCurrentActionIndex() int {
return w.getTaskActionInfo().CurrentActionIndex
}
func (w *Workflow) GetCurrentActionState() WorkflowState {
return w.getTaskActionInfo().CurrentActionState
}
func (w *Workflow) GetTotalNumberOfActions() int {
return w.getTaskActionInfo().TotalNumberOfActions
}