From 85213fdcb0e6a64d8c481b5601fc4abd5d860749 Mon Sep 17 00:00:00 2001
From: Raul Sevilla <rsevilla@redhat.com>
Date: Mon, 19 Feb 2024 22:25:28 +0100
Subject: [PATCH] Fix hostNetwork code

Signed-off-by: Raul Sevilla <rsevilla@redhat.com>
---
 README.md                      |  3 +-
 cmd/k8s-netperf/k8s-netperf.go | 51 +++++++++++-----------------------
 pkg/drivers/iperf.go           |  9 +++---
 3 files changed, 22 insertions(+), 41 deletions(-)

diff --git a/README.md b/README.md
index f92828ae..46c3ab5a 100644
--- a/README.md
+++ b/README.md
@@ -84,13 +84,14 @@ Flags:
   - When using `--prom` with a non-openshift clsuter, it will be necessary to pass the prometheus URL.
 - `--metrics` will enable displaying prometheus captured metrics to stdout. By default they will be written to a csv file.
 - `--iperf` will enable the iperf3 load driver for any stream test (TCP_STREAM, UDP_STREAM). iperf3 doesn't have a RR or CRR test-type.
-- `--uperf ` will enable the uperf load driver for any stream test (TCP_STREAM, UDP_STREAM). uperf doesn't have CRR test-type.
+- `--uperf` will enable the uperf load driver for any stream test (TCP_STREAM, UDP_STREAM). uperf doesn't have CRR test-type.
 
 > *Note: With OpenShift, we attempt to discover the OpenShift route. If that route is not reachable, it might be required to `port-forward` the service and pass that via the `--prom` option.*
 
 ### Config file
 #### Config File v2
 The v2 config file will be executed in the order the tests are presented in the config file.
