From 78a4a0e9fdc1449b1569a0b3ede1a8eee0f6c193 Mon Sep 17 00:00:00 2001 From: Tim Ebert Date: Mon, 14 Jun 2021 11:56:37 +0200 Subject: [PATCH] Adapt to new setup-envtest tool 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. --- Makefile | 2 +- go.mod | 1 + hack/setup-envtest.sh | 89 +++++++------------------------------------ hack/test-cover.sh | 10 +---- hack/test.sh | 10 +---- hack/tools.go | 1 + 6 files changed, 18 insertions(+), 95 deletions(-) diff --git a/Makefile b/Makefile index b27fc33d91d..c5f0fc65d1f 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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: diff --git a/go.mod b/go.mod index 633314b8801..1e655295b0f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/hack/setup-envtest.sh b/hack/setup-envtest.sh index 783f930d447..b7d21c01296 100644 --- a/hack/setup-envtest.sh +++ b/hack/setup-envtest.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash - +# # Copyright 2020 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -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" -} diff --git a/hack/test-cover.sh b/hack/test-cover.sh index 66313d98e70..b028a35977f 100755 --- a/hack/test-cover.sh +++ b/hack/test-cover.sh @@ -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 # @@ -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 diff --git a/hack/test.sh b/hack/test.sh index a4ff327f2a4..0f8290b28e5 100755 --- a/hack/test.sh +++ b/hack/test.sh @@ -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 # @@ -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 diff --git a/hack/tools.go b/hack/tools.go index 1af0eac848a..74ad73954ad 100644 --- a/hack/tools.go +++ b/hack/tools.go @@ -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" )