diff --git a/Makefile b/Makefile index 26464727c8..1f1adafa2e 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +PKG=github.com/kubernetes-sigs/aws-ebs-csi-driver 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 ${PKG}/pkg/driver.driverVersion=${VERSION} -X ${PKG}/pkg/driver.gitCommit=${GIT_COMMIT} -X ${PKG}/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..6b385cb99a --- /dev/null +++ b/pkg/driver/version.go @@ -0,0 +1,59 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +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 +} diff --git a/pkg/driver/version_test.go b/pkg/driver/version_test.go new file mode 100644 index 0000000000..82384997b3 --- /dev/null +++ b/pkg/driver/version_test.go @@ -0,0 +1,61 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package driver + +import ( + "fmt" + "reflect" + "runtime" + "testing" +) + +func TestGetVersion(t *testing.T) { + version := GetVersion() + + expected := VersionInfo{ + DriverVersion: "", + GitCommit: "", + BuildDate: "", + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } + + if !reflect.DeepEqual(version, expected) { + t.Fatalf("structs not equall\ngot:\n%+v\nexpected:\n%+v", version, expected) + } +} + +func TestGetVersionJSON(t *testing.T) { + version, err := GetVersionJSON() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := fmt.Sprintf(`{ + "driverVersion": "", + "gitCommit": "", + "buildDate": "", + "goVersion": "%s", + "compiler": "%s", + "platform": "%s" +}`, runtime.Version(), runtime.Compiler, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)) + + if version != expected { + t.Fatalf("json not equall\ngot:\n%s\nexpected:\n%s", version, expected) + } +}