|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
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"})" |
35 | 19 | }
|
36 | 20 |
|
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. |
56 | 22 | 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)" |
68 | 28 |
|
69 | 29 | if [[ "$goos" != "linux" && "$goos" != "darwin" ]]; then
|
70 | 30 | echo "OS '$goos' not supported. Aborting." >&2
|
71 | 31 | return 1
|
72 | 32 | fi
|
73 | 33 |
|
74 |
| - local dest_dir="${1}" |
75 |
| - |
76 | 34 | # 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) |
79 | 37 | if [[ $version == *"${k8s_version}"* ]]; then
|
80 |
| - header_text "Using cached envtest tools from ${dest_dir}" |
| 38 | + echo "# Using cached envtest tools from '${dest_dir}'" |
81 | 39 | return 0
|
82 | 40 | fi
|
83 | 41 | fi
|
84 | 42 |
|
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}" |
88 | 46 |
|
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" |
92 | 51 | fi
|
93 | 52 |
|
94 | 53 | 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 |
96 | 55 | }
|
| 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