Skip to content

Commit 7dbff0d

Browse files
authored
Merge pull request #71 from Random-Liu/add-version-flag
Add --version flag.
2 parents d79bd65 + aedb371 commit 7dbff0d

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

Makefile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
.PHONY: all container push clean node-problem-detector vet fmt
1+
.PHONY: all container push clean node-problem-detector vet fmt version
22

33
all: push
44

5-
# See node-problem-detector.yaml for the version currently running-- bump this ahead before rebuilding!
6-
TAG = v0.2
5+
VERSION := $(shell git describe --tags --dirty)
6+
7+
TAG ?= $(VERSION)
78

89
PROJ = google_containers
910

10-
PKG_SOURCES := $(shell find pkg -name '*.go')
11+
PKG := k8s.io/node-problem-detector
12+
13+
PKG_SOURCES := $(shell find pkg cmd -name '*.go')
1114

1215
vet:
1316
go list ./... | grep -v "./vendor/*" | xargs go vet
1417

1518
fmt:
1619
find . -type f -name "*.go" | grep -v "./vendor/*" | xargs gofmt -s -w -l
1720

18-
node-problem-detector: $(PKG_SOURCES) node_problem_detector.go fmt vet
19-
GOOS=linux go build -ldflags '-w -extldflags "-static"' -o node-problem-detector
21+
version:
22+
@echo $(VERSION)
23+
24+
node-problem-detector: $(PKG_SOURCES) fmt vet
25+
GOOS=linux go build -o node-problem-detector \
26+
-ldflags '-w -extldflags "-static" -X $(PKG)/pkg/version.version=$(VERSION)' \
27+
cmd/node_problem_detector.go
2028

2129
test:
2230
go test -timeout=1m -v -race ./pkg/...

node_problem_detector.go renamed to cmd/node_problem_detector.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ package main
1919
import (
2020
"flag"
2121
"net/url"
22+
"os"
2223

2324
"k8s.io/node-problem-detector/pkg/kernelmonitor"
2425
"k8s.io/node-problem-detector/pkg/problemdetector"
26+
"k8s.io/node-problem-detector/pkg/version"
2527

2628
"github.com/golang/glog"
2729
)
2830

31+
// TODO: Move flags to options directory.
2932
var (
3033
kernelMonitorConfigPath = flag.String("kernel-monitor", "/config/kernel-monitor.json", "The path to the kernel monitor config file")
31-
apiServerOverride = flag.String("apiserver-override", "", "custom URI used to connect to Kubernetes ApiServer")
34+
apiServerOverride = flag.String("apiserver-override", "", "Custom URI used to connect to Kubernetes ApiServer")
35+
printVersion = flag.Bool("version", false, "Print version information and quit")
3236
)
3337

3438
func validateCmdParams() {
@@ -41,6 +45,11 @@ func main() {
4145
flag.Parse()
4246
validateCmdParams()
4347

48+
if *printVersion {
49+
version.PrintVersion()
50+
os.Exit(0)
51+
}
52+
4453
k := kernelmonitor.NewKernelMonitorOrDie(*kernelMonitorConfigPath)
4554
p := problemdetector.NewProblemDetector(k, *apiServerOverride)
4655
p.Run()

pkg/version/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package version
15+
16+
import "fmt"
17+
18+
// version defines the version
19+
var version string = "UNKNOWN"
20+
21+
func PrintVersion() {
22+
fmt.Println(version)
23+
}

0 commit comments

Comments
 (0)