Skip to content

Commit

Permalink
Added Trimspace
Browse files Browse the repository at this point in the history
Signed-off-by: chinmaym07 <[email protected]>
  • Loading branch information
chinmaym07 committed Sep 24, 2022
1 parent 74dca2e commit 7813ef9
Show file tree
Hide file tree
Showing 51 changed files with 108 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func PrepareAWSSSMChaosByID(experimentsDetails *experimentTypes.ExperimentDetail
// watching for the abort signal and revert the chaos
go lib.AbortWatcher(experimentsDetails, abort)

if experimentsDetails.EC2InstanceID == "" {
if strings.TrimSpace(experimentsDetails.EC2InstanceID) == "" {
return errors.Errorf("no instance id found for chaos injection")
}
//get the instance id or list of instance ids
Expand Down
2 changes: 1 addition & 1 deletion chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PrepareChaos(experimentsDetails *experimentTypes.ExperimentDetails, clients
log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime)
common.WaitForDuration(experimentsDetails.RampTime)
}
if experimentsDetails.VirtualDiskNames == "" {
if strings.TrimSpace(experimentsDetails.VirtualDiskNames) == "" {
return errors.Errorf("no volume names found to detach")
}
//get the disk name or list of disk names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func PrepareAzureStop(experimentsDetails *experimentTypes.ExperimentDetails, cli
log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime)
common.WaitForDuration(experimentsDetails.RampTime)
}
if experimentsDetails.AzureInstanceName == "" {
if strings.TrimSpace(experimentsDetails.AzureInstanceName) == "" {
return errors.Errorf("no instance name found to stop")
}
// get the instance name or list of instance names
Expand Down
10 changes: 5 additions & 5 deletions chaoslib/litmus/container-kill/lib/container-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails,
var podsAffectedPerc int
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.TargetPods == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or TARGET_PODS")
}
//Setup the tunables if provided in range
Expand All @@ -38,13 +38,13 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails,
})

