
This replaces the gitcommit() shell function with kube::version_ldflags() which prepares a string suitable for Go's -ldflags parameter that fills in the git version fields in pkg/version/base.go. The gitCommit is now a full 40-character SHA1, the gitVersion will be filled from `git describe` output (which will only be available once we have annotated git tags) and gitTreeState will be filled with either "clean" or "dirty" depending on the tree status at the time of the build. Use a kube:: "namespace" (there's really no such a thing in shell, but the illusion still makes it nice) in order to make this nice to import into existing shell scripts or on a shell session. (In the future, I'm planning to introduce more functions and convert some of the top-level commands into other kube::* shell functions.) There's a difference now that -version will report a full SHA1, this will be improved in a follow up change which will improve the Go code for -version handling to give a more meaningful string that should be enough to identify the origin of the binary in git. Tested: - Built it and checked output of -version: $ hack/build-go.sh $ output/go/bin/kubelet -version Kubernetes version 0.1+, build 3ff7ee4b8c843c7767cd856fbf7d3027cd5410e6 - Ran the release script and checked output of the common.sls file: $ release/build-release.sh TESTINSTANCE $ cat output/release/master-release/src/saltbase/pillar/common.sls instance_prefix: TESTINSTANCE-minion go_opt: -ldflags '-X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitCommit 3ff7ee4b8c843c7767cd856fbf7d3027cd5410e6 -X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitTreeState clean' - Successful run of hack/e2e-test.sh end-to-end tests. Signed-off-by: Filipe Brandenburger <filbranden@google.com>
122 lines
4.0 KiB
Bash
122 lines
4.0 KiB
Bash
#!/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.
|
|
|
|
# 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.
|
|
|
|
# --- Helper Functions ---
|
|
|
|
# Function kube::version_ldflags() 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() {
|
|
(
|
|
# Run this in a subshell to prevent settings/variables from leaking.
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
unset CDPATH
|
|
|
|
cd "${KUBE_REPO_ROOT}"
|
|
|
|
declare -a ldflags=()
|
|
if git_commit=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then
|
|
ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitCommit" "${git_commit}")
|
|
|
|
# Check if the tree is dirty.
|
|
if git_status=$(git status --porcelain) && [[ -z "${git_status}" ]]; then
|
|
git_tree_state="clean"
|
|
else
|
|
git_tree_state="dirty"
|
|
fi
|
|
ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitTreeState" "${git_tree_state}")
|
|
|
|
# Use git describe to find the version based on annotated tags.
|
|
if git_version=$(git describe --abbrev=14 "${git_commit}^{commit}" 2>/dev/null); then
|
|
ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitVersion" "${git_version}")
|
|
fi
|
|
fi
|
|
|
|
# The -ldflags parameter takes a single string, so join the output.
|
|
echo "${ldflags[*]}"
|
|
)
|
|
}
|
|
|
|
# kube::setup_go_environment will check that `go` and `godep` commands are
|
|
# available in ${PATH}. If not running on Travis, it will also check that the Go
|
|
# version is good enough for the Kubernetes build.
|
|
#
|
|
# Also set ${GOPATH} and environment variables needed by Go.
|
|
kube::setup_go_environment() {
|
|
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
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -z "$(which godep)" ]]; then
|
|
echo "Can't find 'godep' in PATH, please fix and retry." >&2
|
|
echo "See https://github.com/GoogleCloudPlatform/kubernetes#godep-and-dependency-management" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Travis continuous build uses a head go release that doesn't report
|
|
# a version number, so we skip this check on Travis. Its unnecessary
|
|
# there anyway.
|
|
if [[ "${TRAVIS:-}" != "true" ]]; then
|
|
local go_version
|
|
go_version=($(go version))
|
|
if [[ "${go_version[2]}" < "go1.2" ]]; then
|
|
echo "Detected go version: ${go_version[*]}." >&2
|
|
echo "Kubernetes requires go version 1.2 or greater." >&2
|
|
echo "Please install Go version 1.2 or later" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# TODO: get rid of this after PR #1054 gets rid of godep.
|
|
GOPATH="${KUBE_TARGET}:$(godep path)"
|
|
export GOPATH
|
|
|
|
# Unset GOBIN in case it already exsits in the current session.
|
|
unset GOBIN
|
|
}
|
|
|
|
|
|
KUBE_REPO_ROOT=$(dirname "${BASH_SOURCE:-$0}")/..
|
|
if [[ "${OSTYPE:-}" == *darwin* ]]; then
|
|
# Make the path absolute if it is not.
|
|
if [[ "${KUBE_REPO_ROOT}" != /* ]]; then
|
|
KUBE_REPO_ROOT=${PWD}/${KUBE_REPO_ROOT}
|
|
fi
|
|
else
|
|
# Resolve symlinks.
|
|
KUBE_REPO_ROOT=$(readlink -f "${KUBE_REPO_ROOT}")
|
|
fi
|
|
|
|
KUBE_TARGET="${KUBE_REPO_ROOT}/output/go"
|
|
mkdir -p "${KUBE_TARGET}"
|
|
|
|
KUBE_GO_PACKAGE=github.com/GoogleCloudPlatform/kubernetes
|
|
KUBE_GO_PACKAGE_DIR="${KUBE_TARGET}/src/${KUBE_GO_PACKAGE}"
|
|
|
|
KUBE_GO_PACKAGE_BASEDIR=$(dirname "${KUBE_GO_PACKAGE_DIR}")
|
|
mkdir -p "${KUBE_GO_PACKAGE_BASEDIR}"
|
|
|
|
# Create symlink under output/go/src.
|
|
ln -snf "${KUBE_REPO_ROOT}" "${KUBE_GO_PACKAGE_DIR}"
|