32
32
inject , abort chan os.Signal
33
33
)
34
34
35
- var destIps , sPorts , dPorts []string
35
+ var sPorts , dPorts []string
36
36
37
37
// Helper injects the network chaos
38
38
func Helper (clients clients.ClientSets ) {
@@ -178,24 +178,14 @@ func injectChaos(netInterface string, target targetDetails) error {
178
178
179
179
netemCommands := os .Getenv ("NETEM_COMMAND" )
180
180
181
- if len (destIps ) == 0 && len (sPorts ) == 0 && len (dPorts ) == 0 {
181
+ if len (target . DestinationIps ) == 0 && len (sPorts ) == 0 && len (dPorts ) == 0 {
182
182
tc := fmt .Sprintf ("sudo nsenter -t %d -n tc qdisc replace dev %s root netem %v" , target .Pid , netInterface , netemCommands )
183
183
log .Info (tc )
184
184
if err := common .RunBashCommand (tc , "failed to create tc rules" , target .Source ); err != nil {
185
185
return err
186
186
}
187
187
} else {
188
188
189
- ips := strings .Split (target .DestinationIps , "," )
190
- var uniqueIps []string
191
-
192
- // removing duplicates ips from the list, if any
193
- for i := range ips {
194
- if ! common .Contains (ips [i ], uniqueIps ) {
195
- uniqueIps = append (uniqueIps , ips [i ])
196
- }
197
- }
198
-
199
189
// Create a priority-based queue
200
190
// This instantly creates classes 1:1, 1:2, 1:3
201
191
priority := fmt .Sprintf ("sudo nsenter -t %v -n tc qdisc replace dev %v root handle 1: prio" , target .Pid , netInterface )
@@ -212,7 +202,7 @@ func injectChaos(netInterface string, target targetDetails) error {
212
202
return err
213
203
}
214
204
215
- for _ , ip := range uniqueIps {
205
+ for _ , ip := range target . DestinationIps {
216
206
// redirect traffic to specific IP through band 3
217
207
tc := fmt .Sprintf ("sudo nsenter -t %v -n tc filter add dev %v protocol ip parent 1:0 prio 3 u32 match ip dst %v flowid 1:3" , target .Pid , netInterface , ip )
218
208
if strings .Contains (ip , ":" ) {
@@ -272,7 +262,7 @@ type targetDetails struct {
272
262
Name string
273
263
Namespace string
274
264
ServiceMesh string
275
- DestinationIps string
265
+ DestinationIps [] string
276
266
TargetContainer string
277
267
ContainerId string
278
268
Pid int
@@ -295,7 +285,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) {
295
285
experimentDetails .SourcePorts = types .Getenv ("SOURCE_PORTS" , "" )
296
286
experimentDetails .DestinationPorts = types .Getenv ("DESTINATION_PORTS" , "" )
297
287
298
- destIps = getDestinationIPs (experimentDetails .DestinationIPs )
299
288
if strings .TrimSpace (experimentDetails .DestinationPorts ) != "" {
300
289
dPorts = strings .Split (strings .TrimSpace (experimentDetails .DestinationPorts ), "," )
301
290
}
@@ -304,22 +293,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) {
304
293
}
305
294
}
306
295
307
- func getDestinationIPs (ips string ) []string {
308
- if strings .TrimSpace (ips ) == "" {
309
- return nil
310
- }
311
- destIPs := strings .Split (strings .TrimSpace (ips ), "," )
312
- var uniqueIps []string
313
-
314
- // removing duplicates ips from the list, if any
315
- for i := range destIPs {
316
- if ! common .Contains (destIPs [i ], uniqueIps ) {
317
- uniqueIps = append (uniqueIps , destIPs [i ])
318
- }
319
- }
320
- return uniqueIps
321
- }
322
-
323
296
// abortWatcher continuously watch for the abort signals
324
297
func abortWatcher (targets []targetDetails , networkInterface , resultName , chaosNS string ) {
325
298
@@ -347,9 +320,28 @@ func abortWatcher(targets []targetDetails, networkInterface, resultName, chaosNS
347
320
log .Info ("Chaos Revert Completed" )
348
321
os .Exit (1 )
349
322
}
350
- func getDestIps (serviceMesh string ) string {
351
- if serviceMesh == "false" {
352
- return os .Getenv ("DESTINATION_IPS" )
323
+ func getDestIps (serviceMesh string ) []string {
324
+ var (
325
+ destIps = os .Getenv ("DESTINATION_IPS" )
326
+ uniqueIps []string
327
+ )
328
+
329
+ if serviceMesh == "true" {
330
+ destIps = os .Getenv ("DESTINATION_IPS_SERVICE_MESH" )
331
+ }
332
+
333
+ if strings .TrimSpace (destIps ) == "" {
334
+ return nil
353
335
}
354
- return os .Getenv ("DESTINATION_IPS_SERVICE_MESH" )
336
+
337
+ ips := strings .Split (strings .TrimSpace (destIps ), "," )
338
+
339
+ // removing duplicates ips from the list, if any
340
+ for i := range ips {
341
+ if ! common .Contains (ips [i ], uniqueIps ) {
342
+ uniqueIps = append (uniqueIps , ips [i ])
343
+ }
344
+ }
345
+
346
+ return uniqueIps
355
347
}
0 commit comments