Skip to content

Commit 2c238de

Browse files
authored
Merge pull request #1488 from DirectXMan12/feature/envtest-setup-tool
✨ EnvTest Binaries Setup Tool
2 parents 5673ada + 782807d commit 2c238de

26 files changed

+3656
-103
lines changed

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ help: ## Display this help
5656
## --------------------------------------
5757

5858
.PHONY: test
59-
test: ## Run the script check-everything.sh which will check all.
59+
test: test-tools ## Run the script check-everything.sh which will check all.
6060
TRACE=1 ./hack/check-everything.sh
6161

62+
.PHONY: test-tools
63+
test-tools: ## tests the tools codebase (setup-envtest)
64+
cd tools/setup-envtest && go test ./...
65+
6266
## --------------------------------------
6367
## Binaries
6468
## --------------------------------------
@@ -76,10 +80,17 @@ $(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
7680
## Linting
7781
## --------------------------------------
7882

79-
.PHONY: lint
80-
lint: $(GOLANGCI_LINT) ## Lint codebase.
83+
.PHONY: lint-libs
84+
lint-libs: $(GOLANGCI_LINT) ## Lint library codebase.
8185
$(GOLANGCI_LINT) run -v
8286

87+
.PHONY: lint-tools
88+
lint-tools: $(GOLANGCI_LINT) ## Lint tools codebase.
89+
cd tools/setup-envtest && $(GOLANGCI_LINT) run -v
90+
91+
.PHONY: lint
92+
lint: lint-libs lint-tools
93+
8394
## --------------------------------------
8495
## Generate
8596
## --------------------------------------

hack/check-everything.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,27 @@ set -o pipefail
2020

2121
hack_dir=$(dirname ${BASH_SOURCE})
2222
source ${hack_dir}/common.sh
23-
source ${hack_dir}/setup-envtest.sh
2423

2524
tmp_root=/tmp
2625
kb_root_dir=$tmp_root/kubebuilder
2726

2827
ENVTEST_K8S_VERSION=${ENVTEST_K8S_VERSION:-"1.19.2"}
2928

30-
fetch_envtest_tools "$kb_root_dir"
31-
fetch_envtest_tools "${hack_dir}/../pkg/internal/testing/integration/assets"
32-
setup_envtest_env "$kb_root_dir"
29+
# set up envtest tools if necessary
30+
31+
header_text "installing envtest tools@${ENVTEST_K8S_VERSION} with setup-envtest if necessary"
32+
tmp_bin=/tmp/cr-tests-bin
33+
(
34+
# don't presume to install for the user
35+
cd ${hack_dir}/../tools/setup-envtest
36+
GOBIN=${tmp_bin} go install .
37+
)
38+
source <(${tmp_bin}/setup-envtest use --use-env -p env ${ENVTEST_K8S_VERSION})
39+
40+
# link the assets into integration
41+
for tool in kube-apiserver etcd kubectl; do
42+
ln -f -s "${KUBEBUILDER_ASSETS:?unable find envtest assets}/${tool}" "${hack_dir}/../pkg/internal/testing/integration/assets/bin/${tool}"
43+
done
3344

3445
${hack_dir}/verify.sh
3546
${hack_dir}/test-all.sh

hack/setup-envtest.sh

Lines changed: 0 additions & 96 deletions
This file was deleted.

tools/setup-envtest/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Envtest Binaries Manager
2+
3+
This is a small tool that manages binaries for envtest. It can be used to
4+
download new binaries, list currently installed and available ones, and
5+
clean up versions.
6+
7+
To use it, just go-install it on 1.16+ (it's a separate, self-contained
8+
module):
9+
10+
```shell
11+
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
12+
```
13+
14+
For full documentation, run it with the `--help` flag, but here are some
15+
examples:
16+
17+
```shell
18+
# download the latest envtest, and print out info about it
19+
setup-envtest use
20+
21+
# download the latest 1.19 envtest, and print out the path
22+
setup-envtest use -p path 1.19.x!
23+
24+
# switch to the most recent 1.21 envtest on disk
25+
source <(setup-envtest use -i -p env 1.21.x)
26+
27+
# list all available local versions for darwin/amd64
28+
setup-envtest list -i --os darwin --arch amd64
29+
30+
# remove all versions older than 1.16 from disk
31+
setup-envtest cleanup <1.16
32+
33+
# use the value from $KUBEBUILDER_ASSETS if set, otherwise follow the normal
34+
# logic for 'use'
35+
setup-envtest --use-env
36+
37+
# use the value from $KUBEBUILDER_ASSETS if set, otherwise use the latest
38+
# installed version
39+
setup-envtest use -i --use-env
40+
41+
# sideload a pre-downloaded tarball as Kubernetes 1.16.2 into our store
42+
setup-envtest sideload 1.16.2 < downloaded-envtest.tar.gz
43+
```
44+
45+
## Where does it put all those binaries?
46+
47+
By default, binaries are stored in a subdirectory of an OS-specific data
48+
directory, as per the OS's conventions.
49+
50+
On Linux, this is `$XDG_DATA_HOME`; on Windows, `%LocalAppData`; and on
51+
OSX, `~/Library/Application Support`.
52+
53+
There's an overall folder that holds all files, and inside that is
54+
a folder for each version/platform pair. The exact directory structure is
55+
not guarnateed, except that the leaf directory will contain the names
56+
expected by envtest. You should always use `setup-envtest fetch` or
57+
`setup-envtest switch` (generally with the `-p path` or `-p env` flags) to
58+
get the directory that you should use.
59+
60+
## Why do I have to do that `source <(blah blah blah)` thing
61+
62+
This is a normal binary, not a shell script, so we can't set the parent
63+
process's environment variables. If you use this by hand a lot and want
64+
to save the typing, you could put something like the following in your
65+
`~/.zshrc` (or similar for bash/fish/whatever, modified to those):
66+
67+
```shell
68+
setup-envtest() {
69+
if (($@[(Ie)use])); then
70+
source <($GOPATH/bin/setup-envtest "$@" -p env)
71+
else
72+
$GOPATH/bin/setup-envtest "$@"
73+
fi
74+
}
75+
```
76+
77+
## What if I don't want to talk to the internet?
78+
79+
There are a few options.
80+
81+
First, you'll probably want to set the `-i/--installed` flag. If you want
82+
to avoid forgetting to set this flag, set the `ENVTEST_INSTALLED_ONLY`
83+
env variable, which will switch that flag on by default.
84+
85+
Then, you have a few options for managing your binaries:
86+
87+
- If you don't *really* want to manage with this tool, or you want to
88+
respect the $KUBEBUILDER_ASSETS variable if it's set to something
89+
outside the store, use the `use --use-env -i` command.
90+
91+
`--use-env` makes the command unconditionally use the value of
92+
KUBEBUILDER_ASSETS as long as it contains the required binaries, and
93+
`-i` indicates that we only ever want to work with installed binaries
94+
(no reaching out the the remote GCS storage).
95+
96+
As noted about, you can use `ENVTEST_INSTALLED_ONLY=true` to switch `-i`
97+
on by default, and you can use `ENVTEST_USE_ENV=true` to switch
98+
`--use-env` on by default.
99+
100+
- If you want to use this tool, but download your gziped tarballs
101+
separately, you can use the `sideload` command. You'll need to use the
102+
`-k/--version` flag to indicate which version you're sideloading.
103+
104+
After that, it'll be as if you'd installed the binaries with `use`.
105+
106+
- If you want to talk to some internal source, you can use the
107+
`--remote-bucket` and `--remote-server` options. The former sets which
108+
GCS bucket to download from, and the latter sets the host to talk to as
109+
if it were a GCS endpoint. Theoretically, you could use the latter
110+
version to run an internal "mirror" -- the tool expects
111+
112+
- `HOST/storage/v1/b/BUCKET/o` to return JSON like
113+
114+
```json
115+
{"items": [
116+
{"name": "kubebuilder-tools-X.Y.Z-os-arch.tar.gz", "md5Hash": "<base-64-encoded-md5-hash>"},
117+
{"name": "kubebuilder-tools-X.Y.Z-os-arch.tar.gz", "md5Hash": "<base-64-encoded-md5-hash>"},
118+
]}
119+
```
120+
121+
- `HOST/storage/v1/b/BUCKET/o/TARBALL_NAME` to return JSON like
122+
`{"name": "kubebuilder-tools-X.Y.Z-os-arch.tar.gz", "md5Hash": "<base-64-encoded-md5-hash>"}`
123+
124+
- `HOST/storage/v1/b/BUCKET/o/TARBALL_NAME?alt=media` to return the
125+
actual file contents

0 commit comments

Comments
 (0)