Skip to content

Commit 7ca7d13

Browse files
committed
hack/setup-envtest.sh: install binaries and print environment variable command
1 parent f52b618 commit 7ca7d13

File tree

2 files changed

+32
-68
lines changed

2 files changed

+32
-68
lines changed

hack/check-everything.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ set -o pipefail
2020

2121
hack_dir=$(dirname ${BASH_SOURCE})
2222
source ${hack_dir}/common.sh
23-
source ${hack_dir}/setup-envtest.sh
2423

25-
tmp_root=/tmp
26-
kb_root_dir=$tmp_root/kubebuilder
24+
kb_root_dir=/tmp/kubebuilder/bin
2725

28-
ENVTEST_K8S_VERSION=${ENVTEST_K8S_VERSION:-"1.16.4"}
29-
30-
fetch_envtest_tools "$kb_root_dir"
31-
fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
32-
setup_envtest_env "$kb_root_dir"
26+
${hack_dir}/setup-envtest.sh "$kb_root_dir" >/dev/null
27+
${hack_dir}/setup-envtest.sh "${hack_dir}/../pkg/internal/testing/integration/assets/bin" >/dev/null
28+
export KUBEBUILDER_ASSETS="${kb_root_dir}"
3329

3430
${hack_dir}/verify.sh
3531
${hack_dir}/test-all.sh

hack/setup-envtest.sh

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,83 +14,51 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -o errexit
18-
set -o pipefail
19-
20-
# Turn colors in this script off by setting the NO_COLOR variable in your
21-
# environment to any value:
22-
#
23-
# $ NO_COLOR=1 test.sh
24-
NO_COLOR=${NO_COLOR:-""}
25-
if [ -z "$NO_COLOR" ]; then
26-
header=$'\e[1;33m'
27-
reset=$'\e[0m'
28-
else
29-
header=''
30-
reset=''
31-
fi
32-
33-
function header_text {
34-
echo "$header$*$reset"
17+
function get_dest_dir() {
18+
echo "$(realpath -m ${1:-"bin"})"
3519
}
3620

37-
function setup_envtest_env {
38-
header_text "setting up env vars"
39-
40-
# Setup env vars
41-
KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS:-""}
42-
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
43-
export KUBEBUILDER_ASSETS=$1/bin
44-
fi
45-
}
46-
47-
# fetch k8s API gen tools and make it available under envtest_root_dir/bin.
48-
#
49-
# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable
50-
# in your environment to any value:
51-
#
52-
# $ SKIP_FETCH_TOOLS=1 ./check-everything.sh
53-
#
54-
# If you skip fetching tools, this script will use the tools already on your
55-
# machine.
21+
# fetch k8s API gen tools and make it available under $1.
5622
function fetch_envtest_tools {
57-
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
58-
if [ -n "$SKIP_FETCH_TOOLS" ]; then
59-
return 0
60-
fi
61-
62-
tmp_root=/tmp
63-
envtest_root_dir=$tmp_root/envtest
64-
65-
k8s_version="${ENVTEST_K8S_VERSION:-1.16.4}"
66-
goarch="$(go env GOARCH)"
67-
goos="$(go env GOOS)"
23+
local dest_dir="$(get_dest_dir $1)"
24+
local archive_cache_dir=/tmp/envtest
25+
local k8s_version="${ENVTEST_K8S_VERSION:-1.16.4}"
26+
local goarch="$(go env GOARCH)"
27+
local goos="$(go env GOOS)"
6828

6929
if [[ "$goos" != "linux" && "$goos" != "darwin" ]]; then
7030
echo "OS '$goos' not supported. Aborting." >&2
7131
return 1
7232
fi
7333

74-
local dest_dir="${1}"
75-
7634
# use the pre-existing version in the temporary folder if it matches our k8s version
77-
if [[ -x "${dest_dir}/bin/kube-apiserver" ]]; then
78-
version=$("${dest_dir}"/bin/kube-apiserver --version)
35+
if [[ -x "${dest_dir}/kube-apiserver" ]]; then
36+
local version=$("${dest_dir}"/kube-apiserver --version)
7937
if [[ $version == *"${k8s_version}"* ]]; then
80-
header_text "Using cached envtest tools from ${dest_dir}"
38+
echo "# Using cached envtest tools from '${dest_dir}'"
8139
return 0
8240
fi
8341
fi
8442

85-
header_text "fetching envtest tools@${k8s_version} (into '${dest_dir}')"
86-
envtest_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
87-
envtest_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$envtest_tools_archive_name"
43+
echo "# Fetching envtest@${k8s_version} into '${dest_dir}'"
44+
local archive_name="kubebuilder-tools-${k8s_version}-${goos}-${goarch}.tar.gz"
45+
local url="https://storage.googleapis.com/kubebuilder-tools/${archive_name}"
8846

89-
envtest_tools_archive_path="$tmp_root/$envtest_tools_archive_name"
90-
if [ ! -f $envtest_tools_archive_path ]; then
91-
curl -sL ${envtest_tools_download_url} -o "$envtest_tools_archive_path"
47+
local archive_path="$archive_cache_dir/$archive_name"
48+
mkdir -p "$archive_cache_dir"
49+
if [ ! -f $archive_path ]; then
50+
curl -sSL ${url} -o "$archive_path"
9251
fi
9352

9453
mkdir -p "${dest_dir}"
95-
tar -C "${dest_dir}" --strip-components=1 -zvxf "$envtest_tools_archive_path"
54+
tar -C "${dest_dir}" --strip-components=2 -zvxf "$archive_path" >/dev/null
9655
}
56+
57+
set -o errexit
58+
set -o pipefail
59+
60+
fetch_envtest_tools $@
61+
62+
echo "# Run the following command to finish setting up your environment:"
63+
echo
64+
echo "export KUBEBUILDER_ASSETS=$(get_dest_dir $1)"

0 commit comments

Comments
 (0)