Skip to content

Commit d6a30e9

Browse files
committed
Fix: multiarch build by using crosscompilation
Use quay.io/centos/centos:stream9 with the –platform=$BUILDPLATFORM flag, in this case the image is pulled for the builder host's current architecture. The BUILDOS and BUILDARCH args are used to download the right go binary for the build platform. Cross-compilation occurs in the builder image using TARGETOS and TARGETARCH build arguments to determine the target OS/arch. These args are set automatically by the multiarch-build process (with docker buildx). The final container image (registry.access.redhat.com/ubi9/ubi-minimal) is pulled for the correct target architecture, such as amd64 or arm64. This update fixes support for multiarch builds and speeds up image building, as cross-compilation is faster than compiling on a non-native platform. Signed-off-by: Yury Kulazhenkov <[email protected]>
1 parent 546b351 commit d6a30e9

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

.github/workflows/image-build-test.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ jobs:
2727
with:
2828
context: .
2929
push: false
30-
# no need to explicitly set goarch,
31-
# correct arch will be selected for each build platform
32-
build-args: |
33-
goarch=
3430
platforms: ${{ env.BUILD_PLATFORMS }}
3531
file: ./cmd/Dockerfile

.github/workflows/image-push-main.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ jobs:
4545
with:
4646
context: .
4747
push: true
48-
# no need to explicitly set goarch,
49-
# correct arch will be selected for each build platform
50-
build-args: |
51-
goarch=
5248
platforms: ${{ env.BUILD_PLATFORMS }}
5349
tags: |
5450
${{ env.IMAGE_NAME }}:latest

.github/workflows/image-push-release.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ jobs:
4747
with:
4848
context: .
4949
push: true
50-
# no need to explicitly set goarch,
51-
# correct arch will be selected for each build platform
52-
build-args: |
53-
goarch=
5450
platforms: ${{ env.BUILD_PLATFORMS }}
5551
tags: |
5652
${{ steps.docker_meta.outputs.tags }}

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export GOROOT=$(BIN_DIR)/go/
1313
export GOBIN = $(GOROOT)/bin/
1414
export PATH := $(GOBIN):$(PATH):$(BIN_DIR)
1515
GOPATH = $(CURDIR)/.gopath
16-
GOARCH ?= amd64
16+
GOARCH ?= $(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
1717
ORG_PATH = github.com/k8snetworkplumbingwg
1818
PACKAGE = ovs-cni
1919
OCI_BIN ?= $(shell if podman ps >/dev/null 2>&1; then echo podman; elif docker ps >/dev/null 2>&1; then echo docker; fi)

cmd/Dockerfile

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
1-
FROM quay.io/centos/centos:stream9 as builder
1+
FROM --platform=$BUILDPLATFORM quay.io/centos/centos:stream9 AS builder
22

33
RUN mkdir /workdir
4-
WORKDIR /workdir
4+
# Support overriding target GOARCH during `make docker-build`
5+
ARG goarch=
56

6-
COPY go.mod .
7+
# these variable are automatically set during the multiarch build by docker buildx
8+
ARG TARGETOS
9+
ARG TARGETARCH
10+
ENV TARGETOS=${TARGETOS:-linux}
11+
ENV TARGETARCH=${TARGETARCH:-amd64}
12+
13+
ARG BUILDOS
14+
ARG BUILDARCH
15+
ENV BUILDOS=${BUILDOS:-linux}
16+
ENV BUILDARCH=${BUILDARCH:-amd64}
17+
18+
ENV GOOS=${TARGETOS}
19+
ENV GOARCH=${goarch:-$TARGETARCH}
20+
ENV CGO_ENABLED=0
21+
ENV GOFLAGS=-mod=vendor
22+
23+
WORKDIR /workdir
724

825
RUN dnf install -y wget
926

27+
COPY go.mod .
28+
COPY go.sum .
29+
1030
RUN GO_VERSION=$(sed -En 's/^go +(.*)$/\1/p' go.mod) && \
11-
wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
12-
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
13-
rm go${GO_VERSION}.linux-amd64.tar.gz
31+
wget https://dl.google.com/go/go${GO_VERSION}.${BUILDOS}-${BUILDARCH}.tar.gz && \
32+
tar -C /usr/local -xzf go${GO_VERSION}.${BUILDOS}-${BUILDARCH}.tar.gz && \
33+
rm go${GO_VERSION}.${BUILDOS}-${BUILDARCH}.tar.gz
1434

15-
ENV PATH /usr/local/go/bin:$PATH
35+
ENV PATH=/usr/local/go/bin:$PATH
1636

17-
COPY . .
37+
RUN go mod download
1838

19-
ENV GOOS linux
20-
# Support overriding target GOARCH during `make docker-build`
21-
ARG goarch=amd64
22-
ENV GOARCH=$goarch
23-
ENV CGO_ENABLED 0
24-
ENV GOFLAGS -mod=vendor
39+
COPY . .
2540

2641
RUN mkdir /workdir/bin
2742
RUN go build -tags no_openssl -o /workdir/bin/ovs ./cmd/plugin
@@ -30,6 +45,8 @@ RUN go build -tags no_openssl -o /workdir/bin/ovs-mirror-producer ./cmd/mirror-p
3045
RUN go build -tags no_openssl -o /workdir/bin/ovs-mirror-consumer ./cmd/mirror-consumer
3146

3247
FROM registry.access.redhat.com/ubi9/ubi-minimal
48+
3349
RUN microdnf install -y findutils
50+
3451
COPY --from=builder /workdir/.version /.version
3552
COPY --from=builder /workdir/bin/* /

0 commit comments

Comments
 (0)