diff --git a/chaoslib/litmus/network-chaos/helper/netem.go b/chaoslib/litmus/network-chaos/helper/netem.go index 7dfadc875..75e231611 100644 --- a/chaoslib/litmus/network-chaos/helper/netem.go +++ b/chaoslib/litmus/network-chaos/helper/netem.go @@ -32,7 +32,7 @@ var ( inject, abort chan os.Signal ) -var destIps, sPorts, dPorts []string +var sPorts, dPorts []string // Helper injects the network chaos func Helper(clients clients.ClientSets) { @@ -178,7 +178,7 @@ func injectChaos(netInterface string, target targetDetails) error { netemCommands := os.Getenv("NETEM_COMMAND") - if len(destIps) == 0 && len(sPorts) == 0 && len(dPorts) == 0 { + if len(target.DestinationIps) == 0 && len(sPorts) == 0 && len(dPorts) == 0 { tc := fmt.Sprintf("sudo nsenter -t %d -n tc qdisc replace dev %s root netem %v", target.Pid, netInterface, netemCommands) log.Info(tc) if err := common.RunBashCommand(tc, "failed to create tc rules", target.Source); err != nil { @@ -186,16 +186,6 @@ func injectChaos(netInterface string, target targetDetails) error { } } else { - ips := strings.Split(target.DestinationIps, ",") - var uniqueIps []string - - // removing duplicates ips from the list, if any - for i := range ips { - if !common.Contains(ips[i], uniqueIps) { - uniqueIps = append(uniqueIps, ips[i]) - } - } - // Create a priority-based queue // This instantly creates classes 1:1, 1:2, 1:3 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 { return err } - for _, ip := range uniqueIps { + for _, ip := range target.DestinationIps { // redirect traffic to specific IP through band 3 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) if strings.Contains(ip, ":") { @@ -272,7 +262,7 @@ type targetDetails struct { Name string Namespace string ServiceMesh string - DestinationIps string + DestinationIps []string TargetContainer string ContainerId string Pid int @@ -295,7 +285,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) { experimentDetails.SourcePorts = types.Getenv("SOURCE_PORTS", "") experimentDetails.DestinationPorts = types.Getenv("DESTINATION_PORTS", "") - destIps = getDestinationIPs(experimentDetails.DestinationIPs) if strings.TrimSpace(experimentDetails.DestinationPorts) != "" { dPorts = strings.Split(strings.TrimSpace(experimentDetails.DestinationPorts), ",") } @@ -304,22 +293,6 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) { } } -func getDestinationIPs(ips string) []string { - if strings.TrimSpace(ips) == "" { - return nil - } - destIPs := strings.Split(strings.TrimSpace(ips), ",") - var uniqueIps []string - - // removing duplicates ips from the list, if any - for i := range destIPs { - if !common.Contains(destIPs[i], uniqueIps) { - uniqueIps = append(uniqueIps, destIPs[i]) - } - } - return uniqueIps -} - // abortWatcher continuously watch for the abort signals func abortWatcher(targets []targetDetails, networkInterface, resultName, chaosNS string) { @@ -347,9 +320,28 @@ func abortWatcher(targets []targetDetails, networkInterface, resultName, chaosNS log.Info("Chaos Revert Completed") os.Exit(1) } -func getDestIps(serviceMesh string) string { - if serviceMesh == "false" { - return os.Getenv("DESTINATION_IPS") +func getDestIps(serviceMesh string) []string { + var ( + destIps = os.Getenv("DESTINATION_IPS") + uniqueIps []string + ) + + if serviceMesh == "true" { + destIps = os.Getenv("DESTINATION_IPS_SERVICE_MESH") + } + + if strings.TrimSpace(destIps) == "" { + return nil } - return os.Getenv("DESTINATION_IPS_SERVICE_MESH") + + ips := strings.Split(strings.TrimSpace(destIps), ",") + + // removing duplicates ips from the list, if any + for i := range ips { + if !common.Contains(ips[i], uniqueIps) { + uniqueIps = append(uniqueIps, ips[i]) + } + } + + return uniqueIps }