Skip to content

Commit 80e0f3e

Browse files
authored
Merge pull request #2149 from estroz/docs/download-tools
✨ (go/v3) Makefile: set SHELL to the system's `bash` binary with shell opts
2 parents e6f3a70 + c23f90a commit 80e0f3e

File tree

9 files changed

+99
-19
lines changed

9 files changed

+99
-19
lines changed

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

docs/book/src/migration/manually_migration_guide_v2_v3.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -400,39 +400,33 @@ CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
400400

401401
To allow downloading the newer versions of the Kubernetes binaries required by Envtest into the `testbin/` directory of your project instead of the global setup, replace:
402402

403-
```
403+
```makefile
404404
# Run tests
405405
test: generate fmt vet manifests
406406
go test ./... -coverprofile cover.out
407407
```
408408

409409
With:
410410

411-
```
411+
```makefile
412+
# Setting SHELL to bash allows bash commands to be executed by recipes.
413+
# This is a requirement for 'setup-envtest.sh' in the test target.
414+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
415+
SHELL = /usr/bin/env bash -o pipefail
416+
.SHELLFLAGS = -ec
417+
412418
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
413419
test: manifests generate fmt vet ## Run tests.
414420
mkdir -p ${ENVTEST_ASSETS_DIR}
415-
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.0/hack/setup-envtest.sh
421+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
416422
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
417423
```
418424

419425
<aside class="note">
420426
<h1>Envtest binaries</h1>
421427

422-
The Kubernetes binaries that are required for the Envtest were upgraded from `1.16.4` to `1.19.2`. You can still install them globally by running:
423-
424-
```
425-
os=$(go env GOOS)
426-
arch=$(go env GOARCH)
427-
428-
# download kubebuilder and extract it to tmp
429-
curl -LO https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-1.19.2-${os}-${arch}.tar.gz | tar -xz -C /tmp/
430-
431-
# move to a long-term location and put it on your path
432-
# (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
433-
sudo mv /tmp/kubebuilder-tools-1.19.2-${os}-${arch}.tar.gz /usr/local/kubebuilder
434-
export PATH=$PATH:/usr/local/kubebuilder/bin
435-
```
428+
The Kubernetes binaries that are required for the Envtest were upgraded from `1.16.4` to `1.19.2`.
429+
You can still install them globally by following [these installation instructions][doc-envtest].
436430

437431
</aside>
438432

@@ -647,4 +641,5 @@ Now, re-create the APIS(CRDs) and Webhooks manifests by running the `kubebuilde
647641
[migration-v2vsv3]: /migration/v2vsv3.md
648642
[custom-resource-definition-versioning]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/
649643
[issue-1999]: https://github.com/kubernetes-sigs/kubebuilder/issues/1999
650-
[project-customizations]: v2vsv3.md#project-customizations
644+
[project-customizations]: v2vsv3.md#project-customizations
645+
[doc-envtest]:/reference/envtest.md

docs/book/src/multiversion-tutorial/testdata/project/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

docs/book/src/reference/envtest.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
# Configuring envtest for integration tests
2-
[`controller-runtime`](http://sigs.k8s.io/controller-runtime) offers `envtest` ([godoc](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest?tab=doc)), a package that helps write integration tests for your controllers by setting up and starting an instance of etcd and the Kubernetes API server, without kubelet, controller-manager or other components.
2+
3+
The [`controller-runtime/pkg/envtest`][envtest] Go library helps write integration tests for your controllers by setting up and starting an instance of etcd and the Kubernetes API server, without kubelet, controller-manager or other components.
4+
5+
## Installation
6+
7+
The `test` make target, also called by the `docker-build` target,
8+
[downloads][setup-envtest] a set of envtest binaries (described above) to run tests with.
9+
Typically nothing needs to be done on your part,
10+
as the download and install script is fully automated,
11+
although it does require `bash` to run.
12+
13+
If you would like to download the tarball containing these binaries,
14+
to use in a disconnected environment for example,
15+
run the following (Kubernetes version 1.19.2 is an example version):
16+
17+
```sh
18+
K8S_VERSION=1.19.2
19+
curl -sSLo envtest-bins.tar.gz "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-${K8S_VERSION}-$(go env GOOS)-$(go env GOARCH).tar.gz"
20+
```
21+
22+
Then install them:
23+
24+
```sh
25+
mkdir /opt/kubebuilder/testbin
26+
tar -C /opt/kubebuilder/testbin --strip-components=1 -zvxf envtest-bins.tar.gz
27+
```
28+
29+
Once these binaries are installed, you can either change the `test` target to:
30+
31+
```makefile
32+
test: manifests generate fmt vet
33+
go test ./... -coverprofile cover.out
34+
```
35+
36+
Or configure the existing target to skip the download and point to a [custom location](#environment-variables):
37+
38+
```sh
39+
make test SKIP_FETCH_TOOLS=1 KUBEBUILDER_ASSETS=/opt/kubebuilder/testbin
40+
```
41+
42+
## Writing tests
343

444
Using `envtest` in integration tests follows the general flow of:
545

@@ -102,3 +142,6 @@ expectedOwnerReference := v1.OwnerReference{
102142
}
103143
Expect(deployment.ObjectMeta.OwnerReferences).To(ContainElement(expectedOwnerReference))
104144
```
145+
146+
[envtest]:https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest
147+
[setup-envtest]:https://github.com/kubernetes-sigs/controller-runtime/blob/master/hack/setup-envtest.sh

pkg/plugins/golang/v3/scaffolds/internal/templates/makefile.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ else
7070
GOBIN=$(shell go env GOBIN)
7171
endif
7272
73+
# Setting SHELL to bash allows bash commands to be executed by recipes.
74+
# This is a requirement for 'setup-envtest.sh' in the test target.
75+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
76+
SHELL = /usr/bin/env bash -o pipefail
77+
.SHELLFLAGS = -ec
78+
7379
all: build
7480
7581
##@ General

testdata/project-v3-addon/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

testdata/project-v3-config/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

testdata/project-v3-multigroup/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

testdata/project-v3/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
1420
all: build
1521

1622
##@ General

0 commit comments

Comments
 (0)