Skip to content

Commit

Permalink
Add version flag
Browse files Browse the repository at this point in the history
Add the version flag using the go-commons code.

Signed-off-by: Joe Talerico <[email protected]>
  • Loading branch information
Joe Talerico committed Dec 14, 2023
1 parent 4dfa1c5 commit fb57864
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ RHEL_VERSION = ubi9
CONTAINER_BUILD ?= podman build --force-rm
CONTAINER_NS ?= quay.io/cloud-bulldozer/netperf


# k8s-netperf version
GIT_COMMIT = $(shell git rev-parse HEAD)
BUILD_DATE = $(shell date '+%Y-%m-%d-%H:%M:%S')
CMD_VERSION= github.com/cloud-bulldozer/go-commons/version
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
ifeq ($(BRANCH),HEAD)
VERSION := $(shell git describe --tags --abbrev=0)
else
VERSION := $(BRANCH)
endif

all: build container-build

build: $(BIN_PATH)
Expand All @@ -41,4 +53,4 @@ clean: $(BIN_PATH)
rm -rf bin/$(ARCH)

$(BIN_PATH): $(SOURCES)
GOARCH=$(ARCH) CGO_ENABLED=$(CGO) go build -v -o $(BIN_PATH) ./cmd/k8s-netperf
GOARCH=$(ARCH) CGO_ENABLED=$(CGO) go build -v -ldflags "-X $(CMD_VERSION).GitCommit=$(GIT_COMMIT) -X $(CMD_VERSION).BuildDate=$(BUILD_DATE) -X $(CMD_VERSION).Version=$(VERSION)" -o $(BIN_PATH) ./cmd/k8s-netperf
14 changes: 13 additions & 1 deletion cmd/k8s-netperf/k8s-netperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cloud-bulldozer/go-commons/indexers"
ocpmetadata "github.com/cloud-bulldozer/go-commons/ocp-metadata"
"github.com/cloud-bulldozer/go-commons/prometheus"
cmdVersion "github.com/cloud-bulldozer/go-commons/version"
"github.com/cloud-bulldozer/k8s-netperf/pkg/archive"
"github.com/cloud-bulldozer/k8s-netperf/pkg/config"
"github.com/cloud-bulldozer/k8s-netperf/pkg/iperf"
Expand Down Expand Up @@ -47,12 +48,21 @@ var (
showMetrics bool
tcpt float64
json bool
version bool
)

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) {
if version {
fmt.Println("Version:", cmdVersion.Version)
fmt.Println("Git Commit:", cmdVersion.GitCommit)
fmt.Println("Build Date:", cmdVersion.BuildDate)
fmt.Println("Go Version:", cmdVersion.GoVersion)
fmt.Println("OS/Arch:", cmdVersion.OsArch)
os.Exit(0)
}

uid := ""
if len(id) > 0 {
Expand All @@ -79,7 +89,6 @@ var rootCmd = &cobra.Command{
}
cfg = cf
}

// Read in k8s config
kconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
Expand Down Expand Up @@ -137,6 +146,8 @@ var rootCmd = &cobra.Command{
}

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]
Expand Down Expand Up @@ -481,6 +492,7 @@ func main() {
rootCmd.Flags().StringVar(&searchURL, "search", "", "OpenSearch URL, if you have auth, pass in the format of https://user:pass@url:port")
rootCmd.Flags().BoolVar(&showMetrics, "metrics", false, "Show all system metrics retrieved from prom")
rootCmd.Flags().Float64Var(&tcpt, "tcp-tolerance", 10, "Allowed %diff from hostNetwork to podNetwork, anything above tolerance will result in k8s-netperf exiting 1.")
rootCmd.Flags().BoolVar(&version, "version", false, "k8s-netperf version")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Doc struct {
LtcyMetric string `json:"ltcyMetric"`
TCPRetransmit float64 `json:"tcpRetransmits"`
UDPLossPercent float64 `json:"udpLossPercent"`
ToolVersion string `json:"toolVersion"`
ToolGitCommit string `json:"toolGitCommit"`
Metadata result.Metadata `json:"metadata"`
ServerNodeCPU metrics.NodeCPU `json:"serverCPU"`
ServerPodCPU []metrics.PodCPU `json:"serverPods"`
Expand Down Expand Up @@ -87,6 +89,8 @@ func BuildDocs(sr result.ScenarioResults, uuid string) ([]interface{}, error) {
d := Doc{
UUID: uuid,
Timestamp: time,
ToolVersion: sr.Version,
ToolGitCommit: sr.GitCommit,
Driver: r.Driver,
HostNetwork: r.HostNetwork,
Parallelism: r.Parallelism,
Expand Down
4 changes: 3 additions & 1 deletion pkg/results/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ type Data struct {

// ScenarioResults each scenario could have multiple results
type ScenarioResults struct {
Results []Data
Results []Data
Version string
GitCommit string
Metadata
}

Expand Down

0 comments on commit fb57864

Please sign in to comment.