Skip to content

🏃 setup-envtest.sh: standalone script for setting up envtest #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 6 additions & 45 deletions hack/check-everything.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@ set -e

hack_dir=$(dirname ${BASH_SOURCE})
source ${hack_dir}/common.sh

k8s_version=1.16.4
goarch=amd64
goos="unknown"

if [[ "$OSTYPE" == "linux-gnu" ]]; then
goos="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
goos="darwin"
fi

if [[ "$goos" == "unknown" ]]; then
echo "OS '$OSTYPE' not supported. Aborting." >&2
exit 1
fi
source ${hack_dir}/setup-envtest.sh

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

# fetch k8s API gen tools and make it available under kb_root_dir/bin.
function fetch_kb_tools {
local dest_dir="${1}"

# use the pre-existing version in the temporary folder if it matches our k8s version
if [[ -x "${dest_dir}/kubebuilder/bin/kube-apiserver" ]]; then
version=$("${dest_dir}"/kubebuilder/bin/kube-apiserver --version)
if [[ $version == *"${k8s_version}"* ]]; then
header_text "Using cached kubebuilder-tools from ${dest_dir}"
return 0
fi
fi

header_text "fetching tools (into '${dest_dir}')"
kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name"

kb_tools_archive_path="$tmp_root/$kb_tools_archive_name"
if [ ! -f $kb_tools_archive_path ]; then
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
fi

mkdir -p "${dest_dir}"
tar -C "${dest_dir}" --strip-components=1 -zvxf "$kb_tools_archive_path"
}

header_text "using tools"

if [ -z "$SKIP_FETCH_TOOLS" ]; then
fetch_kb_tools "$kb_root_dir"
fetch_kb_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
header_text "fetching envtest tools"
fetch_envtest_tools "$kb_root_dir"
fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
fi

setup_envs
header_text "setting up envtest environment"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
header_text "setting up envtest environment"
header_text "setting up envtest@${k8s_version}"

setup_envtest_env "$kb_root_dir"

${hack_dir}/verify.sh
${hack_dir}/test-all.sh
Expand Down
9 changes: 0 additions & 9 deletions hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,3 @@ fi
function header_text {
echo "$header$*$reset"
}

function setup_envs {
header_text "setting up env vars"

# Setup env vars
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$kb_root_dir/bin
fi
}
66 changes: 66 additions & 0 deletions hack/setup-envtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set -e
set -o errexit
set -o nounset
set -o pipefail


function setup_envtest_env {
# Setup env vars
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$1/bin
fi
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$1/bin
fi
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
header_text "setting up kubebuilder-tools@${k8s_version} env vars"
export KUBEBUILDER_ASSETS=$1/bin
fi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is KUBEBUILDER_ASSETS preferable to what we were doing before

export PATH=${tmp_root}/kubebuilder/bin:$PATH
  export TEST_ASSET_KUBECTL=${tmp_root}/kubebuilder/bin/kubectl
  export TEST_ASSET_KUBE_APISERVER=${tmp_root}/kubebuilder/bin/kube-apiserver
  export TEST_ASSET_ETCD=${tmp_root}/kubebuilder/bin/etcd

Copy link
Member Author

@joelanford joelanford Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed calls to header_text because that lives in common.sh, and I didn't want to force users to download two scripts for this.

If the header text is important, I could copy that code into this script. But I figured callers of these functions could call header_text.

For environment variables, I opted to keep the existing environment setup as what's in master, but I'm not opposed to making this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's copy it, it's great to see some styled output when running it

}

# fetch k8s API gen tools and make it available under envtest_root_dir/bin.
function fetch_envtest_tools {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about checking and returning early if SKIP_FETCH_TOOLS is already set?

tmp_root=/tmp
envtest_root_dir=$tmp_root/envtest

k8s_version=1.16.4
goarch=amd64
goos="unknown"
Comment on lines +31 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about passing these in? So they're not hardcoded and they can be set dynamically when we call fetch_envtest_tools?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about:

  • Auto-detecting GOOS and GOARCH by running go env GOOS and go env GOARCH?
  • Defaulting, but allow overriding the k8s version?
    ENVTEST_K8S_VERSION="${ENVTEST_K8S_VERSION:-1.16.4}"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me


if [[ "$OSTYPE" == "linux-gnu" ]]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [[ "$OSTYPE" == "linux-gnu" ]]; then
if [[ "$OSTYPE" == "linux"* ]]; then

goos="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
goos="darwin"
fi

if [[ "$goos" == "unknown" ]]; then
echo "OS '$OSTYPE' not supported. Aborting." >&2
return 1
fi

local dest_dir="${1}"

# use the pre-existing version in the temporary folder if it matches our k8s version
if [[ -x "${dest_dir}/bin/kube-apiserver" ]]; then
version=$("${dest_dir}"/bin/kube-apiserver --version)
if [[ $version == *"${k8s_version}"* ]]; then
return 0
fi
fi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
header_text "fetching envtest-tools@${k8s_version}"

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"

envtest_tools_archive_path="$tmp_root/$envtest_tools_archive_name"
if [ ! -f $envtest_tools_archive_path ]; then
curl -sL ${envtest_tools_download_url} -o "$envtest_tools_archive_path"
fi

mkdir -p "${dest_dir}"
tar -C "${dest_dir}" --strip-components=1 -zvxf "$envtest_tools_archive_path"
}
2 changes: 0 additions & 2 deletions hack/test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ set -e

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

setup_envs

header_text "running go test"

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