podsAffectedPerc, _ = strconv.Atoi(experimentsDetails.PodsAffectedPerc)
if experimentsDetails.NodeLabel == "" {
if strings.TrimSpace(experimentsDetails.NodeLabel) == "" {
targetPodList, err = common.GetPodList(experimentsDetails.TargetPods, podsAffectedPerc, clients, chaosDetails)
if err != nil {
return err
}
} else {
if experimentsDetails.TargetPods == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" {
targetPodList, err = common.GetPodListFromSpecifiedNodes(experimentsDetails.TargetPods, podsAffectedPerc, experimentsDetails.NodeLabel, clients, chaosDetails)
if err != nil {
return err
Expand All @@ -71,14 +71,14 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails,
}

// Getting the serviceAccountName, need permission inside helper pod to create the events
if experimentsDetails.ChaosServiceAccount == "" {
if strings.TrimSpace(experimentsDetails.ChaosServiceAccount) == "" {
experimentsDetails.ChaosServiceAccount, err = common.GetServiceAccount(experimentsDetails.ChaosNamespace, experimentsDetails.ChaosPodName, clients)
if err != nil {
return errors.Errorf("unable to get the serviceAccountName, err: %v", err)
}
}

if experimentsDetails.EngineName != "" {
if strings.TrimSpace(experimentsDetails.EngineName) != "" {
if err := common.SetHelperData(chaosDetails, experimentsDetails.SetHelperData, clients); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions chaoslib/litmus/disk-fill/lib/disk-fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
execCommandDetails := exec.PodDetails{}
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.TargetPods == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or TARGET_PODS")
}
//setup the tunables if provided in range
Expand All @@ -43,13 +43,13 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
})

podsAffectedPerc, _ = strconv.Atoi(experimentsDetails.PodsAffectedPerc)
if experimentsDetails.NodeLabel == "" {
if strings.TrimSpace(experimentsDetails.NodeLabel) == "" {
targetPodList, err = common.GetPodList(experimentsDetails.TargetPods, podsAffectedPerc, clients, chaosDetails)
if err != nil {
return err
}
} else {
if experimentsDetails.TargetPods == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" {
targetPodList, err = common.GetPodListFromSpecifiedNodes(experimentsDetails.TargetPods, podsAffectedPerc, experimentsDetails.NodeLabel, clients, chaosDetails)
if err != nil {
return err
Expand All @@ -76,7 +76,7 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
}

// Getting the serviceAccountName, need permission inside helper pod to create the events
if experimentsDetails.ChaosServiceAccount == "" {
if strings.TrimSpace(experimentsDetails.ChaosServiceAccount) == "" {
experimentsDetails.ChaosServiceAccount, err = common.GetServiceAccount(experimentsDetails.ChaosNamespace, experimentsDetails.ChaosPodName, clients)
if err != nil {
return errors.Errorf("unable to get the serviceAccountName, err: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"context"
"strconv"
"strings"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/litmuschaos/litmus-go/pkg/events"
Expand All @@ -22,7 +23,7 @@ import (
func PrepareDockerServiceKill(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {

var err error
if experimentsDetails.TargetNode == "" {
if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
//Select node for docker-service-kill
experimentsDetails.TargetNode, err = common.GetNodeName(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.NodeLabel, clients)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PrepareEBSLossByID(experimentsDetails *experimentTypes.ExperimentDetails, c
// stopping the chaos execution, if abort signal received
os.Exit(0)
default:
if experimentsDetails.EBSVolumeID == "" {
if strings.TrimSpace(experimentsDetails.EBSVolumeID) == "" {
return errors.Errorf("no volume id found to detach")
}
//get the volume id or list of instance ids
Expand Down
3 changes: 2 additions & 1 deletion chaoslib/litmus/ebs-loss/lib/ebs-loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"os"
"strings"
"time"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
Expand Down Expand Up @@ -112,7 +113,7 @@ func InjectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
//prepare the instaceIDs and device name for all the given volume
for _, volumeID := range targetEBSVolumeIDList {
ec2InstanceID, device, err := ebs.GetVolumeAttachmentDetails(volumeID, experimentsDetails.VolumeTag, experimentsDetails.Region)
if err != nil || ec2InstanceID == "" || device == "" {
if err != nil || strings.TrimSpace(ec2InstanceID) == "" || strings.TrimSpace(device) == "" {
return errors.Errorf("fail to get the attachment info, err: %v", err)
}
ec2InstanceIDList = append(ec2InstanceIDList, ec2InstanceID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func PrepareEC2TerminateByID(experimentsDetails *experimentTypes.ExperimentDetai
log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime)
common.WaitForDuration(experimentsDetails.RampTime)
}
if experimentsDetails.Ec2InstanceID == "" {
if strings.TrimSpace(experimentsDetails.Ec2InstanceID) == "" {
return errors.Errorf("no instance id found to terminate")
}
//get the instance id or list of instance ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func getDeviceNamesAndVMInstanceNames(diskVolumeNamesList []string, computeServi
for i := range diskVolumeNamesList {

instanceName, err := gcp.GetVolumeAttachmentDetails(computeService, experimentsDetails.GCPProjectID, experimentsDetails.DiskZones, diskVolumeNamesList[i])
if err != nil || instanceName == "" {
if err != nil || strings.TrimSpace(instanceName) == "" {
return errors.Errorf("failed to get the attachment info, err: %v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func PrepareVMStop(computeService *compute.Service, experimentsDetails *experime
common.WaitForDuration(experimentsDetails.RampTime)
}

if experimentsDetails.VMInstanceName == "" {
if strings.TrimSpace(experimentsDetails.VMInstanceName) == "" {
return errors.Errorf("no instance name found to stop")
}
// get the instance name or list of instance names
instanceNamesList := strings.Split(experimentsDetails.VMInstanceName, ",")
if experimentsDetails.InstanceZone == "" {
if strings.TrimSpace(experimentsDetails.InstanceZone) == "" {
return errors.Errorf("no corresponding zones found for the instances")
}
// get the zone name or list of corresponding zones for the instances
Expand Down
8 changes: 4 additions & 4 deletions chaoslib/litmus/http-chaos/lib/http-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
var podsAffectedPerc int
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.TargetPods == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or TARGET_PODS")
}
//set up the tunables if provided in range
SetChaosTunables(experimentsDetails)

podsAffectedPerc, _ = strconv.Atoi(experimentsDetails.PodsAffectedPerc)
if experimentsDetails.NodeLabel == "" {
if strings.TrimSpace(experimentsDetails.NodeLabel) == "" {

targetPodList, err = common.GetPodList(experimentsDetails.TargetPods, podsAffectedPerc, clients, chaosDetails)
if err != nil {
return err
}
} else {
if experimentsDetails.TargetPods == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" {
targetPodList, err = common.GetPodListFromSpecifiedNodes(experimentsDetails.TargetPods, podsAffectedPerc, experimentsDetails.NodeLabel, clients, chaosDetails)
if err != nil {
return err
Expand All @@ -67,7 +67,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}

// Getting the serviceAccountName, need permission inside helper pod to create the events
if experimentsDetails.ChaosServiceAccount == "" {
if strings.TrimSpace(experimentsDetails.ChaosServiceAccount) == "" {
experimentsDetails.ChaosServiceAccount, err = common.GetServiceAccount(experimentsDetails.ChaosNamespace, experimentsDetails.ChaosPodName, clients)
if err != nil {
return errors.Errorf("unable to get the serviceAccountName, err: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions chaoslib/litmus/kafka-broker-pod-failure/lib/pod-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
for duration < experimentsDetails.ChaoslibDetail.ChaosDuration {
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.KafkaBroker == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.KafkaBroker) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or KAFKA_BROKER")
}
podsAffectedPerc, _ := strconv.Atoi(experimentsDetails.ChaoslibDetail.PodsAffectedPerc)
Expand Down Expand Up @@ -157,7 +157,7 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
for duration < experimentsDetails.ChaoslibDetail.ChaosDuration {
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.KafkaBroker == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.KafkaBroker) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or KAFKA_BROKER")
}
podsAffectedPerc, _ := strconv.Atoi(experimentsDetails.ChaoslibDetail.PodsAffectedPerc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"context"
"strconv"
"strings"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/litmuschaos/litmus-go/pkg/events"
Expand All @@ -22,7 +23,7 @@ import (
func PrepareKubeletKill(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {

var err error
if experimentsDetails.TargetNode == "" {
if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
//Select node for kubelet-service-kill
experimentsDetails.TargetNode, err = common.GetNodeName(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.NodeLabel, clients)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion chaoslib/litmus/network-chaos/helper/netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func injectChaos(experimentDetails *experimentTypes.ExperimentDetails, pid int)
// stopping the chaos execution, if abort signal received
os.Exit(1)
default:
if destinationIPs == "" {
if strings.TrimSpace(destinationIPs) == "" {
tc := fmt.Sprintf("sudo nsenter -t %d -n tc qdisc replace dev %s root netem %v", pid, experimentDetails.NetworkInterface, netemCommands)
cmd := exec.Command("/bin/bash", "-c", tc)
out, err := cmd.CombinedOutput()
Expand Down
21 changes: 11 additions & 10 deletions chaoslib/litmus/network-chaos/lib/network-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package lib
import (
"context"
"fmt"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"net"
"strconv"
"strings"

k8serrors "k8s.io/apimachinery/pkg/api/errors"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/network-chaos/types"
"github.com/litmuschaos/litmus-go/pkg/log"
Expand All @@ -31,7 +32,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
var podsAffectedPerc int
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.TargetPods == "" && chaosDetails.AppDetail.Label == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" && strings.TrimSpace(chaosDetails.AppDetail.Label) == "" {
return errors.Errorf("please provide one of the appLabel or TARGET_PODS")
}
//setup the tunables if provided in range
Expand Down Expand Up @@ -64,7 +65,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
})
}
podsAffectedPerc, _ = strconv.Atoi(experimentsDetails.PodsAffectedPerc)
if experimentsDetails.NodeLabel == "" {
if strings.TrimSpace(experimentsDetails.NodeLabel) == "" {

//targetPodList, err := common.GetPodListFromSpecifiedNodes(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails)
targetPodList, err = common.GetPodList(experimentsDetails.TargetPods, podsAffectedPerc, clients, chaosDetails)
Expand All @@ -73,7 +74,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}
} else {
//targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails)
if experimentsDetails.TargetPods == "" {
if strings.TrimSpace(experimentsDetails.TargetPods) == "" {
targetPodList, err = common.GetPodListFromSpecifiedNodes(experimentsDetails.TargetPods, podsAffectedPerc, experimentsDetails.NodeLabel, clients, chaosDetails)
if err != nil {
return err
Expand All @@ -100,14 +101,14 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}

// Getting the serviceAccountName, need permission inside helper pod to create the events
if experimentsDetails.ChaosServiceAccount == "" {
if strings.TrimSpace(experimentsDetails.ChaosServiceAccount) == "" {
experimentsDetails.ChaosServiceAccount, err = common.GetServiceAccount(experimentsDetails.ChaosNamespace, experimentsDetails.ChaosPodName, clients)
if err != nil {
return errors.Errorf("unable to get the serviceAccountName, err: %v", err)
}
}

if experimentsDetails.EngineName != "" {
if strings.TrimSpace(experimentsDetails.EngineName) != "" {
if err := common.SetHelperData(chaosDetails, experimentsDetails.SetHelperData, clients); err != nil {
return err
}
Expand Down Expand Up @@ -364,7 +365,7 @@ func GetTargetIps(targetIPs, targetHosts string, clients clients.ClientSets, ser
if err != nil {
return "", err
}
if targetIPs == "" {
if strings.TrimSpace(targetIPs) == "" {
targetIPs = ipsFromHost
} else if ipsFromHost != "" {
targetIPs = targetIPs + "," + ipsFromHost
Expand Down Expand Up @@ -402,7 +403,7 @@ func getPodIPFromService(host string, clients clients.ClientSets) ([]string, err

// getIpsForTargetHosts resolves IP addresses for comma-separated list of target hosts and returns comma-separated ips
func getIpsForTargetHosts(targetHosts string, clients clients.ClientSets, serviceMesh bool) (string, error) {
if targetHosts == "" {
if strings.TrimSpace(targetHosts) == "" {
return "", nil
}
hosts := strings.Split(targetHosts, ",")
Expand All @@ -417,7 +418,7 @@ func getIpsForTargetHosts(targetHosts string, clients clients.ClientSets, servic
}
log.Infof("Host: {%v}, IP address: {%v}", hosts[i], ips)
commaSeparatedIPs = append(commaSeparatedIPs, ips...)
if finalHosts == "" {
if strings.TrimSpace(finalHosts) == "" {
finalHosts = hosts[i]
} else {
finalHosts = finalHosts + "," + hosts[i]
Expand All @@ -433,7 +434,7 @@ func getIpsForTargetHosts(targetHosts string, clients clients.ClientSets, servic
log.Infof("Host: {%v}, IP address: {%v}", hosts[i], ips[j])
commaSeparatedIPs = append(commaSeparatedIPs, ips[j].String())
}
if finalHosts == "" {
if strings.TrimSpace(finalHosts) == "" {
finalHosts = hosts[i]
} else {
finalHosts = finalHosts + "," + hosts[i]
Expand Down
4 changes: 2 additions & 2 deletions chaoslib/litmus/node-drain/lib/node-drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func PrepareNodeDrain(experimentsDetails *experimentTypes.ExperimentDetails, cli
common.WaitForDuration(experimentsDetails.RampTime)
}

if experimentsDetails.TargetNode == "" {
if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
//Select node for kubelet-service-kill
experimentsDetails.TargetNode, err = common.GetNodeName(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.NodeLabel, clients)
if err != nil {
return err
}
}

if experimentsDetails.EngineName != "" {
if strings.TrimSpace(experimentsDetails.EngineName) != "" {
msg := "Injecting " + experimentsDetails.ExperimentName + " chaos on " + experimentsDetails.TargetNode + " node"
types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails)
events.GenerateEvents(eventsDetails, clients, chaosDetails, "ChaosEngine")
Expand Down
4 changes: 2 additions & 2 deletions chaoslib/litmus/node-restart/lib/node-restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
func PrepareNodeRestart(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {

//Select the node
if experimentsDetails.TargetNode == "" {
if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
//Select node for node-restart
experimentsDetails.TargetNode, err = common.GetNodeName(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.NodeLabel, clients)
if err != nil {
Expand All @@ -47,7 +47,7 @@ func PrepareNodeRestart(experimentsDetails *experimentTypes.ExperimentDetails, c
}

// get the node ip
if experimentsDetails.TargetNodeIP == "" {
if strings.TrimSpace(experimentsDetails.TargetNodeIP) == "" {
experimentsDetails.TargetNodeIP, err = getInternalIP(experimentsDetails.TargetNode, clients)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 7813ef9

Please sign in to comment.