From a9bd9117a77a2f84bbc546e28991136fe0000dc0 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Mon, 3 Aug 2020 15:43:32 -0400 Subject: [PATCH] hack/setup-envtest.sh: follow-up from #1092 --- hack/check-everything.sh | 23 +++++------------ hack/setup-envtest.sh | 54 +++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/hack/check-everything.sh b/hack/check-everything.sh index 918966ca1c..6d80c12591 100755 --- a/hack/check-everything.sh +++ b/hack/check-everything.sh @@ -14,7 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -e +set -o errexit +set -o nounset +set -o pipefail hack_dir=$(dirname ${BASH_SOURCE}) source ${hack_dir}/common.sh @@ -23,23 +25,10 @@ source ${hack_dir}/setup-envtest.sh tmp_root=/tmp kb_root_dir=$tmp_root/kubebuilder -# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable -# in your environment to any value: -# -# $ SKIP_FETCH_TOOLS=1 ./check-everything.sh -# -# If you skip fetching tools, this script will use the tools already on your -# machine, but rebuild the kubebuilder and kubebuilder-bin binaries. -SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} - - -if [ -z "$SKIP_FETCH_TOOLS" ]; then - header_text "fetching envtest tools" - fetch_envtest_tools "$kb_root_dir" - fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets" -fi +ENVTEST_K8S_VERSION=${ENVTEST_K8S_VERSION:-"1.16.4"} -header_text "setting up envtest environment" +fetch_envtest_tools "$kb_root_dir" +fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets" setup_envtest_env "$kb_root_dir" ${hack_dir}/verify.sh diff --git a/hack/setup-envtest.sh b/hack/setup-envtest.sh index 77e40dcbec..61f69db058 100755 --- a/hack/setup-envtest.sh +++ b/hack/setup-envtest.sh @@ -14,32 +14,60 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -e +set -o errexit +set -o pipefail + +# Turn colors in this script off by setting the NO_COLOR variable in your +# environment to any value: +# +# $ NO_COLOR=1 test.sh +NO_COLOR=${NO_COLOR:-""} +if [ -z "$NO_COLOR" ]; then + header=$'\e[1;33m' + reset=$'\e[0m' +else + header='' + reset='' +fi + +function header_text { + echo "$header$*$reset" +} function setup_envtest_env { + header_text "setting up env vars" + # Setup env vars + KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS:-""} if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then export KUBEBUILDER_ASSETS=$1/bin fi } # fetch k8s API gen tools and make it available under envtest_root_dir/bin. +# +# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable +# in your environment to any value: +# +# $ SKIP_FETCH_TOOLS=1 ./check-everything.sh +# +# If you skip fetching tools, this script will use the tools already on your +# machine. function fetch_envtest_tools { + SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} + if [ -n "$SKIP_FETCH_TOOLS" ]; then + return 0 + fi + tmp_root=/tmp envtest_root_dir=$tmp_root/envtest - k8s_version=1.16.4 - goarch=amd64 - goos="unknown" - - if [[ "$OSTYPE" == "linux-gnu" ]]; then - goos="linux" - elif [[ "$OSTYPE" == "darwin"* ]]; then - goos="darwin" - fi + k8s_version="${ENVTEST_K8S_VERSION:-1.16.4}" + goarch="$(go env GOARCH)" + goos="$(go env GOOS)" - if [[ "$goos" == "unknown" ]]; then - echo "OS '$OSTYPE' not supported. Aborting." >&2 + if [[ "$goos" != "linux" && "$goos" != "darwin" ]]; then + echo "OS '$goos' not supported. Aborting." >&2 return 1 fi @@ -49,10 +77,12 @@ function fetch_envtest_tools { if [[ -x "${dest_dir}/bin/kube-apiserver" ]]; then version=$("${dest_dir}"/bin/kube-apiserver --version) if [[ $version == *"${k8s_version}"* ]]; then + header_text "Using cached envtest tools from ${dest_dir}" return 0 fi fi + header_text "fetching envtest tools@${k8s_version} (into '${dest_dir}')" envtest_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz" envtest_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$envtest_tools_archive_name"