Skip to content

Commit 257b50c

Browse files
Merge pull request #53 from thomasferrandiz/useCrossCompilation
Use cross-compilation to fix build issues
2 parents 907edfd + 29793df commit 257b50c

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

Dockerfile

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
1-
ARG ARCH="amd64"
21
ARG GO_IMAGE=rancher/hardened-build-base:v1.21.10b1
32

4-
# Build the multus project
5-
FROM ${GO_IMAGE} as builder
3+
# Image that provides cross compilation tooling.
4+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.3.0 AS xx
5+
6+
FROM --platform=$BUILDPLATFORM ${GO_IMAGE} as base-builder
7+
# copy xx scripts to your build stage
8+
COPY --from=xx / /
9+
RUN apk add file make git clang lld patch
10+
ARG TARGETPLATFORM
611
RUN set -x && \
7-
apk --no-cache add patch
8-
ARG ARCH
12+
xx-apk --no-cache add musl-dev gcc lld
13+
14+
# Build the multus project
15+
FROM base-builder as multus-builder
916
ARG TAG=v4.0.2
10-
ENV GOARCH ${ARCH}
11-
ENV GOOS "linux"
17+
ARG SRC=github.com/k8snetworkplumbingwg/multus-cni
18+
ARG PKG=github.com/k8snetworkplumbingwg/multus-cni
19+
RUN git clone --depth=1 https://${SRC}.git $GOPATH/src/${PKG}
20+
WORKDIR $GOPATH/src/${PKG}
1221
# patch to solve https://github.com/rancher/rke2/issues/4568
1322
# to be removed once upstream merges the fix
1423
# https://github.com/k8snetworkplumbingwg/multus-cni/pull/1137
1524
COPY self_delegation_bug.patch /tmp
16-
RUN git clone --depth=1 https://github.com/k8snetworkplumbingwg/multus-cni && \
17-
cd multus-cni && \
18-
git fetch --all --tags --prune && \
25+
26+
RUN git fetch --all --tags --prune && \
1927
git checkout tags/${TAG} -b ${TAG} && \
20-
git apply /tmp/self_delegation_bug.patch && \
28+
git apply /tmp/self_delegation_bug.patch
29+
RUN go mod download
30+
# cross-compilation setup
31+
ARG TARGETARCH
32+
33+
RUN xx-go --wrap && \
2134
./hack/build-go.sh
35+
RUN xx-verify --static bin/thin_entrypoint bin/multus
36+
37+
FROM ${GO_IMAGE} as strip_binary
38+
#strip needs to run on TARGETPLATFORM, not BUILDPLATFORM
39+
COPY --from=multus-builder /go/src/github.com/k8snetworkplumbingwg/multus-cni/bin/thin_entrypoint /thin_entrypoint
40+
COPY --from=multus-builder /go/src/github.com/k8snetworkplumbingwg/multus-cni/bin/multus /multus
41+
RUN strip /thin_entrypoint /multus
2242

2343
# Create the multus image
2444
FROM scratch as multus-cni
25-
COPY --from=builder /go/multus-cni/bin/multus /usr/src/multus-cni/bin/multus
26-
COPY --from=builder /go/multus-cni/LICENSE /usr/src/multus-cni/LICENSE
27-
COPY --from=builder /go/multus-cni/bin/thin_entrypoint /
45+
COPY --from=strip_binary /multus /usr/src/multus-cni/bin/multus
46+
COPY --from=multus-builder /go/src/github.com/k8snetworkplumbingwg/multus-cni/LICENSE /usr/src/multus-cni/LICENSE
47+
COPY --from=strip_binary /thin_entrypoint /
2848
ENTRYPOINT ["/thin_entrypoint"]

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ endif
1212

1313
BUILD_META=-build$(shell date +%Y%m%d)
1414
ORG ?= rancher
15+
PKG ?= github.com/k8snetworkplumbingwg/multus-cni
16+
SRC ?= github.com/k8snetworkplumbingwg/multus-cni
1517
TAG ?= ${GITHUB_ACTION_TAG}
1618

1719
ifeq ($(TAG),)
@@ -29,7 +31,6 @@ image-build:
2931
--build-arg PKG=$(PKG) \
3032
--build-arg SRC=$(SRC) \
3133
--build-arg TAG=$(TAG:$(BUILD_META)=) \
32-
--build-arg ARCH=$(ARCH) \
3334
--target multus-cni \
3435
--tag $(ORG)/hardened-multus-cni:$(TAG) \
3536
--tag $(ORG)/hardened-multus-cni:$(TAG)-$(ARCH) \

0 commit comments

Comments
 (0)