Skip to content

Commit ba66fc6

Browse files
Merge pull request #7688 from r4f4/rebuild-tf-providers-if-needed
CORS-2870: build: only rebuild terraform providers if needed
2 parents 31504ce + 86ad168 commit ba66fc6

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-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

images/baremetal/Dockerfile.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
ARG libvirt_version="8.0.0"
55

6+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
7+
68
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS builder
79
ARG libvirt_version
810
ARG TAGS="libvirt baremetal"
911
RUN dnf install -y libvirt-devel-$libvirt_version && \
1012
dnf clean all && rm -rf /var/cache/yum/*
1113
WORKDIR /go/src/github.com/openshift/installer
1214
COPY . .
15+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/ terraform/bin/
1316
RUN DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
1417

1518

images/installer-artifacts/Dockerfile.rhel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# This Dockerfile builds an image containing Mac and Linux/AMD64 versions of
22
# the installer layered on top of the cluster-native Linux installer image.
33

4+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
5+
46
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS macbuilder
57
ARG TAGS=""
68
WORKDIR /go/src/github.com/openshift/installer
79
COPY . .
10+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/darwin_amd64 terraform/bin/darwin_amd64
811
RUN GOOS=darwin GOARCH=amd64 DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
912

1013
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS macarmbuilder
1114
ARG TAGS=""
1215
WORKDIR /go/src/github.com/openshift/installer
1316
COPY . .
17+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/darwin_arm64 terraform/bin/darwin_arm64
1418
RUN GOOS=darwin GOARCH=arm64 DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
1519

1620
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS linuxbuilder
1721
ARG TAGS=""
1822
WORKDIR /go/src/github.com/openshift/installer
1923
COPY . .
24+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/linux_amd64 terraform/bin/linux_amd64
2025
RUN GOOS=linux GOARCH=amd64 DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
2126

2227
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS linuxarmbuilder
2328
ARG TAGS=""
2429
WORKDIR /go/src/github.com/openshift/installer
2530
COPY . .
31+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/linux_arm64 terraform/bin/linux_arm64
2632
RUN GOOS=linux GOARCH=arm64 DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
2733

2834
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS builder

images/installer/Dockerfile.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# This Dockerfile is used by CI to publish the installer image.
22
# It builds an image containing only the openshift-install.
33

4+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
5+
46
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.15 AS builder
57
ARG TAGS=""
68
WORKDIR /go/src/github.com/openshift/installer
79
COPY . .
10+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/ terraform/bin/
811
RUN DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
912
RUN go run -mod=vendor hack/build-coreos-manifest.go
1013

images/installer/Dockerfile.upi.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
# It builds an image containing binaries like jq, terraform, awscli, oc, etc. to allow bringing up UPI infrastructure.
33
# It also contains the `upi` directory that contains various terraform and cloud formation templates that are used to create infrastructure resources.
44

5+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
6+
57
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.14 AS builder
68
ARG TAGS=""
79
WORKDIR /go/src/github.com/openshift/installer
810
COPY . .
11+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/ terraform/bin/
912
RUN DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
1013

1114
FROM registry.ci.openshift.org/ocp/4.15:cli as cli

images/libvirt/Dockerfile.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# This Dockerfile is a used by CI to publish an installer image for creating libvirt clusters
22
# It builds an image containing openshift-install and nss-wrapper for remote deployments, as well as the google cloud-sdk for nested GCE environments.
33

4+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
5+
46
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.14 AS builder
57
ARG TAGS="libvirt"
68
RUN yum install -y libvirt-devel && \
79
yum clean all && rm -rf /var/cache/yum/*
810
WORKDIR /go/src/github.com/openshift/installer
911
COPY . .
12+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/ terraform/bin/
1013
RUN DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
1114

1215
FROM quay.io/centos/centos:stream

images/openstack/Dockerfile.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# This Dockerfile is used by CI to test using OpenShift Installer against an OpenStack cloud.
22
# It builds an image containing the openshift-install command as well as the openstack cli.
3+
FROM registry.ci.openshift.org/ocp/4.15:installer-terraform-providers as providers
4+
35
FROM registry.ci.openshift.org/ocp/builder:rhel-8-golang-1.20-openshift-4.14 AS builder
46
ARG TAGS=""
57
WORKDIR /go/src/github.com/openshift/installer
68
COPY . .
9+
COPY --from=providers /go/src/github.com/openshift/installer/terraform/bin/ terraform/bin/
710
RUN DEFAULT_ARCH="$(go env GOHOSTARCH)" hack/build.sh
811

912
FROM registry.ci.openshift.org/ocp/4.15:cli AS cli

0 commit comments

Comments
 (0)