From fb57864c6273d54f85de7d423a51f4cb0a799fcd Mon Sep 17 00:00:00 2001 From: Joe Talerico Date: Wed, 13 Dec 2023 14:27:27 -0500 Subject: [PATCH] Add version flag Add the version flag using the go-commons code. Signed-off-by: Joe Talerico --- Makefile | 14 +++++++++++++- cmd/k8s-netperf/k8s-netperf.go | 14 +++++++++++++- pkg/archive/archive.go | 4 ++++ pkg/results/result.go | 4 +++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2f2b52eb..66c391f9 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 diff --git a/cmd/k8s-netperf/k8s-netperf.go b/cmd/k8s-netperf/k8s-netperf.go index 0768f046..5c83a3a7 100644 --- a/cmd/k8s-netperf/k8s-netperf.go +++ b/cmd/k8s-netperf/k8s-netperf.go @@ -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" @@ -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 { @@ -79,7 +89,6 @@ var rootCmd = &cobra.Command{ } cfg = cf } - // Read in k8s config kconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), @@ -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] @@ -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) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 753db0fe..fc9938b4 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -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"` @@ -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, diff --git a/pkg/results/result.go b/pkg/results/result.go index 69fe64c0..b4793ef8 100644 --- a/pkg/results/result.go +++ b/pkg/results/result.go @@ -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 }