...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package podhttpchaosmanager
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 PodHttpTransaction struct {
27 Steps []Step
28 }
29
30
31 type Step interface {
32
33 Apply(chaos *v1alpha1.PodHttpChaos) error
34 }
35
36
37 type Clear struct {
38 Source string
39 }
40
41
42 func (s *Clear) Apply(chaos *v1alpha1.PodHttpChaos) error {
43 rules := []v1alpha1.PodHttpChaosRule{}
44 for _, rule := range chaos.Spec.Rules {
45 if rule.Source != s.Source {
46 rules = append(rules, rule)
47 }
48 }
49 chaos.Spec.Rules = rules
50 return nil
51 }
52
53
54 type Append struct {
55 Item interface{}
56 }
57
58
59 func (a *Append) Apply(chaos *v1alpha1.PodHttpChaos) error {
60 switch item := a.Item.(type) {
61 case v1alpha1.PodHttpChaosRule:
62 chaos.Spec.Rules = append(chaos.Spec.Rules, item)
63 case *v1alpha1.PodHttpChaosTLS:
64 chaos.Spec.TLS = item
65 default:
66 return errors.Wrapf(utils.ErrUnknownType, "type: %T", item)
67 }
68
69 return nil
70 }
71
72
73 func (t *PodHttpTransaction) Clear(source string) {
74 t.Steps = append(t.Steps, &Clear{
75 Source: source,
76 })
77 }
78
79
80 func (t *PodHttpTransaction) Append(item interface{}) error {
81 switch item.(type) {
82 case v1alpha1.PodHttpChaosRule:
83 t.Steps = append(t.Steps, &Append{
84 Item: item,
85 })
86 case *v1alpha1.PodHttpChaosTLS:
87 t.Steps = append(t.Steps, &Append{
88 Item: item,
89 })
90 default:
91 return errors.Wrapf(utils.ErrUnknownType, "type: %T", item)
92 }
93 return nil
94 }
95
96
97 func (t *PodHttpTransaction) Apply(chaos *v1alpha1.PodHttpChaos) error {
98 for _, s := range t.Steps {
99 err := s.Apply(chaos)
100 if err != nil {
101 return err
102 }
103 }
104
105 return nil
106 }
107