...
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 SetContainer struct {
70 Container string
71 }
72
73
74 func (s *SetContainer) Apply(chaos *v1alpha1.PodIoChaos) error {
75 chaos.Spec.Container = &s.Container
76
77 return nil
78 }
79
80
81 type SetVolumePath struct {
82 Path string
83 }
84
85
86 func (s *SetVolumePath) Apply(chaos *v1alpha1.PodIoChaos) error {
87 chaos.Spec.VolumeMountPath = s.Path
88
89 return nil
90 }
91
92
93 func (t *PodIoTransaction) Clear(source string) {
94 t.Steps = append(t.Steps, &Clear{
95 Source: source,
96 })
97 }
98
99
100 func (t *PodIoTransaction) Append(item interface{}) error {
101 switch item.(type) {
102 case v1alpha1.IoChaosAction:
103 t.Steps = append(t.Steps, &Append{
104 Item: item,
105 })
106 return nil
107 default:
108 return fmt.Errorf("unknown type of item")
109 }
110 }
111
112
113 func (t *PodIoTransaction) SetVolumePath(path string) error {
114 t.Steps = append(t.Steps, &SetVolumePath{
115 Path: path,
116 })
117
118 return nil
119 }
120
121 func (t *PodIoTransaction) SetContainer(container string) error {
122 t.Steps = append(t.Steps, &SetContainer{
123 Container: container,
124 })
125
126 return nil
127 }
128
129
130 func (t *PodIoTransaction) Apply(chaos *v1alpha1.PodIoChaos) error {
131 for _, s := range t.Steps {
132 err := s.Apply(chaos)
133 if err != nil {
134 return err
135 }
136 }
137
138 return nil
139 }
140