@@ -11,6 +11,30 @@ HUB_AGENT_IMAGE_NAME ?= hub-agent
1111MEMBER_AGENT_IMAGE_NAME ?= member-agent
1212REFRESH_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+
1438KUBECONFIG ?= $(HOME ) /.kube/config
1539HUB_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).
290321docker-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
307347docker-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
316359docker-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
0 commit comments