From 7e566091391612d295544a07df7e438bcfc4dc03 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 30 Jul 2014 15:21:34 -0700 Subject: [PATCH 1/2] Handle -version flag on all commands Tested: Passed -version argument to kubelet (and all other binaries): $ output/go/bin/kubecfg -version Kubernetes version 0.1, build 6454a541fd56 Signed-off-by: Filipe Brandenburger --- cmd/apiserver/apiserver.go | 3 +++ cmd/controller-manager/controller-manager.go | 3 +++ cmd/kubecfg/kubecfg.go | 6 +---- cmd/kubelet/kubelet.go | 3 +++ cmd/proxy/proxy.go | 3 +++ hack/test-cmd.sh | 2 +- pkg/version/version.go | 24 ++++++++++++++++++++ 7 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cmd/apiserver/apiserver.go b/cmd/apiserver/apiserver.go index b1b6c4f4d5..c3fef07d79 100644 --- a/cmd/apiserver/apiserver.go +++ b/cmd/apiserver/apiserver.go @@ -29,6 +29,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider" "github.com/GoogleCloudPlatform/kubernetes/pkg/master" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version" "github.com/golang/glog" ) @@ -54,6 +55,8 @@ func main() { util.InitLogs() defer util.FlushLogs() + version.PrintAndExitIfRequested() + if len(machineList) == 0 { glog.Fatal("No machines specified!") } diff --git a/cmd/controller-manager/controller-manager.go b/cmd/controller-manager/controller-manager.go index 1e537f45b9..ceb48b79c0 100644 --- a/cmd/controller-manager/controller-manager.go +++ b/cmd/controller-manager/controller-manager.go @@ -28,6 +28,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/controller" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version" "github.com/coreos/go-etcd/etcd" "github.com/golang/glog" ) @@ -46,6 +47,8 @@ func main() { util.InitLogs() defer util.FlushLogs() + version.PrintAndExitIfRequested() + if len(etcdServerList) == 0 || len(*master) == 0 { glog.Fatal("usage: controller-manager -etcd_servers -master ") } diff --git a/cmd/kubecfg/kubecfg.go b/cmd/kubecfg/kubecfg.go index a9d2609bc3..4ac16f7044 100644 --- a/cmd/kubecfg/kubecfg.go +++ b/cmd/kubecfg/kubecfg.go @@ -37,7 +37,6 @@ import ( ) var ( - versionFlag = flag.Bool("V", false, "Print the version number.") serverVersion = flag.Bool("server_version", false, "Print the server's version number.") preventSkew = flag.Bool("expect_version_match", false, "Fail if server's version doesn't match own version.") httpServer = flag.String("h", "", "The host to connect to.") @@ -107,10 +106,7 @@ func main() { util.InitLogs() defer util.FlushLogs() - if *versionFlag { - fmt.Printf("Version: %#v\n", version.Get()) - os.Exit(0) - } + version.PrintAndExitIfRequested() secure := true var masterServer string diff --git a/cmd/kubelet/kubelet.go b/cmd/kubelet/kubelet.go index 99c19f8e53..839085c270 100644 --- a/cmd/kubelet/kubelet.go +++ b/cmd/kubelet/kubelet.go @@ -35,6 +35,7 @@ import ( kconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version" "github.com/coreos/go-etcd/etcd" "github.com/fsouza/go-dockerclient" "github.com/golang/glog" @@ -95,6 +96,8 @@ func main() { defer util.FlushLogs() rand.Seed(time.Now().UTC().UnixNano()) + version.PrintAndExitIfRequested() + etcd.SetLogger(util.NewLogger("etcd ")) dockerClient, err := docker.NewClient(getDockerEndpoint()) diff --git a/cmd/proxy/proxy.go b/cmd/proxy/proxy.go index 6e6ac24db0..9afb72b03f 100644 --- a/cmd/proxy/proxy.go +++ b/cmd/proxy/proxy.go @@ -22,6 +22,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy" "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version" "github.com/coreos/go-etcd/etcd" "github.com/golang/glog" ) @@ -40,6 +41,8 @@ func main() { util.InitLogs() defer util.FlushLogs() + version.PrintAndExitIfRequested() + // Set up logger for etcd client etcd.SetLogger(util.NewLogger("etcd ")) diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index e3610a6ad2..6ac7154978 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -82,7 +82,7 @@ wait_for_url "http://localhost:4001/version" "etcd: " # Check kubecfg -out=$(${GO_OUT}/kubecfg -V) +out=$(${GO_OUT}/kubecfg -version) echo kubecfg: $out # Start kubelet diff --git a/pkg/version/version.go b/pkg/version/version.go index 48f72bd535..25a2e59be1 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -16,6 +16,16 @@ limitations under the License. package version +import ( + "flag" + "fmt" + "os" +) + +var ( + versionFlag = flag.Bool("version", false, "Print version information and quit") +) + // Info contains versioning information. // TODO: Add []string of api versions supported? It's still unclear // how we'll want to distribute that information. @@ -34,3 +44,17 @@ func Get() Info { GitCommit: commitFromGit, } } + +// String returns info as a human-friendly version string. +func (info Info) String() string { + return fmt.Sprintf("version %s.%s, build %s", info.Major, info.Minor, info.GitCommit) +} + +// PrintAndExitIfRequested will check if the -version flag was passed +// and, if so, print the version and exit. +func PrintAndExitIfRequested() { + if *versionFlag { + fmt.Printf("Kubernetes %s\n", Get()) + os.Exit(0) + } +} From de405ac12635b037aa3d74426d5f6676d2fd53d1 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 30 Jul 2014 16:01:15 -0700 Subject: [PATCH 2/2] Improve generation of version information from the git tree Detect whether the tree is dirty and append a "-dirty" indication to the git commit (common practice with other repos, e.g. kernel, docker.) Properly handle the case where a git tree is not found (e.g. building from archive.) In the sed expression, look for the variable to be updated (commitFromGit) instead of hardcoding a line number. Tested: - Built from a dirty tree: $ output/go/bin/kubelet -version Kubernetes version 0.1, build 2d784c684c75-dirty - Built from a clean tree: $ output/go/bin/kubelet -version Kubernetes version 0.1, build 505f23a31172 - Built from an archive: $ hack/build-go.sh WARNING: unable to find git commit, falling back to commitFromGit = `(none)` $ output/go/bin/kubelet -version Kubernetes version 0.1, build (none) Signed-off-by: Filipe Brandenburger --- hack/version-gen.sh | 30 +++++++++++++++---- pkg/version/{template.go => template.go.tmpl} | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) rename pkg/version/{template.go => template.go.tmpl} (95%) diff --git a/hack/version-gen.sh b/hack/version-gen.sh index 5b14f869cb..77082a8125 100755 --- a/hack/version-gen.sh +++ b/hack/version-gen.sh @@ -14,10 +14,28 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -o errexit +set -o nounset +set -o pipefail + +topdir=$(dirname "$0")/.. +cd "${topdir}" + # TODO: when we start making tags, switch to git describe? -desc=$(git rev-list --abbrev-commit --max-count=1 HEAD) -tab=$'\t' -script="22s/.*/${tab}commitFromGit = \`${desc}\`/" -infile="$(dirname $0)/../pkg/version/template.go" -outfile="$(dirname $0)/../pkg/version/autogenerated.go" -sed "${script}" "${infile}" > "${outfile}" +if git_commit=$(git rev-parse --short "HEAD^{commit}" 2>/dev/null); then + # Remove any invalid characters that might confuse "sed". + git_commit=${git_commit//[^a-f0-9]/} + + # Check if the tree is dirty. + if ! dirty_tree=$(git status --porcelain) || [[ -n "${dirty_tree}" ]]; then + git_commit="${git_commit}-dirty" + fi +else + git_commit="(none)" + echo "WARNING: unable to find git commit, falling back to commitFromGit = \`${git_commit}\`" >&2 +fi + +# TODO: Instead of using an autogenerated file, we could pass this variable +# to the source through Go's -X ldflag. +sed "s/@@GIT_COMMIT@@/${git_commit}/g" \ + pkg/version/template.go.tmpl >pkg/version/autogenerated.go diff --git a/pkg/version/template.go b/pkg/version/template.go.tmpl similarity index 95% rename from pkg/version/template.go rename to pkg/version/template.go.tmpl index f7d0e5c17f..cfcf47dea7 100644 --- a/pkg/version/template.go +++ b/pkg/version/template.go.tmpl @@ -19,5 +19,5 @@ package version // This file is the template for the machine-edited autogenerated.go. // Do not modify this file without also modifying hack/version-gen.sh. var ( - placeholder interface{} + commitFromGit = `@@GIT_COMMIT@@` )