forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.go
441 lines (384 loc) · 15.9 KB
/
experiment.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
// Copyright 2020 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"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jinzhu/gorm"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)
// ExperimentStore defines operations for working with experiments.
type ExperimentStore interface {
// ListMeta returns experiment metadata list from the datastore.
ListMeta(ctx context.Context, kind, namespace, name string, archived bool) ([]*ExperimentMeta, error)
// FindByUID returns an experiment by UID.
FindByUID(ctx context.Context, UID string) (*Experiment, error)
// FindMetaByUID returns an experiment metadata by UID.
FindMetaByUID(context.Context, string) (*ExperimentMeta, error)
// Set saves the experiment to datastore.
Set(context.Context, *Experiment) error
// Archive archives experiments which "archived" field is false.
Archive(ctx context.Context, namespace, name string) error
// Delete deletes the archive from the datastore.
Delete(context.Context, *Experiment) error
// DeleteByFinishTime deletes archives which time difference is greater than the given time from FinishTime.
DeleteByFinishTime(context.Context, time.Duration) error
// DeleteByUIDs deletes archives by the uid list.
DeleteByUIDs(context.Context, []string) error
// DeleteIncompleteExperiments deletes all incomplete experiments.
// If the chaos-dashboard was restarted and the experiment is completed during the restart,
// which means the experiment would never save the finish_time.
// DeleteIncompleteExperiments can be used to delete all incomplete experiments to avoid this case.
DeleteIncompleteExperiments(context.Context) error
}
// Experiment represents an experiment instance. Use in db.
type Experiment struct {
ExperimentMeta
Experiment string `gorm:"size:2048"` // JSON string
}
// ExperimentMeta defines the metadata of an experiment. Use in db.
type ExperimentMeta struct {
gorm.Model
UID string `gorm:"index:uid" json:"uid"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Archived bool `json:"archived"`
}
// ExperimentInfo defines a form data of Experiment from API.
type ExperimentInfo struct {
Name string `json:"name" binding:"required,NameValid"`
Namespace string `json:"namespace" binding:"required,NameValid"`
Labels map[string]string `json:"labels" binding:"MapSelectorsValid"`
Annotations map[string]string `json:"annotations" binding:"MapSelectorsValid"`
Scope ScopeInfo `json:"scope"`
Target TargetInfo `json:"target"`
Scheduler SchedulerInfo `json:"scheduler"`
}
// ScopeInfo defines the scope of the Experiment.
type ScopeInfo struct {
SelectorInfo
Mode string `json:"mode" binding:"oneof='' 'one' 'all' 'fixed' 'fixed-percent' 'random-max-percent'"`
Value string `json:"value" binding:"ValueValid"`
}
// SelectorInfo defines the selector options of the Experiment.
type SelectorInfo struct {
Namespaces []string `json:"namespaces,omitempty" binding:"NamespaceSelectorsValid"`
Nodes []string `json:"nodes,omitempty"`
NodeSelectors map[string]string `json:"node_selectors,omitempty"`
FieldSelectors map[string]string `json:"field_selectors,omitempty" binding:"MapSelectorsValid"`
LabelSelectors map[string]string `json:"label_selectors,omitempty" binding:"MapSelectorsValid"`
ExpressionSelectors []metav1.LabelSelectorRequirement `json:"expression_selectors,omitempty" binding:"RequirementSelectorsValid"`
AnnotationSelectors map[string]string `json:"annotation_selectors,omitempty" binding:"MapSelectorsValid"`
PodPhaseSelectors []string `json:"phase_selectors,omitempty" binding:"PhaseSelectorsValid"`
// Pods is a map of string keys and a set values that used to select pods.
// The key defines the namespace which pods belong,
// and the each values is a set of pod names.
Pods map[string][]string `json:"pods,omitempty" binding:"PodsValid"`
}
// ParseSelector parses SelectorInfo to v1alpha1.SelectorSpec
func (s *SelectorInfo) ParseSelector() v1alpha1.PodSelectorSpec {
selector := v1alpha1.PodSelectorSpec{}
selector.Namespaces = append(selector.Namespaces, s.Namespaces...)
selector.LabelSelectors = make(map[string]string)
for key, val := range s.LabelSelectors {
selector.LabelSelectors[key] = val
}
selector.ExpressionSelectors = append(selector.ExpressionSelectors, s.ExpressionSelectors...)
selector.AnnotationSelectors = make(map[string]string)
for key, val := range s.AnnotationSelectors {
selector.AnnotationSelectors[key] = val
}
selector.FieldSelectors = make(map[string]string)
for key, val := range s.FieldSelectors {
selector.FieldSelectors[key] = val
}
selector.PodPhaseSelectors = append(selector.PodPhaseSelectors, s.PodPhaseSelectors...)
if s.Pods != nil {
selector.Pods = s.Pods
}
return selector
}
// TargetInfo defines the information of target objects.
type TargetInfo struct {
Kind string `json:"kind" binding:"required,oneof=PodChaos NetworkChaos IOChaos KernelChaos TimeChaos StressChaos DNSChaos AwsChaos GcpChaos"`
PodChaos *PodChaosInfo `json:"pod_chaos,omitempty" binding:"RequiredFieldEqual=Kind:PodChaos"`
NetworkChaos *NetworkChaosInfo `json:"network_chaos,omitempty" binding:"RequiredFieldEqual=Kind:NetworkChaos"`
IOChaos *IOChaosInfo `json:"io_chaos,omitempty" binding:"RequiredFieldEqual=Kind:IOChaos"`
KernelChaos *KernelChaosInfo `json:"kernel_chaos,omitempty" binding:"RequiredFieldEqual=Kind:KernelChaos"`
TimeChaos *TimeChaosInfo `json:"time_chaos,omitempty" binding:"RequiredFieldEqual=Kind:TimeChaos"`
StressChaos *StressChaosInfo `json:"stress_chaos,omitempty" binding:"RequiredFieldEqual=Kind:StressChaos"`
DNSChaos *DNSChaosInfo `json:"dns_chaos,omitempty" binding:"RequiredFieldEqual=Kind:DNSChaos"`
AwsChaos *AwsChaosInfo `json:"aws_chaos,omitempty" binding:"RequiredFieldEqual=Kind:AwsChaos"`
GcpChaos *GcpChaosInfo `json:"gcp_chaos,omitempty" binding:"RequiredFieldEqual=Kind:GcpChaos"`
}
// SchedulerInfo defines the scheduler information.
type SchedulerInfo struct {
Duration string `json:"duration" binding:"DurationValid"`
}
// PodChaosInfo defines the basic information of pod chaos for creating a new PodChaos.
type PodChaosInfo struct {
Action string `json:"action" binding:"oneof='' 'pod-kill' 'pod-failure' 'container-kill'"`
ContainerNames []string `json:"container_names,omitempty"`
GracePeriod int64 `json:"grace_period"`
}
// NetworkChaosInfo defines the basic information of network chaos for creating a new NetworkChaos.
type NetworkChaosInfo struct {
Action string `json:"action" binding:"oneof='' 'netem' 'delay' 'loss' 'duplicate' 'corrupt' 'partition' 'bandwidth'"`
Delay *v1alpha1.DelaySpec `json:"delay" binding:"RequiredFieldEqual=Action:delay"`
Loss *v1alpha1.LossSpec `json:"loss" binding:"RequiredFieldEqual=Action:loss"`
Duplicate *v1alpha1.DuplicateSpec `json:"duplicate" binding:"RequiredFieldEqual=Action:duplicate"`
Corrupt *v1alpha1.CorruptSpec `json:"corrupt" binding:"RequiredFieldEqual=Action:corrupt"`
Bandwidth *v1alpha1.BandwidthSpec `json:"bandwidth" binding:"RequiredFieldEqual=Action:bandwidth"`
Direction string `json:"direction" binding:"oneof='' 'to' 'from' 'both'"`
TargetScope *ScopeInfo `json:"target_scope"`
ExternalTargets []string `json:"external_targets"`
}
// IOChaosInfo defines the basic information of io chaos for creating a new IOChaos.
type IOChaosInfo struct {
Action string `json:"action" binding:"oneof='' 'latency' 'fault' 'attrOverride'"`
Delay string `json:"delay"`
Errno uint32 `json:"errno"`
Attr *v1alpha1.AttrOverrideSpec `json:"attr"`
Mistake *v1alpha1.MistakeSpec `json:"mistake"`
Path string `json:"path"`
Percent int `json:"percent"`
Methods []v1alpha1.IoMethod `json:"methods"`
VolumePath string `json:"volume_path"`
ContainerName string `json:"container_name"`
}
// KernelChaosInfo defines the basic information of kernel chaos for creating a new KernelChaos.
type KernelChaosInfo struct {
FailKernRequest v1alpha1.FailKernRequest `json:"fail_kern_request"`
}
// TimeChaosInfo defines the basic information of time chaos for creating a new TimeChaos.
type TimeChaosInfo struct {
TimeOffset string `json:"time_offset"`
ClockIDs []string `json:"clock_ids"`
ContainerNames []string `json:"container_names"`
}
// StressChaosInfo defines the basic information of stress chaos for creating a new StressChaos.
type StressChaosInfo struct {
Stressors *v1alpha1.Stressors `json:"stressors"`
StressngStressors string `json:"stressng_stressors,omitempty"`
ContainerName *string `json:"container_name,omitempty"`
}
// DNSChaosInfo defines the basic information of dns chaos for creating a new DNSChaos.
type DNSChaosInfo struct {
Action string `json:"action" binding:"oneof='error' 'random'"`
DomainNamePatterns []string `json:"patterns"`
ContainerNames []string `json:"container_names,omitempty"`
}
// AwsChaosInfo defines the basic information of aws chaos for creating a new AwsChaos.
type AwsChaosInfo struct {
Action string `json:"action" binding:"oneof='ec2-stop' 'ec2-restart' 'detach-volume'"`
SecretName *string `json:"secretName,omitempty"`
AwsRegion string `json:"awsRegion"`
Ec2Instance string `json:"ec2Instance"`
EbsVolume *string `json:"volumeID,omitempty"`
DeviceName *string `json:"deviceName,omitempty"`
}
// GcpChaosInfo defines the basic information of aws chaos for creating a new AwsChaos.
type GcpChaosInfo struct {
Action string `json:"action" binding:"oneof='node-stop' 'node-reset' 'disk-loss'"`
SecretName *string `json:"secretName,omitempty"`
Project string `json:"project"`
Zone string `json:"zone"`
Instance string `json:"instance"`
DeviceNames *[]string `json:"deviceNames,omitempty"`
}
// ParsePodChaos Parse PodChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParsePodChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.PodChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseNetworkChaos Parse NetworkChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseNetworkChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.NetworkChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseIOChaos Parse IOChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseIOChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.IOChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseTimeChaos Parse TimeChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseTimeChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.TimeChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseKernelChaos Parse KernelChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseKernelChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.KernelChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseStressChaos Parse StressChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseStressChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.StressChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseDNSChaos Parse DNSChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseDNSChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.DNSChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseAwsChaos Parse AwsChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseAwsChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.AwsChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}
// ParseGcpChaos Parse GcpChaos JSON string into KubeObjectDesc.
func (e *Experiment) ParseGcpChaos() (KubeObjectDesc, error) {
chaos := &v1alpha1.GcpChaos{}
if err := json.Unmarshal([]byte(e.Experiment), &chaos); err != nil {
return KubeObjectDesc{}, err
}
return KubeObjectDesc{
TypeMeta: metav1.TypeMeta{
APIVersion: chaos.APIVersion,
Kind: chaos.Kind,
},
Meta: KubeObjectMeta{
Name: chaos.Name,
Namespace: chaos.Namespace,
Labels: chaos.Labels,
Annotations: chaos.Annotations,
},
Spec: chaos.Spec,
}, nil
}