Skip to content

Commit 5c7068f

Browse files
committed
fix: use native cross-compilation in validator Dockerfile
Use --platform=$BUILDPLATFORM with GOOS/GOARCH instead of QEMU emulation for cross-compiling test binaries. Multi-stage build keeps the runtime image identical (golang:1.25-bookworm) while avoiding 10-50x slower emulated compilation for non-native arch.
1 parent aac7b8a commit 5c7068f

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

Dockerfile.validator

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Validator image includes Go toolchain for running validation tests in-cluster
16-
FROM golang:1.25-bookworm
15+
# Build stage: runs on the host platform, cross-compiles via GOOS/GOARCH
16+
# This avoids slow QEMU emulation for the non-native architecture.
17+
FROM --platform=$BUILDPLATFORM golang:1.25-bookworm AS builder
1718

18-
# Install basic dependencies
19-
RUN apt-get update && apt-get install -y \
20-
ca-certificates \
21-
git \
22-
&& rm -rf /var/lib/apt/lists/*
19+
ARG TARGETOS TARGETARCH
2320

24-
# Set working directory
2521
WORKDIR /workspace
2622

2723
# Copy go mod files first for better caching
@@ -31,12 +27,31 @@ RUN go mod download
3127
# Copy source code
3228
COPY . .
3329

34-
# Pre-compile test binaries to speed up Job execution
35-
# This is optional but improves startup time
36-
RUN go test -c -o /tmp/readiness.test ./pkg/validator/checks/readiness && \
37-
go test -c -o /tmp/deployment.test ./pkg/validator/checks/deployment && \
38-
go test -c -o /tmp/performance.test ./pkg/validator/checks/performance && \
39-
go test -c -o /tmp/conformance.test ./pkg/validator/checks/conformance || true
30+
# Cross-compile test binaries for the target platform
31+
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go test -c -o /tmp/readiness.test ./pkg/validator/checks/readiness && \
32+
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go test -c -o /tmp/deployment.test ./pkg/validator/checks/deployment && \
33+
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go test -c -o /tmp/performance.test ./pkg/validator/checks/performance && \
34+
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go test -c -o /tmp/conformance.test ./pkg/validator/checks/conformance || true
35+
36+
# Runtime stage: full Go toolchain needed because agent/job.go runs "go test" at runtime
37+
FROM golang:1.25-bookworm
38+
39+
RUN apt-get update && apt-get install -y \
40+
ca-certificates \
41+
git \
42+
&& rm -rf /var/lib/apt/lists/*
43+
44+
WORKDIR /workspace
45+
46+
# Copy source for runtime "go test" execution
47+
COPY --from=builder /workspace/go.mod /workspace/go.sum ./
48+
COPY --from=builder /workspace/pkg/ pkg/
49+
COPY --from=builder /workspace/cmd/ cmd/
50+
51+
# Copy pre-compiled test binaries
52+
COPY --from=builder /tmp/*.test /usr/local/bin/
53+
54+
# Pre-warm module cache for runtime
55+
RUN go mod download
4056

41-
# Default command runs bash for debugging
4257
CMD ["/bin/bash"]

0 commit comments

Comments
 (0)