...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package podiochaosmanager
15
16 import (
17 "fmt"
18
19 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
20 )
21
22
23 type PodIoTransaction struct {
24 Steps []Step
25 }
26
27
28 type Step interface {
29
30 Apply(chaos *v1alpha1.PodIoChaos) error
31 }
32
33
34 type Clear struct {
35 Source string
36 }
37
38
39 func (s *Clear) Apply(chaos *v1alpha1.PodIoChaos) error {
40 actions := []v1alpha1.IoChaosAction{}
41 for _, action := range chaos.Spec.Actions {
42 if action.Source != s.Source {
43 actions = append(actions, action)
44 }
45 }
46 chaos.Spec.Actions = actions
47
48 return nil
49 }
50
51
52 type Append struct {
53 Item interface{}
54 }
55
56
57 func (a *Append) Apply(chaos *v1alpha1.PodIoChaos) error {
58 switch item := a.Item.(type) {
59 case v1alpha1.IoChaosAction:
60 chaos.Spec.Actions = append(chaos.Spec.Actions, item)
61 default:
62 return fmt.Errorf("unknown type of item")
63 }
64
65 return nil
66 }
67
68
69 type SetVolumePath struct {
70 Path string
71 }
72
73
74 func (s *SetVolumePath) Apply(chaos *v1alpha1.PodIoChaos) error {
75 chaos.Spec.VolumeMountPath = s.Path
76
77 return nil
78 }
79
80
81 func (t *PodIoTransaction) Clear(source string) {
82 t.Steps = append(t.Steps, &Clear{
83 Source: source,
84 })
85 }
86
87
88 func (t *PodIoTransaction) Append(item interface{}) error {
89 switch item.(type) {
90 case v1alpha1.IoChaosAction:
91 t.Steps = append(t.Steps, &Append{
92 Item: item,
93 })
94 return nil
95 default:
96 return fmt.Errorf("unknown type of item")
97 }
98 }
99
100
101 func (t *PodIoTransaction) SetVolumePath(path string) error {
102 t.Steps = append(t.Steps, &SetVolumePath{
103 Path: path,
104 })
105
106 return nil
107 }
108
109
110 func (t *PodIoTransaction) Apply(chaos *v1alpha1.PodIoChaos) error {
111 for _, s := range t.Steps {
112 err := s.Apply(chaos)
113 if err != nil {
114 return err
115 }
116 }
117
118 return nil
119 }
120