From f5f477fe03a60881375c1b338f3f487975bcb24b Mon Sep 17 00:00:00 2001 From: Manuel Buil Date: Tue, 9 Apr 2024 11:26:06 +0200 Subject: [PATCH] Accelerate ARM64 build Signed-off-by: Manuel Buil --- Dockerfile | 33 +++++++++++++++++++++++---------- Makefile | 1 + 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6f7b22c..e5b21a0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,14 +2,17 @@ ARG GO_IMAGE=rancher/hardened-build-base:v1.20.14b1 #FROM ${BCI_IMAGE} as bci -FROM ${GO_IMAGE} as base - +# Image that provides cross compilation tooling. +FROM --platform=$BUILDPLATFORM rancher/mirrored-tonistiigi-xx:1.3.0 as xx +FROM --platform=$BUILDPLATFORM ${GO_IMAGE} as base-builder +# copy xx scripts to your build stage +COPY --from=xx / / RUN set -x && \ - apk --no-cache add \ - git \ - make + apk --no-cache add file make git clang lld +RUN xx-apk --no-cache add musl-dev gcc lld +ARG TARGETPLATFORM -FROM base as builder +FROM --platform=$BUILDPLATFORM base-builder as addon-builder ARG ARCH ARG SRC=github.com/kubernetes/autoscaler ARG PKG=github.com/kubernetes/autoscaler @@ -18,15 +21,25 @@ ARG TAG=1.8.20 WORKDIR $GOPATH/src/${PKG}/addon-resizer RUN git branch -a RUN git checkout addon-resizer-${TAG} -b ${TAG} -RUN ls -RUN GOARCH=${ARCH} GO_LDFLAGS="-linkmode=external -X ${PKG}/pkg/version.VERSION=${TAG}" \ +RUN go mod download + +# cross-compilation setup +ARG TARGETPLATFORM +RUN xx-go --wrap && \ + GOARCH=${ARCH} GO_LDFLAGS="-linkmode=external -X ${PKG}/pkg/version.VERSION=${TAG}" \ go-build-static.sh -gcflags=-trimpath=${GOPATH}/src -o pod_nanny nanny/main/pod_nanny.go RUN go-assert-static.sh pod_nanny +RUN xx-verify --static pod_nanny RUN if [ "${ARCH}" = "amd64" ]; then \ go-assert-boring.sh pod_nanny; \ fi -RUN install -s pod_nanny /usr/local/bin +RUN install pod_nanny /usr/local/bin/ + +FROM ${GO_IMAGE} as strip_binary +#strip needs to run on TARGETPLATFORM, not BUILDPLATFORM +COPY --from=addon-builder /usr/local/bin/pod_nanny /pod_nanny +RUN strip /pod_nanny FROM scratch -COPY --from=builder /usr/local/bin/pod_nanny /pod_nanny +COPY --from=strip_binary /pod_nanny /pod_nanny ENTRYPOINT ["/pod_nanny"] diff --git a/Makefile b/Makefile index ab2b363..6308241 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ endif image-build: docker build \ --pull \ + --platform=$(ARCH) \ --build-arg ARCH=$(ARCH) \ --build-arg PKG=$(PKG) \ --build-arg SRC=$(SRC) \