Skip to content

Commit

Permalink
adding the --bridgeNetwork option to change the default configuration…
Browse files Browse the repository at this point in the history
… for bridge setup
  • Loading branch information
capolrik committed Jan 16, 2025
1 parent e6846cf commit a8468ee
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 55 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ spec:
Then you can launch a test using the bridge interface:
```
./bin/amd64/k8s-netperf --vm --bridge br0
```
If the default IP addresses (10.10.10.12/24 and 10.10.10.14/24) are not available for your setup, it is possible to change it by passing a JSON as a parameter with `--bridgeNetwork`, like follow:
```
k8s-netperf --vm --bridge br0 --bridgeNetwork "{\"bridgeServerNetwork\": \"10.10.40.22/24\",\"bridgeClientNetwork\": \"10.10.40.24/24\"}"
```
### Config file
Expand Down
72 changes: 49 additions & 23 deletions cmd/k8s-netperf/k8s-netperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
encodeJson "encoding/json"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -33,28 +34,29 @@ const index = "k8s-netperf"
const retry = 3

var (
cfgfile string
nl bool
clean bool
netperf bool
iperf3 bool
uperf bool
udn bool
acrossAZ bool
full bool
vm bool
vmimage string
debug bool
bridge string
promURL string
id string
searchURL string
showMetrics bool
tcpt float64
json bool
version bool
csvArchive bool
searchIndex string
cfgfile string
nl bool
clean bool
netperf bool
iperf3 bool
uperf bool
udn bool
acrossAZ bool
full bool
vm bool
vmimage string
debug bool
bridge string
bridgeNetwork string
promURL string
id string
searchURL string
showMetrics bool
tcpt float64
json bool
version bool
csvArchive bool
searchIndex string
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -191,6 +193,10 @@ var rootCmd = &cobra.Command{
log.Error(err)
}
s.Bridge = true
s.BridgeServerNetwork, s.BridgeClientNetwork, err = parseNetworkConfig(bridgeNetwork)
if err != nil {
log.Error(err)
}
}
}

Expand Down Expand Up @@ -403,6 +409,25 @@ func cleanup(client *kubernetes.Clientset) {
}
}

// Function to parse the JSON and return the IP parts (before '/')
func parseNetworkConfig(jsonInput string) (string, string, error) {
// Create an instance of the struct
var netConfig config.BridgeNetworkConfig

// Unmarshal the JSON string into the struct
err := encodeJson.Unmarshal([]byte(jsonInput), &netConfig)
if err != nil {
return "", "", fmt.Errorf("error parsing JSON: %v", err)
}

// Extract the IP parts (before '/')
serverIP := netConfig.BridgeServerNetwork
clientIP := netConfig.BridgeClientNetwork

// Return the extracted IPs
return serverIP, clientIP, nil
}

