Skip to content

Commit fd72112

Browse files
committed
hack: only rebuild terraform providers if needed
Look at the Golang module information from the binary to determine whether the providers need to be rebuilt. It'll be useful when we use an external container image containing pre-built binaries for the providers.
1 parent d64216d commit fd72112

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

hack/build.sh

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,46 @@ copy_terraform_to_mirror() {
3030
cp "${PWD}/terraform/bin/${TARGET_OS_ARCH}/terraform" "${PWD}/pkg/terraform/providers/mirror/terraform/"
3131
}
3232

33+
# Check if a provider has changed based on the version stored in the binary
34+
check_module_changes() {
35+
binpath="$1"
36+
srcpath="$2"
37+
build_hash="$(go version -m "${binpath}" | grep 'vcs.revision' | cut -f2 -d'=')"
38+
# If it's a locally developed provider
39+
if test -n "${build_hash}"; then
40+
# Check if a provider has changed based on git changes since the
41+
# revision the provider was last built
42+
git diff --name-only --exit-code "${build_hash}.." -- "${srcpath}"
43+
# If it's an imported module
44+
else
45+
# Check if a provider has changed based on its go.mod git hash
46+
version_info="$(go version -m "${binpath}" | grep -Eo 'main.builtGoModHash=[a-fA-F0-9]+' | cut -f2 -d'=')"
47+
test "${version_info}" == "$(git hash-object "${srcpath}/go.mod")"
48+
fi
49+
}
50+
51+
# Build terraform and providers only if needed
52+
build_terraform_and_providers() {
53+
TARGET_OS_ARCH=$(go env GOOS)_$(go env GOARCH)
54+
bindir="${PWD}/terraform/bin/${TARGET_OS_ARCH}"
55+
find "${PWD}/terraform/providers/" -maxdepth 1 -mindepth 1 -type d | while read -r dir; do
56+
provider="$(basename "${dir}")"
57+
binpath="${bindir}/terraform-provider-${provider}"
58+
if test -s "${binpath}" && test -s "${binpath}.zip" && check_module_changes "${binpath}" "terraform/providers/${provider}"; then
59+
echo "${provider} is up-to-date"
60+
else
61+
echo "Rebuilding ${provider}"
62+
make -C terraform "go-build.${provider}"
63+
fi
64+
done
65+
if test -s "${bindir}/terraform" && check_module_changes "${bindir}/terraform" "terraform/terraform"; then
66+
echo "terraform is up-to-date"
67+
else
68+
echo "Rebuilding terraform"
69+
make -C terraform go-build-terraform
70+
fi
71+
}
72+
3373
minimum_go_version=1.20
3474
current_go_version=$(go version | cut -d " " -f 3)
3575

@@ -44,8 +84,8 @@ MODE="${MODE:-release}"
4484
# Build terraform binaries before setting environment variables since it messes up make
4585
if test "${SKIP_TERRAFORM}" != y && ! (echo "${TAGS}" | grep -q -e 'aro' -e 'altinfra')
4686
then
47-
make -C terraform all
48-
copy_terraform_to_mirror # Copy terraform parts to embedded mirror.
87+
build_terraform_and_providers
88+
copy_terraform_to_mirror # Copy terraform parts to embedded mirror.
4989
fi
5090

5191
# build cluster-api binaries

0 commit comments

Comments
 (0)