Skip to content

Commit

Permalink
Fix multi-arch build issue by using cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasferrandiz committed Apr 11, 2024
1 parent 5a71d5b commit c095269
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
ARG BCI_IMAGE=registry.suse.com/bci/bci-busybox
ARG GO_IMAGE=rancher/hardened-build-base:v1.20.14b1
ARG GOEXPERIMENT=boringcrypto
ARG ARCH="amd64"

# Image that provides cross compilation tooling.
FROM --platform=$BUILDPLATFORM rancher/mirrored-tonistiigi-xx:1.3.0 as xx

### Build the cni-plugins ###
FROM ${GO_IMAGE} as cni_plugins
ARG ARCH=amd64
FROM --platform=$BUILDPLATFORM ${GO_IMAGE} as cni_plugins_builder
# copy xx scripts to your build stage
COPY --from=xx / /
RUN apk add file make git clang lld
ARG TARGETPLATFORM
# setup required packages
RUN set -x && \
xx-apk --no-cache add musl-dev gcc lld

ARG TAG=v1.4.1
ARG FLANNEL_TAG=v1.4.0-flannel1
ARG GOEXPERIMENT
RUN git clone --depth=1 https://github.com/containernetworking/plugins.git $GOPATH/src/github.com/containernetworking/plugins && \
cd $GOPATH/src/github.com/containernetworking/plugins && \
git fetch --all --tags --prune && \
git checkout tags/${TAG} -b ${TAG} && \
xx-go --wrap\
sh -ex ./build_linux.sh -v \
-gcflags=-trimpath=/go/src \
-ldflags " \
Expand All @@ -22,10 +34,15 @@ RUN git clone --depth=1 https://github.com/flannel-io/cni-plugin $GOPATH/src/git
cd $GOPATH/src/github.com/flannel-io/cni-plugin && \
git fetch --all --tags --prune && \
git checkout tags/${FLANNEL_TAG} -b ${FLANNEL_TAG} && \
export GOOS=$(xx-info os) &&\
export GOARCH=$(xx-info arch) &&\
export ARCH=$(xx-info arch) &&\
make build_linux && \
mkdir -p $GOPATH/src/github.com/containernetworking/plugins/bin && \
mv $GOPATH/src/github.com/flannel-io/cni-plugin/dist/flannel-${ARCH} $GOPATH/src/github.com/containernetworking/plugins/bin/flannel

WORKDIR $GOPATH/src/github.com/containernetworking/plugins
RUN xx-verify --static bin/*
RUN go-assert-static.sh bin/* && \
if [ "${ARCH}" = "amd64" ]; then \
go-assert-boring.sh bin/bandwidth \
Expand All @@ -41,11 +58,19 @@ RUN go-assert-static.sh bin/* && \
bin/vlan ; \
fi && \
mkdir -vp /opt/cni/bin && \
install -D -s bin/* /opt/cni/bin
install -D bin/* /opt/cni/bin

FROM ${GO_IMAGE} as strip_binary
#strip needs to run on TARGETPLATFORM, not BUILDPLATFORM
COPY --from=cni_plugins_builder /opt/cni/ /opt/cni/
RUN for plugin in $(ls /opt/cni/bin); do \
strip /opt/cni/bin/${plugin}; \
done


# Create image with the cni-plugins
FROM ${BCI_IMAGE}
COPY --from=cni_plugins /opt/cni/ /opt/cni/
COPY --from=strip_binary /opt/cni/ /opt/cni/
WORKDIR /
COPY install-cnis.sh .
ENTRYPOINT ["./install-cnis.sh"]

0 comments on commit c095269

Please sign in to comment.