Skip to content

Commit 74aaa18

Browse files
authored
feat: enable ARM64 native support in the upstream repo (#261)
1 parent c0bd328 commit 74aaa18

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

Makefile

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ HUB_AGENT_IMAGE_NAME ?= hub-agent
1111
MEMBER_AGENT_IMAGE_NAME ?= member-agent
1212
REFRESH_TOKEN_IMAGE_NAME := refresh-token
1313

14+
TARGET_OS ?= linux
15+
TARGET_ARCH ?= amd64
16+
AUTO_DETECT_ARCH ?= TRUE
17+
18+
# Auto-detect system architecture if it is allowed and the necessary commands are available on the system.
19+
ifeq ($(AUTO_DETECT_ARCH), TRUE)
20+
ARCH_CMD_INSTALLED := $(shell command -v arch 2>/dev/null)
21+
ifdef ARCH_CMD_INSTALLED
22+
TARGET_ARCH := $(shell arch)
23+
# The arch command may return arch strings that are aliases of expected TARGET_ARCH values;
24+
# do the mapping here.
25+
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86_64))
26+
TARGET_ARCH := amd64
27+
else ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),aarch64 arm))
28+
TARGET_ARCH := arm64
29+
endif
30+
$(info Auto-detected system architecture: $(TARGET_ARCH))
31+
endif
32+
endif
33+
34+
# Note (chenyu1): switch to the `plain` progress type to see the full outputs in the docker build
35+
# progress.
36+
BUILDKIT_PROGRESS_TYPE ?= auto
37+
1438
KUBECONFIG ?= $(HOME)/.kube/config
1539
HUB_SERVER_URL ?= https://172.19.0.2:6443
1640

@@ -287,9 +311,22 @@ push:
287311

288312
# By default, docker buildx create will pull image moby/buildkit:buildx-stable-1 and hit the too many requests error
289313
.PHONY: docker-buildx-builder
314+
# Note (chenyu1): the step below sets up emulation for building/running non-native binaries on the host. The original
315+
# setup assumes that the Makefile is always run on an x86_64 platform, and adds support for non-x86_64 hosts. Here
316+
# we keep the original setup if the build target is x86_64 platforms (default) for compatibility reasons, but will switch to
317+
# a more general setup for non-x86_64 hosts.
318+
#
319+
# On some systems the emulation setup might not work at all (e.g., macOS on Apple Silicon -> Rosetta 2 will be used
320+
# by Docker Desktop as the default emulation option for AMD64 on ARM64 container compatibility).
290321
docker-buildx-builder:
291322
@if ! docker buildx ls | grep $(BUILDX_BUILDER_NAME); then \
292-
docker run --rm --privileged mcr.microsoft.com/mirror/docker/multiarch/qemu-user-static:$(QEMU_VERSION) --reset -p yes; \
323+
if [ "$(TARGET_ARCH)" = "amd64" ] ; then \
324+
echo "The target is an x86_64 platform; setting up emulation for other known architectures"; \
325+
docker run --rm --privileged mcr.microsoft.com/mirror/docker/multiarch/qemu-user-static:$(QEMU_VERSION) --reset -p yes; \
326+
else \
327+
echo "Setting up emulation for known architectures"; \
328+
docker run --rm --privileged tonistiigi/binfmt --install all; \
329+
fi ;\
293330
docker buildx create --driver-opt image=mcr.microsoft.com/oss/v2/moby/buildkit:$(BUILDKIT_VERSION) --name $(BUILDX_BUILDER_NAME) --use; \
294331
docker buildx inspect $(BUILDX_BUILDER_NAME) --bootstrap; \
295332
fi
@@ -299,27 +336,36 @@ docker-build-hub-agent: docker-buildx-builder
299336
docker buildx build \
300337
--file docker/$(HUB_AGENT_IMAGE_NAME).Dockerfile \
301338
--output=$(OUTPUT_TYPE) \
302-
--platform="linux/amd64" \
339+
--platform=$(TARGET_OS)/$(TARGET_ARCH) \
303340
--pull \
304-
--tag $(REGISTRY)/$(HUB_AGENT_IMAGE_NAME):$(HUB_AGENT_IMAGE_VERSION) .
341+
--tag $(REGISTRY)/$(HUB_AGENT_IMAGE_NAME):$(HUB_AGENT_IMAGE_VERSION) \
342+
--progress=$(BUILDKIT_PROGRESS_TYPE) \
343+
--build-arg GOARCH=$(TARGET_ARCH) \
344+
--build-arg GOOS=$(TARGET_OS) .
305345

306346
.PHONY: docker-build-member-agent
307347
docker-build-member-agent: docker-buildx-builder
308348
docker buildx build \
309349
--file docker/$(MEMBER_AGENT_IMAGE_NAME).Dockerfile \
310350
--output=$(OUTPUT_TYPE) \
311-
--platform="linux/amd64" \
351+
--platform=$(TARGET_OS)/$(TARGET_ARCH) \
312352
--pull \
313-
--tag $(REGISTRY)/$(MEMBER_AGENT_IMAGE_NAME):$(MEMBER_AGENT_IMAGE_VERSION) .
353+
--tag $(REGISTRY)/$(MEMBER_AGENT_IMAGE_NAME):$(MEMBER_AGENT_IMAGE_VERSION) \
354+
--progress=$(BUILDKIT_PROGRESS_TYPE) \
355+
--build-arg GOARCH=$(TARGET_ARCH) \
356+
--build-arg GOOS=$(TARGET_OS) .
314357

