From c7b3689df8a5cdf212ca80f9f0332ac3d518eefc Mon Sep 17 00:00:00 2001 From: Dimitri Koshkin Date: Mon, 3 Dec 2018 22:15:34 -0500 Subject: [PATCH] Add version subcommand --- Makefile | 5 ++++- cmd/main.go | 16 +++++++++++++++- pkg/driver/driver.go | 5 ----- pkg/driver/identity.go | 2 +- pkg/driver/version.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 pkg/driver/version.go diff --git a/Makefile b/Makefile index 26464727c8..9bda26938c 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,14 @@ IMAGE=amazon/aws-ebs-csi-driver VERSION=0.1.0-alpha +GIT_COMMIT?=$(shell git rev-parse HEAD) +BUILD_DATE?=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +LDFLAGS?="-X github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver.driverVersion=${VERSION} -X github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver.gitCommit=${GIT_COMMIT} -X github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver.buildDate=${BUILD_DATE}" .PHONY: aws-ebs-csi-driver aws-ebs-csi-driver: mkdir -p bin - CGO_ENABLED=0 GOOS=linux go build -ldflags "-X github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver.vendorVersion=${VERSION}" -o bin/aws-ebs-csi-driver ./cmd/ + CGO_ENABLED=0 GOOS=linux go build -ldflags ${LDFLAGS} -o bin/aws-ebs-csi-driver ./cmd/ .PHONY: test test: diff --git a/cmd/main.go b/cmd/main.go index f46ee60b0f..0b25ef410d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,6 +18,8 @@ package main import ( "flag" + "fmt" + "os" "github.com/golang/glog" "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" @@ -25,9 +27,21 @@ import ( ) func main() { - var endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI Endpoint") + var ( + endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI Endpoint") + version = flag.Bool("version", false, "Print the version and exit.") + ) flag.Parse() + if *version { + info, err := driver.GetVersionJSON() + if err != nil { + glog.Fatalln(err) + } + fmt.Println(info) + os.Exit(0) + } + cloud, err := cloud.NewCloud() if err != nil { glog.Fatalln(err) diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index 8eff745fa6..4170e08968 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -33,11 +33,6 @@ const ( topologyKey = "topology." + driverName + "/zone" ) -var ( - // vendorVersion is the version driver and is set during build - vendorVersion string -) - type Driver struct { endpoint string nodeID string diff --git a/pkg/driver/identity.go b/pkg/driver/identity.go index 40fa8b4569..4a43faa340 100644 --- a/pkg/driver/identity.go +++ b/pkg/driver/identity.go @@ -27,7 +27,7 @@ func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoReques glog.V(4).Infof("GetPluginInfo: called with args %+v", *req) resp := &csi.GetPluginInfoResponse{ Name: driverName, - VendorVersion: vendorVersion, + VendorVersion: driverVersion, } return resp, nil diff --git a/pkg/driver/version.go b/pkg/driver/version.go new file mode 100644 index 0000000000..c6a9360e1a --- /dev/null +++ b/pkg/driver/version.go @@ -0,0 +1,43 @@ +package driver + +import ( + "encoding/json" + "fmt" + "runtime" +) + +// These are set during build time via -ldflags +var ( + driverVersion string + gitCommit string + buildDate string +) + +type VersionInfo struct { + DriverVersion string `json:"driverVersion"` + GitCommit string `json:"gitCommit"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` +} + +func GetVersion() VersionInfo { + return VersionInfo{ + DriverVersion: driverVersion, + GitCommit: gitCommit, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + +func GetVersionJSON() (string, error) { + info := GetVersion() + marshalled, err := json.MarshalIndent(&info, "", " ") + if err != nil { + return "", err + } + return string(marshalled), nil +}