From 36ce60d92722482f444d1dbe9a9f40db959a1f32 Mon Sep 17 00:00:00 2001 From: James DeFelice Date: Tue, 25 Nov 2014 20:11:31 +0000 Subject: [PATCH] more truthfully report the upstream version --- .gitignore | 2 + Makefile | 26 +++++++-- hack/kube-version.sh | 128 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 hack/kube-version.sh diff --git a/.gitignore b/.gitignore index ec247bc2..664f86f6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ _testmain.go *.exe *.test + +.kube-version diff --git a/Makefile b/Makefile index 651a30e7..62ab5ad1 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,11 @@ mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) current_dir := $(patsubst %/,%,$(dir $(mkfile_path))) fail := ${MAKE} --no-print-directory --quiet -f $(current_dir)/Makefile error +KUBE_GO_PACKAGE ?= github.com/GoogleCloudPlatform/kubernetes + K8S_CMD := \ - github.com/GoogleCloudPlatform/kubernetes/cmd/kubecfg \ - github.com/GoogleCloudPlatform/kubernetes/cmd/proxy + ${KUBE_GO_PACKAGE}/cmd/kubecfg \ + ${KUBE_GO_PACKAGE}/cmd/proxy FRAMEWORK_CMD := \ github.com/mesosphere/kubernetes-mesos/controller-manager \ github.com/mesosphere/kubernetes-mesos/kubernetes-mesos \ @@ -20,6 +22,10 @@ FRAMEWORK_LIB := \ github.com/mesosphere/kubernetes-mesos/executor \ github.com/mesosphere/kubernetes-mesos/queue +KUBE_GIT_VERSION_FILE := $(current_dir)/.kube-version + +SHELL := /bin/bash + # a list of upstream projects for which we test the availability of patches PATCH_SCRIPT := $(current_dir)/hack/patches/apply.sh @@ -29,7 +35,7 @@ DESTDIR ?= /target # default build tags TAGS ?= -.PHONY: all error require-godep framework require-vendor proxy install info bootstrap require-gopath format test patch +.PHONY: all error require-godep framework require-vendor proxy install info bootstrap require-gopath format test patch version ifneq ($(WITH_MESOS_DIR),) @@ -51,6 +57,9 @@ WITH_MESOS_CGO_FLAGS := \ endif +export SHELL +export KUBE_GO_PACKAGE + all: patch proxy framework error: @@ -63,8 +72,8 @@ require-godep: require-gopath require-gopath: @test -n "$(GOPATH)" || ${fail} MSG="GOPATH undefined, aborting" -proxy: require-godep - go install $(K8S_CMD) +proxy: require-godep $(KUBE_GIT_VERSION_FILE) + go install -ldflags "$$(cat $(KUBE_GIT_VERSION_FILE))" $(K8S_CMD) require-vendor: @@ -97,5 +106,12 @@ bootstrap: require-godep patch: $(PATCH_SCRIPT) $(PATCH_SCRIPT) +version: $(KUBE_GIT_VERSION_FILE) + +$(KUBE_GIT_VERSION_FILE): require-gopath + @(pkg="$(GOPATH)"; cd "$${pkg%%:*}/src/$(KUBE_GO_PACKAGE)" && \ + source $(current_dir)/hack/kube-version.sh && \ + KUBE_GO_PACKAGE=$(KUBE_GO_PACKAGE) kube::version::ldflags) >$@ + $(PATCH_SCRIPT): test -x $@ || chmod +x $@ diff --git a/hack/kube-version.sh b/hack/kube-version.sh new file mode 100644 index 00000000..20808e55 --- /dev/null +++ b/hack/kube-version.sh @@ -0,0 +1,128 @@ +#!/bin/bash + +# source: https://raw.githubusercontent.com/GoogleCloudPlatform/kubernetes/v0.5.3/hack/lib/version.sh + +# Copyright 2014 Google Inc. All rights reserved. +# +# 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. + +# ----------------------------------------------------------------------------- +# Version management helpers. These functions help to set, save and load the +# following variables: +# +# KUBE_GIT_COMMIT - The git commit id corresponding to this +# source code. +# KUBE_GIT_TREE_STATE - "clean" indicates no changes since the git commit id +# "dirty" indicates source code changes after the git commit id +# KUBE_GIT_VERSION - "vX.Y" used to indicate the last release version. +# KUBE_GIT_MAJOR - The major part of the version +# KUBE_GIT_MINOR - The minor component of the version + +# Grovels through git to set a set of env variables. +# +# If KUBE_GIT_VERSION_FILE, this function will load from that file instead of +# querying git. +kube::version::get_version_vars() { + if [[ -n ${KUBE_GIT_VERSION_FILE-} ]]; then + kube::version::load_version_vars "${KUBE_GIT_VERSION_FILE}" + return + fi + + local git=(git --work-tree "${KUBE_ROOT}") + + if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then + if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then + # Check if the tree is dirty. default to dirty + if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then + KUBE_GIT_TREE_STATE="clean" + else + KUBE_GIT_TREE_STATE="dirty" + fi + fi + + # Use git describe to find the version based on annotated tags. + if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then + if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then + # git describe --dirty only considers changes to existing files, but + # that is problematic since new untracked .go files affect the build, + # so use our idea of "dirty" from git status instead. + KUBE_GIT_VERSION+="-dirty" + fi + + # Try to match the "git describe" output to a regex to try to extract + # the "major" and "minor" versions and whether this is the exact tagged + # version or whether the tree is between two tagged versions. + if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)([.-].*)?$ ]]; then + KUBE_GIT_MAJOR=${BASH_REMATCH[1]} + KUBE_GIT_MINOR=${BASH_REMATCH[2]} + if [[ -n "${BASH_REMATCH[3]}" ]]; then + KUBE_GIT_MINOR+="+" + fi + fi + fi + fi +} + +# Saves the environment flags to $1 +kube::version::save_version_vars() { + local version_file=${1-} + [[ -n ${version_file} ]] || { + echo "!!! Internal error. No file specified in kube::version::save_version_vars" + return 1 + } + + cat <"${version_file}" +KUBE_GIT_COMMIT='${KUBE_GIT_COMMIT-}' +KUBE_GIT_TREE_STATE='${KUBE_GIT_TREE_STATE-}' +KUBE_GIT_VERSION='${KUBE_GIT_VERSION-}' +KUBE_GIT_MAJOR='${KUBE_GIT_MAJOR-}' +KUBE_GIT_MINOR='${KUBE_GIT_MINOR-}' +EOF +} + +# Loads up the version variables from file $1 +kube::version::load_version_vars() { + local version_file=${1-} + [[ -n ${version_file} ]] || { + echo "!!! Internal error. No file specified in kube::version::load_version_vars" + return 1 + } + + source "${version_file}" +} + +# Prints the value that needs to be passed to the -ldflags parameter of go build +# in order to set the Kubernetes based on the git tree status. +kube::version::ldflags() { + kube::version::get_version_vars + + local -a ldflags=() + if [[ -n ${KUBE_GIT_COMMIT-} ]]; then + ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitCommit" "${KUBE_GIT_COMMIT}") + ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitTreeState" "${KUBE_GIT_TREE_STATE}") + fi + + if [[ -n ${KUBE_GIT_VERSION-} ]]; then + ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitVersion" "${KUBE_GIT_VERSION}") + fi + + if [[ -n ${KUBE_GIT_MAJOR-} && -n ${KUBE_GIT_MINOR-} ]]; then + ldflags+=( + -X "${KUBE_GO_PACKAGE}/pkg/version.gitMajor" "${KUBE_GIT_MAJOR}" + -X "${KUBE_GO_PACKAGE}/pkg/version.gitMinor" "${KUBE_GIT_MINOR}" + ) + fi + + # The -ldflags parameter takes a single string, so join the output. + echo "${ldflags[*]-}" +}