Skip to content

Commit 8734ac8

Browse files
authored
Merge pull request #1092 from joelanford/setup-envtest
🏃 setup-envtest.sh: standalone script for setting up envtest
2 parents cea989b + fe03072 commit 8734ac8

File tree

4 files changed

+72
-56
lines changed

4 files changed

+72
-56
lines changed

hack/check-everything.sh

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,7 @@ set -e
1818

1919
hack_dir=$(dirname ${BASH_SOURCE})
2020
source ${hack_dir}/common.sh
21-
22-
k8s_version=1.16.4
23-
goarch=amd64
24-
goos="unknown"
25-
26-
if [[ "$OSTYPE" == "linux-gnu" ]]; then
27-
goos="linux"
28-
elif [[ "$OSTYPE" == "darwin"* ]]; then
29-
goos="darwin"
30-
fi
31-
32-
if [[ "$goos" == "unknown" ]]; then
33-
echo "OS '$OSTYPE' not supported. Aborting." >&2
34-
exit 1
35-
fi
21+
source ${hack_dir}/setup-envtest.sh
3622

3723
tmp_root=/tmp
3824
kb_root_dir=$tmp_root/kubebuilder
@@ -46,40 +32,15 @@ kb_root_dir=$tmp_root/kubebuilder
4632
# machine, but rebuild the kubebuilder and kubebuilder-bin binaries.
4733
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
4834

49-
# fetch k8s API gen tools and make it available under kb_root_dir/bin.
50-
function fetch_kb_tools {
51-
local dest_dir="${1}"
52-
53-
# use the pre-existing version in the temporary folder if it matches our k8s version
54-
if [[ -x "${dest_dir}/kubebuilder/bin/kube-apiserver" ]]; then
55-
version=$("${dest_dir}"/kubebuilder/bin/kube-apiserver --version)
56-
if [[ $version == *"${k8s_version}"* ]]; then
57-
header_text "Using cached kubebuilder-tools from ${dest_dir}"
58-
return 0
59-
fi
60-
fi
61-
62-
header_text "fetching tools (into '${dest_dir}')"
63-
kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
64-
kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name"
65-
66-
kb_tools_archive_path="$tmp_root/$kb_tools_archive_name"
67-
if [ ! -f $kb_tools_archive_path ]; then
68-
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
69-
fi
70-
71-
mkdir -p "${dest_dir}"
72-
tar -C "${dest_dir}" --strip-components=1 -zvxf "$kb_tools_archive_path"
73-
}
74-
75-
header_text "using tools"
7635

7736
if [ -z "$SKIP_FETCH_TOOLS" ]; then
78-
fetch_kb_tools "$kb_root_dir"
79-
fetch_kb_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
37+
header_text "fetching envtest tools"
38+
fetch_envtest_tools "$kb_root_dir"
39+
fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
8040
fi
8141

82-
setup_envs
42+
header_text "setting up envtest environment"
43+
setup_envtest_env "$kb_root_dir"
8344

8445
${hack_dir}/verify.sh
8546
${hack_dir}/test-all.sh

hack/common.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,3 @@ fi
5151
function header_text {
5252
echo "$header$*$reset"
5353
}
54-
55-
function setup_envs {
56-
header_text "setting up env vars"
57-
58-
# Setup env vars
59-
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
60-
export KUBEBUILDER_ASSETS=$kb_root_dir/bin
61-
fi
62-
}

hack/setup-envtest.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -e
18+
19+
function setup_envtest_env {
20+
# Setup env vars
21+
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
22+
export KUBEBUILDER_ASSETS=$1/bin
23+
fi
24+
}
25+
26+
# fetch k8s API gen tools and make it available under envtest_root_dir/bin.
27+
function fetch_envtest_tools {
28+
tmp_root=/tmp
29+
envtest_root_dir=$tmp_root/envtest
30+
31+
k8s_version=1.16.4
32+
goarch=amd64
33+
goos="unknown"
34+
35+
if [[ "$OSTYPE" == "linux-gnu" ]]; then
36+
goos="linux"
37+
elif [[ "$OSTYPE" == "darwin"* ]]; then
38+
goos="darwin"
39+
fi
40+
41+
if [[ "$goos" == "unknown" ]]; then
42+
echo "OS '$OSTYPE' not supported. Aborting." >&2
43+
return 1
44+
fi
45+
46+
local dest_dir="${1}"
47+
48+
# use the pre-existing version in the temporary folder if it matches our k8s version
49+
if [[ -x "${dest_dir}/bin/kube-apiserver" ]]; then
50+
version=$("${dest_dir}"/bin/kube-apiserver --version)
51+
if [[ $version == *"${k8s_version}"* ]]; then
52+
return 0
53+
fi
54+
fi
55+
56+
envtest_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
57+
envtest_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$envtest_tools_archive_name"
58+
59+
envtest_tools_archive_path="$tmp_root/$envtest_tools_archive_name"
60+
if [ ! -f $envtest_tools_archive_path ]; then
61+
curl -sL ${envtest_tools_download_url} -o "$envtest_tools_archive_path"
62+
fi
63+
64+
mkdir -p "${dest_dir}"
65+
tar -C "${dest_dir}" --strip-components=1 -zvxf "$envtest_tools_archive_path"
66+
}

hack/test-all.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ set -e
1818

1919
source $(dirname ${BASH_SOURCE})/common.sh
2020

21-
setup_envs
22-
2321
header_text "running go test"
2422

2523
go test -race ${MOD_OPT} ./...

0 commit comments

Comments
 (0)