diff --git a/Makefile b/Makefile index d4fb221c..1f7181a4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,20 @@ +SHELL = /usr/bin/env bash + +GIT_COMMIT = $(shell git rev-parse HEAD) +GIT_SHA = $(shell git rev-parse --short HEAD) +GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null) +GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") + +VERSION_METADATA = unreleased +# Clear the "unreleased" string in BuildMetadata +ifneq ($(GIT_TAG),) + VERSION_METADATA = +endif + +LDFLAGS += -X github.com/kubesphere/kubekey/version.metadata=${VERSION_METADATA} +LDFLAGS += -X github.com/kubesphere/kubekey/version.gitCommit=${GIT_COMMIT} +LDFLAGS += -X github.com/kubesphere/kubekey/version.gitTreeState=${GIT_DIRTY} + .PHONY: build build: build-linux-amd64 build-linux-arm64 @@ -9,7 +26,7 @@ build-linux-amd64: -e CGO_ENABLED=0 \ -e GO111MODULE=on \ -w /usr/src/myapp golang:1.14 \ - go build -v -o output/linux/amd64/kk ./kubekey.go # linux + go build -ldflags '$(LDFLAGS)' -v -o output/linux/amd64/kk ./kubekey.go # linux sha256sum output/linux/amd64/kk || shasum -a 256 output/linux/amd64/kk build-linux-arm64: @@ -20,5 +37,5 @@ build-linux-arm64: -e CGO_ENABLED=0 \ -e GO111MODULE=on \ -w /usr/src/myapp golang:1.14 \ - go build -v -o output/linux/arm64/kk ./kubekey.go # linux + go build -ldflags '$(LDFLAGS)' -v -o output/linux/arm64/kk ./kubekey.go # linux sha256sum output/linux/arm64/kk || shasum -a 256 output/linux/arm64/kk diff --git a/build.sh b/build.sh index 6c45531c..cb9b2b29 100755 --- a/build.sh +++ b/build.sh @@ -1,12 +1,30 @@ #! /bin/bash +GIT_COMMIT=$(git rev-parse HEAD) +GIT_SHA=$(git rev-parse --short HEAD) +GIT_TAG=$(git describe --tags --abbrev=0 --exact-match 2>/dev/null ) +GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "dirty" || echo "clean") + + + +VERSION_METADATA=unreleased +# Clear the "unreleased" string in BuildMetadata +if [[ -n $GIT_TAG ]] +then + VERSION_METADATA= +fi + +LDFLAGS="-X github.com/kubesphere/kubekey/version.metadata=${VERSION_METADATA} + -X github.com/kubesphere/kubekey/version.gitCommit=${GIT_COMMIT} + -X github.com/kubesphere/kubekey/version.gitTreeState=${GIT_DIRTY}" + if [ -n "$1" ]; then if [ "$1" == "-p" ] || [ "$1" == "--proxy" ]; then # Using the most trusted Go module proxy in China - docker run --rm -e GO111MODULE=on -e GOPROXY=https://goproxy.cn -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.14.2-buster go build -v -o output/kk + docker run --rm -e GO111MODULE=on -e GOPROXY=https://goproxy.cn -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.14.7 go build -ldflags "$LDFLAGS" -v -o output/kk else echo "The option should be '-p' or '--proxy'" fi else - docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.14.2-buster go build -v -o output/kk + docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.14.7 go build -ldflags "$LDFLAGS" -v -o output/kk fi diff --git a/cmd/upgradeKs.go b/cmd/upgrade_ks.go similarity index 100% rename from cmd/upgradeKs.go rename to cmd/upgrade_ks.go diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..2b57d590 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,52 @@ +/* +Copyright 2020 The KubeSphere 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 cmd + +import ( + "fmt" + "github.com/kubesphere/kubekey/version" + "github.com/spf13/cobra" +) + +var shortVersion bool + +// versionCmd represents the scale command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "print the client version information", + RunE: func(cmd *cobra.Command, args []string) error { + return printVersion(shortVersion) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + versionCmd.Flags().BoolVarP(&shortVersion, "short", "", false, "print the version number") +} + +func printVersion(short bool) error { + v := version.Get() + if short { + if len(v.GitCommit) >= 7 { + fmt.Printf("%s+g%s\n", v.Version, v.GitCommit[:7]) + return nil + } + fmt.Println(version.GetVersion()) + } + fmt.Printf("%#v\n", v) + return nil +} diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..b55cec88 --- /dev/null +++ b/version/version.go @@ -0,0 +1,69 @@ +/* +Copyright 2020 The KubeSphere 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 ( + "runtime" +) + +var ( + // Update this whenever making a new release. + // The version is of the format Major.Minor.Patch[-Prerelease][+BuildMetadata] + // + // Increment major number for new feature additions and behavioral changes. + // Increment minor number for bug fixes and performance enhancements. + version = "v1.0.0" + + // metadata is extra build time data + metadata = "" + // gitCommit is the git sha1 + gitCommit = "" + // gitTreeState is the state of the git tree + gitTreeState = "" +) + +// BuildInfo describes the compile time information. +type BuildInfo struct { + // Version is the current semver. + Version string `json:"version,omitempty"` + // GitCommit is the git sha1. + GitCommit string `json:"git_commit,omitempty"` + // GitTreeState is the state of the git tree. + GitTreeState string `json:"git_tree_state,omitempty"` + // GoVersion is the version of the Go compiler used. + GoVersion string `json:"go_version,omitempty"` +} + +// GetVersion returns the semver string of the version +func GetVersion() string { + if metadata == "" { + return version + } + return version + "+" + metadata +} + +// Get returns build info +func Get() BuildInfo { + v := BuildInfo{ + Version: GetVersion(), + GitCommit: gitCommit, + GitTreeState: gitTreeState, + GoVersion: runtime.Version(), + } + + return v +}