|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
| 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. |
| 12 | +# |
| 13 | +# NOTE: We use this technique of building using `go build` within a temp |
| 14 | +# directory because controller-tools does not have a binary release artifact |
| 15 | +# for controller-gen. |
| 16 | +# |
| 17 | +# See: https://github.com/kubernetes-sigs/controller-tools/issues/500 |
3 | 18 | 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 |
| 19 | + if ! is_installed controller-gen; then |
| 20 | + # GOBIN not always set... so default to installing into $GOPATH/bin if |
| 21 | + # not... |
| 22 | + __install_dir=${GOBIN:-$GOPATH/bin} |
| 23 | + __install_path="$__install_dir/controller-gen" |
| 24 | + __work_dir=$(mktemp -d /tmp/controller-gen-XXX) |
| 25 | + |
| 26 | + cd "$__work_dir" |
| 27 | + |
| 28 | + go mod init tmp |
| 29 | + go get -d "sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_TOOLS_VERSION}" |
| 30 | + go build -o "$__work_dir/controller-gen" sigs.k8s.io/controller-tools/cmd/controller-gen |
| 31 | + mv "$__work_dir/controller-gen" "$__install_path" |
| 32 | + |
| 33 | + rm -rf "$WORK_DIR" |
| 34 | + echo "****************************************************************************" |
| 35 | + echo "WARNING: You may need to reload your Bash shell and path. If you see an" |
| 36 | + echo " error like this following:" |
| 37 | + echo "" |
| 38 | + echo "Error: couldn't find github.com/aws/aws-sdk-go in the go.mod require block" |
| 39 | + echo "" |
| 40 | + echo "simply reload your Bash shell with \`exec bash\`" and then re-run whichever |
| 41 | + echo "command you were running." |
| 42 | + echo "****************************************************************************" |
| 43 | + else |
| 44 | + # Don't overide the existing version let the user decide. |
| 45 | + if ! controller_gen_version_equals "$CONTROLLER_TOOLS_VERSION"; then |
| 46 | + echo "FAIL: Existing version of controller-gen "`controller-gen --version`", required version is $CONTROLLER_TOOLS_VERSION." |
| 47 | + echo "FAIL: Please uninstall controller-gen and re-run this script, which will install the required version." |
| 48 | + exit 1 |
| 49 | + fi |
14 | 50 | fi
|
15 |
| - fi |
16 |
| - |
17 | 51 | }
|
18 | 52 |
|
19 |
| -is_min_controller_gen_version() { |
20 |
| - currentver="$(controller-gen --version | cut -d' ' -f2)"; |
| 53 | +# controller_gen_version_equals accepts a string version and returns 0 if the |
| 54 | +# installed version of controller-gen matches the supplied version, otherwise |
| 55 | +# returns 1 |
| 56 | +# |
| 57 | +# Usage: |
| 58 | +# |
| 59 | +# if controller_gen_version_equals "v0.4.0"; then |
| 60 | +# echo "controller-gen is at version 0.4.0" |
| 61 | +# fi |
| 62 | +controller_gen_version_equals() { |
| 63 | + currentver="$(controller-gen --version | cut -d' ' -f2 | tr -d '\n')"; |
21 | 64 | requiredver="$1";
|
22 |
| - if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then |
| 65 | + if [ "$currentver" = "$requiredver" ]; then |
23 | 66 | return 0
|
24 | 67 | else
|
25 | 68 | return 1
|
|
0 commit comments