diff --git a/Makefile b/Makefile index 668740c710..3477a5afd1 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ REL_OSARCH=linux/amd64 REPO_PATH=volcano.sh/volcano IMAGE_PREFIX=volcanosh/vk TAG=latest +RELEASE_VER=v0.1 GitSHA=`git rev-parse HEAD` Date=`date "+%Y-%m-%d %H:%M:%S"` LD_FLAGS=" \ @@ -31,9 +32,9 @@ vkctl: init image_bins: go get github.com/mitchellh/gox - CGO_ENABLED=0 gox -osarch=${REL_OSARCH} -output ${BIN_DIR}/${REL_OSARCH}/vkctl ./cmd/cli + CGO_ENABLED=0 gox -osarch=${REL_OSARCH} -ldflags ${LD_FLAGS} -output ${BIN_DIR}/${REL_OSARCH}/vkctl ./cmd/cli for name in controllers scheduler admission; do\ - CGO_ENABLED=0 gox -osarch=${REL_OSARCH} -output ${BIN_DIR}/${REL_OSARCH}/vk-$$name ./cmd/$$name; \ + CGO_ENABLED=0 gox -osarch=${REL_OSARCH} -ldflags ${LD_FLAGS} -output ${BIN_DIR}/${REL_OSARCH}/vk-$$name ./cmd/$$name; \ done images: image_bins diff --git a/cmd/admission/app/configure/configure.go b/cmd/admission/app/configure/configure.go index 2918f247eb..1875841463 100644 --- a/cmd/admission/app/configure/configure.go +++ b/cmd/admission/app/configure/configure.go @@ -40,6 +40,7 @@ type Config struct { MutateWebhookName string ValidateWebhookConfigName string ValidateWebhookName string + PrintVersion bool } func NewConfig() *Config { @@ -64,6 +65,7 @@ func (c *Config) AddFlags() { "Name of the mutatingwebhookconfiguration resource in Kubernetes.") flag.StringVar(&c.ValidateWebhookName, "validate-webhook-name", "validatejob.volcano.sh", "Name of the webhook entry in the webhook config.") + flag.BoolVar(&c.PrintVersion, "version", false, "Show version and quit") } func (c *Config) CheckPortOrDie() error { diff --git a/cmd/admission/main.go b/cmd/admission/main.go index 458edb960a..d784575358 100644 --- a/cmd/admission/main.go +++ b/cmd/admission/main.go @@ -26,6 +26,7 @@ import ( "volcano.sh/volcano/cmd/admission/app" appConf "volcano.sh/volcano/cmd/admission/app/configure" admissioncontroller "volcano.sh/volcano/pkg/admission" + "volcano.sh/volcano/pkg/version" ) func serveJobs(w http.ResponseWriter, r *http.Request) { @@ -41,6 +42,10 @@ func main() { config.AddFlags() flag.Parse() + if config.PrintVersion { + version.PrintVersionAndExit() + } + http.HandleFunc(admissioncontroller.AdmitJobPath, serveJobs) http.HandleFunc(admissioncontroller.MutateJobPath, serveMutateJobs) diff --git a/cmd/cli/vkctl.go b/cmd/cli/vkctl.go index 52ae5fc5f9..5e5511d3f2 100644 --- a/cmd/cli/vkctl.go +++ b/cmd/cli/vkctl.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/wait" + "volcano.sh/volcano/pkg/version" ) var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes") @@ -41,6 +42,7 @@ func main() { rootCmd.AddCommand(buildJobCmd()) rootCmd.AddCommand(buildQueueCmd()) + rootCmd.AddCommand(versionCommand()) if err := rootCmd.Execute(); err != nil { fmt.Printf("Failed to execute command: %v", err) @@ -59,3 +61,19 @@ func checkError(cmd *cobra.Command, err error) { fmt.Printf("%s: %v\n", msg, err) } } + +var versionExample = `vkctl version` + +func versionCommand() *cobra.Command { + + var command = &cobra.Command{ + Use: "version", + Short: "Print the version information", + Long: "Print the version information", + Example: versionExample, + Run: func(cmd *cobra.Command, args []string) { + version.PrintVersionAndExit() + }, + } + return command +} diff --git a/cmd/controllers/app/options/options.go b/cmd/controllers/app/options/options.go index 61acaa05b9..dfb2017e86 100644 --- a/cmd/controllers/app/options/options.go +++ b/cmd/controllers/app/options/options.go @@ -35,6 +35,7 @@ type ServerOption struct { LockObjectNamespace string KubeAPIBurst int KubeAPIQPS float32 + PrintVersion bool } // NewServerOption creates a new CMServer with a default config. @@ -52,6 +53,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.LockObjectNamespace, "lock-object-namespace", s.LockObjectNamespace, "Define the namespace of the lock object.") fs.Float32Var(&s.KubeAPIQPS, "kube-api-qps", defaultQPS, "QPS to use while talking with kubernetes apiserver") fs.IntVar(&s.KubeAPIBurst, "kube-api-burst", defaultBurst, "Burst to use while talking with kubernetes apiserver") + fs.BoolVar(&s.PrintVersion, "version", false, "Show version and quit") } func (s *ServerOption) CheckOptionOrDie() error { diff --git a/cmd/controllers/main.go b/cmd/controllers/main.go index 54ed1dfbab..64ee534b34 100644 --- a/cmd/controllers/main.go +++ b/cmd/controllers/main.go @@ -28,6 +28,7 @@ import ( "volcano.sh/volcano/cmd/controllers/app" "volcano.sh/volcano/cmd/controllers/app/options" + "volcano.sh/volcano/pkg/version" ) var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes") @@ -37,6 +38,10 @@ func main() { s.AddFlags(pflag.CommandLine) flag.InitFlags() + + if s.PrintVersion { + version.PrintVersionAndExit() + } if err := s.CheckOptionOrDie(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000000..5329535d18 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,53 @@ +/* +Copyright 2019 The Volcano 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 + +import ( + "fmt" + "os" + "runtime" +) + +var ( + // Version shows the version of volcano. + Version = "Not provided." + // GitSHA shoows the git commit id of volcano. + GitSHA = "Not provided." + // Built shows the built time of the binary. + Built = "Not provided." + apiVersion = "v1alpha1" +) + +// PrintVersionAndExit prints versions from the array returned by Info() and exit +func PrintVersionAndExit() { + for _, i := range Info(apiVersion) { + fmt.Printf("%v\n", i) + } + os.Exit(0) +} + +// Info returns an array of various service versions +func Info(apiVersion string) []string { + return []string{ + fmt.Sprintf("API Version: %s", apiVersion), + fmt.Sprintf("Version: %s", Version), + fmt.Sprintf("Git SHA: %s", GitSHA), + fmt.Sprintf("Built At: %s", Built), + fmt.Sprintf("Go Version: %s", runtime.Version()), + fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH), + } +}