From 5d6ef9aa71fc1f84ca8b551c6eda0c6260a115f5 Mon Sep 17 00:00:00 2001 From: chenk008 Date: Thu, 3 Mar 2022 15:49:32 +0800 Subject: [PATCH] [Feature]Add subcommand `--version` (#166) * add subcommand --version * fix * lint Co-authored-by: wuhua.ck --- ray-operator/Makefile | 10 +++++++++- ray-operator/main.go | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ray-operator/Makefile b/ray-operator/Makefile index 50459f0ae9..245ad22c90 100644 --- a/ray-operator/Makefile +++ b/ray-operator/Makefile @@ -1,3 +1,5 @@ +BUILD_TIME := $(shell date "+%F %T") +COMMIT_SHA1 := $(shell git rev-parse HEAD ) # Image URL to use all building/pushing image targets IMG ?= controller:latest @@ -58,7 +60,13 @@ test: manifests generate fmt vet ## Run tests. ##@ Build build: generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + go build \ + -ldflags \ + " \ + -X 'main._buildTime_=${BUILD_TIME}' \ + -X 'main._commitId_=${COMMIT_SHA1}' \ + " \ + -o bin/manager main.go run: manifests generate fmt vet ## Run a controller from your host. go run ./main.go diff --git a/ray-operator/main.go b/ray-operator/main.go index de83a6a9c0..d78d240c74 100644 --- a/ray-operator/main.go +++ b/ray-operator/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "os" "sigs.k8s.io/controller-runtime/pkg/healthz" @@ -19,8 +20,11 @@ import ( ) var ( - scheme = runtime.NewScheme() - setupLog = ctrl.Log.WithName("setup") + _version_ = "0.2" + _buildTime_ = "" + _commitId_ = "" + scheme = runtime.NewScheme() + setupLog = ctrl.Log.WithName("setup") ) func init() { @@ -30,14 +34,16 @@ func init() { } func main() { + var version bool var metricsAddr string var enableLeaderElection bool var probeAddr string var reconcileConcurrency int var watchNamespace string + flag.BoolVar(&version, "version", false, "Show the version information.") flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") - flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") - flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, + flag.StringVar(&probeAddr, "health-probe-bind-address", ":8082", "The address the probe endpoint binds to.") + flag.BoolVar(&enableLeaderElection, "enable-leader-election", true, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") flag.IntVar(&reconcileConcurrency, "reconcile-concurrency", 1, "max concurrency for reconciling") flag.StringVar( @@ -50,6 +56,12 @@ func main() { } opts.BindFlags(flag.CommandLine) flag.Parse() + if version { + fmt.Printf("Version:\t%s\n", _version_) + fmt.Printf("Commit ID:\t%s\n", _commitId_) + fmt.Printf("Build time:\t%s\n", _buildTime_) + os.Exit(0) + } ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))