Skip to content

Commit 392ea29

Browse files
authored
chore(network): fix the destination ips for network experiment for service mesh (litmuschaos#666)
Signed-off-by: Shubham Chaudhary <[email protected]>
1 parent db13d05 commit 392ea29

File tree

1 file changed

+27
-35
lines changed
  • chaoslib/litmus/network-chaos/helper

1 file changed

+27
-35
lines changed

chaoslib/litmus/network-chaos/helper/netem.go

+27-35
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
inject, abort chan os.Signal
3333
)
3434

35-
var destIps, sPorts, dPorts []string
35+
var sPorts, dPorts []string
3636

3737
// Helper injects the network chaos
3838
func Helper(clients clients.ClientSets) {
@@ -178,24 +178,14 @@ func injectChaos(netInterface string, target targetDetails) error {
178178

179179
netemCommands := os.Getenv("NETEM_COMMAND")
180180

181-
if len(destIps) == 0 && len(sPorts) == 0 && len(dPorts) == 0 {
181+
if len(target.DestinationIps) == 0 && len(sPorts) == 0 && len(dPorts) == 0 {
182182
tc := fmt.Sprintf("sudo nsenter -t %d -n tc qdisc replace dev %s root netem %v", target.Pid, netInterface, netemCommands)
183183
log.Info(tc)
184184
if err := common.RunBashCommand(tc, "failed to create tc rules", target.Source); err != nil {
185185
return err
186186
}
187187
} else {
188188

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-
199189
// Create a priority-based queue
200190
// This instantly creates classes 1:1, 1:2, 1:3
201191
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 {
212202
return err
213203
}
214204

215-
for _, ip := range uniqueIps {
205+
for _, ip := range target.DestinationIps {
216206
// redirect traffic to specific IP through band 3
217207
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)
218208
if strings.Contains(ip, ":") {
@@ -272,7 +262,7 @@ type targetDetails struct {
272262
Name string
273263
Namespace string
274264
ServiceMesh string
275-
DestinationIps string
265+
DestinationIps []string
276266
TargetContainer string
277267
ContainerId string
278268
Pid int
@@ -295,7 +285,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) {
295285
experimentDetails.SourcePorts = types.Getenv("SOURCE_PORTS", "")
296286
experimentDetails.DestinationPorts = types.Getenv("DESTINATION_PORTS", "")
297287

298-
destIps = getDestinationIPs(experimentDetails.DestinationIPs)
299288
if strings.TrimSpace(experimentDetails.DestinationPorts) != "" {
300289
dPorts = strings.Split(strings.TrimSpace(experimentDetails.DestinationPorts), ",")
301290
}
@@ -304,22 +293,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) {
304293
}
305294
}
306295

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-
323296
// abortWatcher continuously watch for the abort signals
324297
func abortWatcher(targets []targetDetails, networkInterface, resultName, chaosNS string) {
325298

@@ -347,9 +320,28 @@ func abortWatcher(targets []targetDetails, networkInterface, resultName, chaosNS
347320
log.Info("Chaos Revert Completed")
348321
os.Exit(1)
349322
}
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
353335
}
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
355347
}

0 commit comments

Comments
 (0)