From d360c5a0f4b6ccc2f388c4eeca8b738649b2089e Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Mon, 5 Oct 2020 12:54:06 -0400 Subject: [PATCH] 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 #349 Related: https://github.com/kubernetes-sigs/controller-tools/issues/500 --- scripts/lib/k8s.sh | 64 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/scripts/lib/k8s.sh b/scripts/lib/k8s.sh index b7eafadd96..c7f0a62c95 100644 --- a/scripts/lib/k8s.sh +++ b/scripts/lib/k8s.sh @@ -1,25 +1,59 @@ #!/usr/bin/env bash +CONTROLLER_TOOLS_VERSION="v0.4.0" + +# ensure_controller_gen checks that the `controller-gen` binary is available on +# the host system and if it is, that it matches the exact version that we +# require in order to standardize the YAML manifests for CRDs and Kubernetes +# Roles. +# +# If the locally-installed controller-gen does not match the required version, +# prints an error message asking the user to uninstall it. ensure_controller_gen() { - if ! is_installed controller-gen; then - # Need this version of controller-gen to allow dangerous types and float - # type support - go get sigs.k8s.io/controller-tools/cmd/controller-gen@4a903ddb7005459a7baf4777c67244a74c91083d - else - minimum_req_version="v0.3.1" - # Don't overide the existing version let the user decide. - if ! is_min_controller_gen_version "$minimum_req_version"; then - echo "Existing version of controller-gen "`controller-gen --version`", minimum required is $minimum_req_version" - exit 1 + if ! is_installed controller-gen; then + __install_path=/usr/local/bin/controller-gen + __work_dir=$(mktemp --tmpdir -d controller-gen-XXX) + + cd "$__work_dir" + + go mod init tmp + go get -d "sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_TOOLS_VERSION}" + go build -o "$__work_dir/controller-gen" sigs.k8s.io/controller-tools/cmd/controller-gen + sudo mv "$__work_dir/controller-gen" "$__install_path" + + rm -rf "$WORK_DIR" + echo "****************************************************************************" + echo "WARNING: You may need to reload your Bash shell and path. If you see an" + echo " error like this following:" + echo "" + echo "Error: couldn't find github.com/aws/aws-sdk-go in the go.mod require block" + echo "" + echo "simply reload your Bash shell with \`exec bash\`" and then re-run whichever + echo "command you were running." + echo "****************************************************************************" + else + # Don't overide the existing version let the user decide. + if ! controller_gen_version_equals "$CONTROLLER_TOOLS_VERSION"; then + echo "FAIL: Existing version of controller-gen "`controller-gen --version`", required version is $CONTROLLER_TOOLS_VERSION." + echo "FAIL: Please uninstall controller-gen and re-run this script, which will install the required version." + exit 1 + fi fi - fi - } -is_min_controller_gen_version() { - currentver="$(controller-gen --version | cut -d' ' -f2)"; +# controller_gen_version_equals accepts a string version and returns 0 if the +# installed version of controller-gen matches the supplied version, otherwise +# returns 1 +# +# Usage: +# +# if controller_gen_version_equals "v0.4.0"; then +# echo "controller-gen is at version 0.4.0" +# fi +controller_gen_version_equals() { + currentver="$(controller-gen --version | cut -d' ' -f2 | tr -d '\n')"; requiredver="$1"; - if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then + if [ "$currentver" = "$requiredver" ]; then return 0 else return 1