Skip to content

Commit d360c5a

Browse files
committed
align on controller-gen 0.4.0
We need to ensure that all ACK developers are using the same controller-gen version otherwise we get into situations where: 1) go.mod/go.sum change unnecessarily 2) The annotations for things like CRDs will be different for generated YAML manifests This patch makes v0.4.0 of the controller-tools repo and controller-gen binary our target version. It changes the `ensure_controller_gen` Bash function to ensure that controller-gen at that version is installed and refuses to proceed with building controllers if there is a different version. ``` jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ controller-gen --version Version: v0.3.1-0.20200716001835-4a903ddb7005 jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ make build-controller SERVICE=dynamodb make[1]: Entering directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' go build -tags codegen -ldflags "-X main.version="v0.0.0" -X main.buildHash=598a3e29bb514d98660d04a088641922ccc020c9 -X main.buildDate=2020-10-05T16:52:56Z" -o bin/ack-generate cmd/ack-generate/main.go ./scripts/build-controller.sh dynamodb FAIL: Existing version of controller-gen Version: v0.3.1-0.20200716001835-4a903ddb7005, required version is v0.4.0. FAIL: Please uninstall controller-gen and re-run this script, which will install the required version. make[1]: *** [Makefile:49: build-controller] Error 1 make[1]: Leaving directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ sudo rm -f `which controller-gen` jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ make build-controller SERVICE=dynamodb make[1]: Entering directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' go build -tags codegen -ldflags "-X main.version="v0.0.0" -X main.buildHash=598a3e29bb514d98660d04a088641922ccc020c9 -X main.buildDate=2020-10-05T16:53:07Z" -o bin/ack-generate cmd/ack-generate/main.go ./scripts/build-controller.sh dynamodb go: creating new go.mod: module tmp go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.4.0 **************************************************************************** WARNING: You may need to reload your Bash shell and path. If you see an error like this following: Error: couldn't find github.com/aws/aws-sdk-go in the go.mod require block simply reload your Bash shell with `exec bash` and then re-run whichever command you were running. **************************************************************************** Building Kubernetes API objects for dynamodb Error: couldn't find github.com/aws/aws-sdk-go in the go.mod require block make[1]: *** [Makefile:49: build-controller] Error 2 make[1]: Leaving directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ exec bash jaypipes@thelio:~/go/src/github.com/aws/aws-controllers-k8s$ make build-controller SERVICE=dynamodb make[1]: Entering directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' go build -tags codegen -ldflags "-X main.version="v0.0.0" -X main.buildHash=598a3e29bb514d98660d04a088641922ccc020c9 -X main.buildDate=2020-10-05T16:53:21Z" -o bin/ack-generate cmd/ack-generate/main.go ./scripts/build-controller.sh dynamodb Building Kubernetes API objects for dynamodb Generating deepcopy code for dynamodb Generating custom resource definitions for dynamodb Building service controller for dynamodb Generating RBAC manifests for dynamodb Running gofmt against generated code for dynamodb make[1]: Leaving directory '/home/jaypipes/go/src/github.com/aws/aws-controllers-k8s' ``` Issue aws-controllers-k8s#349 Related: kubernetes-sigs/controller-tools#500
1 parent 598a3e2 commit d360c5a

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

scripts/lib/k8s.sh

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
11
#!/usr/bin/env bash
22

3+
CONTROLLER_TOOLS_VERSION="v0.4.0"
4+
5+
# ensure_controller_gen checks that the `controller-gen` binary is available on
6+
# the host system and if it is, that it matches the exact version that we
7+
# require in order to standardize the YAML manifests for CRDs and Kubernetes
8+
# Roles.
9+
#
10+
# If the locally-installed controller-gen does not match the required version,
11+
# prints an error message asking the user to uninstall it.
312
ensure_controller_gen() {
4-
if ! is_installed controller-gen; then
5-
# Need this version of controller-gen to allow dangerous types and float
6-
# type support
7-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@4a903ddb7005459a7baf4777c67244a74c91083d
8-
else
9-
minimum_req_version="v0.3.1"
10-
# Don't overide the existing version let the user decide.
11-
if ! is_min_controller_gen_version "$minimum_req_version"; then
12-
echo "Existing version of controller-gen "`controller-gen --version`", minimum required is $minimum_req_version"
13-
exit 1
13+
if ! is_installed controller-gen; then
14+
__install_path=/usr/local/bin/controller-gen
15+
__work_dir=$(mktemp --tmpdir -d controller-gen-XXX)
16+
17+
cd "$__work_dir"
18+
19+
go mod init tmp
20+
go get -d "sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_TOOLS_VERSION}"
21+
go build -o "$__work_dir/controller-gen" sigs.k8s.io/controller-tools/cmd/controller-gen
22+
sudo mv "$__work_dir/controller-gen" "$__install_path"
23+
24+
rm -rf "$WORK_DIR"
25+
echo "****************************************************************************"
26+
echo "WARNING: You may need to reload your Bash shell and path. If you see an"
27+
echo " error like this following:"
28+
echo ""
29+
echo "Error: couldn't find github.com/aws/aws-sdk-go in the go.mod require block"
30+
echo ""
31+
echo "simply reload your Bash shell with \`exec bash\`" and then re-run whichever
32+
echo "command you were running."
33+
echo "****************************************************************************"
34+
else
35+
# Don't overide the existing version let the user decide.
36+
if ! controller_gen_version_equals "$CONTROLLER_TOOLS_VERSION"; then
37+
echo "FAIL: Existing version of controller-gen "`controller-gen --version`", required version is $CONTROLLER_TOOLS_VERSION."
38+
echo "FAIL: Please uninstall controller-gen and re-run this script, which will install the required version."
39+
exit 1
40+
fi
1441
fi
15-
fi
16-
1742
}
1843

19-
is_min_controller_gen_version() {
20-
currentver="$(controller-gen --version | cut -d' ' -f2)";
44+
# controller_gen_version_equals accepts a string version and returns 0 if the
45+
# installed version of controller-gen matches the supplied version, otherwise
46+
# returns 1
47+
#
48+
# Usage:
49+
#
50+
# if controller_gen_version_equals "v0.4.0"; then
51+
# echo "controller-gen is at version 0.4.0"
52+
# fi
53+
controller_gen_version_equals() {
54+
currentver="$(controller-gen --version | cut -d' ' -f2 | tr -d '\n')";
2155
requiredver="$1";
22-
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
56+
if [ "$currentver" = "$requiredver" ]; then
2357
return 0
2458
else
2559
return 1

0 commit comments

Comments
 (0)