forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
465 lines (395 loc) · 13.4 KB
/
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
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
// Copyright 2021 Chaos Mesh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
wfcontrollers "github.com/chaos-mesh/chaos-mesh/pkg/workflow/controllers"
)
type WorkflowRepository interface {
List(ctx context.Context) ([]WorkflowMeta, error)
ListByNamespace(ctx context.Context, namespace string) ([]WorkflowMeta, error)
Create(ctx context.Context, workflow v1alpha1.Workflow) (WorkflowDetail, error)
Get(ctx context.Context, namespace, name string) (WorkflowDetail, error)
Delete(ctx context.Context, namespace, name string) error
Update(ctx context.Context, namespace, name string, workflow v1alpha1.Workflow) (WorkflowDetail, error)
}
type WorkflowStatus string
const (
WorkflowRunning WorkflowStatus = "running"
WorkflowSucceed WorkflowStatus = "finished"
WorkflowFailed WorkflowStatus = "failed"
WorkflowUnknown WorkflowStatus = "unknown"
)
// WorkflowMeta defines the root structure of a workflow.
type WorkflowMeta struct {
gorm.Model
UID string `gorm:"index:workflow_uid" json:"uid"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Entry string `json:"entry"` // the entry node name
CreatedAt string `json:"created_at"`
EndTime string `json:"end_time"`
Status WorkflowStatus `json:"status,omitempty"`
Archived bool `json:"-"`
}
type WorkflowDetail struct {
WorkflowMeta `json:",inline"`
Topology Topology `json:"topology"`
KubeObject KubeObjectDesc `json:"kube_object,omitempty"`
}
// Topology describes the process of a workflow.
type Topology struct {
Nodes []Node `json:"nodes"`
}
type NodeState string
const (
NodeRunning NodeState = "Running"
NodeSucceed NodeState = "Succeed"
NodeFailed NodeState = "Failed"
)
// Node defines a single step of a workflow.
type Node struct {
Name string `json:"name"`
Type NodeType `json:"type"`
State NodeState `json:"state"`
Serial *NodeSerial `json:"serial,omitempty"`
Parallel *NodeParallel `json:"parallel,omitempty"`
Template string `json:"template"`
UID string `json:"uid"`
}
type NodeNameWithTemplate struct {
Name string `json:"name,omitempty"`
Template string `json:"template,omitempty"`
}
// NodeSerial defines SerialNode's specific fields.
type NodeSerial struct {
Children []NodeNameWithTemplate `json:"children"`
}
// NodeParallel defines ParallelNode's specific fields.
type NodeParallel struct {
Children []NodeNameWithTemplate `json:"children"`
}
// NodeType represents the type of a workflow node.
//
// There will be five types can be referred as NodeType:
// ChaosNode, SerialNode, ParallelNode, SuspendNode, TaskNode.
//
// Const definitions can be found below this type.
type NodeType string
const (
// ChaosNode represents a node will perform a single Chaos Experiment.
ChaosNode NodeType = "ChaosNode"
// SerialNode represents a node that will perform continuous templates.
SerialNode NodeType = "SerialNode"
// ParallelNode represents a node that will perform parallel templates.
ParallelNode NodeType = "ParallelNode"
// SuspendNode represents a node that will perform wait operation.
SuspendNode NodeType = "SuspendNode"
// TaskNode represents a node that will perform user-defined task.
TaskNode NodeType = "TaskNode"
)
var nodeTypeTemplateTypeMapping = map[v1alpha1.TemplateType]NodeType{
v1alpha1.TypeSerial: SerialNode,
v1alpha1.TypeParallel: ParallelNode,
v1alpha1.TypeSuspend: SuspendNode,
v1alpha1.TypeTask: TaskNode,
}
type KubeWorkflowRepository struct {
kubeclient client.Client
}
func NewKubeWorkflowRepository(kubeclient client.Client) *KubeWorkflowRepository {
return &KubeWorkflowRepository{kubeclient: kubeclient}
}
func (it *KubeWorkflowRepository) Create(ctx context.Context, workflow v1alpha1.Workflow) (WorkflowDetail, error) {
err := it.kubeclient.Create(ctx, &workflow)
if err != nil {
return WorkflowDetail{}, err
}
return it.Get(ctx, workflow.Namespace, workflow.Name)
}
func (it *KubeWorkflowRepository) Update(ctx context.Context, namespace, name string, workflow v1alpha1.Workflow) (WorkflowDetail, error) {
current := v1alpha1.Workflow{}
err := it.kubeclient.Get(ctx, types.NamespacedName{
Namespace: namespace,
Name: name,
}, ¤t)
if err != nil {
return WorkflowDetail{}, err
}
workflow.ObjectMeta.ResourceVersion = current.ObjectMeta.ResourceVersion
err = it.kubeclient.Update(ctx, &workflow)
if err != nil {
return WorkflowDetail{}, err
}
return it.Get(ctx, workflow.Namespace, workflow.Name)
}
func (it *KubeWorkflowRepository) ListByNamespace(ctx context.Context, namespace string) ([]WorkflowMeta, error) {
workflowList := v1alpha1.WorkflowList{}
err := it.kubeclient.List(ctx, &workflowList, &client.ListOptions{
Namespace: namespace,
})
if err != nil {
return nil, err
}
var result []WorkflowMeta
for _, item := range workflowList.Items {
result = append(result, convertWorkflow(item))
}
return result, nil
}
func (it *KubeWorkflowRepository) List(ctx context.Context) ([]WorkflowMeta, error) {
return it.ListByNamespace(ctx, "")
}
func (it *KubeWorkflowRepository) Get(ctx context.Context, namespace, name string) (WorkflowDetail, error) {
kubeWorkflow := v1alpha1.Workflow{}
err := it.kubeclient.Get(ctx, types.NamespacedName{
Namespace: namespace,
Name: name,
}, &kubeWorkflow)
if err != nil {
return WorkflowDetail{}, err
}
workflowNodes := v1alpha1.WorkflowNodeList{}
// labeling workflow nodes, see pkg/workflow/controllers/new_node.go
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
v1alpha1.LabelWorkflow: kubeWorkflow.Name,
},
})
if err != nil {
return WorkflowDetail{}, err
}
err = it.kubeclient.List(ctx, &workflowNodes, &client.ListOptions{
Namespace: namespace,
LabelSelector: selector,
})
if err != nil {
return WorkflowDetail{}, err
}
return convertWorkflowDetail(kubeWorkflow, workflowNodes.Items)
}
func (it *KubeWorkflowRepository) Delete(ctx context.Context, namespace, name string) error {
kubeWorkflow := v1alpha1.Workflow{}
err := it.kubeclient.Get(ctx, types.NamespacedName{
Namespace: namespace,
Name: name,
}, &kubeWorkflow)
if err != nil {
return err
}
return it.kubeclient.Delete(ctx, &kubeWorkflow)
}
func convertWorkflow(kubeWorkflow v1alpha1.Workflow) WorkflowMeta {
result := WorkflowMeta{
Namespace: kubeWorkflow.Namespace,
Name: kubeWorkflow.Name,
Entry: kubeWorkflow.Spec.Entry,
UID: string(kubeWorkflow.UID),
}
if kubeWorkflow.Status.StartTime != nil {
result.CreatedAt = kubeWorkflow.Status.StartTime.Format(time.RFC3339)
}
if kubeWorkflow.Status.EndTime != nil {
result.EndTime = kubeWorkflow.Status.EndTime.Format(time.RFC3339)
}
if wfcontrollers.WorkflowConditionEqualsTo(kubeWorkflow.Status, v1alpha1.WorkflowConditionAccomplished, corev1.ConditionTrue) {
result.Status = WorkflowSucceed
} else if wfcontrollers.WorkflowConditionEqualsTo(kubeWorkflow.Status, v1alpha1.WorkflowConditionScheduled, corev1.ConditionTrue) {
result.Status = WorkflowRunning
} else {
result.Status = WorkflowUnknown
}
// TODO: status failed
return result
}
func convertWorkflowDetail(kubeWorkflow v1alpha1.Workflow, kubeNodes []v1alpha1.WorkflowNode) (WorkflowDetail, error) {
nodes := make([]Node, 0)
for _, item := range kubeNodes {
node, err := convertWorkflowNode(item)
if err != nil {
return WorkflowDetail{}, nil
}
nodes = append(nodes, node)
}
result := WorkflowDetail{
WorkflowMeta: convertWorkflow(kubeWorkflow),
Topology: Topology{
Nodes: nodes,
},
KubeObject: KubeObjectDesc{
TypeMeta: kubeWorkflow.TypeMeta,
Meta: KubeObjectMeta{
Name: kubeWorkflow.Name,
Namespace: kubeWorkflow.Namespace,
Labels: kubeWorkflow.Labels,
Annotations: kubeWorkflow.Annotations,
},
Spec: kubeWorkflow.Spec,
},
}
return result, nil
}
func convertWorkflowNode(kubeWorkflowNode v1alpha1.WorkflowNode) (Node, error) {
templateType, err := mappingTemplateType(kubeWorkflowNode.Spec.Type)
if err != nil {
return Node{}, err
}
result := Node{
Name: kubeWorkflowNode.Name,
Type: templateType,
Serial: nil,
Parallel: nil,
Template: kubeWorkflowNode.Spec.TemplateName,
UID: string(kubeWorkflowNode.UID),
}
if kubeWorkflowNode.Spec.Type == v1alpha1.TypeSerial {
var nodes []string
for _, child := range kubeWorkflowNode.Status.FinishedChildren {
nodes = append(nodes, child.Name)
}
for _, child := range kubeWorkflowNode.Status.ActiveChildren {
nodes = append(nodes, child.Name)
}
result.Serial = &NodeSerial{
Children: composeSerialTaskAndNodes(kubeWorkflowNode.Spec.Children, nodes),
}
} else if kubeWorkflowNode.Spec.Type == v1alpha1.TypeParallel {
var nodes []string
for _, child := range kubeWorkflowNode.Status.FinishedChildren {
nodes = append(nodes, child.Name)
}
for _, child := range kubeWorkflowNode.Status.ActiveChildren {
nodes = append(nodes, child.Name)
}
result.Parallel = &NodeParallel{
Children: composeParallelTaskAndNodes(kubeWorkflowNode.Spec.Children, nodes),
}
}
if wfcontrollers.WorkflowNodeFinished(kubeWorkflowNode.Status) {
result.State = NodeSucceed
} else {
result.State = NodeRunning
}
return result, nil
}
// composeSerialTaskAndNodes need nodes to be ordered with its creation time
func composeSerialTaskAndNodes(children []string, nodes []string) []NodeNameWithTemplate {
var result []NodeNameWithTemplate
for _, node := range nodes {
// TODO: that reverse the generated name, maybe we could use WorkflowNode.TemplateName in the future
templateName := node[0:strings.LastIndex(node, "-")]
result = append(result, NodeNameWithTemplate{Name: node, Template: templateName})
}
for _, task := range children[len(nodes):] {
result = append(result, NodeNameWithTemplate{Template: task})
}
return result
}
func composeParallelTaskAndNodes(children []string, nodes []string) []NodeNameWithTemplate {
var result []NodeNameWithTemplate
for _, task := range children {
result = append(result, NodeNameWithTemplate{
Name: "",
Template: task,
})
}
for _, node := range nodes {
for i, item := range result {
if len(item.Name) == 0 && strings.HasPrefix(node, item.Template) {
result[i].Name = node
break
}
}
}
return result
}
func mappingTemplateType(templateType v1alpha1.TemplateType) (NodeType, error) {
if v1alpha1.IsChaosTemplateType(templateType) {
return ChaosNode, nil
} else if target, ok := nodeTypeTemplateTypeMapping[templateType]; ok {
return target, nil
} else {
return "", errors.Errorf("can not resolve such type called %s", templateType)
}
}
// The WorkflowStore of workflow is not so similar with others store.
type WorkflowStore interface {
List(ctx context.Context, namespace, name string, archived bool) ([]*WorkflowEntity, error)
ListMeta(ctx context.Context, namespace, name string, archived bool) ([]*WorkflowMeta, error)
FindByID(ctx context.Context, ID uint) (*WorkflowEntity, error)
FindByUID(ctx context.Context, UID string) (*WorkflowEntity, error)
FindMetaByUID(ctx context.Context, UID string) (*WorkflowMeta, error)
Save(ctx context.Context, entity *WorkflowEntity) error
DeleteByUID(ctx context.Context, UID string) error
DeleteByUIDs(ctx context.Context, UIDs []string) error
MarkAsArchived(ctx context.Context, namespace, name string) error
MarkAsArchivedWithUID(ctx context.Context, UID string) error
}
// WorkflowEntity is the gorm entity, refers to a row of data
type WorkflowEntity struct {
WorkflowMeta
Workflow string `gorm:"size:32768"`
}
func WorkflowCR2WorkflowEntity(workflow *v1alpha1.Workflow) (*WorkflowEntity, error) {
if workflow == nil {
return nil, nil
}
jsonContent, err := json.Marshal(workflow)
if err != nil {
return nil, err
}
return &WorkflowEntity{
WorkflowMeta: convertWorkflow(*workflow),
Workflow: string(jsonContent),
}, nil
}
func WorkflowEntity2WorkflowCR(entity *WorkflowEntity) (*v1alpha1.Workflow, error) {
if entity == nil {
return nil, nil
}
result := v1alpha1.Workflow{}
err := json.Unmarshal([]byte(entity.Workflow), &result)
if err != nil {
return nil, err
}
return &result, nil
}
func WorkflowEntity2WorkflowDetail(entity *WorkflowEntity) (*WorkflowDetail, error) {
workflowCustomResource, err := WorkflowEntity2WorkflowCR(entity)
if err != nil {
return nil, err
}
return &WorkflowDetail{
WorkflowMeta: entity.WorkflowMeta,
KubeObject: KubeObjectDesc{
TypeMeta: workflowCustomResource.TypeMeta,
Meta: KubeObjectMeta{
Name: workflowCustomResource.Name,
Namespace: workflowCustomResource.Namespace,
Labels: workflowCustomResource.Labels,
Annotations: workflowCustomResource.Annotations,
},
Spec: workflowCustomResource.Spec,
},
}, nil
}