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