Skip to content

Commit ddceef5

Browse files
authored
Add support for cross arch docker builds (#183)
Add support for cross architeture Docker builds. Since Arch linux doesn't have an official image for arm64, switch to Debian for building the dataplane.
2 parents 2aaaef9 + f61d9ad commit ddceef5

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ COPY pkg/ pkg/
2121
COPY internal/ internal/
2222

2323
# Build
24-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
24+
RUN CGO_ENABLED=0 GOOS=linux go build -a -o manager main.go
2525

2626
# Use distroless as minimal base image to package the manager binary
2727
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ EXISTING_CLUSTER ?=
88

99
# Image URL to use all building/pushing image targets
1010
TAG ?= integration-tests
11+
ifeq ($(shell uname -m),arm64)
12+
BUILD_PLATFORMS ?= linux/arm64
13+
else
14+
BUILD_PLATFORMS ?= linux/amd64
15+
endif
16+
BUILD_ARGS ?= --load
1117

1218
# VERSION defines the project version for the bundle.
1319
# Update this value when you upgrade the version of your project.
@@ -187,7 +193,7 @@ debug: manifests generate fmt vet ## Run a controller from your host via debugge
187193

188194
.PHONY: build.image
189195
build.image:
190-
DOCKER_BUILDKIT=1 docker build -t $(BLIXT_CONTROLPLANE_IMAGE):$(TAG) .
196+
DOCKER_BUILDKIT=1 docker buildx build --platform=$(BUILD_PLATFORMS) $(BUILD_ARGS) -t $(BLIXT_CONTROLPLANE_IMAGE):$(TAG) .
191197

192198
.PHONY: build.all.images
193199
build.all.images: build.image

dataplane/Dockerfile

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
FROM archlinux as builder
1+
FROM rust:1.75-slim-bookworm as builder
22

3-
RUN pacman -Syu --noconfirm
4-
RUN pacman -S base-devel protobuf rustup --noconfirm
3+
ARG TARGETARCH
4+
5+
RUN apt-get update
6+
RUN apt-get install --yes build-essential protobuf-compiler pkg-config llvm-16
57

68
RUN rustup default stable
79
RUN rustup install nightly
810
RUN rustup component add rust-src --toolchain nightly
9-
RUN rustup target add x86_64-unknown-linux-musl
1011
RUN --mount=type=cache,target=/root/.cargo/registry \
1112
cargo install bpf-linker
1213

1314
WORKDIR /workspace
15+
# Docker uses the amd64/arm64 convention while Rust uses the x86_64/aarch64 convention.
16+
# Since Dockerfile doesn't support conditional variables (sigh), write the arch in Rust's
17+
# convention to a file for later usage.
18+
RUN if [ "$TARGETARCH" = "amd64" ]; \
19+
then echo "x86_64" >> arch; \
20+
else echo "aarch64" >> arch; \
21+
fi
22+
RUN rustup target add $(eval cat arch)-unknown-linux-musl
1423

1524
COPY . .
25+
26+
# We need to tell bpf-linker where it can find LLVM's shared library file.
27+
# Ref: https://github.com/aya-rs/rustc-llvm-proxy/blob/cbcb3c6/src/lib.rs#L48
28+
ENV LD_LIBRARY_PATH="/usr/lib/llvm-16/lib"
29+
1630
RUN --mount=type=cache,target=/workspace/target/ \
1731
--mount=type=cache,target=/root/.cargo/registry \
1832
cargo xtask build-ebpf --release
1933
RUN --mount=type=cache,target=/workspace/target/ \
2034
--mount=type=cache,target=/root/.cargo/registry \
21-
RUSTFLAGS=-Ctarget-feature=+crt-static cargo build --release --target=x86_64-unknown-linux-musl
35+
RUSTFLAGS=-Ctarget-feature=+crt-static cargo build --release --target=$(eval cat arch)-unknown-linux-musl
2236
RUN --mount=type=cache,target=/workspace/target/ \
23-
cp /workspace/target/x86_64-unknown-linux-musl/release/loader /workspace/dataplane
37+
cp /workspace/target/$(eval cat arch)-unknown-linux-musl/release/loader /workspace/dataplane
2438

2539
FROM alpine
2640

dataplane/Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ IMAGE ?= ghcr.io/kubernetes-sigs/blixt-dataplane
22
TAG ?= latest
33
KIND_CLUSTER ?= blixt-dev
44

5+
ifeq ($(shell uname -m),arm64)
6+
BUILD_PLATFORMS ?= linux/arm64
7+
else
8+
BUILD_PLATFORMS ?= linux/amd64
9+
endif
10+
BUILD_ARGS ?= --load
11+
512
all: build
613

714
.PHONY:
@@ -20,7 +27,7 @@ build.release:
2027

2128
.PHONY: build.image
2229
build.image:
23-
DOCKER_BUILDKIT=1 docker build -t $(IMAGE):$(TAG) ./
30+
DOCKER_BUILDKIT=1 docker buildx build --platform $(BUILD_PLATFORMS) $(BUILD_ARGS) -t $(IMAGE):$(TAG) ./
2431

2532
.PHONY: load.image
2633
load.image: build.image

0 commit comments

Comments
 (0)