From c1377589b3dd6ee3c356e13ff28067900605d3e5 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 10 Jan 2019 18:02:53 +0200 Subject: [PATCH] Move version information into a separate module --- Dockerfile | 2 +- main.go | 15 ++++++--------- main_test.go | 3 ++- pkg/version/version.go | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 pkg/version/version.go diff --git a/Dockerfile b/Dockerfile index 1330fb58d..c7e82d146 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ ARG NFD_VERSION RUN go get github.com/golang/dep/cmd/dep RUN dep ensure RUN go install \ - -ldflags "-s -w -X main.version=$NFD_VERSION" \ + -ldflags "-s -w -X sigs.k8s.io/node-feature-discovery/pkg/version.version=$NFD_VERSION" \ sigs.k8s.io/node-feature-discovery RUN install -D -m644 node-feature-discovery.conf.example /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf diff --git a/main.go b/main.go index 6fb371c87..a236c6283 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation" k8sclient "k8s.io/client-go/kubernetes" restclient "k8s.io/client-go/rest" + "sigs.k8s.io/node-feature-discovery/pkg/version" "sigs.k8s.io/node-feature-discovery/source" "sigs.k8s.io/node-feature-discovery/source/cpu" "sigs.k8s.io/node-feature-discovery/source/cpuid" @@ -48,10 +49,6 @@ const ( NodeNameEnv = "NODE_NAME" ) -var ( - version = "" // Must not be const, set using ldflags at build time -) - // package loggers var ( stdoutLogger = log.New(os.Stdout, "", log.LstdFlags) @@ -115,10 +112,10 @@ type Args struct { func main() { // Assert that the version is known - if version == "" { - stderrLogger.Fatalf("main.version not set! Set -ldflags \"-X main.version `git describe --tags --dirty --always`\" during build or run.") + if version.Get() == "undefined" { + stderrLogger.Fatalf("version not set! Set -ldflags \"-X sigs.k8s.io/node-feature-discovery/pkg/version.version=`git describe --tags --dirty --always`\" during build or run.") } - stdoutLogger.Printf("Node Feature Discovery %s", version) + stdoutLogger.Printf("Node Feature Discovery %s", version.Get()) // Parse command-line arguments. args := argsParse(nil) @@ -199,7 +196,7 @@ func argsParse(argv []string) (args Args) { ) arguments, _ := docopt.Parse(usage, argv, true, - fmt.Sprintf("%s %s", ProgramName, version), false) + fmt.Sprintf("%s %s", ProgramName, version.Get()), false) // Parse argument values as usable types. var err error @@ -332,7 +329,7 @@ func updateNodeWithFeatureLabels(helper APIHelpers, noPublish bool, labels Label keys = append(keys, k) } sort.Strings(keys) - annotations := Annotations{"version": version, + annotations := Annotations{"version": version.Get(), "feature-labels": strings.Join(keys, ",")} err := advertiseFeatureLabels(helper, labels, annotations) diff --git a/main_test.go b/main_test.go index de7bd25c9..cf4a11901 100644 --- a/main_test.go +++ b/main_test.go @@ -17,6 +17,7 @@ import ( "sigs.k8s.io/node-feature-discovery/source" "sigs.k8s.io/node-feature-discovery/source/fake" "sigs.k8s.io/node-feature-discovery/source/panic_fake" + "sigs.k8s.io/node-feature-discovery/pkg/version" ) func TestDiscoveryWithMockSources(t *testing.T) { @@ -26,7 +27,7 @@ func TestDiscoveryWithMockSources(t *testing.T) { fakeFeatureNames := []string{"testfeature1", "testfeature2", "testfeature3"} fakeFeatures := source.Features{} fakeFeatureLabels := Labels{} - fakeAnnotations := Annotations{"version": version, + fakeAnnotations := Annotations{"version": version.Get(), "feature-labels": "testSource-testfeature1,testSource-testfeature2,testSource-testfeature3"} fakeFeatureLabelNames := make([]string, 0, len(fakeFeatureNames)) for _, f := range fakeFeatureNames { diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..0520c4cc4 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,24 @@ +/* +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 version + +// Must not be const, supposed to be set using ldflags at build time +var version = "undefined" + +func Get() string { + return version +}