Skip to content

Commit

Permalink
Adapt to new setup-envtest tool
Browse files Browse the repository at this point in the history
Makes use of the new setup-envtest tool (kubernetes-sigs/controller-runtime#1488)
in hack/setup-envtest.sh instead of vendoring hack/setup-envtest.sh and fetching
binaries with that.
  • Loading branch information
timebertt committed Jun 16, 2021
1 parent eb8f950 commit 78a4a0e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ install-requirements:
@go install -mod=vendor github.com/onsi/ginkgo/ginkgo
@go install -mod=vendor github.com/ahmetb/gen-crd-api-reference-docs
@go install -mod=vendor github.com/golang/mock/mockgen
@go install -mod=vendor sigs.k8s.io/controller-runtime/tools/setup-envtest
@go install -mod=vendor sigs.k8s.io/controller-tools/cmd/controller-gen
@./hack/install-promtool.sh
@./hack/install-requirements.sh
Expand All @@ -164,7 +165,6 @@ install-requirements:
revendor:
@GO111MODULE=on go mod vendor
@GO111MODULE=on go mod tidy
@curl -sSLo hack/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/$(CR_VERSION)/hack/setup-envtest.sh

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
k8s.io/metrics v0.21.1
k8s.io/utils v0.0.0-20210527160623-6fdb442a123b
sigs.k8s.io/controller-runtime v0.9.0
sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20210609022947-fbf50b04fe17
sigs.k8s.io/controller-tools v0.4.1
sigs.k8s.io/yaml v1.2.0
)
Expand Down
89 changes: 13 additions & 76 deletions hack/setup-envtest.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash

#
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,80 +17,17 @@
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'
ENVTEST_K8S_VERSION=${ENVTEST_K8S_VERSION:-"1.20"}

echo "> Installing envtest tools@${ENVTEST_K8S_VERSION} with setup-envtest if necessary"
if ! command -v setup-envtest &> /dev/null ; then
# Some repos that vendor g/g and reuse this hack script might not need the envtest tools.
# Thus, just skip installing anything if setup-envtest is not installed.
# If envtest tools are needed, users will notice when their tests fail.
echo "setup-envtest not available, skip installing envtest tools"
else
header=''
reset=''
# --use-env allows overwriting the envtest tools path via the KUBEBUILDER_ASSETS env var just like it was before
setup-envtest use --use-env -p env ${ENVTEST_K8S_VERSION}
source <(setup-envtest use --use-env -p env ${ENVTEST_K8S_VERSION})
echo "using envtest tools installed at '$KUBEBUILDER_ASSETS'"
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="${ENVTEST_K8S_VERSION:-1.19.2}"
goarch="$(go env GOARCH)"
goos="$(go env GOOS)"

if [[ "$goos" != "linux" && "$goos" != "darwin" ]]; then
echo "OS '$goos' 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
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"

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"
}
10 changes: 1 addition & 9 deletions hack/test-cover.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
Expand All @@ -15,16 +15,8 @@
# limitations under the License.
set -e

TEST_BIN_DIR="$(dirname "${0}")/../dev/testbin"
mkdir -p ${TEST_BIN_DIR}

ENVTEST_ASSETS_DIR="$(realpath ${TEST_BIN_DIR})"

source "$(dirname $0)/setup-envtest.sh"

fetch_envtest_tools ${ENVTEST_ASSETS_DIR}
setup_envtest_env ${ENVTEST_ASSETS_DIR}

echo "> Test Cover"

export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT=1m
Expand Down
10 changes: 1 addition & 9 deletions hack/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
Expand All @@ -15,16 +15,8 @@
# limitations under the License.
set -e

TEST_BIN_DIR="$(dirname "${0}")/../dev/testbin"
mkdir -p ${TEST_BIN_DIR}

ENVTEST_ASSETS_DIR="$(realpath ${TEST_BIN_DIR})"

source "$(dirname $0)/setup-envtest.sh"

fetch_envtest_tools ${ENVTEST_ASSETS_DIR}
setup_envtest_env ${ENVTEST_ASSETS_DIR}

echo "> Test"

export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT=1m
Expand Down
1 change: 1 addition & 0 deletions hack/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ import (
_ "k8s.io/code-generator"
_ "k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo"
_ "k8s.io/kube-openapi/cmd/openapi-gen"
_ "sigs.k8s.io/controller-runtime/tools/setup-envtest"
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
)

0 comments on commit 78a4a0e

Please sign in to comment.