// executeWorkload executes the workload and returns the result data.
func executeWorkload(nc config.Config,
s config.PerfScenarios,
Expand All @@ -427,7 +452,7 @@ func executeWorkload(nc config.Config,
}
//when using a bridge, ip is static
} else if s.Bridge {
serverIP = "10.10.10.14"
serverIP = strings.Split(s.BridgeServerNetwork, "/")[0]
} else {
if hostNet {
serverIP = s.ServerHost.Items[0].Status.PodIP
Expand Down Expand Up @@ -528,6 +553,7 @@ func main() {
rootCmd.Flags().BoolVar(&debug, "debug", false, "Enable debug log")
rootCmd.Flags().BoolVar(&udn, "udn", false, "Create and use a UDN called 'udn-l2-primary' as primary network.")
rootCmd.Flags().StringVar(&bridge, "bridge", "", "Name of the NNCP to be used for creating bridge interface - VM only.")
rootCmd.Flags().StringVar(&bridgeNetwork, "bridgeNetwork", "{\"bridgeServerNetwork\": \"10.10.10.12/24\",\"bridgeClientNetwork\": \"10.10.10.14/24\"}", "A json use to override the network defined by the bridge interface - bridge should be enabled")
rootCmd.Flags().StringVar(&promURL, "prom", "", "Prometheus URL")
rootCmd.Flags().StringVar(&id, "uuid", "", "User provided UUID")
rootCmd.Flags().StringVar(&searchURL, "search", "", "OpenSearch URL, if you have auth, pass in the format of https://user:pass@url:port")
Expand Down
56 changes: 32 additions & 24 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ type Config struct {

// PerfScenarios describes the different scenarios
type PerfScenarios struct {
NodeLocal bool
AcrossAZ bool
HostNetwork bool
Configs []Config
VM bool
VMImage string
VMHost string
Udn bool
Bridge bool
ServerNodeInfo metrics.NodeInfo
ClientNodeInfo metrics.NodeInfo
Client apiv1.PodList
Server apiv1.PodList
ClientAcross apiv1.PodList
ClientHost apiv1.PodList
ServerHost apiv1.PodList
NetperfService *apiv1.Service
IperfService *apiv1.Service
UperfService *apiv1.Service
RestConfig rest.Config
ClientSet *kubernetes.Clientset
KClient *kubevirtv1.KubevirtV1Client
DClient *dynamic.DynamicClient
SSHClient *goph.Client
NodeLocal bool
AcrossAZ bool
HostNetwork bool
Configs []Config
VM bool
VMImage string
VMHost string
Udn bool
Bridge bool
BridgeServerNetwork string
BridgeClientNetwork string
ServerNodeInfo metrics.NodeInfo
ClientNodeInfo metrics.NodeInfo
Client apiv1.PodList
Server apiv1.PodList
ClientAcross apiv1.PodList
ClientHost apiv1.PodList
ServerHost apiv1.PodList
NetperfService *apiv1.Service
IperfService *apiv1.Service
UperfService *apiv1.Service
RestConfig rest.Config
ClientSet *kubernetes.Clientset
KClient *kubevirtv1.KubevirtV1Client
DClient *dynamic.DynamicClient
SSHClient *goph.Client
}

// struct for bridge options
type BridgeNetworkConfig struct {
BridgeServerNetwork string `json:"bridgeServerNetwork"`
BridgeClientNetwork string `json:"bridgeClientNetwork"`
}

// Tests we will support in k8s-netperf
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func ExtractUdnIp(s config.PerfScenarios) (string, error) {

// launchServerVM will create the ServerVM with the specific node and pod affinity.
func launchServerVM(perf *config.PerfScenarios, name string, podAff *corev1.PodAntiAffinity, nodeAff *corev1.NodeAffinity) error {
_, err := CreateVMServer(perf.KClient, serverRole, serverRole, *podAff, *nodeAff, perf.VMImage, perf.Bridge)
_, err := CreateVMServer(perf.KClient, serverRole, serverRole, *podAff, *nodeAff, perf.VMImage, perf.Bridge, perf.BridgeServerNetwork)
if err != nil {
return err
}
Expand All @@ -612,7 +612,7 @@ func launchServerVM(perf *config.PerfScenarios, name string, podAff *corev1.PodA

// launchClientVM will create the ClientVM with the specific node and pod affinity.
func launchClientVM(perf *config.PerfScenarios, name string, podAff *corev1.PodAntiAffinity, nodeAff *corev1.NodeAffinity) error {
host, err := CreateVMClient(perf.KClient, perf.ClientSet, perf.DClient, name, podAff, nodeAff, perf.VMImage, perf.Bridge)
host, err := CreateVMClient(perf.KClient, perf.ClientSet, perf.DClient, name, podAff, nodeAff, perf.VMImage, perf.Bridge, perf.BridgeClientNetwork)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/k8s/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func exposeService(client *kubernetes.Clientset, dynamicClient *dynamic.DynamicC

// CreateVMClient takes in the affinity rules and deploys the VMI
func CreateVMClient(kclient *kubevirtv1.KubevirtV1Client, client *kubernetes.Clientset,
dyn *dynamic.DynamicClient, name string, podAff *corev1.PodAntiAffinity, nodeAff *corev1.NodeAffinity, vmimage string, bridge bool) (string, error) {
dyn *dynamic.DynamicClient, name string, podAff *corev1.PodAntiAffinity, nodeAff *corev1.NodeAffinity, vmimage string, bridge bool, bridgeNetwork string) (string, error) {
label := map[string]string{
"app": name,
"role": name,
Expand Down Expand Up @@ -228,10 +228,10 @@ runcmd:
},
},
})
netData = `version: 2
netData = fmt.Sprintf(`version: 2
ethernets:
eth1:
addresses: [ 10.10.10.12/24 ]`
addresses: [ %s ]`, bridgeNetwork)
}
_, err = CreateVMI(kclient, name, label, b64.StdEncoding.EncodeToString([]byte(data)), *podAff, *nodeAff, vmimage, interfaces, networks, b64.StdEncoding.EncodeToString([]byte(netData)))
if err != nil {
Expand All @@ -250,7 +250,7 @@ ethernets:

// CreateVMServer will take the pod and node affinity and deploy the VMI
func CreateVMServer(client *kubevirtv1.KubevirtV1Client, name string, role string, podAff corev1.PodAntiAffinity,
nodeAff corev1.NodeAffinity, vmimage string, bridge bool) (*v1.VirtualMachineInstance, error) {
nodeAff corev1.NodeAffinity, vmimage string, bridge bool, bridgeNetwork string) (*v1.VirtualMachineInstance, error) {
label := map[string]string{
"app": name,
"role": role,
Expand Down Expand Up @@ -321,10 +321,10 @@ runcmd:
},
},
})
netData = `version: 2
netData = fmt.Sprintf(`version: 2
ethernets:
eth1:
addresses: [ 10.10.10.14/24 ]`
addresses: [ %s ]`, bridgeNetwork)
}
return CreateVMI(client, name, label, b64.StdEncoding.EncodeToString([]byte(data)), podAff, nodeAff, vmimage, interfaces, networks, b64.StdEncoding.EncodeToString([]byte(netData)))
}
Expand Down

0 comments on commit a8468ee

Please sign in to comment.