Skip to content

Commit 8c21111

Browse files
SEJeffmurali-reddy
authored andcommitted
Adding --version / -V support to print version information (#312)
* Alphabetize options.options.KubeRouterConfig * Add --version / -V support to kube-router Fixes #287
1 parent 2147b09 commit 8c21111

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
NAME?=kube-router
22
DEV_SUFFIX?=-git
3+
BUILD_DATE?=$(shell date --iso-8601)
34
LOCAL_PACKAGES?=app app/controllers app/options app/watchers utils
45
IMG_NAMESPACE?=cloudnativelabs
56
GIT_COMMIT=$(shell git describe --tags --dirty)
@@ -18,7 +19,7 @@ all: test kube-router container ## Default target. Runs tests, builds binaries a
1819

1920
kube-router:
2021
@echo Starting kube-router binary build.
21-
CGO_ENABLED=0 go build -o kube-router kube-router.go
22+
CGO_ENABLED=0 go build -ldflags '-X github.com/cloudnativelabs/kube-router/app.version=$(GIT_COMMIT) -X github.com/cloudnativelabs/kube-router/app.buildDate=$(BUILD_DATE)' -o kube-router kube-router.go
2223
@echo Finished kube-router binary build.
2324

2425
test: gofmt ## Runs code quality pipelines (gofmt, tests, coverage, lint, etc)

app/options/options.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,41 @@ import (
88
)
99

1010
type KubeRouterConfig struct {
11-
HelpRequested bool
12-
Kubeconfig string
13-
Master string
14-
ConfigSyncPeriod time.Duration
15-
CleanupConfig bool
16-
IPTablesSyncPeriod time.Duration
17-
IpvsSyncPeriod time.Duration
18-
RoutesSyncPeriod time.Duration
19-
RunServiceProxy bool
20-
RunFirewall bool
21-
RunRouter bool
22-
MasqueradeAll bool
23-
ClusterCIDR string
24-
EnablePodEgress bool
25-
HostnameOverride string
2611
AdvertiseClusterIp bool
2712
AdvertiseExternalIp bool
28-
PeerRouters []net.IP
29-
PeerASNs []uint
30-
PeerMultihopTtl uint8
31-
ClusterAsn uint
32-
FullMeshMode bool
3313
BGPGracefulRestart bool
14+
CleanupConfig bool
15+
ClusterAsn uint
16+
ClusterCIDR string
17+
ConfigSyncPeriod time.Duration
3418
EnableiBGP bool
35-
GlobalHairpinMode bool
36-
NodePortBindOnAllIp bool
3719
EnableOverlay bool
38-
PeerPasswords []string
20+
EnablePodEgress bool
3921
EnablePprof bool
22+
FullMeshMode bool
23+
GlobalHairpinMode bool
24+
HealthPort uint16
25+
HelpRequested bool
26+
HostnameOverride string
27+
IPTablesSyncPeriod time.Duration
28+
IpvsSyncPeriod time.Duration
29+
Kubeconfig string
30+
MasqueradeAll bool
31+
Master string
4032
MetricsEnabled bool
41-
MetricsPort uint16
4233
MetricsPath string
34+
MetricsPort uint16
35+
NodePortBindOnAllIp bool
36+
PeerASNs []uint
37+
PeerMultihopTtl uint8
38+
PeerPasswords []string
39+
PeerRouters []net.IP
40+
RoutesSyncPeriod time.Duration
41+
RunFirewall bool
42+
RunRouter bool
43+
RunServiceProxy bool
44+
Version bool
4345
VLevel string
44-
HealthPort uint16
4546
// FullMeshPassword string
4647
}
4748

@@ -57,6 +58,8 @@ func NewKubeRouterConfig() *KubeRouterConfig {
5758
func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
5859
fs.BoolVarP(&s.HelpRequested, "help", "h", false,
5960
"Print usage information.")
61+
fs.BoolVarP(&s.Version, "version", "V", false,
62+
"Print version information.")
6063
fs.BoolVar(&s.RunServiceProxy, "run-service-proxy", true,
6164
"Enables Service Proxy -- sets up IPVS for Kubernetes Services.")
6265
fs.BoolVar(&s.RunFirewall, "run-firewall", true,
@@ -120,5 +123,4 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
120123
// "Password that cluster-node BGP servers will use to authenticate one another when \"--nodes-full-mesh\" is set.")
121124
fs.StringVarP(&s.VLevel, "v", "v", "0", "log level for V logs")
122125
fs.Uint16Var(&s.HealthPort, "health-port", 20244, "Health check port, 0 = Disabled")
123-
124126
}

app/server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package app
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67
"os/signal"
8+
"runtime"
79
"sync"
810
"syscall"
911

@@ -16,6 +18,10 @@ import (
1618
"k8s.io/client-go/tools/clientcmd"
1719
)
1820

21+
// These get set at build time via -ldflags magic
22+
var version string
23+
var buildDate string
24+
1925
// KubeRouter holds the information needed to run server
2026
type KubeRouter struct {
2127
Client *kubernetes.Clientset
@@ -27,6 +33,7 @@ func NewKubeRouterDefault(config *options.KubeRouterConfig) (*KubeRouter, error)
2733

2834
var clientconfig *rest.Config
2935
var err error
36+
PrintVersion(true)
3037
// Use out of cluster config if the URL or kubeconfig have been specified. Otherwise use incluster config.
3138
if len(config.Master) != 0 || len(config.Kubeconfig) != 0 {
3239
clientconfig, err = clientcmd.BuildConfigFromFlags(config.Master, config.Kubeconfig)
@@ -193,3 +200,13 @@ func (kr *KubeRouter) Run() error {
193200
wg.Wait()
194201
return nil
195202
}
203+
204+
func PrintVersion(logOutput bool) {
205+
output := fmt.Sprintf("Running %v version %s, built on %s, %s\n", os.Args[0], version, buildDate, runtime.Version())
206+
207+
if !logOutput {
208+
fmt.Fprintf(os.Stderr, output)
209+
} else {
210+
glog.Info(output)
211+
}
212+
}

kube-router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func main() {
3131
os.Exit(0)
3232
}
3333

34+
if config.Version {
35+
app.PrintVersion(false)
36+
os.Exit(0)
37+
}
38+
3439
if os.Geteuid() != 0 {
3540
fmt.Fprintf(os.Stderr, "kube-router needs to be run with privileges to execute iptables, ipset and configure ipvs\n")
3641
os.Exit(1)

0 commit comments

Comments
 (0)