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