315358
.PHONY: docker-build-refresh-token
316359
docker-build-refresh-token: docker-buildx-builder
317360
docker buildx build \
318361
--file docker/$(REFRESH_TOKEN_IMAGE_NAME).Dockerfile \
319362
--output=$(OUTPUT_TYPE) \
320-
--platform="linux/amd64" \
363+
--platform=$(TARGET_OS)/$(TARGET_ARCH) \
321364
--pull \
322-
--tag $(REGISTRY)/$(REFRESH_TOKEN_IMAGE_NAME):$(REFRESH_TOKEN_IMAGE_VERSION) .
365+
--tag $(REGISTRY)/$(REFRESH_TOKEN_IMAGE_NAME):$(REFRESH_TOKEN_IMAGE_VERSION) \
366+
--progress=$(BUILDKIT_PROGRESS_TYPE) \
367+
--build-arg GOARCH=$(TARGET_ARCH) \
368+
--build-arg GOOS=${TARGET_OS} .
323369

324370
## -----------------------------------
325371
## Cleanup

docker/hub-agent.Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Build the hubagent binary
22
FROM mcr.microsoft.com/oss/go/microsoft/golang:1.24.6 AS builder
33

4+
ARG GOOS=linux
5+
ARG GOARCH=amd64
6+
47
WORKDIR /workspace
58
# Copy the Go Modules manifests
69
COPY go.mod go.mod
@@ -14,14 +17,13 @@ COPY cmd/hubagent/ cmd/hubagent/
1417
COPY apis/ apis/
1518
COPY pkg/ pkg/
1619

17-
ARG TARGETARCH
18-
1920
# Build
20-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -o hubagent cmd/hubagent/main.go
21+
RUN echo "Building images with GOOS=$GOOS GOARCH=$GOARCH"
22+
RUN CGO_ENABLED=1 GOOS=$GOOS GOARCH=$GOARCH GOEXPERIMENT=systemcrypto GO111MODULE=on go build -o hubagent cmd/hubagent/main.go
2123

2224
# Use distroless as minimal base image to package the hubagent binary
2325
# Refer to https://github.com/GoogleContainerTools/distroless for more details
24-
FROM gcr.io/distroless/static:nonroot
26+
FROM gcr.io/distroless/base:nonroot
2527
WORKDIR /
2628
COPY --from=builder /workspace/hubagent .
2729
USER 65532:65532

docker/member-agent.Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Build the memberagent binary
22
FROM mcr.microsoft.com/oss/go/microsoft/golang:1.24.6 AS builder
33

4+
ARG GOOS=linux
5+
ARG GOARCH=amd64
6+
47
WORKDIR /workspace
58
# Copy the Go Modules manifests
69
COPY go.mod go.mod
@@ -14,14 +17,13 @@ COPY cmd/memberagent/main.go main.go
1417
COPY apis/ apis/
1518
COPY pkg/ pkg/
1619

17-
ARG TARGETARCH
18-
1920
# Build
20-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -o memberagent main.go
21+
RUN echo "Building images with GOOS=$GOOS GOARCH=$GOARCH"
22+
RUN CGO_ENABLED=1 GOOS=$GOOS GOARCH=$GOARCH GOEXPERIMENT=systemcrypto GO111MODULE=on go build -o memberagent main.go
2123

2224
# Use distroless as minimal base image to package the memberagent binary
2325
# Refer to https://github.com/GoogleContainerTools/distroless for more details
24-
FROM gcr.io/distroless/static:nonroot
26+
FROM gcr.io/distroless/base:nonroot
2527
WORKDIR /
2628
COPY --from=builder /workspace/memberagent .
2729
USER 65532:65532

docker/refresh-token.Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Build the refreshtoken binary
22
FROM mcr.microsoft.com/oss/go/microsoft/golang:1.24.6 AS builder
33

4+
ARG GOOS="linux"
5+
ARG GOARCH="amd64"
6+
47
WORKDIR /workspace
58
# Copy the Go Modules manifests
69
COPY go.mod go.mod
@@ -16,11 +19,12 @@ COPY pkg/authtoken pkg/authtoken
1619
ARG TARGETARCH
1720

1821
# Build
19-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -o refreshtoken main.go
22+
RUN echo "Building images with GOOS=${GOOS} GOARCH=${GOARCH}"
23+
RUN CGO_ENABLED=1 GOOS=$GOOS GOARCH=$GOARCH GOEXPERIMENT=systemcrypto GO111MODULE=on go build -o refreshtoken main.go
2024

2125
# Use distroless as minimal base image to package the refreshtoken binary
2226
# Refer to https://github.com/GoogleContainerTools/distroless for more details
23-
FROM gcr.io/distroless/static:nonroot
27+
FROM gcr.io/distroless/base:nonroot
2428
WORKDIR /
2529
COPY --from=builder /workspace/refreshtoken .
2630
USER 65532:65532

0 commit comments

Comments
 (0)