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