-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_handler.go
330 lines (265 loc) · 10.1 KB
/
state_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
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
package pipelines
import (
"context"
"errors"
"fmt"
argo "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/sky-uk/kfp-operator/apis"
pipelinesv1 "github.com/sky-uk/kfp-operator/apis/pipelines/v1alpha5"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type StateHandler[R pipelinesv1.Resource] struct {
WorkflowFactory WorkflowFactory[R]
WorkflowRepository WorkflowRepository
}
var StateHandlerConstants = struct {
ProviderChangedError string
}{
ProviderChangedError: "the provider has changed",
}
func (st *StateHandler[R]) stateTransition(ctx context.Context, provider string, resource R) (commands []Command) {
resourceProvider := resource.GetStatus().ProviderId.Provider
if resourceProvider != "" && resourceProvider != provider {
commands = []Command{*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).
WithMessage(StateHandlerConstants.ProviderChangedError)}
} else {
switch resource.GetStatus().SynchronizationState {
case apis.Creating:
commands = st.onCreating(ctx, resource, st.WorkflowRepository.GetByLabels(ctx, CommonWorkflowLabels(resource)))
case apis.Succeeded, apis.Failed:
if !resource.GetDeletionTimestamp().IsZero() {
commands = st.onDelete(ctx, provider, resource)
} else {
commands = st.onSucceededOrFailed(ctx, provider, resource)
}
case apis.Updating:
commands = st.onUpdating(ctx, resource, st.WorkflowRepository.GetByLabels(ctx, CommonWorkflowLabels(resource)))
case apis.Deleting:
commands = st.onDeleting(ctx, resource, st.WorkflowRepository.GetByLabels(ctx, CommonWorkflowLabels(resource)))
case apis.Deleted:
default:
commands = st.onUnknown(ctx, provider, resource)
}
}
if resource.GetStatus().SynchronizationState == apis.Deleted {
commands = append([]Command{ReleaseResource{}}, commands...)
} else {
commands = append([]Command{AcquireResource{}}, commands...)
}
return
}
func (st *StateHandler[R]) StateTransition(ctx context.Context, provider string, resource R) []Command {
logger := log.FromContext(ctx)
logger.Info("state transition start")
stateTransitionCommands := st.stateTransition(ctx, provider, resource)
return alwaysSetObservedGeneration(ctx, stateTransitionCommands, resource)
}
func (st *StateHandler[R]) onUnknown(ctx context.Context, provider string, resource R) []Command {
logger := log.FromContext(ctx)
newVersion := resource.ComputeVersion()
if resource.GetStatus().ProviderId.Id != "" {
logger.Info("empty state but ProviderId already exists, updating resource")
workflow, err := st.WorkflowFactory.ConstructUpdateWorkflow(provider, resource)
if err != nil {
failureMessage := WorkflowConstants.ConstructionFailedError
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).
WithVersion(newVersion).
WithMessage(failureMessage),
}
}
return []Command{
*From(resource.GetStatus()).
WithSynchronizationState(apis.Updating).
WithVersion(newVersion),
CreateWorkflow{Workflow: *workflow},
}
}
logger.Info("empty state, creating resource")
workflow, err := st.WorkflowFactory.ConstructCreationWorkflow(provider, resource)
if err != nil {
failureMessage := WorkflowConstants.ConstructionFailedError
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).
WithVersion(newVersion).
WithMessage(failureMessage),
}
}
return []Command{
SetStatus{
Status: pipelinesv1.Status{
Version: newVersion,
SynchronizationState: apis.Creating,
},
},
CreateWorkflow{Workflow: *workflow},
}
}
func (st StateHandler[R]) onDelete(ctx context.Context, provider string, resource R) []Command {
logger := log.FromContext(ctx)
logger.Info("deletion requested, deleting")
if resource.GetStatus().ProviderId.Id == "" {
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Deleted),
}
}
workflow, err := st.WorkflowFactory.ConstructDeletionWorkflow(provider, resource)
if err != nil {
failureMessage := WorkflowConstants.ConstructionFailedError
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).WithMessage(failureMessage),
}
}
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Deleting),
CreateWorkflow{Workflow: *workflow},
}
}
func (st StateHandler[R]) onSucceededOrFailed(ctx context.Context, provider string, resource R) []Command {
logger := log.FromContext(ctx)
newExperimentVersion := resource.ComputeVersion()
if resource.GetStatus().Version == newExperimentVersion {
logger.V(2).Info("resource version has not changed")
return []Command{}
}
var workflow *argo.Workflow
var err error
var targetState apis.SynchronizationState
if resource.GetStatus().ProviderId.Id == "" {
logger.Info("no providerId exists, creating")
workflow, err = st.WorkflowFactory.ConstructCreationWorkflow(provider, resource)
if err != nil {
failureMessage := WorkflowConstants.ConstructionFailedError
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).
WithSynchronizationState(apis.Failed).
WithVersion(newExperimentVersion).
WithMessage(failureMessage),
}
}
targetState = apis.Creating
} else {
logger.Info("providerId exists, updating")
workflow, err = st.WorkflowFactory.ConstructUpdateWorkflow(provider, resource)
if err != nil {
failureMessage := WorkflowConstants.ConstructionFailedError
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).
WithSynchronizationState(apis.Failed).
WithVersion(newExperimentVersion).
WithMessage(failureMessage),
}
}
targetState = apis.Updating
}
return []Command{
*From(resource.GetStatus()).
WithSynchronizationState(targetState).
WithVersion(newExperimentVersion),
CreateWorkflow{Workflow: *workflow},
}
}
type IdVerifier struct {
SuccessState apis.SynchronizationState
FailureState apis.SynchronizationState
VerifyId func(string) error
}
var succeedForEmptyId = IdVerifier{
SuccessState: apis.Succeeded,
FailureState: apis.Failed,
VerifyId: func(id string) error {
if id == "" {
return errors.New("id was empty")
}
return nil
},
}
var deletedForNonEmptyId = IdVerifier{
SuccessState: apis.Deleted,
FailureState: apis.Deleting,
VerifyId: func(id string) error {
if id != "" {
return errors.New("id should be empty")
}
return nil
},
}
func (st StateHandler[R]) setStateIfProviderFinished(ctx context.Context, status pipelinesv1.Status, workflows []argo.Workflow, states IdVerifier) []Command {
logger := log.FromContext(ctx)
statusFromProviderOutput := func(workflow *argo.Workflow) *SetStatus {
result, err := getWorkflowOutput(workflow, WorkflowConstants.ProviderOutputParameterName)
provider := getWorkflowParameter(workflow, WorkflowConstants.ProviderNameParameterName)
if err != nil {
failureMessage := "could not retrieve workflow output"
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return From(status).WithSynchronizationState(states.FailureState).WithMessage(failureMessage)
}
providerAndId := pipelinesv1.ProviderAndId{Provider: provider, Id: result.Id}
if result.ProviderError != "" {
logger.Error(err, fmt.Sprintf("%s, failing resource", result.ProviderError))
return From(status).WithSynchronizationState(states.FailureState).WithMessage(result.ProviderError).WithProviderId(providerAndId)
}
err = states.VerifyId(result.Id)
if err != nil {
failureMessage := err.Error()
logger.Error(err, fmt.Sprintf("%s, failing resource", failureMessage))
return From(status).WithSynchronizationState(states.FailureState).WithMessage(failureMessage)
}
return From(status).WithSynchronizationState(states.SuccessState).WithProviderId(providerAndId)
}
inProgress, succeeded, failed := latestWorkflowByPhase(workflows)
if inProgress != nil {
logger.V(2).Info("operation in progress")
return []Command{}
}
var setStatusCommand *SetStatus
if succeeded != nil {
logger.Info("operation succeeded")
setStatusCommand = statusFromProviderOutput(succeeded)
} else {
var failureMessage string
if failed != nil {
failureMessage = "operation failed"
} else {
failureMessage = "operation progress unknown"
}
logger.Info(fmt.Sprintf("%s, failing resource", failureMessage))
setStatusCommand = From(status).WithSynchronizationState(states.FailureState).WithMessage(failureMessage)
}
return []Command{
*setStatusCommand,
MarkWorkflowsAsProcessed{
Workflows: workflows,
},
}
}
func (st StateHandler[R]) onCreating(ctx context.Context, resource R, creationWorkflows []argo.Workflow) []Command {
logger := log.FromContext(ctx)
if resource.GetStatus().Version == "" {
failureMessage := "creating resource with empty version"
logger.Info(fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).WithMessage(failureMessage),
}
}
return st.setStateIfProviderFinished(ctx, resource.GetStatus(), creationWorkflows, succeedForEmptyId)
}
func (st StateHandler[R]) onUpdating(ctx context.Context, resource R, updateWorkflows []argo.Workflow) []Command {
logger := log.FromContext(ctx)
if resource.GetStatus().Version == "" || resource.GetStatus().ProviderId.Id == "" {
failureMessage := "updating resource with empty version or providerId"
logger.Info(fmt.Sprintf("%s, failing resource", failureMessage))
return []Command{
*From(resource.GetStatus()).WithSynchronizationState(apis.Failed).WithMessage(failureMessage),
}
}
return st.setStateIfProviderFinished(ctx, resource.GetStatus(), updateWorkflows, succeedForEmptyId)
}
func (st StateHandler[R]) onDeleting(ctx context.Context, resource R, deletionWorkflows []argo.Workflow) []Command {
return st.setStateIfProviderFinished(ctx, resource.GetStatus(), deletionWorkflows, deletedForNonEmptyId)
}