ci: use native Go cross-compilation instead of QEMU emulation for arm64 builds - #869
Conversation
…64 builds
Add --platform=$BUILDPLATFORM to the builder stage in both Go Containerfiles
so that the Go compiler runs natively on the build host (x86_64) and
cross-compiles for arm64 via GOOS/GOARCH, instead of running the entire
compilation under QEMU user-static emulation.
This is safe because CGO_ENABLED=0 and the Containerfiles already set
GOOS=${TARGETOS} GOARCH=${TARGETARCH} for cross-compilation.
Also removes the Containerfile.cross workaround from both Makefile
docker-buildx targets, since the Containerfiles now include the
--platform directive directly.
Expected speedup: ~10-20x for the arm64 Go compilation portion of CI
builds (QEMU emulation vs native cross-compile).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe PR pins builder stage base images to $BUILDPLATFORM in controller/Containerfile and controller/Containerfile.operator, adding comments about native compilation via GOOS/GOARCH and CGO_ENABLED=0. Corresponding Makefiles are simplified to build directly from these Containerfiles, removing temporary Containerfile.cross/Containerfile.operator.cross generation and cleanup steps. ChangesBuildPlatform Pinning and Makefile Simplification
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Container ImagesThe following container images have been built for this PR:
Images expire after 7 days. |
|
Reduces the build time of multiarch golang images to 4-6m instead of 20-25m , no more qemu-user-static. We had that hack because podman buildx didn't support the build platform references well in previous versions, but it's fixed now. |
Problem
The
build-images.yamlCI workflow builds Go container images forlinux/amd64,linux/arm64using Docker Buildx onubuntu-latest(x86_64) runners. For arm64 images, the entire builder stage — including Go compilation — runs under QEMU user-static emulation, which is ~10-20x slower than native execution.The irony is that the Containerfiles were already set up for cross-compilation (
CGO_ENABLED=0,GOOS=${TARGETOS},GOARCH=${TARGETARCH}), but the builder stage was still being emulated because it lacked the--platform=$BUILDPLATFORMdirective.Solution
Add
--platform=$BUILDPLATFORMto the builderFROMline in both Go Containerfiles. This tells BuildKit to run the builder stage natively on the host CPU (x86_64), while Go cross-compiles for the target architecture viaGOARCH=${TARGETARCH}.Files changed
controller/Containerfile--platform=$BUILDPLATFORMto builder FROMcontroller/Containerfile.operator--platform=$BUILDPLATFORMto builder FROMcontroller/MakefileContainerfile.crossworkaround fromdocker-buildxtargetcontroller/deploy/operator/MakefileContainerfile.operator.crossworkaround fromdocker-buildxtargetWhy this is safe
CGO_ENABLED=0— pure Go, no C toolchain neededdocker-buildxtargets already applied this exact transformation via asedworkaround (generating temporaryContainerfile.crossfiles) — this PR makes it permanent and removes the workarounddocker-buildis unaffected ($BUILDPLATFORMequals the target platform)FROM ubi-micro) is unaffected — it pulls the correct arch variant and only runs metadata operations (COPY,USER,ENTRYPOINT)Expected impact