diff --git a/hack/build-go.sh b/hack/build-go.sh index eef27c72e22..4f2a1468469 100755 --- a/hack/build-go.sh +++ b/hack/build-go.sh @@ -28,8 +28,8 @@ hackdir=$(CDPATH="" cd $(dirname $0); pwd) # Go to the top of the tree. cd "${KUBE_REPO_ROOT}" -# Update the version. -"${hackdir}/version-gen.sh" +# Fetch the version. +version=$(gitcommit) if [[ $# == 0 ]]; then # Update $@ with the default list of targets to build. @@ -41,4 +41,4 @@ for arg; do binaries+=("${KUBE_GO_PACKAGE}/${arg}") done -go install "${binaries[@]}" +go install -ldflags "-X github.com/GoogleCloudPlatform/kubernetes/pkg/version.commitFromGit '${version}'" "${binaries[@]}" diff --git a/hack/config-go.sh b/hack/config-go.sh index 5d6c60c887b..15f52332433 100644 --- a/hack/config-go.sh +++ b/hack/config-go.sh @@ -17,6 +17,27 @@ # This script sets up a go workspace locally and builds all go components. # You can 'source' this file if you want to set up GOPATH in your local shell. +# gitcommit prints the current Git commit information +function gitcommit() { + set -o errexit + set -o nounset + set -o pipefail + + topdir=$(dirname "$0")/.. + cd "${topdir}" + + # TODO: when we start making tags, switch to git describe? + if git_commit=$(git rev-parse --short "HEAD^{commit}" 2>/dev/null); then + # Check if the tree is dirty. + if ! dirty_tree=$(git status --porcelain) || [[ -n "${dirty_tree}" ]]; then + echo "${git_commit}-dirty" + fi + else + echo "(none)" + fi + return 0 +} + if [[ -z "$(which go)" ]]; then echo "Can't find 'go' in PATH, please fix and retry." >&2 echo "See http://golang.org/doc/install for installation instructions." >&2 diff --git a/hack/version-gen.sh b/hack/version-gen.sh deleted file mode 100755 index 77082a81253..00000000000 --- a/hack/version-gen.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -# 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. - -set -o errexit -set -o nounset -set -o pipefail - -topdir=$(dirname "$0")/.. -cd "${topdir}" - -# TODO: when we start making tags, switch to git describe? -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.tmpl b/pkg/version/template.go.tmpl deleted file mode 100644 index cfcf47dea74..00000000000 --- a/pkg/version/template.go.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -/* -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. -*/ - -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 ( - commitFromGit = `@@GIT_COMMIT@@` -) diff --git a/pkg/version/version.go b/pkg/version/version.go index f8af642dfaf..b44b7a185e1 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -20,6 +20,10 @@ import ( "fmt" ) +// commitFromGit is a constant representing the source version that +// generated this build. It should be set during build via -ldflags. +var commitFromGit string + // Info contains versioning information. // TODO: Add []string of api versions supported? It's still unclear // how we'll want to distribute that information. @@ -41,5 +45,9 @@ func Get() Info { // 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) + commit := info.GitCommit + if commit == "" { + commit = "(unknown)" + } + return fmt.Sprintf("version %s.%s, build %s", info.Major, info.Minor, commit) } diff --git a/release/build-release.sh b/release/build-release.sh index 198b9c6b443..48fe0ddc8e4 100755 --- a/release/build-release.sh +++ b/release/build-release.sh @@ -27,10 +27,6 @@ INSTANCE_PREFIX=$1 KUBE_DIR=$SCRIPT_DIR/.. -# First ensure the version pkg is complete and is up to date -HACK_DIR=$KUBE_DIR/hack -$HACK_DIR/version-gen.sh - # Next build the release tar. This gets copied on to the master and installed # from there. It includes the go source for the necessary servers along with # the salt configs.