From 023ccd99f50780bb954a5b034d655ce757b7e1ef Mon Sep 17 00:00:00 2001 From: ravisantoshgudimetla Date: Mon, 9 Oct 2017 18:01:55 -0400 Subject: [PATCH] Introducing versioning in descheduler Signed-off-by: ravisantoshgudimetla --- Makefile | 11 ++++- cmd/descheduler/app/version.go | 86 ++++++++++++++++++++++++++++++++++ cmd/descheduler/descheduler.go | 1 + 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 cmd/descheduler/app/version.go diff --git a/Makefile b/Makefile index 9c493d4a8..b00e53c72 100644 --- a/Makefile +++ b/Makefile @@ -15,15 +15,22 @@ .PHONY: test # VERSION is currently based on the last commit -VERSION:=$(shell git rev-parse --short HEAD) +VERSION=`git describe --tags` +COMMIT=`git rev-parse HEAD` +BUILD=`date +%FT%T%z` +LDFLAG_LOCATION=github.com/kubernetes-incubator/descheduler/cmd/descheduler/app + +LDFLAGS=-ldflags "-X ${LDFLAG_LOCATION}.version=${VERSION} -X ${LDFLAG_LOCATION}.buildDate=${BUILD} -X ${LDFLAG_LOCATION}.gitCommit=${COMMIT}" + # IMAGE is the image name of descheduler +# Should this be changed? IMAGE:=descheduler:$(VERSION) all: build build: - go build -o _output/bin/descheduler github.com/kubernetes-incubator/descheduler/cmd/descheduler + go build ${LDFLAGS} -o _output/bin/descheduler github.com/kubernetes-incubator/descheduler/cmd/descheduler image: build docker build -t $(IMAGE) . diff --git a/cmd/descheduler/app/version.go b/cmd/descheduler/app/version.go new file mode 100644 index 000000000..020dfd32f --- /dev/null +++ b/cmd/descheduler/app/version.go @@ -0,0 +1,86 @@ +/* +Copyright 2017 The Kubernetes 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 app + +import ( + "fmt" + "github.com/spf13/cobra" + "runtime" + "strings" +) + +var ( + // gitCommit is a constant representing the source version that + // generated this build. It should be set during build via -ldflags. + gitCommit string + // version is a constant representing the version tag that + // generated this build. It should be set during build via -ldflags. + version string + // buildDate in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') + //It should be set during build via -ldflags. + buildDate string +) + +// Info holds the information related to descheduler app version. +type Info struct { + Major string `json:"major"` + Minor string `json:"minor"` + GitCommit string `json:"gitCommit"` + GitVersion string `json:"gitVersion"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` +} + +// Get returns the overall codebase version. It's for detecting +// what code a binary was built from. +func Get() Info { + majorVersion, minorVersion := splitVersion(version) + return Info{ + Major: majorVersion, + Minor: minorVersion, + GitCommit: gitCommit, + GitVersion: version, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + +func NewVersionCommand() *cobra.Command { + var versionCmd = &cobra.Command{ + Use: "version", + Short: "Version of descheduler", + Long: `Prints the version of descheduler.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Descheduler version %+v\n", Get()) + }, + } + return versionCmd +} + +// splitVersion splits the git version to generate major and minor versions needed. +func splitVersion(version string) (string, string) { + if version == "" { + return "", "" + } + // A sample version would be of form v0.1.0-7-ge884046, so split at first '.' and + // then return 0 and 1+(+ appended to follow semver convention) for major and minor versions. + return strings.Trim(strings.Split(version, ".")[0], "v"), strings.Split(version, ".")[1] + "+" +} diff --git a/cmd/descheduler/descheduler.go b/cmd/descheduler/descheduler.go index 0d6fa8379..1e26646d2 100644 --- a/cmd/descheduler/descheduler.go +++ b/cmd/descheduler/descheduler.go @@ -25,6 +25,7 @@ import ( func main() { out := os.Stdout cmd := app.NewDeschedulerCommand(out) + cmd.AddCommand(app.NewVersionCommand()) if err := cmd.Execute(); err != nil { fmt.Println(err) os.Exit(1)