Skip to content

Commit

Permalink
Add version subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Dec 5, 2018
1 parent 9a17b67 commit 182211d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ package main

import (
"flag"
"fmt"
"os"

"github.com/golang/glog"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
)

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)
Expand Down
5 changes: 0 additions & 5 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions pkg/driver/version.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions pkg/driver/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 182211d

Please sign in to comment.