Skip to content

[WIP] made cross compile instead of qemu #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SergeyKanzhelev
Copy link
Member

Gemini generated:

The Problem

The build was failing specifically when creating the arm64 version of the container image. The build log showed a segmentation fault from the C compiler
(aarch64-linux-gnu-gcc).

This happens because the build machine is an amd64 architecture, but it's being asked to build an image for arm64. To do this, Docker's build system (BuildKit)
uses QEMU to emulate an arm64 environment. Running a compiler inside this emulated environment is often slow and can be unreliable, sometimes leading to crashes
like the one we saw.

The Solution: Native Cross-Compilation

The standard and much more reliable solution is to use native cross-compilation. Instead of emulating the target architecture, we run the compiler on the native
architecture of the build machine (amd64) and tell it to produce binaries for the target architecture (arm64). This is faster and avoids the instability of
emulation.

This is achieved with a multi-stage Dockerfile. The first stage (the builder) runs on the native build platform, compiles the code for all target platforms, and
the final stage copies the compiled binaries into the target platform's image.

Detailed Dockerfile Changes

  1. FROM --platform=$BUILDPLATFORM builder-base AS builder

    • Old: FROM builder-base AS builder
    • Change: I added the --platform=$BUILDPLATFORM flag.
    • Why: This is the key change. BUILDPLATFORM is an automatic variable provided by Docker's BuildKit that represents the architecture of the machine
      performing the build (e.g., linux/amd64). This flag forces the builder stage to always run natively on the build machine, completely avoiding QEMU
      emulation during the compilation step. The TARGETARCH variable (e.g., arm64) is still available inside this stage, so we can use it to tell our tools what
      architecture to build for.
  2. RUN apt-get ... install ... gcc-x86-64-linux-gnu

    • Old: ... install libsystemd-dev gcc-aarch64-linux-gnu
    • Change: I added gcc-x86-64-linux-gnu to the list of packages to install.
    • Why: The original file only installed the cross-compiler for arm64. Since the Makefile is also responsible for building the amd64 binary, I added the
      corresponding amd64 C compiler to ensure the toolchain is complete for both target architectures within our native amd64 builder.

The rest of the Dockerfile and the Makefile already worked correctly with this setup, as the Makefile uses the GOARCH=${TARGETARCH} variable to select the
appropriate C compiler (CC).

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 13, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: SergeyKanzhelev

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 13, 2025
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Aug 13, 2025
@SergeyKanzhelev
Copy link
Member Author

now it is a different segfault. Still a segfault though

@SergeyKanzhelev
Copy link
Member Author

I am curious if this is also flaky:

/retest

@SergeyKanzhelev SergeyKanzhelev force-pushed the crosscompile branch 2 times, most recently from d2263cf to 4b3baa5 Compare August 14, 2025 00:27
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 14, 2025
@SergeyKanzhelev
Copy link
Member Author

experimenting here, any binary that compiles first out of three we compile, will fail with segfault. So it doesn't look like it is binary-specific.

build.log Outdated
@@ -0,0 +1,1066 @@
Docker in Docker enabled, initializing...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably this wasn't meant to be checked in?

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants