1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package netem
17
18 import (
19 "strconv"
20 "time"
21
22 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
23 chaosdaemonpb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
24 )
25
26
27 func FromDelay(in *v1alpha1.DelaySpec) (*chaosdaemonpb.Netem, error) {
28
29 if _, err := time.ParseDuration(in.Latency); err != nil {
30 return nil, err
31 }
32 if _, err := time.ParseDuration(in.Jitter); err != nil {
33 return nil, err
34 }
35
36 corr, err := strconv.ParseFloat(in.Correlation, 32)
37 if err != nil {
38 return nil, err
39 }
40
41 netem := &chaosdaemonpb.Netem{
42 Time: in.Latency,
43 DelayCorr: float32(corr),
44 Jitter: in.Jitter,
45 }
46
47 if in.Reorder != nil {
48 reorderPercentage, err := strconv.ParseFloat(in.Reorder.Reorder, 32)
49 if err != nil {
50 return nil, err
51 }
52
53 corr, err := strconv.ParseFloat(in.Reorder.Correlation, 32)
54 if err != nil {
55 return nil, err
56 }
57
58 netem.Reorder = float32(reorderPercentage)
59 netem.ReorderCorr = float32(corr)
60 netem.Gap = uint32(in.Reorder.Gap)
61 }
62
63 return netem, nil
64 }
65
66
67 func FromLoss(in *v1alpha1.LossSpec) (*chaosdaemonpb.Netem, error) {
68 lossPercentage, err := strconv.ParseFloat(in.Loss, 32)
69 if err != nil {
70 return nil, err
71 }
72
73 corr, err := strconv.ParseFloat(in.Correlation, 32)
74 if err != nil {
75 return nil, err
76 }
77
78 return &chaosdaemonpb.Netem{
79 Loss: float32(lossPercentage),
80 LossCorr: float32(corr),
81 }, nil
82 }
83
84
85 func FromDuplicate(in *v1alpha1.DuplicateSpec) (*chaosdaemonpb.Netem, error) {
86 duplicatePercentage, err := strconv.ParseFloat(in.Duplicate, 32)
87 if err != nil {
88 return nil, err
89 }
90
91 corr, err := strconv.ParseFloat(in.Correlation, 32)
92 if err != nil {
93 return nil, err
94 }
95
96 return &chaosdaemonpb.Netem{
97 Duplicate: float32(duplicatePercentage),
98 DuplicateCorr: float32(corr),
99 }, nil
100 }
101
102
103 func FromCorrupt(in *v1alpha1.CorruptSpec) (*chaosdaemonpb.Netem, error) {
104 corruptPercentage, err := strconv.ParseFloat(in.Corrupt, 32)
105 if err != nil {
106 return nil, err
107 }
108
109 corr, err := strconv.ParseFloat(in.Correlation, 32)
110 if err != nil {
111 return nil, err
112 }
113
114 return &chaosdaemonpb.Netem{
115 Corrupt: float32(corruptPercentage),
116 CorruptCorr: float32(corr),
117 }, nil
118 }
119
120
121 func FromRate(in *v1alpha1.RateSpec) (*chaosdaemonpb.Netem, error) {
122 return &chaosdaemonpb.Netem{
123 Rate: in.Rate,
124 }, nil
125 }
126
127
128
129
130
131
132 func FromBandwidth(in *v1alpha1.BandwidthSpec) (*chaosdaemonpb.Tbf, error) {
133 tbf := &chaosdaemonpb.Tbf{
134 Rate: in.Rate,
135 Limit: in.Limit,
136 Buffer: in.Buffer,
137 }
138
139 if in.Peakrate != nil && in.Minburst != nil {
140 tbf.PeakRate = *in.Peakrate
141 tbf.MinBurst = *in.Minburst
142 }
143
144 return tbf, nil
145 }
146