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

provide more driver info in logs #149

Merged
merged 1 commit into from
Feb 5, 2021
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
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/nfs/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
67 changes: 67 additions & 0 deletions pkg/nfs/version.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions pkg/nfs/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down