Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit c4896a4

Browse files
authored
Merge pull request #490 from jiaxuanzhou/version
Add version options for kube-batch
2 parents aa9eb21 + ea871c8 commit c4896a4

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
BIN_DIR=_output/bin
22
RELEASE_VER=v0.2
3+
REPO_PATH=github.com/kubernetes-sigs/kube-batch
4+
GitSHA=`git rev-parse HEAD`
5+
Date=`date "+%Y-%m-%d %H:%M:%S"`
36

47
kube-batch: init
5-
go build -o ${BIN_DIR}/kube-batch ./cmd/kube-batch/
8+
go build -ldflags " \
9+
-X '${REPO_PATH}/pkg/version.GitSHA=${GitSHA}' \
10+
-X '${REPO_PATH}/pkg/version.Built=${Date}' \
11+
-X '${REPO_PATH}/pkg/version.Version=${RELEASE_VER}'" \
12+
-o _output/bin/kube-batch ./cmd/kube-batch
613

714
verify: generate-code
815
hack/verify-gofmt.sh

cmd/kube-batch/app/options/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ServerOption struct {
3434
EnableLeaderElection bool
3535
LockObjectNamespace string
3636
DefaultQueue string
37+
PrintVersion bool
3738
}
3839

3940
var (
@@ -66,6 +67,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
6667
"executing the main loop. Enable this when running replicated kube-batch for high availability")
6768
fs.BoolVar(&s.NamespaceAsQueue, "enable-namespace-as-queue", true, "Make Namespace as Queue with weight one, "+
6869
"but kube-batch will not handle Queue CRD anymore")
70+
fs.BoolVar(&s.PrintVersion, "version", false, "Show version and quit")
6971
fs.StringVar(&s.LockObjectNamespace, "lock-object-namespace", s.LockObjectNamespace, "Define the namespace of the lock object")
7072
}
7173

cmd/kube-batch/app/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/golang/glog"
2525
"github.com/kubernetes-sigs/kube-batch/cmd/kube-batch/app/options"
2626
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler"
27+
"github.com/kubernetes-sigs/kube-batch/pkg/version"
28+
2729
"k8s.io/api/core/v1"
2830
"k8s.io/apimachinery/pkg/util/uuid"
2931
clientset "k8s.io/client-go/kubernetes"
@@ -42,6 +44,7 @@ const (
4244
leaseDuration = 15 * time.Second
4345
renewDeadline = 10 * time.Second
4446
retryPeriod = 5 * time.Second
47+
apiVersion = "v1alpha1"
4548
)
4649

4750
func buildConfig(master, kubeconfig string) (*rest.Config, error) {
@@ -52,6 +55,10 @@ func buildConfig(master, kubeconfig string) (*rest.Config, error) {
5255
}
5356

5457
func Run(opt *options.ServerOption) error {
58+
if opt.PrintVersion {
59+
version.PrintVersionAndExit(apiVersion)
60+
}
61+
5562
config, err := buildConfig(opt.Master, opt.Kubeconfig)
5663
if err != nil {
5764
return err

pkg/version/version.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"runtime"
23+
)
24+
25+
var (
26+
// Version shows the version of kube-batch.
27+
Version = "Not provided."
28+
// GitSHA shoows the git commit id of kube-batch.
29+
GitSHA = "Not provided."
30+
// Built shows the built time of the binary.
31+
Built = "Not provided."
32+
)
33+
34+
// PrintVersionAndExit prints versions from the array returned by Info() and exit
35+
func PrintVersionAndExit(apiVersion string) {
36+
for _, i := range Info(apiVersion) {
37+
fmt.Printf("%v\n", i)
38+
}
39+
os.Exit(0)
40+
}
41+
42+
// Info returns an array of various service versions
43+
func Info(apiVersion string) []string {
44+
return []string{
45+
fmt.Sprintf("API Version: %s", apiVersion),
46+
fmt.Sprintf("Version: %s", Version),
47+
fmt.Sprintf("Git SHA: %s", GitSHA),
48+
fmt.Sprintf("Built At: %s", Built),
49+
fmt.Sprintf("Go Version: %s", runtime.Version()),
50+
fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH),
51+
}
52+
}

0 commit comments

Comments
 (0)