From 09dd08154324e10b00f5cd1520cc4c73582d36a2 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 27 Aug 2014 20:35:27 -0700 Subject: [PATCH 1/3] Document public KUBE_* environment variables and export them Add a section for environment variables in config-go.sh. Document the three public environment variables defined by this script. Make sure the variables get exported so that they can be used by commands spawned by a shell that sourced the config script. Signed-off-by: Filipe Brandenburger --- hack/config-go.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hack/config-go.sh b/hack/config-go.sh index 52d04d1d857..d8c11f3d472 100644 --- a/hack/config-go.sh +++ b/hack/config-go.sh @@ -97,6 +97,12 @@ kube::setup_go_environment() { } +# --- Environment Variables --- + +# KUBE_REPO_ROOT - Path to the top of the build tree. +# KUBE_TARGET - Path where output Go files are saved. +# KUBE_GO_PACKAGE - Full name of the Kubernetes Go package. + KUBE_REPO_ROOT=$(dirname "${BASH_SOURCE:-$0}")/.. if [[ "${OSTYPE:-}" == *darwin* ]]; then # Make the path absolute if it is not. @@ -107,11 +113,15 @@ else # Resolve symlinks. KUBE_REPO_ROOT=$(readlink -f "${KUBE_REPO_ROOT}") fi +export KUBE_REPO_ROOT KUBE_TARGET="${KUBE_REPO_ROOT}/output/go" mkdir -p "${KUBE_TARGET}" +export KUBE_TARGET KUBE_GO_PACKAGE=github.com/GoogleCloudPlatform/kubernetes +export KUBE_GO_PACKAGE + KUBE_GO_PACKAGE_DIR="${KUBE_TARGET}/src/${KUBE_GO_PACKAGE}" KUBE_GO_PACKAGE_BASEDIR=$(dirname "${KUBE_GO_PACKAGE_DIR}") From 391cd856c1e453eafc7165d2004f01eba0022601 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 27 Aug 2014 20:33:58 -0700 Subject: [PATCH 2/3] Use `cd` and `pwd` in a subshell to define ${KUBE_REPO_ROOT} The old method (using `readlink`) was convoluted and not portable. We can achieve the same result using only bash builtins. Signed-off-by: Filipe Brandenburger --- hack/config-go.sh | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/hack/config-go.sh b/hack/config-go.sh index d8c11f3d472..719b5e0b25b 100644 --- a/hack/config-go.sh +++ b/hack/config-go.sh @@ -103,16 +103,15 @@ kube::setup_go_environment() { # KUBE_TARGET - Path where output Go files are saved. # KUBE_GO_PACKAGE - Full name of the Kubernetes Go package. -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 +# Make ${KUBE_REPO_ROOT} an absolute path. +KUBE_REPO_ROOT=$( + set -eu + unset CDPATH + scripts_dir=$(dirname "${BASH_SOURCE[0]}") + cd "${scripts_dir}" + cd .. + pwd +) export KUBE_REPO_ROOT KUBE_TARGET="${KUBE_REPO_ROOT}/output/go" From 0ade8a5cf419d4fb5a0952b3d2977c1c9e004a14 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 27 Aug 2014 20:45:39 -0700 Subject: [PATCH 3/3] Do not leak undocumented variables from config-go.sh Run the snippet that creates the output/go/ tree in a subshell. Tested: - Built it with hack/build-go.sh - Ran release/build-release.sh successfully. - Moved away the git tree to confirm no regression in PR #1073. - Sourced hack/config-go.sh in the shell, confirmed no variable or function other than the expected ones leaked into the environment. - Used `git grep` to confirm the no longer exported variables were not in use by any script other than config-go.sh. Signed-off-by: Filipe Brandenburger --- hack/config-go.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/hack/config-go.sh b/hack/config-go.sh index 719b5e0b25b..aedebed4b38 100644 --- a/hack/config-go.sh +++ b/hack/config-go.sh @@ -121,10 +121,17 @@ export KUBE_TARGET KUBE_GO_PACKAGE=github.com/GoogleCloudPlatform/kubernetes export KUBE_GO_PACKAGE -KUBE_GO_PACKAGE_DIR="${KUBE_TARGET}/src/${KUBE_GO_PACKAGE}" +( + # Create symlink named ${KUBE_GO_PACKAGE} under output/go/src. + # So that Go knows how to import Kubernetes sources by full path. + # Use a subshell to avoid leaking these variables. -KUBE_GO_PACKAGE_BASEDIR=$(dirname "${KUBE_GO_PACKAGE_DIR}") -mkdir -p "${KUBE_GO_PACKAGE_BASEDIR}" + set -eu + go_pkg_dir="${KUBE_TARGET}/src/${KUBE_GO_PACKAGE}" + go_pkg_basedir=$(dirname "${go_pkg_dir}") + mkdir -p "${go_pkg_basedir}" + rm -f "${go_pkg_dir}" + # TODO: This symlink should be relative. + ln -s "${KUBE_REPO_ROOT}" "${go_pkg_dir}" +) -# Create symlink under output/go/src. -ln -snf "${KUBE_REPO_ROOT}" "${KUBE_GO_PACKAGE_DIR}"