diff --git a/Makefile b/Makefile index 0925c8b96..7c171a1c4 100644 --- a/Makefile +++ b/Makefile @@ -24,9 +24,12 @@ DOCKER_CLI_EXPERIMENTAL = enabled export GOPATH GOBIN GO111MODULE DOCKER_CLI_EXPERIMENTAL include release-tools/build.make -LDFLAGS = "-X ${PKG}/pkg/nfs.driverVersion=${IMAGE_VERSION} -s -w -extldflags '-static'" -GIT_COMMIT ?= $(shell git rev-parse HEAD) -IMAGE_VERSION ?= v3.0.0 + +GIT_COMMIT = $(shell git rev-parse HEAD) +BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +IMAGE_VERSION = v3.0.0 +LDFLAGS = -X ${PKG}/pkg/nfs.driverVersion=${IMAGE_VERSION} -X ${PKG}/pkg/nfs.gitCommit=${GIT_COMMIT} -X ${PKG}/pkg/nfs.buildDate=${BUILD_DATE} +EXT_LDFLAGS = -s -w -extldflags "-static" # Use a custom version for E2E tests if we are testing in CI ifdef CI ifndef PUBLISH @@ -82,7 +85,7 @@ local-k8s-uninstall: .PHONY: nfs nfs: - CGO_ENABLED=0 GOOS=linux go build -a -ldflags ${LDFLAGS} -mod vendor -o bin/nfsplugin ./cmd/nfsplugin + CGO_ENABLED=0 GOOS=linux go build -a -ldflags "${LDFLAGS} ${EXT_LDFLAGS}" -mod vendor -o bin/nfsplugin ./cmd/nfsplugin .PHONY: container container: nfs diff --git a/go.mod b/go.mod index 253ffa7e5..a0b1fec88 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( k8s.io/klog/v2 v2.4.0 k8s.io/kubernetes v1.21.0-alpha.0.0.20201210005053-f58c4d8cd725 k8s.io/utils v0.0.0-20201110183641-67b214c5f920 + sigs.k8s.io/yaml v1.2.0 ) replace k8s.io/api => k8s.io/api v0.20.0 diff --git a/pkg/nfs/nfs.go b/pkg/nfs/nfs.go index 32ba22681..f6558c363 100644 --- a/pkg/nfs/nfs.go +++ b/pkg/nfs/nfs.go @@ -98,6 +98,12 @@ func NewNodeServer(n *Driver, mounter mount.Interface) *NodeServer { } func (n *Driver) Run(testMode bool) { + versionMeta, err := GetVersionYAML() + if err != nil { + klog.Fatalf("%v", err) + } + klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta) + n.ns = NewNodeServer(n, mount.New("")) s := NewNonBlockingGRPCServer() s.Start(n.endpoint, diff --git a/pkg/nfs/version.go b/pkg/nfs/version.go new file mode 100644 index 000000000..1c8a313b4 --- /dev/null +++ b/pkg/nfs/version.go @@ -0,0 +1,67 @@ +/* +Copyright 2021 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 nfs + +import ( + "fmt" + "runtime" + "strings" + + "sigs.k8s.io/yaml" +) + +// These are set during build time via -ldflags +var ( + driverVersion = "N/A" + gitCommit = "N/A" + buildDate = "N/A" +) + +// VersionInfo holds the version information of the driver +type VersionInfo struct { + DriverName string `json:"Driver Name"` + DriverVersion string `json:"Driver Version"` + GitCommit string `json:"Git Commit"` + BuildDate string `json:"Build Date"` + GoVersion string `json:"Go Version"` + Compiler string `json:"Compiler"` + Platform string `json:"Platform"` +} + +// GetVersion returns the version information of the driver +func GetVersion() VersionInfo { + return VersionInfo{ + DriverName: DriverName, + DriverVersion: driverVersion, + GitCommit: gitCommit, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + +// GetVersionYAML returns the version information of the driver +// in YAML format +func GetVersionYAML() (string, error) { + info := GetVersion() + marshalled, err := yaml.Marshal(&info) + if err != nil { + return "", err + } + return strings.TrimSpace(string(marshalled)), nil +} diff --git a/pkg/nfs/version_test.go b/pkg/nfs/version_test.go new file mode 100644 index 000000000..a1215fdde --- /dev/null +++ b/pkg/nfs/version_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2021 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 nfs + +import ( + "fmt" + "reflect" + "runtime" + "strings" + "testing" + + "sigs.k8s.io/yaml" +) + +func TestGetVersion(t *testing.T) { + version := GetVersion() + + expected := VersionInfo{ + DriverName: DriverName, + DriverVersion: "N/A", + GitCommit: "N/A", + BuildDate: "N/A", + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } + + if !reflect.DeepEqual(version, expected) { + t.Errorf("Unexpected error. \n Expected: %v \n Found: %v", expected, version) + } + +} + +func TestGetVersionYAML(t *testing.T) { + resp, err := GetVersionYAML() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + versionInfo := GetVersion() + marshalled, _ := yaml.Marshal(&versionInfo) + expected := strings.TrimSpace(string(marshalled)) + + if resp != expected { + t.Fatalf("Unexpected error. \n Expected:%v\nFound:%v", expected, resp) + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index bcde69a08..2be794dc8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -742,6 +742,7 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client # sigs.k8s.io/structured-merge-diff/v4 v4.0.2 sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.2.0 +## explicit sigs.k8s.io/yaml # k8s.io/api => k8s.io/api v0.20.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.20.0