diff --git a/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go b/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go index 29f5c00d6..2620e393a 100644 --- a/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go +++ b/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go @@ -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 diff --git a/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go b/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go index c76ce5abd..ff7683501 100644 --- a/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go +++ b/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go @@ -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 diff --git a/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go b/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go index 64594d3ef..b43d58aa1 100644 --- a/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go +++ b/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go @@ -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 diff --git a/chaoslib/litmus/container-kill/lib/container-kill.go b/chaoslib/litmus/container-kill/lib/container-kill.go index 5216e92cd..a3747952a 100644 --- a/chaoslib/litmus/container-kill/lib/container-kill.go +++ b/chaoslib/litmus/container-kill/lib/container-kill.go @@ -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 @@ -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 @@ -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 } diff --git a/chaoslib/litmus/disk-fill/lib/disk-fill.go b/chaoslib/litmus/disk-fill/lib/disk-fill.go index ce5f75bb9..68f679c44 100644 --- a/chaoslib/litmus/disk-fill/lib/disk-fill.go +++ b/chaoslib/litmus/disk-fill/lib/disk-fill.go @@ -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 @@ -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 @@ -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) diff --git a/chaoslib/litmus/docker-service-kill/lib/docker-service-kill.go b/chaoslib/litmus/docker-service-kill/lib/docker-service-kill.go index 5595402d9..687eba8f8 100644 --- a/chaoslib/litmus/docker-service-kill/lib/docker-service-kill.go +++ b/chaoslib/litmus/docker-service-kill/lib/docker-service-kill.go @@ -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" @@ -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 { diff --git a/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go b/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go index 81658dcff..c7b1bee5d 100644 --- a/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go +++ b/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go @@ -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 diff --git a/chaoslib/litmus/ebs-loss/lib/ebs-loss.go b/chaoslib/litmus/ebs-loss/lib/ebs-loss.go index 6baf3a3b1..3d7c09d33 100644 --- a/chaoslib/litmus/ebs-loss/lib/ebs-loss.go +++ b/chaoslib/litmus/ebs-loss/lib/ebs-loss.go @@ -2,6 +2,7 @@ package lib import ( "os" + "strings" "time" clients "github.com/litmuschaos/litmus-go/pkg/clients" @@ -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) diff --git a/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go b/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go index 51bbea715..9d3f04c17 100644 --- a/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go +++ b/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go @@ -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 diff --git a/chaoslib/litmus/gcp-vm-disk-loss-by-label/lib/gcp-vm-disk-loss-by-label.go b/chaoslib/litmus/gcp-vm-disk-loss-by-label/lib/gcp-vm-disk-loss-by-label.go index 2fdd9c430..a5da70ab1 100644 --- a/chaoslib/litmus/gcp-vm-disk-loss-by-label/lib/gcp-vm-disk-loss-by-label.go +++ b/chaoslib/litmus/gcp-vm-disk-loss-by-label/lib/gcp-vm-disk-loss-by-label.go @@ -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) } diff --git a/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go b/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go index 8fc2e5e3c..4d1eb8c86 100644 --- a/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go +++ b/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go @@ -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 diff --git a/chaoslib/litmus/http-chaos/lib/http-chaos.go b/chaoslib/litmus/http-chaos/lib/http-chaos.go index c4aae34e2..316a7f96b 100644 --- a/chaoslib/litmus/http-chaos/lib/http-chaos.go +++ b/chaoslib/litmus/http-chaos/lib/http-chaos.go @@ -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 @@ -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) diff --git a/chaoslib/litmus/kafka-broker-pod-failure/lib/pod-delete.go b/chaoslib/litmus/kafka-broker-pod-failure/lib/pod-delete.go index 75892dbe3..0912307a0 100644 --- a/chaoslib/litmus/kafka-broker-pod-failure/lib/pod-delete.go +++ b/chaoslib/litmus/kafka-broker-pod-failure/lib/pod-delete.go @@ -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) @@ -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) diff --git a/chaoslib/litmus/kubelet-service-kill/lib/kubelet-service-kill.go b/chaoslib/litmus/kubelet-service-kill/lib/kubelet-service-kill.go index 95fa6058b..0adf7c90c 100644 --- a/chaoslib/litmus/kubelet-service-kill/lib/kubelet-service-kill.go +++ b/chaoslib/litmus/kubelet-service-kill/lib/kubelet-service-kill.go @@ -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" @@ -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 { diff --git a/chaoslib/litmus/network-chaos/helper/netem.go b/chaoslib/litmus/network-chaos/helper/netem.go index 83ae9aff7..eba3b58a0 100644 --- a/chaoslib/litmus/network-chaos/helper/netem.go +++ b/chaoslib/litmus/network-chaos/helper/netem.go @@ -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() diff --git a/chaoslib/litmus/network-chaos/lib/network-chaos.go b/chaoslib/litmus/network-chaos/lib/network-chaos.go index 489c5b0e6..07a7e7226 100644 --- a/chaoslib/litmus/network-chaos/lib/network-chaos.go +++ b/chaoslib/litmus/network-chaos/lib/network-chaos.go @@ -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" @@ -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 @@ -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) @@ -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 @@ -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 } @@ -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 @@ -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, ",") @@ -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] @@ -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] diff --git a/chaoslib/litmus/node-drain/lib/node-drain.go b/chaoslib/litmus/node-drain/lib/node-drain.go index 84a54279c..bf30c0acf 100644 --- a/chaoslib/litmus/node-drain/lib/node-drain.go +++ b/chaoslib/litmus/node-drain/lib/node-drain.go @@ -49,7 +49,7 @@ 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 { @@ -57,7 +57,7 @@ func PrepareNodeDrain(experimentsDetails *experimentTypes.ExperimentDetails, cli } } - 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") diff --git a/chaoslib/litmus/node-restart/lib/node-restart.go b/chaoslib/litmus/node-restart/lib/node-restart.go index 14238bb82..5b667052e 100644 --- a/chaoslib/litmus/node-restart/lib/node-restart.go +++ b/chaoslib/litmus/node-restart/lib/node-restart.go @@ -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 { @@ -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 diff --git a/chaoslib/litmus/node-taint/lib/node-taint.go b/chaoslib/litmus/node-taint/lib/node-taint.go index f25654928..7e4c8e543 100644 --- a/chaoslib/litmus/node-taint/lib/node-taint.go +++ b/chaoslib/litmus/node-taint/lib/node-taint.go @@ -45,7 +45,7 @@ func PrepareNodeTaint(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 { @@ -53,7 +53,7 @@ func PrepareNodeTaint(experimentsDetails *experimentTypes.ExperimentDetails, cli } } - 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") diff --git a/chaoslib/litmus/pod-cpu-hog-exec/lib/pod-cpu-hog-exec.go b/chaoslib/litmus/pod-cpu-hog-exec/lib/pod-cpu-hog-exec.go index 2347124eb..d6c1bfaf6 100644 --- a/chaoslib/litmus/pod-cpu-hog-exec/lib/pod-cpu-hog-exec.go +++ b/chaoslib/litmus/pod-cpu-hog-exec/lib/pod-cpu-hog-exec.go @@ -40,7 +40,7 @@ func experimentCPU(experimentsDetails *experimentTypes.ExperimentDetails, client // 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") } targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails) diff --git a/chaoslib/litmus/pod-delete/lib/pod-delete.go b/chaoslib/litmus/pod-delete/lib/pod-delete.go index f05d8eae4..1a7342eef 100644 --- a/chaoslib/litmus/pod-delete/lib/pod-delete.go +++ b/chaoslib/litmus/pod-delete/lib/pod-delete.go @@ -80,17 +80,17 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai for duration < experimentsDetails.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.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") } 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 @@ -196,17 +196,17 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet for duration < experimentsDetails.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.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") } 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 diff --git a/chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go b/chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go index 586618eb2..5120dbd51 100644 --- a/chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go +++ b/chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go @@ -23,7 +23,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails // 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") } targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails) @@ -44,7 +44,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) diff --git a/chaoslib/litmus/pod-fio-stress/lib/pod-fio-stress.go b/chaoslib/litmus/pod-fio-stress/lib/pod-fio-stress.go index 0e795de9c..7999bde02 100644 --- a/chaoslib/litmus/pod-fio-stress/lib/pod-fio-stress.go +++ b/chaoslib/litmus/pod-fio-stress/lib/pod-fio-stress.go @@ -48,7 +48,7 @@ func experimentExecution(experimentsDetails *experimentTypes.ExperimentDetails, // 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 either of the appLabel or TARGET_PODS") } targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails) diff --git a/chaoslib/litmus/pod-memory-hog-exec/lib/pod-memory-hog-exec.go b/chaoslib/litmus/pod-memory-hog-exec/lib/pod-memory-hog-exec.go index 812c918ca..bf5e4d723 100644 --- a/chaoslib/litmus/pod-memory-hog-exec/lib/pod-memory-hog-exec.go +++ b/chaoslib/litmus/pod-memory-hog-exec/lib/pod-memory-hog-exec.go @@ -49,7 +49,7 @@ func experimentMemory(experimentsDetails *experimentTypes.ExperimentDetails, cli // 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") } targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails) diff --git a/chaoslib/litmus/pod-network-partition/lib/pod-network-partition.go b/chaoslib/litmus/pod-network-partition/lib/pod-network-partition.go index bed2385d1..fa9c3f21b 100644 --- a/chaoslib/litmus/pod-network-partition/lib/pod-network-partition.go +++ b/chaoslib/litmus/pod-network-partition/lib/pod-network-partition.go @@ -4,6 +4,7 @@ import ( "context" "os" "os/signal" + "strings" "syscall" "time" @@ -40,7 +41,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails signal.Notify(abort, os.Interrupt, syscall.SIGTERM) // validate the appLabels - if chaosDetails.AppDetail.Label == "" { + if strings.TrimSpace(chaosDetails.AppDetail.Label) == "" { return errors.Errorf("please provide the appLabel") } // Get the target pod details for the chaos execution diff --git a/chaoslib/litmus/spring-boot-chaos/lib/spring-boot-chaos.go b/chaoslib/litmus/spring-boot-chaos/lib/spring-boot-chaos.go index 9b0b614a5..65710d914 100644 --- a/chaoslib/litmus/spring-boot-chaos/lib/spring-boot-chaos.go +++ b/chaoslib/litmus/spring-boot-chaos/lib/spring-boot-chaos.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - corev1 "k8s.io/api/core/v1" "net/http" "os" "os/signal" @@ -12,6 +11,8 @@ import ( "syscall" "time" + corev1 "k8s.io/api/core/v1" + "github.com/litmuschaos/litmus-go/pkg/clients" "github.com/litmuschaos/litmus-go/pkg/events" "github.com/litmuschaos/litmus-go/pkg/log" @@ -38,7 +39,7 @@ func SetTargetPodList(experimentsDetails *experimentTypes.ExperimentDetails, cli // if the target pod is not defined it will derive the random target pod list using pod affected percentage var err error - 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") } if experimentsDetails.TargetPodList, err = common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails); err != nil { diff --git a/chaoslib/litmus/stress-chaos/helper/stress-helper.go b/chaoslib/litmus/stress-chaos/helper/stress-helper.go index 327193c9c..48e1e2954 100644 --- a/chaoslib/litmus/stress-chaos/helper/stress-helper.go +++ b/chaoslib/litmus/stress-chaos/helper/stress-helper.go @@ -267,7 +267,7 @@ func prepareStressor(experimentDetails *experimentTypes.ExperimentDetails) []str "Timeout": experimentDetails.ChaosDuration, "Volume Mount Path": experimentDetails.VolumeMountPath, }) - if experimentDetails.VolumeMountPath == "" { + if strings.TrimSpace(experimentDetails.VolumeMountPath) == "" { stressArgs = append(stressArgs, "--io "+experimentDetails.NumberOfWorkers+" --hdd "+experimentDetails.NumberOfWorkers+" --hdd-bytes "+hddbytes) } else { stressArgs = append(stressArgs, "--io "+experimentDetails.NumberOfWorkers+" --hdd "+experimentDetails.NumberOfWorkers+" --hdd-bytes "+hddbytes+" --temp-path "+experimentDetails.VolumeMountPath) diff --git a/chaoslib/litmus/stress-chaos/lib/stress-chaos.go b/chaoslib/litmus/stress-chaos/lib/stress-chaos.go index 53d2a7e25..d4cef15a2 100644 --- a/chaoslib/litmus/stress-chaos/lib/stress-chaos.go +++ b/chaoslib/litmus/stress-chaos/lib/stress-chaos.go @@ -56,17 +56,17 @@ func PrepareAndInjectStressChaos(experimentsDetails *experimentTypes.ExperimentD // 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") } 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 @@ -93,7 +93,7 @@ func PrepareAndInjectStressChaos(experimentsDetails *experimentTypes.ExperimentD } // 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) diff --git a/chaoslib/powerfulseal/pod-delete/lib/pod-delete.go b/chaoslib/powerfulseal/pod-delete/lib/pod-delete.go index 068f936c4..af542d719 100644 --- a/chaoslib/powerfulseal/pod-delete/lib/pod-delete.go +++ b/chaoslib/powerfulseal/pod-delete/lib/pod-delete.go @@ -3,6 +3,7 @@ package lib import ( "context" "strconv" + "strings" "time" clients "github.com/litmuschaos/litmus-go/pkg/clients" @@ -28,7 +29,7 @@ func PreparePodDelete(experimentsDetails *experimentTypes.ExperimentDetails, cli common.WaitForDuration(experimentsDetails.RampTime) } - if experimentsDetails.ChaosServiceAccount == "" { + if strings.TrimSpace(experimentsDetails.ChaosServiceAccount) == "" { // Getting the serviceAccountName for the powerfulseal pod err := GetServiceAccount(experimentsDetails, clients) if err != nil { diff --git a/chaoslib/pumba/container-kill/lib/container-kill.go b/chaoslib/pumba/container-kill/lib/container-kill.go index 838f464b1..5622999d6 100644 --- a/chaoslib/pumba/container-kill/lib/container-kill.go +++ b/chaoslib/pumba/container-kill/lib/container-kill.go @@ -27,7 +27,7 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails, // 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 diff --git a/chaoslib/pumba/cpu-chaos/lib/cpu-chaos.go b/chaoslib/pumba/cpu-chaos/lib/cpu-chaos.go index 23b0ee1b2..103f53926 100644 --- a/chaoslib/pumba/cpu-chaos/lib/cpu-chaos.go +++ b/chaoslib/pumba/cpu-chaos/lib/cpu-chaos.go @@ -28,7 +28,7 @@ func PreparePodCPUHog(experimentsDetails *experimentTypes.ExperimentDetails, cli // 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") } podsAffectedPerc, _ := strconv.Atoi(experimentsDetails.PodsAffectedPerc) diff --git a/chaoslib/pumba/memory-chaos/lib/memory-chaos.go b/chaoslib/pumba/memory-chaos/lib/memory-chaos.go index 102fe26aa..a754c40cc 100644 --- a/chaoslib/pumba/memory-chaos/lib/memory-chaos.go +++ b/chaoslib/pumba/memory-chaos/lib/memory-chaos.go @@ -28,7 +28,7 @@ func PreparePodMemoryHog(experimentsDetails *experimentTypes.ExperimentDetails, // 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") } podsAffectedPerc, _ := strconv.Atoi(experimentsDetails.PodsAffectedPerc) diff --git a/chaoslib/pumba/network-chaos/lib/network-chaos.go b/chaoslib/pumba/network-chaos/lib/network-chaos.go index 388228b50..462d4309c 100644 --- a/chaoslib/pumba/network-chaos/lib/network-chaos.go +++ b/chaoslib/pumba/network-chaos/lib/network-chaos.go @@ -26,7 +26,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails // 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 @@ -294,7 +294,7 @@ func AddTargetIpsArgs(targetIPs, targetHosts string, args []string) ([]string, e return nil, err } - if targetIPs == "" { + if strings.TrimSpace(targetIPs) == "" { return args, nil } ips := strings.Split(targetIPs, ",") diff --git a/chaoslib/pumba/pod-io-stress/lib/pod-io-stress.go b/chaoslib/pumba/pod-io-stress/lib/pod-io-stress.go index 6ebbc52cf..87cfc3a6d 100644 --- a/chaoslib/pumba/pod-io-stress/lib/pod-io-stress.go +++ b/chaoslib/pumba/pod-io-stress/lib/pod-io-stress.go @@ -28,7 +28,7 @@ func PreparePodIOStress(experimentsDetails *experimentTypes.ExperimentDetails, c // 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") } podsAffectedPerc, _ := strconv.Atoi(experimentsDetails.PodsAffectedPerc) @@ -297,7 +297,7 @@ func getContainerArguments(experimentsDetails *experimentTypes.ExperimentDetails "--stressors", } args := stressArgs - if experimentsDetails.VolumeMountPath == "" { + if strings.TrimSpace(experimentsDetails.VolumeMountPath) == "" { args = append(args, "--cpu 1 --io "+experimentsDetails.NumberOfWorkers+" --hdd "+experimentsDetails.NumberOfWorkers+" --hdd-bytes "+hddbytes+" --timeout "+strconv.Itoa(experimentsDetails.ChaosDuration)+"s") } else { args = append(args, "--cpu 1 --io "+experimentsDetails.NumberOfWorkers+" --hdd "+experimentsDetails.NumberOfWorkers+" --hdd-bytes "+hddbytes+" --temp-path "+experimentsDetails.VolumeMountPath+" --timeout "+strconv.Itoa(experimentsDetails.ChaosDuration)+"s") diff --git a/experiments/gcp/gcp-vm-disk-loss-by-label/experiment/gcp-vm-disk-loss-by-label.go b/experiments/gcp/gcp-vm-disk-loss-by-label/experiment/gcp-vm-disk-loss-by-label.go index 71b7adaf8..a784f062c 100644 --- a/experiments/gcp/gcp-vm-disk-loss-by-label/experiment/gcp-vm-disk-loss-by-label.go +++ b/experiments/gcp/gcp-vm-disk-loss-by-label/experiment/gcp-vm-disk-loss-by-label.go @@ -2,6 +2,7 @@ package experiment import ( "os" + "strings" "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" litmusLIB "github.com/litmuschaos/litmus-go/chaoslib/litmus/gcp-vm-disk-loss-by-label/lib" @@ -135,7 +136,7 @@ func GCPVMDiskLossByLabel(clients clients.ClientSets) { for i := range experimentsDetails.TargetDiskVolumeNamesList { instanceName, err := gcp.GetVolumeAttachmentDetails(computeService, experimentsDetails.GCPProjectID, experimentsDetails.DiskZones, experimentsDetails.TargetDiskVolumeNamesList[i]) - if err != nil || instanceName == "" { + if err != nil || strings.TrimSpace(instanceName) == "" { log.Errorf("Failed to verify disk volume attachment status, err: %v", err) failStep := "[post-chaos]: Failed to verify disk volume attachment status, err: " + err.Error() result.RecordAfterFailure(&chaosDetails, &resultDetails, failStep, clients, &eventsDetails) @@ -146,7 +147,7 @@ func GCPVMDiskLossByLabel(clients clients.ClientSets) { log.Infof("[Confirmation]: %v chaos has been injected successfully", experimentsDetails.ExperimentName) resultDetails.Verdict = v1alpha1.ResultVerdictPassed - if experimentsDetails.EngineName != "" { + if strings.TrimSpace(experimentsDetails.EngineName) != "" { // marking AUT as running, as we already checked the status of application under test msg := "AUT: Running" diff --git a/experiments/kafka/kafka-broker-pod-failure/experiment/kafka-broker-pod-failure.go b/experiments/kafka/kafka-broker-pod-failure/experiment/kafka-broker-pod-failure.go index 280cdab04..b3c7bd775 100644 --- a/experiments/kafka/kafka-broker-pod-failure/experiment/kafka-broker-pod-failure.go +++ b/experiments/kafka/kafka-broker-pod-failure/experiment/kafka-broker-pod-failure.go @@ -120,7 +120,7 @@ func KafkaBrokerPodFailure(clients clients.ClientSets) { log.Info("The Liveness pod gets established") log.Infof("[Info]: Kafka partition leader is %v", livenessTopicLeader) - if experimentsDetails.KafkaBroker == "" { + if strings.TrimSpace(experimentsDetails.KafkaBroker) == "" { experimentsDetails.KafkaBroker = livenessTopicLeader } } diff --git a/pkg/cassandra/node-tools.go b/pkg/cassandra/node-tools.go index 2580c357e..038b53649 100644 --- a/pkg/cassandra/node-tools.go +++ b/pkg/cassandra/node-tools.go @@ -77,7 +77,7 @@ func CheckLoadPercentage(loadPercentage []string, replicaCount int) error { for count := 0; count < len(loadPercentage); count++ { - if loadPercentage[count] == "0%" || loadPercentage[count] == "" { + if loadPercentage[count] == "0%" || strings.TrimSpace(loadPercentage[count]) == "" { return errors.Errorf("the load distribution percentage failed, as its value is: '%v'", loadPercentage[count]) } } diff --git a/pkg/clients/clientset.go b/pkg/clients/clientset.go index 732038dc8..057253afa 100644 --- a/pkg/clients/clientset.go +++ b/pkg/clients/clientset.go @@ -2,6 +2,7 @@ package clients import ( "flag" + "strings" chaosClient "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/typed/litmuschaos/v1alpha1" "github.com/pkg/errors" @@ -78,7 +79,7 @@ func generateLitmusClientSet(config *rest.Config) (*chaosClient.LitmuschaosV1alp // buildConfigFromFlags is a helper function that builds configs from a master // url or a kubeconfig filepath, if nothing is provided it falls back to inClusterConfig func buildConfigFromFlags(masterUrl, kubeconfigPath string) (*restclient.Config, error) { - if kubeconfigPath == "" && masterUrl == "" { + if strings.TrimSpace(kubeconfigPath) == "" && strings.TrimSpace(masterUrl) == "" { kubeconfig, err := restclient.InClusterConfig() if err == nil { return kubeconfig, nil diff --git a/pkg/cloud/aws/ebs/ebs-volume-state.go b/pkg/cloud/aws/ebs/ebs-volume-state.go index 8cc00dc03..cc479fb6c 100644 --- a/pkg/cloud/aws/ebs/ebs-volume-state.go +++ b/pkg/cloud/aws/ebs/ebs-volume-state.go @@ -101,7 +101,7 @@ func GetEBSStatus(ebsVolumeID, ec2InstanceID, region string) (string, error) { //EBSStateCheckByID will check the attachment state of the given volume func EBSStateCheckByID(volumeIDs, region string) error { - if volumeIDs == "" { + if strings.TrimSpace(volumeIDs) == "" { return errors.Errorf("no volumeID provided, please provide a volume to detach") } volumeIDList := strings.Split(volumeIDs, ",") @@ -125,7 +125,7 @@ func PostChaosVolumeStatusCheck(experimentsDetails *experimentTypes.ExperimentDe for i, volumeID := range experimentsDetails.TargetVolumeIDList { //Get volume attachment details ec2InstanceID, _, err := GetVolumeAttachmentDetails(volumeID, experimentsDetails.VolumeTag, experimentsDetails.Region) - if err != nil || ec2InstanceID == "" { + if err != nil || strings.TrimSpace(ec2InstanceID) == "" { return errors.Errorf("fail to get the attachment info, err: %v", err) } ec2InstanceIDList = append(ec2InstanceIDList, ec2InstanceID) diff --git a/pkg/cloud/aws/ec2/ec2-instance-status.go b/pkg/cloud/aws/ec2/ec2-instance-status.go index 3daf88409..71c437d22 100644 --- a/pkg/cloud/aws/ec2/ec2-instance-status.go +++ b/pkg/cloud/aws/ec2/ec2-instance-status.go @@ -41,7 +41,7 @@ func GetEC2InstanceStatus(instanceID, region string) (string, error) { //InstanceStatusCheckByID is used to check the instance status of all the instance under chaos. func InstanceStatusCheckByID(instanceID, region string) error { - if instanceID == "" { + if strings.TrimSpace(instanceID) == "" { return errors.Errorf("no instance id found to terminate") } instanceIDList := strings.Split(instanceID, ",") @@ -87,7 +87,7 @@ func PreChaosNodeCountCheck(instanceIDList []string, region string) (int, string } // finding the autoscaling group name for the provided instance id - if autoScalingGroupName = findAutoScalingGroupName(instanceIDList[0], nodeList); autoScalingGroupName == "" { + if autoScalingGroupName = findAutoScalingGroupName(instanceIDList[0], nodeList); strings.TrimSpace(autoScalingGroupName) == "" { return 0, "", errors.Errorf("instances not part of autoscaling group") } @@ -111,7 +111,7 @@ func PostChaosNodeCountCheck(activeNodeCount int, autoScalingGroupName, region s if nodeList, err = getAutoScalingInstances(region); err != nil { return err } - if autoScalingGroupName == "" { + if strings.TrimSpace(autoScalingGroupName) == "" { return errors.Errorf("autoscaling group not provided") } diff --git a/pkg/cloud/azure/instance/instance-status.go b/pkg/cloud/azure/instance/instance-status.go index e6a568854..0d1f861c2 100644 --- a/pkg/cloud/azure/instance/instance-status.go +++ b/pkg/cloud/azure/instance/instance-status.go @@ -71,7 +71,7 @@ func GetAzureScaleSetInstanceStatus(subscriptionID, resourceGroup, virtualMachin // InstanceStatusCheckByName is used to check the instance status of all the instance under chaos func InstanceStatusCheckByName(azureInstanceNames, scaleSet, subscriptionID, resourceGroup string) error { - if azureInstanceNames == "" { + if strings.TrimSpace(azureInstanceNames) == "" { return errors.Errorf("no instance found to check the status") } instanceNameList := strings.Split(azureInstanceNames, ",") diff --git a/pkg/cloud/gcp/disk-volume-status.go b/pkg/cloud/gcp/disk-volume-status.go index 4529a5b05..8dc02f934 100644 --- a/pkg/cloud/gcp/disk-volume-status.go +++ b/pkg/cloud/gcp/disk-volume-status.go @@ -99,16 +99,16 @@ func GetDiskVolumeState(computeService *compute.Service, diskName, gcpProjectID, //DiskVolumeStateCheck will check the attachment state of the given volume func DiskVolumeStateCheck(computeService *compute.Service, experimentsDetails *experimentTypes.ExperimentDetails, stage string) error { - if experimentsDetails.GCPProjectID == "" { + if strings.TrimSpace(experimentsDetails.GCPProjectID) == "" { return errors.Errorf("no gcp project id provided, please provide the project id") } - if experimentsDetails.DiskVolumeNames == "" { + if strings.TrimSpace(experimentsDetails.DiskVolumeNames) == "" { return errors.Errorf("no disk name provided, please provide the name of the disk") } diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",") - if experimentsDetails.DiskZones == "" { + if strings.TrimSpace(experimentsDetails.DiskZones) == "" { return errors.Errorf("no zone provided, please provide the zone of the disk") } @@ -121,7 +121,7 @@ func DiskVolumeStateCheck(computeService *compute.Service, experimentsDetails *e for i := range diskNamesList { instanceName, err := GetVolumeAttachmentDetails(computeService, experimentsDetails.GCPProjectID, zonesList[i], diskNamesList[i]) - if err != nil || instanceName == "" { + if err != nil || strings.TrimSpace(instanceName) == "" { return errors.Errorf("failed to get the vm instance name for %s disk volume, err: %v", diskNamesList[i], err) } diff --git a/pkg/cloud/gcp/vm-instance-status.go b/pkg/cloud/gcp/vm-instance-status.go index 04319dbe4..f5c39726f 100644 --- a/pkg/cloud/gcp/vm-instance-status.go +++ b/pkg/cloud/gcp/vm-instance-status.go @@ -26,12 +26,12 @@ func GetVMInstanceStatus(computeService *compute.Service, instanceName string, g //InstanceStatusCheckByName is used to check the status of all the VM instances under chaos func InstanceStatusCheckByName(computeService *compute.Service, managedInstanceGroup string, delay, timeout int, check string, instanceNames string, gcpProjectId string, instanceZones string) error { - if instanceNames == "" { + if strings.TrimSpace(instanceNames) == "" { return errors.Errorf("no vm instance name found to stop") } instanceNamesList := strings.Split(instanceNames, ",") - if instanceZones == "" { + if strings.TrimSpace(instanceZones) == "" { return errors.Errorf("no corresponding zones found for the instances") } instanceZonesList := strings.Split(instanceZones, ",") diff --git a/pkg/cloud/gcp/vm-operations.go b/pkg/cloud/gcp/vm-operations.go index 413dcb9ed..39e11fad9 100644 --- a/pkg/cloud/gcp/vm-operations.go +++ b/pkg/cloud/gcp/vm-operations.go @@ -1,6 +1,7 @@ package gcp import ( + "strings" "time" experimentTypes "github.com/litmuschaos/litmus-go/pkg/gcp/gcp-vm-instance-stop/types" @@ -97,7 +98,7 @@ func WaitForVMInstanceUp(computeService *compute.Service, timeout int, delay int //SetTargetInstance will select the target vm instances which are in RUNNING state and filtered from the given label func SetTargetInstance(computeService *compute.Service, experimentsDetails *experimentTypes.ExperimentDetails) error { - if experimentsDetails.InstanceLabel == "" { + if strings.TrimSpace(experimentsDetails.InstanceLabel) == "" { return errors.Errorf("label not found, please provide a valid label") } diff --git a/pkg/cloud/vmware/vm-status.go b/pkg/cloud/vmware/vm-status.go index b461ddf95..b8328350d 100644 --- a/pkg/cloud/vmware/vm-status.go +++ b/pkg/cloud/vmware/vm-status.go @@ -58,7 +58,7 @@ func GetVMStatus(vcenterServer, vmId, cookie string) (string, error) { //VMStatusCheck validates the steady state for the given vm ids func VMStatusCheck(vcenterServer, vmIds, cookie string) error { - if vmIds == "" { + if strings.TrimSpace(vmIds) == "" { return errors.Errorf("no vm received, please input the target VMMoids") } vmIdList := strings.Split(vmIds, ",") diff --git a/pkg/kafka/kafka-liveness-stream.go b/pkg/kafka/kafka-liveness-stream.go index 8d2554497..a0d2d31a4 100644 --- a/pkg/kafka/kafka-liveness-stream.go +++ b/pkg/kafka/kafka-liveness-stream.go @@ -38,7 +38,7 @@ func LivenessStream(experimentsDetails *experimentTypes.ExperimentDetails, clien } log.Info("[Liveness]: Obtain the leader broker ordinality for the topic (partition) created by kafka-liveness") - if experimentsDetails.KafkaInstanceName == "" { + if strings.TrimSpace(experimentsDetails.KafkaInstanceName) == "" { execCommandDetails := litmusexec.PodDetails{} command := append([]string{"/bin/sh", "-c"}, "kafka-topics --topic topic-"+experimentsDetails.RunID+" --describe --zookeeper "+experimentsDetails.ZookeeperService+":"+experimentsDetails.ZookeeperPort+" | grep -o 'Leader: [^[:space:]]*' | awk '{print $2}'") diff --git a/pkg/probe/cmdprobe.go b/pkg/probe/cmdprobe.go index 375160066..1a11b593a 100644 --- a/pkg/probe/cmdprobe.go +++ b/pkg/probe/cmdprobe.go @@ -195,7 +195,7 @@ func getProbeArgs(sourceArgs []string) []string { // getProbeImagePullPolicy adds the image pull policy to the source pod func getProbeImagePullPolicy(policy string, chaosDetails *types.ChaosDetails) string { - if policy == "" { + if strings.TrimSpace(policy) == "" { return chaosDetails.ProbeImagePullPolicy } return policy diff --git a/pkg/probe/promProbe.go b/pkg/probe/promProbe.go index fd88982e1..1807f6b71 100644 --- a/pkg/probe/promProbe.go +++ b/pkg/probe/promProbe.go @@ -332,7 +332,7 @@ func extractValueFromMetrics(metrics string) (string, error) { // splitting the metrics entries which are available as comma separated values := strings.Split(rows[1], ",") - if values[indexForValueColumn] == "" { + if strings.TrimSpace(values[indexForValueColumn]) == "" { return "", errors.Errorf("error while parsing value from derived matrics") } return values[indexForValueColumn], nil diff --git a/pkg/types/types.go b/pkg/types/types.go index 39a113a2b..e59a1892d 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -3,6 +3,7 @@ package types import ( "os" "strconv" + "strings" "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" corev1 "k8s.io/api/core/v1" @@ -182,7 +183,7 @@ func SetResultEventAttributes(eventsDetails *EventDetails, Reason, Message, Type // Getenv fetch the env and set the default value, if any func Getenv(key string, defaultValue string) string { value := os.Getenv(key) - if value == "" { + if strings.TrimSpace(value) == "" { value = defaultValue } return value diff --git a/pkg/utils/common/common.go b/pkg/utils/common/common.go index e6ad34dd2..bd006d30d 100644 --- a/pkg/utils/common/common.go +++ b/pkg/utils/common/common.go @@ -167,12 +167,12 @@ func HelperFailedError(err error) error { // GetStatusMessage returns the event message func GetStatusMessage(defaultCheck bool, defaultMsg, probeStatus string) string { if defaultCheck { - if probeStatus == "" { + if strings.TrimSpace(probeStatus) == "" { return defaultMsg } return defaultMsg + ", Probes: " + probeStatus } - if probeStatus == "" { + if strings.TrimSpace(probeStatus) == "" { return "Skipped the default checks" } return "Probes: " + probeStatus diff --git a/pkg/utils/common/pods.go b/pkg/utils/common/pods.go index 60dd3678d..209bb622f 100644 --- a/pkg/utils/common/pods.go +++ b/pkg/utils/common/pods.go @@ -184,7 +184,7 @@ func GetPodList(targetPods string, podAffPerc int, clients clients.ClientSets, c // CheckForAvailibiltyOfPod check the availibility of the specified pod func CheckForAvailibiltyOfPod(namespace, name string, clients clients.ClientSets) (bool, error) { - if name == "" { + if strings.TrimSpace(name) == "" { return false, nil } _, err := clients.KubeClient.CoreV1().Pods(namespace).Get(context.Background(), name, v1.GetOptions{}) @@ -209,7 +209,7 @@ func FilterNonChaosPods(clients clients.ClientSets, chaosDetails *types.ChaosDet nonChaosPods := core_v1.PodList{} // ignore chaos pods for index, pod := range podList.Items { - if pod.Labels["chaosUID"] == "" && pod.Labels["name"] != "chaos-operator" { + if strings.TrimSpace(pod.Labels["chaosUID"]) == "" && pod.Labels["name"] != "chaos-operator" { nonChaosPods.Items = append(nonChaosPods.Items, podList.Items[index]) } }