+
 ```yml
 tests :
   - TCPStream:              # Place-holder of a test name
diff --git a/cmd/k8s-netperf/k8s-netperf.go b/cmd/k8s-netperf/k8s-netperf.go
index 48871a66..b7606436 100644
--- a/cmd/k8s-netperf/k8s-netperf.go
+++ b/cmd/k8s-netperf/k8s-netperf.go
@@ -54,6 +54,8 @@ var rootCmd = &cobra.Command{
 	Use:   "k8s-netperf",
 	Short: "A tool to run network performance tests in Kubernetes cluster",
 	Run: func(cmd *cobra.Command, args []string) {
+		var acrossAZ, hostNetwork bool
+		var sr result.ScenarioResults
 		if version {
 			fmt.Println("Version:", cmdVersion.Version)
 			fmt.Println("Git Commit:", cmdVersion.GitCommit)
@@ -139,13 +141,11 @@ var rootCmd = &cobra.Command{
 			log.Fatal(err)
 		}
 
-		var sr result.ScenarioResults
 		sr.Version = cmdVersion.Version
 		sr.GitCommit = cmdVersion.GitCommit
 		// If the client and server needs to be across zones
 		lz, zones, _ := k8s.GetZone(client)
 		nodesInZone := zones[lz]
-		var acrossAZ bool
 		if nodesInZone > 1 {
 			acrossAZ = false
 		} else {
@@ -161,40 +161,24 @@ var rootCmd = &cobra.Command{
 			}
 			nc.Metric = metric
 			nc.AcrossAZ = acrossAZ
-
-			if s.HostNetwork {
-				// No need to run hostNetwork through Service.
-				if !nc.Service {
-					if netperf {
-						npr := executeWorkload(nc, s, true, true, false, false)
-						sr.Results = append(sr.Results, npr)
-					}
-					if iperf3 {
-						ipr := executeWorkload(nc, s, true, true, true, false)
-						if len(ipr.Profile) > 1 {
-							sr.Results = append(sr.Results, ipr)
-						}
-					}
-					if uperf {
-						upr := executeWorkload(nc, s, true, true, true, true)
-						if len(upr.Profile) > 1 {
-							sr.Results = append(sr.Results, upr)
-						}
-					}
-				}
+			// No need to run hostNetwork through Service.
+			if s.HostNetwork && !nc.Service {
+				hostNetwork = true
+			} else {
+				hostNetwork = false
 			}
 			if netperf {
-				npr := executeWorkload(nc, s, false, true, false, false)
+				npr := executeWorkload(nc, s, hostNetwork, true, false, false)
 				sr.Results = append(sr.Results, npr)
 			}
 			if iperf3 {
-				ipr := executeWorkload(nc, s, false, false, true, false)
+				ipr := executeWorkload(nc, s, hostNetwork, false, true, false)
 				if len(ipr.Profile) > 1 {
 					sr.Results = append(sr.Results, ipr)
 				}
 			}
 			if uperf {
-				upr := executeWorkload(nc, s, false, false, false, true)
+				upr := executeWorkload(nc, s, hostNetwork, false, false, true)
 				if len(upr.Profile) > 1 {
 					sr.Results = append(sr.Results, upr)
 				}
@@ -338,14 +322,12 @@ func cleanup(client *kubernetes.Clientset) {
 
 }
 
-func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, netperfDriver, iperf3, uperf bool) result.Data {
+// executeWorkload executes the workload and returns the result data.
+func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, netperf, iperf3, uperf bool) result.Data {
 	serverIP := ""
-	service := false
-	sameNode := true
 	Client := s.Client
 	var driver drivers.Driver
 	if nc.Service {
-		service = true
 		if iperf3 {
 			serverIP = s.IperfService.Spec.ClusterIP
 		} else if uperf {
@@ -362,7 +344,6 @@ func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, net
 	}
 	if !s.NodeLocal {
 		Client = s.ClientAcross
-		sameNode = false
 	}
 	if hostNet {
 		Client = s.ClientHost
@@ -370,8 +351,8 @@ func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, net
 	npr := result.Data{}
 	npr.Config = nc
 	npr.Metric = nc.Metric
-	npr.Service = service
-	npr.SameNode = sameNode
+	npr.Service = nc.Service
+	npr.SameNode = s.NodeLocal
 	npr.HostNetwork = hostNet
 	if s.AcrossAZ {
 		npr.AcrossAZ = true
@@ -379,10 +360,10 @@ func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, net
 		npr.AcrossAZ = nc.AcrossAZ
 	}
 	npr.StartTime = time.Now()
-	log.Debugf("Executing workloads. hostNetwork is %t, service is %t", hostNet, service)
+	log.Debugf("Executing workloads. hostNetwork is %t, service is %t", hostNet, nc.Service)
 	for i := 0; i < nc.Samples; i++ {
 		nr := sample.Sample{}
-		if netperfDriver {
+		if netperf {
 			driver = drivers.NewDriver("netperf")
 			npr.Driver = "netperf"
 		}
diff --git a/pkg/drivers/iperf.go b/pkg/drivers/iperf.go
index 5abf4e20..0ab4b40d 100644
--- a/pkg/drivers/iperf.go
+++ b/pkg/drivers/iperf.go
@@ -177,10 +177,9 @@ func (i *iperf3) ParseResults(stdout *bytes.Buffer) (sample.Sample, error) {
 	sample.Driver = i.driverName
 	result := IperfResult{}
 	sample.Metric = "Mb/s"
-	error := json.NewDecoder(stdout).Decode(&result)
-	if error != nil {
-		log.Error("Issue while decoding")
-		log.Error(error)
+	err := json.NewDecoder(stdout).Decode(&result)
+	if err != nil {
+		log.Errorf("Issue while decoding: %v", err)
 	}
 	if result.Data.TCPStream.Rate > 0 {
 		sample.Throughput = float64(result.Data.TCPStream.Rate) / 1000000
@@ -191,7 +190,7 @@ func (i *iperf3) ParseResults(stdout *bytes.Buffer) (sample.Sample, error) {
 		sample.LossPercent = result.Data.UDPStream.LossPercent
 	}
 
-	log.Debugf("Storing %s sample throughput:  %f", sample.Driver, sample.Throughput)
+	log.Debugf("Storing %s sample throughput: %f", sample.Driver, sample.Throughput)
 
 	return sample, nil
 }