Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag for version information output #44

Merged
merged 2 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

PKG=github.com/kubernetes-sigs/aws-efs-csi-driver
IMAGE=amazon/aws-efs-csi-driver
VERSION=0.1.0
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-efs-csi-driver
aws-efs-csi-driver:
mkdir -p bin
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/driver.vendorVersion=${VERSION}" -o bin/aws-efs-csi-driver ./cmd/
CGO_ENABLED=0 GOOS=linux go build -ldflags ${LDFLAGS} -o bin/aws-efs-csi-driver ./cmd/

.PHONY: verify
verify:
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/kubernetes-sigs/aws-efs-csi-driver/pkg/driver"
"k8s.io/klog"
)

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")
)
klog.InitFlags(nil)
flag.Parse()

if *version {
info, err := driver.GetVersionJSON()
if err != nil {
klog.Fatalln(err)
}
fmt.Println(info)
os.Exit(0)
}

drv := driver.NewDriver(*endpoint)
if err := drv.Run(); err != nil {
klog.Fatalln(err)
Expand Down
4 changes: 0 additions & 4 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const (
driverName = "efs.csi.aws.com"
)

var (
vendorVersion = "0.1.0"
)

var (
volumeCaps = []csi.VolumeCapability_AccessMode{
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
resp := &csi.GetPluginInfoResponse{
Name: driverName,
VendorVersion: vendorVersion,
VendorVersion: driverVersion,
}

return resp, nil
Expand Down
53 changes: 53 additions & 0 deletions pkg/driver/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2019 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"
)

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
}
55 changes: 55 additions & 0 deletions pkg/driver/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2019 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 equal\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 equal\ngot:\n%s\nexpected:\n%s", version, expected)
}
}