From f879b6ac039467689c2acc8b42ec3e154c73a51f Mon Sep 17 00:00:00 2001 From: Eric Promislow Date: Wed, 28 Feb 2024 09:37:25 -0800 Subject: [PATCH] Bring the 3.0 CROSS_ARCH changes into v4.0 --- DEVELOPING.md | 5 ++++- Dockerfile.dapper | 2 +- scripts/build | 29 +++++++++++++++++++++++++---- scripts/package | 6 +++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/DEVELOPING.md b/DEVELOPING.md index f6c9a053..81d2db7f 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -63,7 +63,10 @@ If you want to interact with the Minio deployed in the cluster, you can use the ### Building on Different Architectures -Currently we only support building this chart for amd64 nodes. The binary built by `make build` is set to build for amd64 and we have also included a flag for users running different architectures when trying to package the code into a docker image. Setting `USE_DOCKER_BUILDX=1` will force the package script to use docker buildx, setting the target platform to build for amd64. If you do not have `docker-buildx` installed please reference [this page](https://docs.docker.com/buildx/working-with-buildx/). +Currently, we only support building this chart fully for `amd64` nodes. The binary and image building scripts will build "arch native" by default. +So when building on `amd64` the resulting binary and image will both be `amd64` - same for `arm64`. This is how the drone builds operate. + +We have also included a flag for users running different architectures when trying to package the code into an `amd64` docker image. Building the binary with `CROSS_ARCH=true` set will cause it to build `amd64` and `arm64` binaries, and then setting `USE_DOCKER_BUILDX=1` will force the package script to use docker buildx, setting the container's target platform to build for `amd64`. If you do not have `docker-buildx` installed please reference [this page](https://docs.docker.com/buildx/working-with-buildx/). ### Developer Uninstallation diff --git a/Dockerfile.dapper b/Dockerfile.dapper index ff67a9f4..8e950c0d 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -37,7 +37,7 @@ RUN if [ "${ARCH}" != "s390x" ]; then \ chmod +x /usr/local/bin/mc; \ fi -ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS USE_DOCKER_BUILDX +ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS CROSS_ARCH USE_DOCKER_BUILDX ENV DAPPER_SOURCE /go/src/github.com/rancher/backup-restore-operator/ ENV DAPPER_OUTPUT ./bin ./dist ENV GOCACHE /root/.cache/go-build diff --git a/scripts/build b/scripts/build index bdd7f84c..c78fbd16 100755 --- a/scripts/build +++ b/scripts/build @@ -11,8 +11,29 @@ if [ "$(uname)" = "Linux" ]; then fi LINKFLAGS="-X main.Version=$VERSION" LINKFLAGS="-X main.GitCommit=$COMMIT $LINKFLAGS" -CGO_ENABLED=0 go build -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -o bin/backup-restore-operator -if [ "$CROSS" = "true" ] && [ "$ARCH" = "amd64" ]; then - GOOS=darwin go build -ldflags "$LINKFLAGS" -o bin/backup-restore-operator-darwin - GOOS=windows go build -ldflags "$LINKFLAGS" -o bin/backup-restore-operator-windows + + +ARCHES=( "$ARCH" ) +# Set CROSS_ARCH to build for the other architecture +if [ "$CROSS_ARCH" == "true" ]; then + case "$ARCH" in + amd64) XARCH=arm64 ;; + arm64) XARCH=amd64 ;; + *) echo "Unsupported ARCH of $ARCH" 1>&2 ; exit 1 + esac + ARCHES+=( "$XARCH" ) fi + +for A in "${ARCHES[@]}" ; do + GOARCH="$A" CGO_ENABLED=0 go build -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -o "bin/backup-restore-operator-$A" + # Set CROSS to build for other OS'es + if [ "$CROSS" = "true" ]; then + for OS in darwin windows ; do + GOARCH="$A" GOOS=$OS go build -ldflags "$LINKFLAGS" -o "bin/backup-restore-operator-$OS-$A" + done + fi +done + +cd bin +ln -sf "./backup-restore-operator-$ARCH" "./backup-restore-operator" +cd .. diff --git a/scripts/package b/scripts/package index dfd35339..e0a4b80f 100755 --- a/scripts/package +++ b/scripts/package @@ -6,7 +6,11 @@ source $(dirname $0)/version cd $(dirname $0)/.. mkdir -p dist/artifacts -cp bin/backup-restore-operator dist/artifacts/backup-restore-operator${SUFFIX} +if [ "$CROSS_ARCH" != "true" ]; then + cp bin/backup-restore-operator dist/artifacts/backup-restore-operator${SUFFIX} +else + cp bin/backup-restore-operator${SUFFIX} dist/artifacts/ +fi IMAGE=${REPO}/backup-restore-operator:${TAG} DOCKERFILE=package/Dockerfile