|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ./scripts/install-controller-gen.sh |
| 4 | +# |
| 5 | +# Checks that the `controller-gen` binary is available on the host system and |
| 6 | +# if it is, that it matches the exact version that we require in order to |
| 7 | +# standardize the YAML manifests for CRDs and Kubernetes Roles. |
| 8 | +# |
| 9 | +# If the locally-installed controller-gen does not match the required version, |
| 10 | +# prints an error message asking the user to uninstall it. |
| 11 | +# |
| 12 | +# NOTE: We use this technique of building using `go build` within a temp |
| 13 | +# directory because controller-tools does not have a binary release artifact |
| 14 | +# for controller-gen. |
| 15 | +# |
| 16 | +# See: https://github.com/kubernetes-sigs/controller-tools/issues/500 |
| 17 | + |
| 18 | +set -Eo pipefail |
| 19 | + |
| 20 | +SCRIPTS_DIR=$(cd "$(dirname "$0")"; pwd) |
| 21 | +ROOT_DIR="$SCRIPTS_DIR/.." |
| 22 | +CONTROLLER_TOOLS_VERSION="v0.4.0" |
| 23 | + |
| 24 | +source "$SCRIPTS_DIR/lib/common.sh" |
| 25 | + |
| 26 | +if ! is_installed controller-gen; then |
| 27 | + # GOBIN not always set... so default to installing into $GOPATH/bin if |
| 28 | + # not... |
| 29 | + __install_dir=${GOBIN:-$GOPATH/bin} |
| 30 | + __install_path="$__install_dir/controller-gen" |
| 31 | + __work_dir=$(mktemp -d /tmp/controller-gen-XXX) |
| 32 | + |
| 33 | + echo -n "installing controller-gen ${CONTROLLER_TOOLS_VERSION} ... " |
| 34 | + cd "$__work_dir" |
| 35 | + |
| 36 | + go mod init tmp 1>/dev/null 2>&1 |
| 37 | + go get -d "sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_TOOLS_VERSION}" 1>/dev/null 2>&1 |
| 38 | + go build -o "$__work_dir/controller-gen" sigs.k8s.io/controller-tools/cmd/controller-gen 1>/dev/null 2>&1 |
| 39 | + mv "$__work_dir/controller-gen" "$__install_path" |
| 40 | + |
| 41 | + rm -rf "$WORK_DIR" |
| 42 | + echo "ok." |
| 43 | +fi |
0 commit comments