-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_api_workflow.go
214 lines (196 loc) · 7.58 KB
/
kubernetes_api_workflow.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
package server
import (
"context"
"github.com/jacobweinstock/tink-stack/api/v1alpha1"
"github.com/jacobweinstock/tink-stack/proto"
"github.com/jacobweinstock/tink-stack/tink/controller/workflow"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
errInvalidWorkflowID = "invalid workflow id"
errInvalidTaskName = "invalid task name"
errInvalidActionName = "invalid action name"
errInvalidTaskReported = "reported task name does not match the current action details"
errInvalidActionReported = "reported action name does not match the current action details"
)
func getWorkflowContext(wf v1alpha1.Workflow) *proto.WorkflowContext {
return &proto.WorkflowContext{
WorkflowId: wf.Name,
CurrentWorker: wf.GetCurrentWorker(),
CurrentTask: wf.GetCurrentTask(),
CurrentAction: wf.GetCurrentAction(),
CurrentActionIndex: int64(wf.GetCurrentActionIndex()),
CurrentActionState: proto.State(proto.State_value[string(wf.GetCurrentActionState())]),
TotalNumberOfActions: int64(wf.GetTotalNumberOfActions()),
}
}
func (s *KubernetesBackedServer) getCurrentAssignedNonTerminalWorkflowsForWorker(ctx context.Context, workerID string) ([]v1alpha1.Workflow, error) {
stored := &v1alpha1.WorkflowList{}
err := s.ClientFunc().List(ctx, stored, &client.MatchingFields{
workflowByNonTerminalState: workerID,
})
if err != nil {
return nil, err
}
wfs := []v1alpha1.Workflow{}
for _, wf := range stored.Items {
// If the current assigned or running action is assigned to the requested worker, include it
if wf.Status.Tasks[wf.GetCurrentTaskIndex()].WorkerAddr == workerID {
wfs = append(wfs, wf)
}
}
return wfs, nil
}
func (s *KubernetesBackedServer) getWorkflowByName(ctx context.Context, workflowID, namespace string) (*v1alpha1.Workflow, error) {
wflw := &v1alpha1.Workflow{}
err := s.ClientFunc().Get(ctx, types.NamespacedName{Name: workflowID, Namespace: namespace}, wflw)
if err != nil {
s.logger.Error(err, "get client", "workflow", workflowID)
return nil, err
}
return wflw, nil
}
// The following APIs are used by the worker.
func (s *KubernetesBackedServer) GetWorkflowContexts(req *proto.WorkflowContextRequest, stream proto.WorkflowService_GetWorkflowContextsServer) error {
if req.GetWorkerId() == "" {
return status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
wflows, err := s.getCurrentAssignedNonTerminalWorkflowsForWorker(stream.Context(), req.WorkerId)
if err != nil {
return err
}
for _, wf := range wflows {
if err := stream.Send(getWorkflowContext(wf)); err != nil {
return err
}
}
return nil
}
func (s *KubernetesBackedServer) GetWorkflowActions(ctx context.Context, req *proto.WorkflowActionsRequest) (*proto.WorkflowActionList, error) {
wfID := req.GetWorkflowId()
if wfID == "" {
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
wf, err := s.getWorkflowByName(ctx, wfID, s.namespace)
if err != nil {
return nil, err
}
return workflow.ActionListCRDToProto(wf), nil
}
// Modifies a workflow for a given workflowContext.
func (s *KubernetesBackedServer) modifyWorkflowState(wf *v1alpha1.Workflow, wfContext *proto.WorkflowContext) error {
if wf == nil {
return errors.New("no workflow provided")
}
if wfContext == nil {
return errors.New("no workflow context provided")
}
var (
taskIndex = -1
actionIndex = -1
)
for ti, task := range wf.Status.Tasks {
if wfContext.CurrentTask == task.Name {
taskIndex = ti
for ai, action := range task.Actions {
if action.Name == wfContext.CurrentAction && wfContext.CurrentActionIndex == int64(ai) {
actionIndex = ai
goto cont
}
}
}
}
cont:
if taskIndex < 0 {
return errors.New("task not found")
}
if actionIndex < 0 {
return errors.New("action not found")
}
wf.Status.Tasks[taskIndex].Actions[actionIndex].Status = v1alpha1.WorkflowState(proto.State_name[int32(wfContext.CurrentActionState)])
switch wfContext.CurrentActionState {
case proto.State_STATE_RUNNING:
// Workflow is running, so set the start time to now
wf.Status.State = v1alpha1.WorkflowState(proto.State_name[int32(wfContext.CurrentActionState)])
wf.Status.Tasks[taskIndex].Actions[actionIndex].StartedAt = func() *metav1.Time {
t := metav1.NewTime(s.nowFunc())
return &t
}()
case proto.State_STATE_FAILED, proto.State_STATE_TIMEOUT:
// Handle terminal statuses by updating the workflow state and time
wf.Status.State = v1alpha1.WorkflowState(proto.State_name[int32(wfContext.CurrentActionState)])
if wf.Status.Tasks[taskIndex].Actions[actionIndex].StartedAt != nil {
wf.Status.Tasks[taskIndex].Actions[actionIndex].Seconds = int64(s.nowFunc().Sub(wf.Status.Tasks[taskIndex].Actions[actionIndex].StartedAt.Time).Seconds())
}
case proto.State_STATE_SUCCESS:
// Handle a success by marking the task as complete
if wf.Status.Tasks[taskIndex].Actions[actionIndex].StartedAt != nil {
wf.Status.Tasks[taskIndex].Actions[actionIndex].Seconds = int64(s.nowFunc().Sub(wf.Status.Tasks[taskIndex].Actions[actionIndex].StartedAt.Time).Seconds())
}
// Mark success on last action success
if wfContext.CurrentActionIndex+1 == wfContext.TotalNumberOfActions {
wf.Status.State = v1alpha1.WorkflowState(proto.State_name[int32(wfContext.CurrentActionState)])
}
case proto.State_STATE_PENDING:
// This is probably a client bug?
return errors.New("no update requested")
}
return nil
}
func validateActionStatusRequest(req *proto.WorkflowActionStatus) error {
if req.GetWorkflowId() == "" {
return status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
if req.GetTaskName() == "" {
return status.Errorf(codes.InvalidArgument, errInvalidTaskName)
}
if req.GetActionName() == "" {
return status.Errorf(codes.InvalidArgument, errInvalidActionName)
}
return nil
}
func getWorkflowContextForRequest(req *proto.WorkflowActionStatus, wf *v1alpha1.Workflow) *proto.WorkflowContext {
wfContext := getWorkflowContext(*wf)
wfContext.CurrentWorker = req.GetWorkerId()
wfContext.CurrentTask = req.GetTaskName()
wfContext.CurrentActionState = req.GetActionStatus()
wfContext.CurrentActionIndex = int64(wf.GetCurrentActionIndex())
return wfContext
}
func (s *KubernetesBackedServer) ReportActionStatus(ctx context.Context, req *proto.WorkflowActionStatus) (*proto.Empty, error) {
err := validateActionStatusRequest(req)
if err != nil {
return nil, err
}
wfID := req.GetWorkflowId()
l := s.logger.WithValues("actionName", req.GetActionName(), "status", req.GetActionStatus(), "workflowID", req.GetWorkflowId(), "taskName", req.GetTaskName(), "worker", req.WorkerId)
wf, err := s.getWorkflowByName(ctx, wfID, s.namespace)
if err != nil {
l.Error(err, "get workflow")
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
if req.GetTaskName() != wf.GetCurrentTask() {
return nil, status.Errorf(codes.InvalidArgument, errInvalidTaskReported)
}
if req.GetActionName() != wf.GetCurrentAction() {
return nil, status.Errorf(codes.InvalidArgument, errInvalidActionReported)
}
wfContext := getWorkflowContextForRequest(req, wf)
err = s.modifyWorkflowState(wf, wfContext)
if err != nil {
l.Error(err, "modify workflow state")
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
l.Info("updating workflow in Kubernetes")
err = s.ClientFunc().Status().Update(ctx, wf)
if err != nil {
l.Error(err, "applying update to workflow")
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
return &proto.Empty{}, nil
}