Skip to content

Commit

Permalink
chore(network): fix the destination ips for network experiment for se…
Browse files Browse the repository at this point in the history
…rvice mesh

Signed-off-by: Shubham Chaudhary <[email protected]>
  • Loading branch information
ispeakc0de committed Sep 7, 2023
1 parent db13d05 commit a14bbef
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions chaoslib/litmus/network-chaos/helper/netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -178,24 +178,14 @@ 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 {
return err
}
} 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)
Expand All @@ -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, ":") {
Expand Down Expand Up @@ -272,7 +262,7 @@ type targetDetails struct {
Name string
Namespace string
ServiceMesh string
DestinationIps string
DestinationIps []string
TargetContainer string
ContainerId string
Pid int
Expand All @@ -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), ",")
}
Expand All @@ -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) {

Expand Down Expand Up @@ -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
}

0 comments on commit a14bbef

Please sign in to comment.